use assert_cmd::cargo::CommandCargoExt as _; use std::process::{Command, Stdio}; const NULL: &str = "/dev/null"; const TRICKY: &str = "testdata/tricky"; const A: &str = "testdata/a"; const B: &str = "testdata/b"; const ANCESTOR: &str = "testdata/ancestor"; const EXPECTED2: &str = "testdata/expected2"; const EXPECTED3: &str = "testdata/expected3"; fn merge_then_diff(args: &[&str], expected: &str) -> Result<(), Box> { let mut merge = Command::cargo_bin("zsh_history")? .args(args) .stdout(Stdio::piped()) .spawn()?; let binary_stdout = merge.stdout.take().expect("Failed to capture stdout"); let mut diff = Command::new("diff") .args(&["-u", expected, "-"]) .stdin(Stdio::from(binary_stdout)) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .spawn()?; let binary_status = merge.wait()?; if !binary_status.success() { return Err(format!("Merge failed with status {:?}", binary_status).into()); } let diff_status = diff.wait()?; if !diff_status.success() { return Err(format!( "Merging {} produced unexpected result (see diff above).", args.join(" ") ) .into()); } Ok(()) } #[test] fn test_pass_through() -> Result<(), Box> { for file in [NULL, TRICKY] { merge_then_diff(&["merge", file, NULL], file)?; merge_then_diff(&["merge", NULL, file], file)?; merge_then_diff(&["merge", file, file], file)?; } Ok(()) } #[test] fn test_merge() -> Result<(), Box> { merge_then_diff(&["merge", A, B], EXPECTED2) } #[test] fn test_merge_with_ancestor() -> Result<(), Box> { merge_then_diff(&["merge", A, B, ANCESTOR], EXPECTED3) }