use std::os::unix::process::CommandExt as _;
const CHILD_VARIABLE: &str = "CLI_GIT_UMASK_CHILD";
pub(crate) fn ran_in_child(module: &str, name: &str, mask: u32) -> bool {
if std::env::var_os(CHILD_VARIABLE).is_some() {
return false;
}
let path: String = format!("{}::{name}", module.split_once("::").map_or(module, |(_, rest)| { return rest; }));
let mut command: std::process::Command = std::process::Command::new(crate::test_support::this_test_binary().expect("the test binary"));
command.args(["--exact", path.as_str(), "--nocapture"]).env(CHILD_VARIABLE, "1");
unsafe {
command.pre_exec(move || {
libc::umask(mask as libc::mode_t);
return Ok(());
});
}
command.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::piped());
let child: std::process::Child = crate::test_support::spawn_owned(&mut command).expect("start the child test");
let output: std::process::Output = child.wait_with_output().expect("child test");
let said: String = String::from_utf8_lossy(&output.stdout).into_owned();
assert!(output.status.success(), "the child test {path} under umask {mask:o} failed:\n{said}\n{}", String::from_utf8_lossy(&output.stderr));
assert!(said.contains("running 1 test") && said.contains("1 passed"), "the child ran no test named {path}:\n{said}");
return true;
}