diff --git a/src/process/mod.rs b/src/process/mod.rs index 8c2bad0..6c48040 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -10,8 +10,10 @@ //! //! This module is designed to work consistently across Windows, macOS, and Linux. -pub mod run; -pub mod mgmt; +mod run; +mod mgmt; +#[cfg(test)] +mod tests; -use run::*; -use mgmt::*; \ No newline at end of file +pub use run::*; +pub use mgmt::*; \ No newline at end of file diff --git a/src/process/tests.rs b/src/process/tests.rs new file mode 100644 index 0000000..42dab96 --- /dev/null +++ b/src/process/tests.rs @@ -0,0 +1,92 @@ +#[cfg(test)] +mod tests { + use crate::process::run::{run, run_silent, run_script, run_command}; + use crate::text::dedent; + + #[test] + fn test_run_command() { + // Test running a simple echo command + let result = run_command("echo hello").unwrap(); + assert!(result.success); + assert_eq!(result.code, 0); + assert!(result.stdout.trim().contains("hello")); + assert_eq!(result.stderr, ""); + } + + #[test] + fn test_run_silent_command() { + // Test running a command silently + let result = run_silent("echo silent test").unwrap(); + assert!(result.success); + assert_eq!(result.code, 0); + assert!(result.stdout.trim().contains("silent test")); + assert_eq!(result.stderr, ""); + } + + #[test] + fn test_run_script() { + // Test running a multi-line script + let script = r#" + echo "line 1" + echo "line 2" + "#; + + let result = run_script(script).unwrap(); + assert!(result.success); + assert_eq!(result.code, 0); + assert!(result.stdout.contains("line 1")); + assert!(result.stdout.contains("line 2")); + assert_eq!(result.stderr, ""); + } + + #[test] + fn test_run_with_dedent() { + // Test that run properly dedents scripts + let script = r#" + echo "This has 12 spaces of indentation" + echo "This has 16 spaces (4 more than the common indentation)" + "#; + + // The dedent function should remove the common 12 spaces + let dedented = dedent(script); + assert!(dedented.contains("echo \"This has 12 spaces of indentation\"")); + assert!(dedented.contains(" echo \"This has 16 spaces (4 more than the common indentation)\"")); + + // Running the script should work with the dedented content + let result = run(script).unwrap(); + assert!(result.success); + assert_eq!(result.code, 0); + assert!(result.stdout.contains("This has 12 spaces of indentation")); + assert!(result.stdout.contains("This has 16 spaces (4 more than the common indentation)")); + } + + #[test] + fn test_run_detects_script_vs_command() { + // Test that run correctly identifies scripts vs commands + + // One-liner should be treated as a command + let one_liner = "echo one-liner test"; + let result = run(one_liner).unwrap(); + assert!(result.success); + assert!(result.stdout.contains("one-liner test")); + + // Multi-line input should be treated as a script + let multi_line = "echo first line\necho second line"; + let result = run(multi_line).unwrap(); + assert!(result.success); + assert!(result.stdout.contains("first line")); + assert!(result.stdout.contains("second line")); + } + + #[test] + fn test_run_empty_command() { + // Test handling of empty commands + let result = run(""); + assert!(result.is_err()); + // The specific error should be EmptyCommand + match result { + Err(crate::process::run::RunError::EmptyCommand) => (), + _ => panic!("Expected EmptyCommand error"), + } + } +} \ No newline at end of file