#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
use super::*;
use crate::installer::spec::InstallerSpec;
use std::collections::HashMap;
#[test]
fn test_EXCOV_001_script_with_env() {
let mut env = HashMap::new();
env.insert("TEST_VAR".to_string(), "test_value".to_string());
let executor = StepExecutor::with_config(ExecutorConfig {
environment: env,
..Default::default()
});
let result = executor
.execute_script("env-test", "sh", "echo $TEST_VAR")
.unwrap();
assert!(result.success, "Should succeed: {:?}", result);
assert!(
result.stdout.contains("test_value"),
"Env var should be set: {}",
result.stdout
);
}
#[test]
fn test_EXCOV_002_script_with_working_dir() {
let executor = StepExecutor::with_config(ExecutorConfig {
working_dir: Some("/tmp".to_string()),
..Default::default()
});
let result = executor.execute_script("wd-test", "sh", "pwd").unwrap();
assert!(result.success, "Should succeed: {:?}", result);
assert!(
result.stdout.contains("/tmp"),
"Should be in /tmp: {}",
result.stdout
);
}
#[test]
fn test_EXCOV_003_script_stderr_output() {
let executor = StepExecutor::new();
let result = executor
.execute_script("stderr-test", "sh", "echo 'error msg' >&2")
.unwrap();
assert!(result.success);
assert!(
result.stderr.contains("error msg"),
"Stderr: {}",
result.stderr
);
}
#[test]
fn test_EXCOV_004_file_write_creates_parent_dirs() {
let test_dir = "/tmp/bashrs_excov_004_nested/sub/dir";
let test_path = format!("{}/test.txt", test_dir);
let _ = std::fs::remove_dir_all("/tmp/bashrs_excov_004_nested");
let executor = StepExecutor::new();
let result = executor
.execute_file_write("nested-write", &test_path, "nested content")
.unwrap();
assert!(result.success, "Should succeed: {:?}", result);
assert!(
std::path::Path::new(&test_path).exists(),
"File should exist"
);
let content = std::fs::read_to_string(&test_path).unwrap();
assert_eq!(content, "nested content");
let _ = std::fs::remove_dir_all("/tmp/bashrs_excov_004_nested");
}
#[test]
fn test_EXCOV_005_file_write_empty_parent() {
let test_path = "/tmp/bashrs_excov_005.txt";
let _ = std::fs::remove_file(test_path);
let executor = StepExecutor::new();
let result = executor
.execute_file_write("root-write", test_path, "root content")
.unwrap();
assert!(result.success);
assert!(result.stdout.contains("12 bytes"));
let _ = std::fs::remove_file(test_path);
}
#[test]
fn test_EXCOV_006_check_command_succeeds_details() {
let executor = StepExecutor::new();
let pass = executor.check_command_succeeds("true");
assert!(pass.passed);
assert!(
pass.details.contains("Command succeeded"),
"Details: {}",
pass.details
);
let fail = executor.check_command_succeeds("false");
assert!(!fail.passed);
assert!(
fail.details.contains("Command failed"),
"Details: {}",
fail.details
);
}
#[test]
fn test_EXCOV_007_check_command_exit_code_in_details() {
let executor = StepExecutor::new();
let result = executor.check_command_succeeds("exit 42");
assert!(!result.passed);
assert!(
result.details.contains("exit"),
"Should mention exit: {}",
result.details
);
}
#[test]
fn test_EXCOV_008_check_service_inactive() {
let executor = StepExecutor::new();
let result = executor.check_service_active("nonexistent_service_xyz_12345");
assert!(!result.passed);
assert_eq!(result.check_type, "service_active");
assert!(
result.details.contains("not active") || result.details.contains("Failed"),
"Details: {}",
result.details
);
}
#[test]
fn test_EXCOV_009_postconditions_packages_absent() {
let toml = r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "s"
name = "S"
action = "script"
[step.script]
content = "true"
[step.postconditions]
packages_absent = ["nonexistent_pkg_xyz_12345"]
"#;
let spec = InstallerSpec::parse(toml).unwrap();
let executor = StepExecutor::new();
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(result.success, "Should succeed since package is absent");
let absent_check = result
.postcondition_results
.iter()
.find(|r| r.check_type == "package_absent");
assert!(absent_check.is_some(), "Should have package_absent check");
assert!(
absent_check.unwrap().passed,
"Non-existent package should be absent"
);
}
#[test]
fn test_EXCOV_010_execute_step_file_write_dry_run() {
let toml = r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "fw"
name = "File Write"
action = "file-write"
path = "/tmp/bashrs_excov_010.txt"
content = "dry run content"
"#;
let spec = InstallerSpec::parse(toml).unwrap();
let executor = StepExecutor::with_config(ExecutorConfig {
dry_run: true,
..Default::default()
});
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(result.success);
assert!(result.stdout.contains("[DRY-RUN]"));
}
#[test]
fn test_EXCOV_011_execute_step_user_group_dry_run() {
let toml = r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "ug"
name = "User Group"
action = "user-add-to-group"
user = "testuser"
group = "docker"
"#;
let spec = InstallerSpec::parse(toml).unwrap();
let executor = StepExecutor::with_config(ExecutorConfig {
dry_run: true,
..Default::default()
});
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(result.success);
assert!(result.stdout.contains("[DRY-RUN]"));
assert!(result.stdout.contains("testuser"));
assert!(result.stdout.contains("docker"));
}
#[test]
fn test_EXCOV_012_execute_step_postcondition_command_succeeds() {
let toml = r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "s"
name = "S"
action = "script"
[step.script]
content = "true"
[step.postconditions]
command_succeeds = "test -d /tmp"
"#;
let spec = InstallerSpec::parse(toml).unwrap();
let executor = StepExecutor::new();
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(result.success);
let cmd_check = result
.postcondition_results
.iter()
.find(|r| r.check_type == "command_succeeds");
assert!(cmd_check.is_some());
assert!(cmd_check.unwrap().passed);
}
#[test]
fn test_EXCOV_013_postcondition_command_fails_step_fails() {
let toml = r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "s"
name = "S"
action = "script"
[step.script]
content = "true"
[step.postconditions]
command_succeeds = "test -f /nonexistent_xyz_12345"
"#;
let spec = InstallerSpec::parse(toml).unwrap();
let executor = StepExecutor::new();
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(!result.success, "Step should fail: {:?}", result);
assert!(
result.stderr.contains("Postcondition"),
"Stderr: {}",
result.stderr
);
}
#[test]
fn test_EXCOV_014_multiple_postconditions() {
let temp = tempfile::NamedTempFile::new().unwrap();
let path = temp.path().to_str().unwrap();
let toml = format!(
r#"
[installer]
name = "t"
version = "1.0.0"
[[step]]
id = "s"
name = "S"
action = "script"
[step.script]
content = "true"
[step.postconditions]
file_exists = "{}"
command_succeeds = "true"
"#,
path
);
let spec = InstallerSpec::parse(&toml).unwrap();
let executor = StepExecutor::new();
let result = executor.execute_step(&spec.step[0]).unwrap();
assert!(result.success);
assert_eq!(result.postcondition_results.len(), 2);
assert!(result.postcondition_results.iter().all(|r| r.passed));
}
#[test]
include!("executor_cov_tests_tests_EXCOV.rs");