diff --git a/src/lib.rs b/src/lib.rs index baae4da..d0313e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,26 +19,46 @@ pub struct CmdOutput { impl CmdOutput { /// Returns only stdout pub fn stdout(self) -> Option> { - self.lines.and_then(|lines| { - Some( - lines - .into_iter() - .filter(|line| line.printed_to == LineType::Stdout) - .collect(), - ) - }) + match self.lines { + Some(lines) => { + return Some( + lines + .into_iter() + .filter(|l| { + if l.printed_to == LineType::Stdout { + return true; + } + return false; + }) + .collect(), + ); + } + None => { + return None; + } + } } /// Returns only stdout pub fn stderr(self) -> Option> { - self.lines.and_then(|lines| { - Some( - lines - .into_iter() - .filter(|line| line.printed_to == LineType::Stderr) - .collect(), - ) - }) + match self.lines { + Some(lines) => { + return Some( + lines + .into_iter() + .filter(|l| { + if l.printed_to == LineType::Stderr { + return true; + } + return false; + }) + .collect(), + ); + } + None => { + return None; + } + } } /// Returns all output @@ -195,7 +215,7 @@ pub fn run(command: &mut Command) -> CmdOutput { }; } -pub fn run_funcs( +pub fn run_with_funcs( command: &mut Command, stdout_func: impl Fn(Lines>) -> () + std::marker::Send + 'static, stderr_func: impl Fn(Lines>) -> () + std::marker::Send + 'static, @@ -231,42 +251,3 @@ pub fn run_funcs( duration: end.duration_since(start), }; } - -pub fn run_funcs_with_lines( - command: &mut Command, - stdout_func: impl Fn(Lines>) -> Vec + std::marker::Send + 'static, - stderr_func: impl Fn(Lines>) -> Vec + std::marker::Send + 'static, -) -> CmdOutput { - // https://stackoverflow.com/a/72831067/16432246 - let start = Instant::now(); - let mut child = command - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .unwrap(); - - let child_stdout = child.stdout.take().unwrap(); - let child_stderr = child.stderr.take().unwrap(); - - let stdout_lines = BufReader::new(child_stdout).lines(); - let stderr_lines = BufReader::new(child_stderr).lines(); - - let stdout_thread = thread::spawn(move || stdout_func(stdout_lines)); - let stderr_thread = thread::spawn(move || stderr_func(stderr_lines)); - - let mut lines = stdout_thread.join().unwrap(); - let mut lines_printed_to_stderr = stderr_thread.join().unwrap(); - lines.append(&mut lines_printed_to_stderr); - lines.sort(); - - let status = child.wait().unwrap().code(); - let end = Instant::now(); - - return CmdOutput { - lines: Some(lines), - status_code: status, - start_time: start, - end_time: end, - duration: end.duration_since(start), - }; -} diff --git a/src/tests.rs b/src/tests.rs index 30f87d4..d91aa69 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -88,19 +88,19 @@ fn shuffle_vec(vec: &mut [T]) { } #[test] -fn test_run_funcs() { +fn test_run_with_funcs() { let _ = thread::spawn(|| { - let _ = run_funcs( + let _ = run_with_funcs( Command::new("bash") .arg("-c") - .arg("echo hi; >&2 echo hello"), + .arg("echo hi; sleep 0.5; >&2 echo hello"), { |stdout_lines| { sleep(Duration::from_secs(1)); for _ in stdout_lines { Command::new("bash") .arg("-c") - .arg("echo stdout >> ./tmp-run_runcs") + .arg("echo stdout >> ./tmp") .output() .unwrap(); } @@ -112,7 +112,7 @@ fn test_run_funcs() { for _ in stderr_lines { Command::new("bash") .arg("-c") - .arg("echo stderr >> ./tmp-run_runcs") + .arg("echo stderr >> ./tmp") .output() .unwrap(); } @@ -121,7 +121,7 @@ fn test_run_funcs() { ); }); sleep(Duration::from_secs(2)); - let f = File::open("./tmp-run_runcs").unwrap(); + let f = File::open("./tmp").unwrap(); let mut buf: [u8; 14] = [0u8; 14]; f.read_at(&mut buf, 0).unwrap(); assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]); @@ -133,58 +133,5 @@ fn test_run_funcs() { [115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10] ); - remove_file("./tmp-run_runcs").unwrap(); -} - -#[test] -fn test_run_funcs_with_lines() { - let _ = thread::spawn(|| { - let _ = run_funcs_with_lines( - Command::new("bash") - .arg("-c") - .arg("echo hi; >&2 echo hello"), - { - |stdout_lines| { - sleep(Duration::from_secs(1)); - for line in stdout_lines { - assert_eq!(line.unwrap(), "hi"); - Command::new("bash") - .arg("-c") - .arg("echo stdout >> ./tmp-run_runcs_with_lines") - .output() - .unwrap(); - } - return Vec::new(); - } - }, - { - |stderr_lines| { - sleep(Duration::from_secs(3)); - for line in stderr_lines { - assert_eq!(line.unwrap(), "hello"); - Command::new("bash") - .arg("-c") - .arg("echo stderr >> ./tmp-run_runcs_with_lines") - .output() - .unwrap(); - } - return Vec::new(); - } - }, - ); - }); - sleep(Duration::from_secs(2)); - let f = File::open("./tmp-run_runcs_with_lines").unwrap(); - let mut buf: [u8; 14] = [0u8; 14]; - f.read_at(&mut buf, 0).unwrap(); - assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]); - - sleep(Duration::from_secs(2)); - f.read_at(&mut buf, 0).unwrap(); - assert_eq!( - buf, - [115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10] - ); - - remove_file("./tmp-run_runcs_with_lines").unwrap(); + remove_file("./tmp").unwrap(); }