diff --git a/src/lib.rs b/src/lib.rs index 6b81f31..dcdf487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,8 +251,8 @@ pub fn run(command: &mut Command) -> CmdOutput { /// ``` pub fn run_funcs( command: &mut Command, - stdout_func: impl FnOnce(Lines>) -> () + std::marker::Send + 'static, - stderr_func: impl FnOnce(Lines>) -> () + std::marker::Send + 'static, + stdout_func: impl Fn(Lines>) -> () + std::marker::Send + 'static, + stderr_func: impl Fn(Lines>) -> () + std::marker::Send + 'static, ) -> CmdOutput { // https://stackoverflow.com/a/72831067/16432246 let start = Instant::now(); @@ -330,8 +330,8 @@ pub fn run_funcs( /// ``` pub fn run_funcs_with_lines( command: &mut Command, - stdout_func: impl FnOnce(Lines>) -> Vec + std::marker::Send + 'static, - stderr_func: impl FnOnce(Lines>) -> Vec + std::marker::Send + 'static, + 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(); diff --git a/src/tests.rs b/src/tests.rs index c4acb7d..fc209ec 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,13 +1,12 @@ #[cfg(test)] use crate::*; -use std::io::Write; -use std::os::unix::fs::FileExt; use std::process::Command; use std::{ fs::remove_file, hash::{BuildHasher, Hasher, RandomState}, }; use std::{fs::File, thread::sleep}; +use std::os::unix::fs::FileExt; /// Tests what stdout prints #[test] @@ -89,8 +88,6 @@ fn shuffle_vec(vec: &mut [T]) { #[test] fn test_run_funcs() { - // TODO: Add error handling to delete the file on exit - File::create_new("./tmp-run_funcs").unwrap(); let threads = thread::spawn(|| { return run_funcs( Command::new("bash") @@ -100,12 +97,11 @@ fn test_run_funcs() { |stdout_lines| { sleep(Duration::from_secs(1)); for _ in stdout_lines { - let mut f = File::options() - .write(true) - .open("./tmp-run_funcs") + Command::new("bash") + .arg("-c") + .arg("echo stdout >> ./tmp-run_funcs") // col + .output() .unwrap(); - f.write_all(b"stdout\n").unwrap(); - drop(f); } } }, @@ -113,12 +109,11 @@ fn test_run_funcs() { |stderr_lines| { sleep(Duration::from_secs(3)); for _ in stderr_lines { - let f = File::options() - .write(true) - .open("./tmp-run_funcs") + Command::new("bash") + .arg("-c") + .arg("echo stderr >> ./tmp-run_funcs") // col + .output() .unwrap(); - f.write_at(b"stderr\n", 7).unwrap(); - drop(f); } } }, @@ -145,8 +140,6 @@ fn test_run_funcs() { #[test] fn test_run_funcs_with_lines() { - // TODO: Add error handling to delete the file on exit - File::create_new("./tmp-run_funcs_with_lines").unwrap(); let threads = thread::spawn(|| { return run_funcs_with_lines( &mut Command::new("bash") @@ -160,12 +153,11 @@ fn test_run_funcs_with_lines() { let line = line.unwrap(); lines.push(Line::from_stdout(&line)); assert_eq!(&line, "hi"); - let mut f = File::options() - .write(true) - .open("./tmp-run_funcs_with_lines") + Command::new("bash") + .arg("-c") + .arg("echo stdout >> ./tmp-run_funcs_with_lines") + .output() .unwrap(); - f.write_all(b"stdout\n").unwrap(); - drop(f); } return lines; } @@ -178,13 +170,11 @@ fn test_run_funcs_with_lines() { let line = line.unwrap(); lines.push(Line::from_stdout(&line)); assert_eq!(line, "hello"); - let mut f = File::options() - .write(true) - .append(true) - .open("./tmp-run_funcs_with_lines") - .unwrap(); - f.write(b"stderr\n").unwrap(); - drop(f); + Command::new("bash") + .arg("-c") + .arg("echo stderr >> ./tmp-run_funcs_with_lines") + .output() + .unwrap(); // oops sorry lol } return lines; } @@ -192,13 +182,17 @@ fn test_run_funcs_with_lines() { ); }); sleep(Duration::from_secs(2)); - let read = std::fs::read_to_string("tmp-run_funcs_with_lines").unwrap(); - assert_eq!(read, "stdout\n"); + let f = File::open("./tmp-run_funcs_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)); - let read = std::fs::read_to_string("tmp-run_funcs_with_lines").unwrap(); - assert_eq!(read, "stdout\nstderr\n"); - + 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_funcs_with_lines").unwrap();