Compare commits

..

No commits in common. "dedacf0ea4cd9619400533b1aeef7c78a6170c2f" and "98192dd46a0385d1c22c9e233fe344c19a592d0e" have entirely different histories.

3 changed files with 51 additions and 31 deletions

View file

@ -5,4 +5,4 @@ A better way of running commands - get stdout and stderr together, in order with
## TODO ## TODO
- Improve docs - Improve docs
- Add `lines` capability to `run_funcs()` - Add `lines` capability to `run_with_funcs()`

View file

@ -167,38 +167,41 @@ pub fn run(command: &mut Command) -> CmdOutput {
let child_stdout = child.stdout.take().unwrap(); let child_stdout = child.stdout.take().unwrap();
let child_stderr = child.stderr.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(); let stdout_lines = BufReader::new(child_stdout).lines();
let stdout_thread = thread::spawn(move || { thread::spawn(move || {
let mut lines: Vec<Line> = Vec::new();
for line in stdout_lines { for line in stdout_lines {
lines.push(Line { stdout_tx
.send(Line {
content: line.unwrap(), content: line.unwrap(),
printed_to: LineType::Stdout, printed_to: LineType::Stdout,
time: Instant::now(), time: Instant::now(),
}); })
.unwrap();
} }
return lines;
}); });
let stderr_lines = BufReader::new(child_stderr).lines(); let stderr_lines = BufReader::new(child_stderr).lines();
let stderr_thread = thread::spawn(move || { thread::spawn(move || {
let mut lines: Vec<Line> = Vec::new();
for line in stderr_lines { for line in stderr_lines {
let time = Instant::now(); let time = Instant::now();
lines.push(Line { stderr_tx
.send(Line {
content: line.unwrap(), content: line.unwrap(),
printed_to: LineType::Stderr, printed_to: LineType::Stderr,
time: time, time: time,
}); })
.unwrap();
} }
return lines;
}); });
let status = child.wait().unwrap().code(); let status = child.wait().unwrap().code();
let end = Instant::now(); let end = Instant::now();
let mut lines = stdout_thread.join().unwrap(); let mut lines = stdout_rx.into_iter().collect::<Vec<Line>>();
lines.append(&mut stderr_thread.join().unwrap()); lines.append(&mut stderr_rx.into_iter().collect::<Vec<Line>>());
lines.sort(); lines.sort();
return CmdOutput { return CmdOutput {

View file

@ -6,23 +6,38 @@ use std::{
hash::{BuildHasher, Hasher, RandomState}, hash::{BuildHasher, Hasher, RandomState},
}; };
use std::{fs::File, os::unix::fs::FileExt, thread::sleep}; 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 /// Tests what stdout prints
#[test] #[test]
fn stdout_content() { fn stdout_content() {
let expected = vec!["helloooooooooo".to_string(), "hiiiiiiiiiiiii".to_string()]; let expected = "[\"helloooooooooo\", \"hiiiiiiiiiiiii\"]";
assert_eq!(
let output = run(Command::new("echo").arg( expected,
"helloooooooooo format!(
hiiiiiiiiiiiii", "{:?}",
)) run(&mut Command::new("echo")
.arg("-n")
.arg("helloooooooooo\nhiiiiiiiiiiiii"))
.stdout() .stdout()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|line| line.content) .map(|line| { line.content })
.collect::<Vec<String>>(); .collect::<Vec<String>>()
)
assert_eq!(expected, output); );
} }
/// Tests what stderr prints /// Tests what stderr prints
@ -96,7 +111,7 @@ fn test_run_funcs() {
|stdout_lines| { |stdout_lines| {
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
for _ in stdout_lines { for _ in stdout_lines {
Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs") // col command!("bash", "-c", "echo stdout >> ./tmp-run_funcs") // col
.output() .output()
.unwrap(); .unwrap();
} }
@ -106,7 +121,7 @@ fn test_run_funcs() {
|stderr_lines| { |stderr_lines| {
sleep(Duration::from_secs(3)); sleep(Duration::from_secs(3));
for _ in stderr_lines { for _ in stderr_lines {
Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs") // col command!("bash", "-c", "echo stderr >> ./tmp-run_funcs") // col
.output() .output()
.unwrap(); .unwrap();
} }
@ -137,7 +152,9 @@ fn test_run_funcs() {
fn test_run_funcs_with_lines() { fn test_run_funcs_with_lines() {
let threads = thread::spawn(|| { let threads = thread::spawn(|| {
return run_funcs_with_lines( return run_funcs_with_lines(
&mut Command::new("bash").arg("-c").arg("echo hi; >&2 echo hello"), Command::new("bash")
.arg("-c")
.arg("echo hi; >&2 echo hello"),
{ {
|stdout_lines| { |stdout_lines| {
let mut lines: Vec<Line> = Vec::new(); let mut lines: Vec<Line> = Vec::new();
@ -146,7 +163,7 @@ fn test_run_funcs_with_lines() {
let line = line.unwrap(); let line = line.unwrap();
lines.push(Line::from_stdout(&line)); lines.push(Line::from_stdout(&line));
assert_eq!(&line, "hi"); assert_eq!(&line, "hi");
Command::new("bash").arg("-c").arg("echo stdout >> ./tmp-run_funcs_with_lines") command!("bash", "-c", "echo stdout >> ./tmp-run_funcs_with_lines")
.output() .output()
.unwrap(); .unwrap();
} }
@ -161,7 +178,7 @@ fn test_run_funcs_with_lines() {
let line = line.unwrap(); let line = line.unwrap();
lines.push(Line::from_stdout(&line)); lines.push(Line::from_stdout(&line));
assert_eq!(line, "hello"); assert_eq!(line, "hello");
Command::new("bash").arg("-c").arg("echo stderr >> ./tmp-run_funcs_with_lines") command!("bash", "-c", "echo stderr >> ./tmp-run_funcs_with_lines") // col
.output() .output()
.unwrap(); // oops sorry lol .unwrap(); // oops sorry lol
} }