use super::test_utils::*;
#[test]
fn test_exec_basic_shell_command() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo 'Hello from exec'");
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("Hello from exec"), None);
}
#[test]
fn test_exec_with_retry() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo 'Success'")
.arg("--retry")
.arg("3");
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("Success"), None);
}
#[test]
fn test_exec_with_timeout() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: sleep 10")
.arg("--timeout")
.arg("1");
let output = test.run();
assert!(
output.exit_code == exit_codes::SUCCESS || output.exit_code == exit_codes::GENERAL_ERROR
);
}
#[test]
fn test_exec_with_working_directory() {
let test_dir = tempfile::TempDir::new().unwrap();
let test_file = test_dir.path().join("test.txt");
std::fs::write(&test_file, "test content").unwrap();
let mut test = CliTest::new()
.arg("exec")
.arg("shell: ls test.txt")
.arg("--path")
.arg(test_dir.path().to_str().unwrap());
let output = test.run();
assert!(
output.exit_code == exit_codes::SUCCESS || output.exit_code == exit_codes::GENERAL_ERROR
);
}
#[test]
fn test_exec_failing_command() {
let mut test = CliTest::new().arg("exec").arg("shell: exit 1");
let output = test.run();
assert_eq!(output.exit_code, exit_codes::GENERAL_ERROR);
}
#[test]
fn test_exec_with_retry_on_failure() {
let test_file = tempfile::NamedTempFile::new().unwrap();
let path = test_file.path().to_str().unwrap();
let command = format!("shell: test -f {} || {{ touch {} && exit 1; }}", path, path);
let mut test = CliTest::new()
.arg("exec")
.arg(&command)
.arg("--retry")
.arg("2");
let _output = test.run();
}
#[test]
fn test_exec_claude_command() {
let mut test = CliTest::new().arg("exec").arg("claude: /test-command");
let output = test.run();
assert!(
output.exit_code == exit_codes::SUCCESS
|| output.stderr_contains("claude")
|| output.stderr_contains("not found")
);
}
#[test]
fn test_exec_invalid_command_format() {
let mut test = CliTest::new().arg("exec").arg("invalid command format");
let output = test.run();
assert_eq!(output.exit_code, exit_codes::GENERAL_ERROR);
}
#[test]
fn test_exec_with_pipe() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo 'test' | grep test");
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("test"), None);
}
#[test]
fn test_exec_with_environment_variable() {
let mut test = CliTest::new()
.env("TEST_VAR", "test_value")
.arg("exec")
.arg("shell: echo $TEST_VAR");
let output = test.run();
assert!(
output.exit_code == exit_codes::SUCCESS || output.exit_code == exit_codes::GENERAL_ERROR
);
}
#[test]
fn test_exec_multiple_retries_with_eventual_success() {
let test_dir = tempfile::TempDir::new().unwrap();
let counter_file = test_dir.path().join("counter");
std::fs::write(&counter_file, "0").unwrap();
let script = format!(
r#"
count=$(cat {})
count=$((count + 1))
echo $count > {}
if [ $count -lt 3 ]; then
echo "Attempt $count failed"
exit 1
else
echo "Success on attempt $count"
fi
"#,
counter_file.display(),
counter_file.display()
);
let script_file = test_dir.path().join("test.sh");
std::fs::write(&script_file, &script).unwrap();
let mut test = CliTest::new()
.arg("exec")
.arg(&format!("shell: bash {}", script_file.display()))
.arg("--retry")
.arg("3");
let output = test.run();
assert!(
output.exit_code == exit_codes::SUCCESS || output.exit_code == exit_codes::GENERAL_ERROR
);
}
#[test]
fn test_exec_with_zero_timeout() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo 'Quick'")
.arg("--timeout")
.arg("0");
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("Quick"), None);
}
#[test]
fn test_exec_command_with_quotes() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo \"Hello World\"");
let output = test.run();
assert_output(&output, exit_codes::SUCCESS, Some("Hello World"), None);
}
#[test]
fn test_exec_command_with_special_characters() {
let mut test = CliTest::new()
.arg("exec")
.arg("shell: echo 'Test $HOME & special | chars'");
let output = test.run();
assert_eq!(output.exit_code, exit_codes::SUCCESS);
}