1
0
Fork 0
mirror of https://github.com/imjasonh/testscript-rs synced 2026-07-08 17:16:38 +00:00
testscript-rs/tests/setup_hook.rs
Jason Hall 448cf615db
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>
2025-09-26 19:08:48 -04:00

146 lines
4.5 KiB
Rust

//! Tests for setup hook functionality
use std::fs;
use tempfile::TempDir;
// Tests for setup hook functionality - uses internal APIs
use testscript_rs::run::{run_script, RunParams};
#[test]
fn test_setup_hook_basic() {
let temp_dir = TempDir::new().unwrap();
let script_path = temp_dir.path().join("setup_test.txt");
let script_content = r#"# Test that setup hook ran
exists setup_was_here.txt
exec cat setup_was_here.txt
stdout "Setup ran successfully"
-- other_file.txt --
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 result = run_script(&script_path, &params);
assert!(result.is_ok(), "Setup hook test failed: {:?}", result);
}
#[test]
fn test_setup_hook_compile_binary() {
let temp_dir = TempDir::new().unwrap();
let script_path = temp_dir.path().join("binary_test.txt");
let script_content = r#"# Test that setup hook compiled a binary
exists my-tool
exec ./my-tool --help
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
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)?;
// 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(())
});
let result = run_script(&script_path, &params);
// 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
);
} else {
println!("Setup hook binary test passed!");
}
}
#[test]
fn test_setup_hook_environment() {
let temp_dir = TempDir::new().unwrap();
let script_path = temp_dir.path().join("env_test.txt");
let script_content = r#"# Test setup hook can modify environment
env TEST_FROM_SETUP=should_be_set
exec echo "Value: $TEST_FROM_SETUP"
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());
// 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
params = params.condition("TEST_FROM_SETUP", true);
let result = run_script(&script_path, &params);
// 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
);
}
}
#[test]
fn test_setup_hook_failure() {
let temp_dir = TempDir::new().unwrap();
let script_path = temp_dir.path().join("fail_test.txt");
let script_content = r#"# This should never run because setup fails
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 result = run_script(&script_path, &params);
assert!(result.is_err(), "Setup hook should have failed the test");
assert!(result
.unwrap_err()
.to_string()
.contains("Setup deliberately failed"));
}