Skip to main content

run_make_support/
run.rs

1use std::env;
2use std::ffi::{OsStr, OsString};
3use std::path::PathBuf;
4
5use crate::command::{Command, CompletedProcess};
6use crate::{cwd, env_var};
7
8#[track_caller]
9fn run_common(name: &str, args: Option<&[&str]>) -> Command {
10    let mut bin_path = PathBuf::new();
11    bin_path.push(cwd());
12    bin_path.push(name);
13    let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
14
15    let mut cmd = if let Some(rtc) = env::var_os("REMOTE_TEST_CLIENT") {
16        let mut cmd = Command::new(rtc);
17        cmd.arg("run");
18        // FIXME: the "0" indicates how many support files should be uploaded along with the binary
19        // to execute. If a test requires additional files to be pushed to the remote machine, this
20        // will have to be changed (and the support files will have to be uploaded).
21        cmd.arg("0");
22        cmd.arg(bin_path);
23        cmd
24    } else if let Ok(runner) = std::env::var("RUNNER") {
25        let mut args = split_maybe_args(&runner);
26
27        let prog = args.remove(0);
28        let mut cmd = Command::new(prog);
29
30        for arg in args {
31            cmd.arg(arg);
32        }
33
34        cmd.arg("--");
35        cmd.arg(bin_path);
36
37        cmd
38    } else {
39        Command::new(bin_path)
40    };
41
42    if let Some(args) = args {
43        for arg in args {
44            cmd.arg(arg);
45        }
46    }
47
48    cmd.env(&ld_lib_path_envvar, {
49        let mut paths = vec![];
50        paths.push(cwd());
51        for p in env::split_paths(&env_var("TARGET_EXE_DYLIB_PATH")) {
52            paths.push(p.to_path_buf());
53        }
54        for p in env::split_paths(&env_var(&ld_lib_path_envvar)) {
55            paths.push(p.to_path_buf());
56        }
57        env::join_paths(paths.iter()).unwrap()
58    });
59    cmd.env("LC_ALL", "C"); // force english locale
60
61    cmd.inspect(|std_cmd| eprintln!("running: {std_cmd:?}"));
62    cmd
63}
64
65/// Run a built binary and make sure it succeeds.
66#[track_caller]
67pub fn run(name: &str) -> CompletedProcess {
68    run_common(name, None).run()
69}
70
71/// Run a built binary with one or more argument(s) and make sure it succeeds.
72#[track_caller]
73pub fn run_with_args(name: &str, args: &[&str]) -> CompletedProcess {
74    run_common(name, Some(args)).run()
75}
76
77/// Run a built binary and make sure it fails.
78#[track_caller]
79pub fn run_fail(name: &str) -> CompletedProcess {
80    run_common(name, None).run_fail()
81}
82
83/// Create a new custom [`Command`]. This should be preferred to creating [`std::process::Command`]
84/// directly.
85#[track_caller]
86pub fn cmd<S: AsRef<OsStr>>(program: S) -> Command {
87    let mut command = Command::new(program);
88    command.env("LC_ALL", "C"); // force english locale
89    command
90}
91
92fn split_maybe_args(s: &str) -> Vec<OsString> {
93    // FIXME(132599): implement proper env var/shell argument splitting.
94    s.split(' ')
95        .filter_map(|s| {
96            if s.chars().all(|c| c.is_whitespace()) { None } else { Some(OsString::from(s)) }
97        })
98        .collect()
99}