run_make_support/
run.rs

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