use std::ffi::OsString;
const ON_A_TERMINAL: &str = "AGIX_TEST_ON_A_TERMINAL";
pub const PROBE: &str =
"{ true <>/dev/tty; } 2>/dev/null && echo has-a-terminal || echo has-no-terminal";
pub fn on_a_terminal_of_its_own(
module: &str,
test: &str,
env: &[(&str, OsString)],
body: impl FnOnce(),
) {
if std::env::var_os(ON_A_TERMINAL).is_some() {
assert!(
std::fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/tty")
.is_ok(),
"the test did not get a terminal of its own"
);
return body();
}
let (mut master, mut slave) = (-1, -1);
let opened = unsafe {
libc::openpty(
&mut master,
&mut slave,
std::ptr::null_mut(),
std::ptr::null_mut(),
std::ptr::null_mut(),
)
};
assert_eq!(opened, 0, "openpty: {}", std::io::Error::last_os_error());
let filter = match module.split_once("::") {
Some((_, inner)) => format!("{inner}::{test}"),
None => test.to_string(),
};
let exe = std::env::current_exe().unwrap_or_else(|e| panic!("current_exe: {e}"));
let mut again = std::process::Command::new(exe);
again
.args([
filter.as_str(),
"--exact",
"--nocapture",
"--test-threads=1",
])
.env(ON_A_TERMINAL, "1")
.envs(env.iter().map(|(k, v)| (k, v)))
.stdin(std::process::Stdio::null());
unsafe {
std::os::unix::process::CommandExt::pre_exec(&mut again, move || {
if libc::setsid() == -1 || libc::ioctl(slave, libc::TIOCSCTTY as _, 0) == -1 {
return Err(std::io::Error::last_os_error());
}
Ok(())
});
}
let out = again.output();
unsafe {
libc::close(slave);
libc::close(master);
}
let out = out.unwrap_or_else(|e| panic!("running {filter} again: {e}"));
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
out.status.success() && stdout.contains("1 passed"),
"{filter}: {}\n{stdout}\n{}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
}