pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;
pub const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;
pub fn command_std(program: impl AsRef<std::ffi::OsStr>) -> std::process::Command {
let mut command = std::process::Command::new(program);
command.no_window();
command
}
pub trait NoWindow {
fn no_window(&mut self) -> &mut Self;
}
impl NoWindow for std::process::Command {
fn no_window(&mut self) -> &mut Self {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
self.creation_flags(CREATE_NO_WINDOW);
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_flag_value_is_the_windows_constant() {
assert_eq!(CREATE_NO_WINDOW, 0x0800_0000);
}
#[test]
fn breakaway_flag_is_the_windows_constant() {
assert_eq!(CREATE_BREAKAWAY_FROM_JOB, 0x0100_0000);
}
#[test]
fn command_std_returns_a_ready_command_by_value() {
let command = command_std(if cfg!(windows) { "cmd" } else { "true" });
assert_eq!(
command.get_program(),
if cfg!(windows) { "cmd" } else { "true" }
);
}
#[test]
fn a_std_command_chains_and_still_runs() {
let mut command = if cfg!(windows) {
let mut c = std::process::Command::new("cmd");
c.args(["/C", "exit 0"]);
c
} else {
std::process::Command::new("true")
};
let status = command.no_window().status().expect("run");
assert!(status.success());
}
}