#![allow(dead_code)]
use std::process::{Child, Command, ExitStatus, Stdio};
use std::time::{Duration, Instant};
pub fn docsrs_cli_bin() -> &'static str {
env!("CARGO_BIN_EXE_docsrs-cli")
}
pub fn docsrs_cli_cmd() -> Command {
let mut c = Command::new(docsrs_cli_bin());
c.stdin(Stdio::null());
c.stdout(Stdio::piped());
c.stderr(Stdio::piped());
sanitize_child_env(&mut c);
c
}
pub fn docsrs_cli_cmd_silent() -> Command {
let mut c = Command::new(docsrs_cli_bin());
c.stdin(Stdio::null());
c.stdout(Stdio::null());
c.stderr(Stdio::null());
sanitize_child_env(&mut c);
c
}
fn sanitize_child_env(c: &mut Command) {
c.env_remove("RUST_LOG");
c.env_remove("LD_PRELOAD");
c.env_remove("LD_LIBRARY_PATH");
c.env_remove("LD_AUDIT");
c.env_remove("DYLD_INSERT_LIBRARIES");
c.env_remove("DYLD_LIBRARY_PATH");
}
pub fn wait_with_timeout(child: &mut Child, max: Duration) -> std::io::Result<ExitStatus> {
let start = Instant::now();
loop {
match child.try_wait()? {
Some(status) => return Ok(status),
None => {
if start.elapsed() >= max {
let _ = child.kill();
return child.wait();
}
std::thread::sleep(Duration::from_millis(25));
}
}
}
}