diff --git a/Cargo.toml b/Cargo.toml index 6b3e315..749b6dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,6 @@ regex = "1" [features] which_problem = ["dep:which_problem"] + +[dev-dependencies] +pretty_assertions = "1" diff --git a/src/command.rs b/src/command.rs index 7946949..4e1dd70 100644 --- a/src/command.rs +++ b/src/command.rs @@ -23,7 +23,7 @@ pub(crate) fn output_and_write_streams( let stdout_thread = mem::take(&mut child.stdout).map(|mut child_stdout| { scope.spawn(move || std::io::copy(&mut child_stdout, &mut stdout)) }); - let stderr_thread = mem::take(&mut child.stdout).map(|mut child_stderr| { + let stderr_thread = mem::take(&mut child.stderr).map(|mut child_stderr| { scope.spawn(move || std::io::copy(&mut child_stderr, &mut stderr)) }); @@ -56,11 +56,12 @@ pub(crate) fn output_and_write_streams( #[cfg(test)] mod test { use super::*; + use pretty_assertions::assert_str_eq; use std::process::Command; #[test] #[cfg(unix)] - fn test_output_and_write_streams() { + fn test_output_and_write_streams_stdout() { let mut stdout_buf = Vec::new(); let mut stderr_buf = Vec::new(); @@ -76,6 +77,20 @@ mod test { assert_eq!(output.stdout, "Hello World!".as_bytes()); assert_eq!(output.stderr, Vec::::new()); } + + #[test] + #[cfg(unix)] + fn test_output_and_write_streams_stderr() { + let mut stdout_buf = Vec::new(); + let mut stderr_buf = Vec::new(); + + let mut cmd = Command::new("bash"); + cmd.args(["-c", "echo -n Hello World! >&2"]); + + let _ = output_and_write_streams(&mut cmd, &mut stdout_buf, &mut stderr_buf).unwrap(); + + assert_str_eq!(&String::from_utf8_lossy(&stderr_buf), "Hello World!"); + } } /// Constructs a writer that writes to two other writers. Similar to the UNIX `tee` command.