From 5deaaa4265081066cb2273507d7f37fdcfc1f069 Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 11 Jan 2025 18:06:29 -0600 Subject: [PATCH 1/2] misc improvements notably, run() is better and no longer uses channels --- src/lib.rs | 27 ++++++++++++-------------- src/tests.rs | 53 ++++++++++++++++++---------------------------------- 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ebaec5e..f3dc58a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,41 +167,38 @@ pub fn run(command: &mut Command) -> CmdOutput { let child_stdout = child.stdout.take().unwrap(); let child_stderr = child.stderr.take().unwrap(); - let (stdout_tx, stdout_rx) = std::sync::mpsc::channel(); - let (stderr_tx, stderr_rx) = std::sync::mpsc::channel(); - let stdout_lines = BufReader::new(child_stdout).lines(); - thread::spawn(move || { + let stdout_thread = thread::spawn(move || { + let mut lines: Vec = Vec::new(); for line in stdout_lines { - stdout_tx - .send(Line { + lines.push(Line { content: line.unwrap(), printed_to: LineType::Stdout, time: Instant::now(), - }) - .unwrap(); + }); } + return lines; }); let stderr_lines = BufReader::new(child_stderr).lines(); - thread::spawn(move || { + let stderr_thread = thread::spawn(move || { + let mut lines: Vec = Vec::new(); for line in stderr_lines { let time = Instant::now(); - stderr_tx - .send(Line { + lines.push(Line { content: line.unwrap(), printed_to: LineType::Stderr, time: time, - }) - .unwrap(); + }); } + return lines; }); let status = child.wait().unwrap().code(); let end = Instant::now(); - let mut lines = stdout_rx.into_iter().collect::>(); - lines.append(&mut stderr_rx.into_iter().collect::>()); + let mut lines = stdout_thread.join().unwrap(); + lines.append(&mut stderr_thread.join().unwrap()); lines.sort(); return CmdOutput { diff --git a/src/tests.rs b/src/tests.rs index 063025f..0c27cdb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -6,38 +6,23 @@ use std::{ hash::{BuildHasher, Hasher, RandomState}, }; use std::{fs::File, os::unix::fs::FileExt, thread::sleep}; -use crate::command; - -#[macro_export] -macro_rules! command { - ($command:expr, $($args:expr),*) => { - { - Command::new($command) - $( - .arg($args) - )* - } - }; -} /// Tests what stdout prints #[test] fn stdout_content() { - let expected = "[\"helloooooooooo\", \"hiiiiiiiiiiiii\"]"; - assert_eq!( - expected, - format!( - "{:?}", - run(&mut Command::new("echo") - .arg("-n") - .arg("helloooooooooo\nhiiiiiiiiiiiii")) - .stdout() - .unwrap() - .into_iter() - .map(|line| { line.content }) - .collect::>() - ) - ); + let expected = vec!["helloooooooooo".to_string(), "hiiiiiiiiiiiii".to_string()]; + + let output = run(Command::new("echo").arg( + "helloooooooooo +hiiiiiiiiiiiii", + )) + .stdout() + .unwrap() + .into_iter() + .map(|line| line.content) + .collect::>(); + + assert_eq!(expected, output); } /// Tests what stderr prints @@ -111,7 +96,7 @@ fn test_run_funcs() { |stdout_lines| { sleep(Duration::from_secs(1)); for _ in stdout_lines { - command!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col + Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs") // col .output() .unwrap(); } @@ -121,7 +106,7 @@ fn test_run_funcs() { |stderr_lines| { sleep(Duration::from_secs(3)); for _ in stderr_lines { - command!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col + Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs") // col .output() .unwrap(); } @@ -152,9 +137,7 @@ fn test_run_funcs() { fn test_run_funcs_with_lines() { let threads = thread::spawn(|| { return run_funcs_with_lines( - Command::new("bash") - .arg("-c") - .arg("echo hi; >&2 echo hello"), + &mut Command::new("bash").arg("-c").arg("echo hi; >&2 echo hello"), { |stdout_lines| { let mut lines: Vec = Vec::new(); @@ -163,7 +146,7 @@ fn test_run_funcs_with_lines() { let line = line.unwrap(); lines.push(Line::from_stdout(&line)); assert_eq!(&line, "hi"); - command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines") + Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs_with_lines") .output() .unwrap(); } @@ -178,7 +161,7 @@ fn test_run_funcs_with_lines() { let line = line.unwrap(); lines.push(Line::from_stdout(&line)); assert_eq!(line, "hello"); - command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col + Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs_with_lines") .output() .unwrap(); // oops sorry lol } From dedacf0ea4cd9619400533b1aeef7c78a6170c2f Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 11 Jan 2025 19:16:35 -0600 Subject: [PATCH 2/2] fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb732f9..56afb0a 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ A better way of running commands - get stdout and stderr together, in order with ## TODO - Improve docs -- Add `lines` capability to `run_with_funcs()` +- Add `lines` capability to `run_funcs()`