use processkit::Command;
pub(crate) fn five_lines() -> Command {
if cfg!(windows) {
Command::new("cmd").args(["/c", "echo 1& echo 2& echo 3& echo 4& echo 5"])
} else {
Command::new("sh").args(["-c", "printf '1\\n2\\n3\\n4\\n5\\n'"])
}
}
pub(crate) fn two_line_echo() -> Command {
if cfg!(windows) {
Command::new("cmd").args(["/c", "echo first& echo second"])
} else {
Command::new("sh").args(["-c", "printf 'first\\nsecond\\n'"])
}
}
pub(crate) fn sleeper() -> Command {
if cfg!(windows) {
Command::new("cmd").args(["/c", "ping", "-n", "30", "127.0.0.1"])
} else {
Command::new("sleep").arg("30")
}
}
pub(crate) fn sleep_secs(secs: u32) -> Command {
if cfg!(windows) {
Command::new("ping").args([
"-n".to_string(),
(secs + 1).to_string(),
"127.0.0.1".to_string(),
])
} else {
Command::new("sleep").arg(secs.to_string())
}
}
pub(crate) fn banner_then_idle() -> Command {
if cfg!(windows) {
Command::new("cmd").args([
"/c",
"ping -n 2 127.0.0.1 >nul & echo ready & ping -n 30 127.0.0.1 >nul",
])
} else {
Command::new("sh").args(["-c", "sleep 0.5; echo ready; sleep 30"])
}
}
pub(crate) fn endless_yes() -> Command {
if cfg!(windows) {
Command::new("powershell").args(["-NoProfile", "-Command", "while($true){'y'}"])
} else {
Command::new("yes")
}
}
pub(crate) fn first_line_consumer() -> Command {
if cfg!(windows) {
Command::new("powershell").args(["-NoProfile", "-Command", "[Console]::In.ReadLine()"])
} else {
Command::new("head").args(["-n", "1"])
}
}
pub(crate) fn failing_exit(code: i32) -> Command {
if cfg!(windows) {
Command::new("cmd").args(["/c", "exit", &code.to_string()])
} else {
Command::new("sh").args(["-c", &format!("exit {code}")])
}
}
pub(crate) fn print_env() -> Command {
if cfg!(windows) {
Command::new("cmd").args(["/c", "set"])
} else {
Command::new("sh").args(["-c", "env"])
}
}
#[cfg(windows)]
pub(crate) fn windows_pid_alive(pid: u32) -> bool {
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION};
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
if handle.is_null() {
return false;
}
unsafe { CloseHandle(handle) };
true
}