use std::process::{Output, Stdio};
pub async fn output_with_tree_kill(mut cmd: tokio::process::Command) -> std::io::Result<Output> {
cmd.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true);
let child = cmd.spawn()?;
#[cfg(target_os = "windows")]
let _job = crate::supervisor::JobObject::new().ok().inspect(|j| {
if let Some(pid) = child.id() {
let _ = j.assign(pid);
}
});
child.wait_with_output().await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn runs_and_captures_output() {
let cmd = if cfg!(windows) {
let mut c = tokio::process::Command::new("cmd");
c.args(["/C", "echo hi"]);
c
} else {
let mut c = tokio::process::Command::new("sh");
c.args(["-c", "echo hi"]);
c
};
let out = output_with_tree_kill(cmd).await.expect("command runs");
assert!(out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("hi"));
}
}