misc improvements

notably, run() is better and no longer uses channels
This commit is contained in:
askiiart 2025-01-11 18:06:29 -06:00
parent 98192dd46a
commit 5deaaa4265
Signed by untrusted user who does not match committer: askiiart
GPG key ID: 6A32977DAF31746A
2 changed files with 30 additions and 50 deletions

View file

@ -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<Line> = 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<Line> = 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::<Vec<Line>>();
lines.append(&mut stderr_rx.into_iter().collect::<Vec<Line>>());
let mut lines = stdout_thread.join().unwrap();
lines.append(&mut stderr_thread.join().unwrap());
lines.sort();
return CmdOutput {

View file

@ -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::<Vec<String>>()
)
);
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::<Vec<String>>();
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<Line> = 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
}