use std::process::Command;
pub fn get_bin(name: &str) -> Command {
let mut exe_path = std::env::current_exe().expect("getting current executable filename");
exe_path.pop();
if exe_path.ends_with("deps") {
exe_path.pop();
}
exe_path.push(name);
exe_path.set_extension(std::env::consts::EXE_EXTENSION);
if !exe_path.is_file() {
panic!(
"could not find executable for `{}` in the build directory",
name
);
}
Command::new(exe_path)
}
pub fn run_bin(cmd: &str, args: &[&str]) -> String {
let mut cmd = get_bin(cmd);
cmd.args(args);
let output = cmd.output().expect("running binary");
get_process_output(output)
}
pub fn get_process_output(output: std::process::Output) -> String {
let stderr = String::from_utf8_lossy(&output.stderr);
if !output.status.success() {
panic!(
"executable exited with error ({}){}",
output.status,
if stderr.len() > 0 {
format!(" and error output: {}", stderr)
} else {
"".into()
}
);
} else if stderr.len() > 0 {
panic!("executable generated error output: {}", stderr);
}
String::from_utf8(output.stdout).expect("reading output as utf-8")
}