| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use std::ffi::OsString; |
| 7 |
use uufuzz::{CommandResult, run_gnu_cmd}; |
| 8 |
|
| 9 |
fn main() { |
| 10 |
println!("=== Simple Integration Testing uufuzz Example ==="); |
| 11 |
println!("This demonstrates how to use uufuzz to compare against GNU tools"); |
| 12 |
println!("without the complex file descriptor manipulation.\n"); |
| 13 |
|
| 14 |
// Test cases that work well with external command comparison |
| 15 |
let test_cases = [ |
| 16 |
( |
| 17 |
"echo test", |
| 18 |
"echo", |
| 19 |
vec![OsString::from("hello"), OsString::from("world")], |
| 20 |
None, |
| 21 |
), |
| 22 |
( |
| 23 |
"echo with flag", |
| 24 |
"echo", |
| 25 |
vec![OsString::from("-n"), OsString::from("no-newline")], |
| 26 |
None, |
| 27 |
), |
| 28 |
( |
| 29 |
"cat with input", |
| 30 |
"cat", |
| 31 |
vec![], |
| 32 |
Some("Hello from cat!\nLine 2\n"), |
| 33 |
), |
| 34 |
("sort basic", "sort", vec![], Some("zebra\napple\nbanana\n")), |
| 35 |
( |
| 36 |
"sort numeric", |
| 37 |
"sort", |
| 38 |
vec![OsString::from("-n")], |
| 39 |
Some("10\n2\n1\n20\n"), |
| 40 |
), |
| 41 |
]; |
| 42 |
|
| 43 |
for (test_name, cmd, args, input) in test_cases { |
| 44 |
println!("--- {} ---", test_name); |
| 45 |
|
| 46 |
// Run GNU command |
| 47 |
match run_gnu_cmd(cmd, &args, false, input) { |
| 48 |
Ok(gnu_result) => { |
| 49 |
println!("✓ GNU {} succeeded", cmd); |
| 50 |
println!( |
| 51 |
" Stdout: {:?}", |
| 52 |
gnu_result.stdout.trim().replace('\n', "\\n") |
| 53 |
); |
| 54 |
println!(" Exit code: {}", gnu_result.exit_code); |
| 55 |
|
| 56 |
// This demonstrates how you would compare results |
| 57 |
// In real usage, you'd run your implementation and compare: |
| 58 |
// let my_result = run_my_implementation(&args, input); |
| 59 |
// assert_eq!(my_result.stdout, gnu_result.stdout); |
| 60 |
// assert_eq!(my_result.exit_code, gnu_result.exit_code); |
| 61 |
} |
| 62 |
Err(error_result) => { |
| 63 |
println!( |
| 64 |
"⚠ GNU {} failed or not available: {}", |
| 65 |
cmd, error_result.stderr |
| 66 |
); |
| 67 |
println!(" This is normal if GNU coreutils isn't installed"); |
| 68 |
} |
| 69 |
} |
| 70 |
println!(); |
| 71 |
} |
| 72 |
|
| 73 |
println!("=== Practical Example: Compare two echo implementations ==="); |
| 74 |
|
| 75 |
// Simple echo comparison |
| 76 |
let args = vec![OsString::from("hello"), OsString::from("world")]; |
| 77 |
match run_gnu_cmd("echo", &args, false, None) { |
| 78 |
Ok(gnu_result) => { |
| 79 |
println!("GNU echo result: {:?}", gnu_result.stdout.trim()); |
| 80 |
|
| 81 |
// Simulate our own echo implementation result |
| 82 |
let our_result = CommandResult { |
| 83 |
stdout: "hello world\n".to_string(), |
| 84 |
stderr: String::new(), |
| 85 |
exit_code: 0, |
| 86 |
}; |
| 87 |
|
| 88 |
if our_result.stdout.trim() == gnu_result.stdout.trim() |
| 89 |
&& our_result.exit_code == gnu_result.exit_code |
| 90 |
{ |
| 91 |
println!("✓ Our echo matches GNU echo!"); |
| 92 |
} else { |
| 93 |
println!("✗ Our echo differs from GNU echo"); |
| 94 |
println!(" Our result: {:?}", our_result.stdout.trim()); |
| 95 |
println!(" GNU result: {:?}", gnu_result.stdout.trim()); |
| 96 |
} |
| 97 |
} |
| 98 |
Err(_) => { |
| 99 |
println!("Cannot compare - GNU echo not available"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
println!("\n=== Example completed ==="); |
| 104 |
println!("This approach is simpler and more reliable for integration testing."); |
| 105 |
} |