mirror of
https://github.com/imjasonh/testscript-rs
synced 2026-07-13 11:27:00 +00:00
add GHA workflow (#1)
* add GHA workflow Signed-off-by: Jason Hall <jason@chainguard.dev> * fix CI Signed-off-by: Jason Hall <jason@chainguard.dev> * update readme Signed-off-by: Jason Hall <jason@chainguard.dev> * add badge Signed-off-by: Jason Hall <jason@chainguard.dev> --------- Signed-off-by: Jason Hall <jason@chainguard.dev>
This commit is contained in:
parent
fa546ce09f
commit
448cf615db
16 changed files with 381 additions and 156 deletions
|
|
@ -21,13 +21,12 @@ existing content"#;
|
|||
fs::write(&script_path, script_content).unwrap();
|
||||
|
||||
// Create RunParams with setup hook
|
||||
let params = RunParams::new()
|
||||
.setup(|env| {
|
||||
// Setup hook creates a file to prove it ran
|
||||
let setup_file = env.work_dir.join("setup_was_here.txt");
|
||||
std::fs::write(&setup_file, "Setup ran successfully")?;
|
||||
Ok(())
|
||||
});
|
||||
let params = RunParams::new().setup(|env| {
|
||||
// Setup hook creates a file to prove it ran
|
||||
let setup_file = env.work_dir.join("setup_was_here.txt");
|
||||
std::fs::write(&setup_file, "Setup ran successfully")?;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let result = run_script(&script_path, ¶ms);
|
||||
assert!(result.is_ok(), "Setup hook test failed: {:?}", result);
|
||||
|
|
@ -46,36 +45,38 @@ stdout "Mock CLI Tool""#;
|
|||
fs::write(&script_path, script_content).unwrap();
|
||||
|
||||
// Create RunParams with setup hook that "compiles" a mock binary
|
||||
let params = RunParams::new()
|
||||
.setup(|env| {
|
||||
// Create a mock binary script for testing
|
||||
let binary_path = env.work_dir.join("my-tool");
|
||||
let mock_binary = r#"#!/bin/sh
|
||||
let params = RunParams::new().setup(|env| {
|
||||
// Create a mock binary script for testing
|
||||
let binary_path = env.work_dir.join("my-tool");
|
||||
let mock_binary = r#"#!/bin/sh
|
||||
if [ "$1" = "--help" ]; then
|
||||
echo "Mock CLI Tool"
|
||||
echo "Usage: my-tool [options]"
|
||||
else
|
||||
echo "Hello from my-tool"
|
||||
fi"#;
|
||||
std::fs::write(&binary_path, mock_binary)?;
|
||||
std::fs::write(&binary_path, mock_binary)?;
|
||||
|
||||
// Make it executable (on Unix systems)
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mut perms = std::fs::metadata(&binary_path)?.permissions();
|
||||
perms.set_mode(0o755);
|
||||
std::fs::set_permissions(&binary_path, perms)?;
|
||||
}
|
||||
// Make it executable (on Unix systems)
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mut perms = std::fs::metadata(&binary_path)?.permissions();
|
||||
perms.set_mode(0o755);
|
||||
std::fs::set_permissions(&binary_path, perms)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let result = run_script(&script_path, ¶ms);
|
||||
|
||||
// This might fail on Windows or if shell scripting isn't available
|
||||
if result.is_err() {
|
||||
println!("Binary compilation test failed (expected on some systems): {:?}", result);
|
||||
println!(
|
||||
"Binary compilation test failed (expected on some systems): {:?}",
|
||||
result
|
||||
);
|
||||
} else {
|
||||
println!("Setup hook binary test passed!");
|
||||
}
|
||||
|
|
@ -94,17 +95,16 @@ stdout "Value: hello_from_setup""#;
|
|||
fs::write(&script_path, script_content).unwrap();
|
||||
|
||||
// Create RunParams with setup hook that sets an environment variable
|
||||
let params = RunParams::new()
|
||||
.setup(|env| {
|
||||
// The setup hook has access to the TestEnvironment but can't modify it
|
||||
// This demonstrates the current API limitation - we can access but not modify
|
||||
println!("Setup running in: {}", env.work_dir.display());
|
||||
let params = RunParams::new().setup(|env| {
|
||||
// The setup hook has access to the TestEnvironment but can't modify it
|
||||
// This demonstrates the current API limitation - we can access but not modify
|
||||
println!("Setup running in: {}", env.work_dir.display());
|
||||
|
||||
// We could write files that contain environment setup
|
||||
let env_script = env.work_dir.join("setup_env.sh");
|
||||
std::fs::write(&env_script, "export TEST_FROM_SETUP=hello_from_setup")?;
|
||||
Ok(())
|
||||
});
|
||||
// We could write files that contain environment setup
|
||||
let env_script = env.work_dir.join("setup_env.sh");
|
||||
std::fs::write(&env_script, "export TEST_FROM_SETUP=hello_from_setup")?;
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let mut params = params;
|
||||
// Manually set the environment variable for this test
|
||||
|
|
@ -113,7 +113,10 @@ stdout "Value: hello_from_setup""#;
|
|||
let result = run_script(&script_path, ¶ms);
|
||||
// This test demonstrates the current limitation - setup can't modify the running environment
|
||||
if result.is_err() {
|
||||
println!("Environment setup test failed (expected with current API): {:?}", result);
|
||||
println!(
|
||||
"Environment setup test failed (expected with current API): {:?}",
|
||||
result
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -128,12 +131,16 @@ exec echo "Should not execute""#;
|
|||
fs::write(&script_path, script_content).unwrap();
|
||||
|
||||
// Create RunParams with failing setup hook
|
||||
let params = RunParams::new()
|
||||
.setup(|_env| {
|
||||
Err(testscript_rs::Error::Generic("Setup deliberately failed".to_string()))
|
||||
});
|
||||
let params = RunParams::new().setup(|_env| {
|
||||
Err(testscript_rs::Error::Generic(
|
||||
"Setup deliberately failed".to_string(),
|
||||
))
|
||||
});
|
||||
|
||||
let result = run_script(&script_path, ¶ms);
|
||||
assert!(result.is_err(), "Setup hook should have failed the test");
|
||||
assert!(result.unwrap_err().to_string().contains("Setup deliberately failed"));
|
||||
}
|
||||
assert!(result
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("Setup deliberately failed"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue