repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
smith-core/tests/support/child.rs
Raw//! SMH-SPEC-SPEC0001: controlled child fixtures with bounded output and a deadline.
use std::io::Read;
use std::process::{Command, Output, Stdio};
use std::sync::mpsc;
use std::time::Duration;
pub fn run(command: &mut Command) -> Output {
let mut child = command
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
let (sender, receiver) = mpsc::sync_channel(1);
let reader = std::thread::spawn(move || {
let mut output = Vec::new();
let mut errors = Vec::new();
stdout.take(128 * 1024).read_to_end(&mut output).unwrap();
stderr.take(128 * 1024).read_to_end(&mut errors).unwrap();
sender.send((output, errors)).unwrap();
});
let result = receiver.recv_timeout(Duration::from_secs(30));
let exceeded = result
.as_ref()
.is_ok_and(|(output, errors)| output.len() == 128 * 1024 || errors.len() == 128 * 1024);
if result.is_err() || exceeded {
// Child remains unreaped and exclusively owned here: kill cannot target
// a reused PID. Closing its pipes also releases the bounded reader.
let _kill_result = child.kill();
}
// Controlled libtest children retain both standard streams until exit;
// EOF is readiness, the channel supplies its deadline, then reap exactly once.
let status = child.wait().unwrap();
reader.join().unwrap();
let (stdout, stderr) = result.expect("allocation fixture child exceeded its deadline");
assert!(
!exceeded,
"allocation fixture child exceeded its output bound"
);
assert!(
String::from_utf8_lossy(&stdout).contains("running 1 test"),
"allocation fixture must execute exactly one named test"
);
Output {
status,
stdout,
stderr,
}
}