#![allow(dead_code)]
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
pub const TIMEOUT: Duration = Duration::from_secs(15);
#[cfg(windows)]
pub mod flags {
pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;
pub const DETACHED_PROCESS: u32 = 0x0000_0008;
}
pub struct Scratch(PathBuf);
impl Scratch {
pub fn new(name: &str) -> Self {
let dir =
std::env::temp_dir().join(format!("terminator-test-{}-{name}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
Scratch(dir)
}
pub fn file(&self, name: &str) -> PathBuf {
self.0.join(name)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
pub fn cli_exe() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_terminator"))
}
pub fn victim_exe() -> PathBuf {
example_exe("victim")
}
pub fn example_exe(name: &str) -> PathBuf {
let file = format!("{name}{}", std::env::consts::EXE_SUFFIX);
let test_exe = std::env::current_exe().unwrap();
test_exe
.ancestors()
.skip(1)
.map(|dir| dir.join("examples").join(&file))
.find(|candidate| candidate.is_file())
.unwrap_or_else(|| {
panic!(
"examples/{file} not found above {}; run `cargo test` (it builds examples)",
test_exe.display()
)
})
}
pub struct Victim {
pub child: Child,
pub log: PathBuf,
}
impl Victim {
pub fn pid(&self) -> u32 {
self.child.id()
}
pub fn lines(&self) -> Vec<String> {
fs::read_to_string(&self.log)
.map(|s| s.lines().map(str::to_owned).collect())
.unwrap_or_default()
}
pub fn wait_line(&self, line: &str) -> bool {
wait_for_line(&self.log, line, TIMEOUT)
}
pub fn wait_exit(&mut self, timeout: Duration) -> bool {
let start = Instant::now();
while start.elapsed() < timeout {
if matches!(self.child.try_wait(), Ok(Some(_))) {
return true;
}
std::thread::sleep(Duration::from_millis(20));
}
false
}
}
impl Drop for Victim {
fn drop(&mut self) {
for pid in terminator::child_tree(self.child.id(), false) {
let _ = terminator::kill(pid);
}
let _ = self.child.kill();
let _ = self.child.wait();
}
}
pub fn spawn_victim(scratch: &Scratch, name: &str, args: &[&str], creation_flags: u32) -> Victim {
let log = scratch.file(&format!("{name}.log"));
let mut cmd = Command::new(victim_exe());
cmd.arg("--out")
.arg(&log)
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(creation_flags);
}
#[cfg(not(windows))]
let _ = creation_flags;
let child = cmd
.spawn()
.unwrap_or_else(|e| panic!("cannot start {}: {e}", victim_exe().display()));
Victim { child, log }
}
pub fn wait_for_line(log: &Path, line: &str, timeout: Duration) -> bool {
let start = Instant::now();
while start.elapsed() < timeout {
if fs::read_to_string(log).is_ok_and(|s| s.lines().any(|l| l == line)) {
return true;
}
std::thread::sleep(Duration::from_millis(20));
}
false
}
pub struct CliRun {
pub code: i32,
pub stdout: String,
pub stderr: String,
}
pub fn run_cli(creation_flags: u32, args: &[&str]) -> CliRun {
run_exe(&cli_exe(), creation_flags, args)
}
pub fn run_exe(exe: &Path, creation_flags: u32, args: &[&str]) -> CliRun {
let mut cmd = Command::new(exe);
cmd.env("TERMINATOR_DEBUG", "1").args(args);
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(creation_flags);
}
#[cfg(not(windows))]
let _ = creation_flags;
let out = cmd
.output()
.unwrap_or_else(|e| panic!("cannot run {}: {e}", exe.display()));
CliRun {
code: out.status.code().unwrap_or(-1),
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
}
}