This commit is contained in:
kristof 2025-04-02 08:47:36 +02:00
parent efd29eeb99
commit a3c6322b49
2 changed files with 98 additions and 4 deletions

View File

@ -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::*;
pub use run::*;
pub use mgmt::*;

92
src/process/tests.rs Normal file
View File

@ -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"),
}
}
}