Compare commits
No commits in common. "1f1070b90c7410a482d50d8a3d3ee5a71cf11e2c" and "ba92ea03b38d71dac85e30da55e78cb4995b5bbd" have entirely different histories.
1f1070b90c
...
ba92ea03b3
2 changed files with 44 additions and 116 deletions
93
src/lib.rs
93
src/lib.rs
|
@ -19,26 +19,46 @@ pub struct CmdOutput {
|
||||||
impl CmdOutput {
|
impl CmdOutput {
|
||||||
/// Returns only stdout
|
/// Returns only stdout
|
||||||
pub fn stdout(self) -> Option<Vec<Line>> {
|
pub fn stdout(self) -> Option<Vec<Line>> {
|
||||||
self.lines.and_then(|lines| {
|
match self.lines {
|
||||||
Some(
|
Some(lines) => {
|
||||||
lines
|
return Some(
|
||||||
.into_iter()
|
lines
|
||||||
.filter(|line| line.printed_to == LineType::Stdout)
|
.into_iter()
|
||||||
.collect(),
|
.filter(|l| {
|
||||||
)
|
if l.printed_to == LineType::Stdout {
|
||||||
})
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns only stdout
|
/// Returns only stdout
|
||||||
pub fn stderr(self) -> Option<Vec<Line>> {
|
pub fn stderr(self) -> Option<Vec<Line>> {
|
||||||
self.lines.and_then(|lines| {
|
match self.lines {
|
||||||
Some(
|
Some(lines) => {
|
||||||
lines
|
return Some(
|
||||||
.into_iter()
|
lines
|
||||||
.filter(|line| line.printed_to == LineType::Stderr)
|
.into_iter()
|
||||||
.collect(),
|
.filter(|l| {
|
||||||
)
|
if l.printed_to == LineType::Stderr {
|
||||||
})
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all output
|
/// Returns all output
|
||||||
|
@ -195,7 +215,7 @@ pub fn run(command: &mut Command) -> CmdOutput {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_funcs(
|
pub fn run_with_funcs(
|
||||||
command: &mut Command,
|
command: &mut Command,
|
||||||
stdout_func: impl Fn(Lines<BufReader<ChildStdout>>) -> () + std::marker::Send + 'static,
|
stdout_func: impl Fn(Lines<BufReader<ChildStdout>>) -> () + std::marker::Send + 'static,
|
||||||
stderr_func: impl Fn(Lines<BufReader<ChildStderr>>) -> () + std::marker::Send + 'static,
|
stderr_func: impl Fn(Lines<BufReader<ChildStderr>>) -> () + std::marker::Send + 'static,
|
||||||
|
@ -231,42 +251,3 @@ pub fn run_funcs(
|
||||||
duration: end.duration_since(start),
|
duration: end.duration_since(start),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_funcs_with_lines(
|
|
||||||
command: &mut Command,
|
|
||||||
stdout_func: impl Fn(Lines<BufReader<ChildStdout>>) -> Vec<Line> + std::marker::Send + 'static,
|
|
||||||
stderr_func: impl Fn(Lines<BufReader<ChildStderr>>) -> Vec<Line> + std::marker::Send + 'static,
|
|
||||||
) -> CmdOutput {
|
|
||||||
// https://stackoverflow.com/a/72831067/16432246
|
|
||||||
let start = Instant::now();
|
|
||||||
let mut child = command
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stderr(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let child_stdout = child.stdout.take().unwrap();
|
|
||||||
let child_stderr = child.stderr.take().unwrap();
|
|
||||||
|
|
||||||
let stdout_lines = BufReader::new(child_stdout).lines();
|
|
||||||
let stderr_lines = BufReader::new(child_stderr).lines();
|
|
||||||
|
|
||||||
let stdout_thread = thread::spawn(move || stdout_func(stdout_lines));
|
|
||||||
let stderr_thread = thread::spawn(move || stderr_func(stderr_lines));
|
|
||||||
|
|
||||||
let mut lines = stdout_thread.join().unwrap();
|
|
||||||
let mut lines_printed_to_stderr = stderr_thread.join().unwrap();
|
|
||||||
lines.append(&mut lines_printed_to_stderr);
|
|
||||||
lines.sort();
|
|
||||||
|
|
||||||
let status = child.wait().unwrap().code();
|
|
||||||
let end = Instant::now();
|
|
||||||
|
|
||||||
return CmdOutput {
|
|
||||||
lines: Some(lines),
|
|
||||||
status_code: status,
|
|
||||||
start_time: start,
|
|
||||||
end_time: end,
|
|
||||||
duration: end.duration_since(start),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
67
src/tests.rs
67
src/tests.rs
|
@ -88,19 +88,19 @@ fn shuffle_vec<T>(vec: &mut [T]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_run_funcs() {
|
fn test_run_with_funcs() {
|
||||||
let _ = thread::spawn(|| {
|
let _ = thread::spawn(|| {
|
||||||
let _ = run_funcs(
|
let _ = run_with_funcs(
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg("echo hi; >&2 echo hello"),
|
.arg("echo hi; sleep 0.5; >&2 echo hello"),
|
||||||
{
|
{
|
||||||
|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")
|
Command::new("bash")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg("echo stdout >> ./tmp-run_runcs")
|
.arg("echo stdout >> ./tmp")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ fn test_run_funcs() {
|
||||||
for _ in stderr_lines {
|
for _ in stderr_lines {
|
||||||
Command::new("bash")
|
Command::new("bash")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg("echo stderr >> ./tmp-run_runcs")
|
.arg("echo stderr >> ./tmp")
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ fn test_run_funcs() {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
sleep(Duration::from_secs(2));
|
sleep(Duration::from_secs(2));
|
||||||
let f = File::open("./tmp-run_runcs").unwrap();
|
let f = File::open("./tmp").unwrap();
|
||||||
let mut buf: [u8; 14] = [0u8; 14];
|
let mut buf: [u8; 14] = [0u8; 14];
|
||||||
f.read_at(&mut buf, 0).unwrap();
|
f.read_at(&mut buf, 0).unwrap();
|
||||||
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]);
|
assert_eq!(buf, [115, 116, 100, 111, 117, 116, 10, 0, 0, 0, 0, 0, 0, 0]);
|
||||||
|
@ -133,58 +133,5 @@ fn test_run_funcs() {
|
||||||
[115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10]
|
[115, 116, 100, 111, 117, 116, 10, 115, 116, 100, 101, 114, 114, 10]
|
||||||
);
|
);
|
||||||
|
|
||||||
remove_file("./tmp-run_runcs").unwrap();
|
remove_file("./tmp").unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_run_funcs_with_lines() {
|
|
||||||
let _ = thread::spawn(|| {
|
|
||||||
let _ = run_funcs_with_lines(
|
|
||||||
Command::new("bash")
|
|
||||||
.arg("-c")
|
|
||||||
.arg("echo hi; >&2 echo hello"),
|
|
||||||
{
|
|
||||||
|stdout_lines| {
|
|
||||||
sleep(Duration::from_secs(1));
|
|
||||||
for line in stdout_lines {
|
|
||||||
assert_eq!(line.unwrap(), "hi");
|
|
||||||
Command::new("bash")
|
|
||||||
.arg("-c")
|
|
||||||
.arg("echo stdout >> ./tmp-run_runcs_with_lines")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
|stderr_lines| {
|
|
||||||
sleep(Duration::from_secs(3));
|
|
||||||
for line in stderr_lines {
|
|
||||||
assert_eq!(line.unwrap(), "hello");
|
|
||||||
Command::new("bash")
|
|
||||||
.arg("-c")
|
|
||||||
.arg("echo stderr >> ./tmp-run_runcs_with_lines")
|
|
||||||
.output()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
sleep(Duration::from_secs(2));
|
|
||||||
let f = File::open("./tmp-run_runcs_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));
|
|
||||||
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_runcs_with_lines").unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue