Compare commits
2 commits
2597ea1233
...
d030ac779d
Author | SHA1 | Date | |
---|---|---|---|
|
d030ac779d | ||
|
e0b962b00f |
2 changed files with 37 additions and 31 deletions
|
@ -251,8 +251,8 @@ pub fn run(command: &mut Command) -> CmdOutput {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn run_funcs(
|
pub fn run_funcs(
|
||||||
command: &mut Command,
|
command: &mut Command,
|
||||||
stdout_func: impl Fn(Lines<BufReader<ChildStdout>>) -> () + std::marker::Send + 'static,
|
stdout_func: impl FnOnce(Lines<BufReader<ChildStdout>>) -> () + std::marker::Send + 'static,
|
||||||
stderr_func: impl Fn(Lines<BufReader<ChildStderr>>) -> () + std::marker::Send + 'static,
|
stderr_func: impl FnOnce(Lines<BufReader<ChildStderr>>) -> () + std::marker::Send + 'static,
|
||||||
) -> CmdOutput {
|
) -> CmdOutput {
|
||||||
// https://stackoverflow.com/a/72831067/16432246
|
// https://stackoverflow.com/a/72831067/16432246
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
@ -330,8 +330,8 @@ pub fn run_funcs(
|
||||||
/// ```
|
/// ```
|
||||||
pub fn run_funcs_with_lines(
|
pub fn run_funcs_with_lines(
|
||||||
command: &mut Command,
|
command: &mut Command,
|
||||||
stdout_func: impl Fn(Lines<BufReader<ChildStdout>>) -> Vec<Line> + std::marker::Send + 'static,
|
stdout_func: impl FnOnce(Lines<BufReader<ChildStdout>>) -> Vec<Line> + std::marker::Send + 'static,
|
||||||
stderr_func: impl Fn(Lines<BufReader<ChildStderr>>) -> Vec<Line> + std::marker::Send + 'static,
|
stderr_func: impl FnOnce(Lines<BufReader<ChildStderr>>) -> Vec<Line> + std::marker::Send + 'static,
|
||||||
) -> CmdOutput {
|
) -> CmdOutput {
|
||||||
// https://stackoverflow.com/a/72831067/16432246
|
// https://stackoverflow.com/a/72831067/16432246
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
60
src/tests.rs
60
src/tests.rs
|
@ -1,12 +1,13 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::os::unix::fs::FileExt;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::{
|
use std::{
|
||||||
fs::remove_file,
|
fs::remove_file,
|
||||||
hash::{BuildHasher, Hasher, RandomState},
|
hash::{BuildHasher, Hasher, RandomState},
|
||||||
};
|
};
|
||||||
use std::{fs::File, thread::sleep};
|
use std::{fs::File, thread::sleep};
|
||||||
use std::os::unix::fs::FileExt;
|
|
||||||
|
|
||||||
/// Tests what stdout prints
|
/// Tests what stdout prints
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -88,6 +89,8 @@ fn shuffle_vec<T>(vec: &mut [T]) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_run_funcs() {
|
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(|| {
|
let threads = thread::spawn(|| {
|
||||||
return run_funcs(
|
return run_funcs(
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
|
@ -97,11 +100,12 @@ 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")
|
let mut f = File::options()
|
||||||
.arg("-c")
|
.write(true)
|
||||||
.arg("echo stdout >> ./tmp-run_funcs") // col
|
.open("./tmp-run_funcs")
|
||||||
.output()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
f.write_all(b"stdout\n").unwrap();
|
||||||
|
drop(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -109,11 +113,12 @@ 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")
|
let f = File::options()
|
||||||
.arg("-c")
|
.write(true)
|
||||||
.arg("echo stderr >> ./tmp-run_funcs") // col
|
.open("./tmp-run_funcs")
|
||||||
.output()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
f.write_at(b"stderr\n", 7).unwrap();
|
||||||
|
drop(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -140,6 +145,8 @@ fn test_run_funcs() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_run_funcs_with_lines() {
|
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(|| {
|
let threads = thread::spawn(|| {
|
||||||
return run_funcs_with_lines(
|
return run_funcs_with_lines(
|
||||||
&mut Command::new("bash")
|
&mut Command::new("bash")
|
||||||
|
@ -153,11 +160,12 @@ 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")
|
let mut f = File::options()
|
||||||
.arg("-c")
|
.write(true)
|
||||||
.arg("echo stdout >> ./tmp-run_funcs_with_lines")
|
.open("./tmp-run_funcs_with_lines")
|
||||||
.output()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
f.write_all(b"stdout\n").unwrap();
|
||||||
|
drop(f);
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
@ -170,11 +178,13 @@ 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")
|
let mut f = File::options()
|
||||||
.arg("-c")
|
.write(true)
|
||||||
.arg("echo stderr >> ./tmp-run_funcs_with_lines")
|
.append(true)
|
||||||
.output()
|
.open("./tmp-run_funcs_with_lines")
|
||||||
.unwrap(); // oops sorry lol
|
.unwrap();
|
||||||
|
f.write(b"stderr\n").unwrap();
|
||||||
|
drop(f);
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
@ -182,17 +192,13 @@ fn test_run_funcs_with_lines() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
sleep(Duration::from_secs(2));
|
sleep(Duration::from_secs(2));
|
||||||
let f = File::open("./tmp-run_funcs_with_lines").unwrap();
|
let read = std::fs::read_to_string("tmp-run_funcs_with_lines").unwrap();
|
||||||
let mut buf: [u8; 14] = [0u8; 14];
|
assert_eq!(read, "stdout\n");
|
||||||
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));
|
sleep(Duration::from_secs(2));
|
||||||
f.read_at(&mut buf, 0).unwrap();
|
let read = std::fs::read_to_string("tmp-run_funcs_with_lines").unwrap();
|
||||||
assert_eq!(
|
assert_eq!(read, "stdout\nstderr\n");
|
||||||
buf,
|
|
||||||
[115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10]
|
|
||||||
);
|
|
||||||
|
|
||||||
remove_file("./tmp-run_funcs_with_lines").unwrap();
|
remove_file("./tmp-run_funcs_with_lines").unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue