use std::io;
use std::process::{Child, Command, ExitStatus, Output, Stdio};
#[cfg(windows)]
use std::os::windows::process::CommandExt as _;
#[path = "../tests/common/console_flags.rs"]
pub(crate) mod console_flags;
thread_local! {
static BETWEEN_SPAWN_AND_WAIT: crate::oneshot_hook::OneShotHook = const { crate::oneshot_hook::OneShotHook::new() };
}
#[cfg_attr(not(unix), allow(dead_code, reason = "only the unix helper tests arm it"))]
pub(crate) fn set_between_spawn_and_wait(hook: impl FnOnce() + 'static) -> crate::oneshot_hook::Armed {
crate::oneshot_hook::arm(&BETWEEN_SPAWN_AND_WAIT, hook)
}
fn run_between_spawn_and_wait() {
crate::oneshot_hook::fire(&BETWEEN_SPAWN_AND_WAIT);
}
struct Held {
_guard: crate::child::spawn::SpawnLockGuard,
}
impl Held {
fn take() -> Self {
Self {
_guard: crate::child::spawn::spawn_lock(),
}
}
}
#[cfg(unix)]
pub(crate) fn held_by_this_thread() -> bool {
crate::child::spawn::spawn_lock_held_by_this_thread()
}
pub(crate) fn spawn(cmd: &mut Command) -> io::Result<Child> {
#[cfg(windows)]
cmd.creation_flags(console_flags::word(0, console_flags::Console::Hidden));
spawn_held(cmd)
}
#[cfg(windows)]
pub(crate) fn spawn_with(cmd: &mut Command, flags: u32, console: console_flags::Console) -> io::Result<Child> {
cmd.creation_flags(console_flags::word(flags, console));
spawn_held(cmd)
}
fn spawn_held(cmd: &mut Command) -> io::Result<Child> {
let _held = Held::take();
#[allow(clippy::disallowed_methods, reason = "`_held` is spawn_lock")]
cmd.spawn()
}
#[cfg_attr(
not(unix),
allow(dead_code, reason = "the ban names it; only unix lib tests call it so far")
)]
pub(crate) fn output_captured(cmd: &mut Command) -> io::Result<Output> {
cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped());
let child = spawn(cmd)?;
run_between_spawn_and_wait();
child.wait_with_output()
}
#[cfg_attr(
not(unix),
allow(dead_code, reason = "the ban names it; only unix lib tests call it so far")
)]
pub(crate) fn status(cmd: &mut Command) -> io::Result<ExitStatus> {
let mut child = spawn(cmd)?;
run_between_spawn_and_wait();
child.wait()
}
#[cfg(feature = "tokio")]
pub(crate) fn spawn_tokio(cmd: &mut ::tokio::process::Command) -> io::Result<::tokio::process::Child> {
#[cfg(windows)]
cmd.creation_flags(console_flags::word(0, console_flags::Console::Hidden));
let _held = Held::take();
#[allow(clippy::disallowed_methods, reason = "`_held` is spawn_lock")]
cmd.spawn()
}
#[cfg(feature = "tokio")]
#[cfg_attr(
not(unix),
allow(dead_code, reason = "the ban names it; only unix lib tests call it so far")
)]
pub(crate) async fn output_captured_tokio(cmd: &mut ::tokio::process::Command) -> io::Result<Output> {
cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped());
let child = spawn_tokio(cmd)?;
run_between_spawn_and_wait();
child.wait_with_output().await
}
#[cfg(feature = "tokio")]
#[cfg_attr(
not(unix),
allow(dead_code, reason = "the ban names it; only unix lib tests call it so far")
)]
pub(crate) async fn status_tokio(cmd: &mut ::tokio::process::Command) -> io::Result<ExitStatus> {
let mut child = spawn_tokio(cmd)?;
run_between_spawn_and_wait();
child.wait().await
}
#[cfg(test)]
#[path = "test_spawn_tests.rs"]
mod tests;