Debian Codegraph @

tests/integration/util.rs

raw symbols SCIP
1 use std::{env, ffi::OsString, path::Path, process::Command};
2
3 #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
4 use portable_pty::{Child, CommandBuilder, MasterPty, PtySize, native_pty_system};
5 use rustc_hash::FxHashMap as HashMap;
6
7 pub fn abs_path(path: &str) -> OsString {
8 let path = Path::new(path);
9
10 if path.exists() {
11 path.canonicalize().unwrap().into_os_string()
12 } else {
13 // We are going to trust that the path given is valid...
14 path.to_owned().into_os_string()
15 }
16 }
17
18 /// Returns a QEMU runner target given an architecture.
19 fn get_qemu_target(arch: &str) -> &str {
20 match arch {
21 "armv7" => "arm",
22 "i686" => "i386",
23 "powerpc" => "ppc",
24 "powerpc64le" => "ppc64le",
25 _ => arch,
26 }
27 }
28
29 /// This is required since running binary tests via cross can cause be tricky!
30 /// We need to basically "magically" grab the correct runner in some cases,
31 /// which can be done by inspecting env variables that should only show up while
32 /// using cross.
33 ///
34 /// Originally inspired by [ripgrep's test files](https://cs.github.com/BurntSushi/ripgrep/blob/9f0e88bcb14e02da1b88872435b17d74786640b5/tests/util.rs#L470),
35 /// but adapted to work more generally with the architectures supported by
36 /// bottom after looking through cross' [linux-runner](https://github.com/cross-rs/cross/blob/main/docker/linux-runner) file.
37 fn cross_runner() -> Option<String> {
38 const TARGET_RUNNER: &str = "CARGO_TARGET_RUNNER";
39 const CROSS_RUNNER: &str = "CROSS_RUNNER";
40
41 let env_mapping = env::vars_os()
42 .filter_map(|(k, v)| {
43 let (k, v) = (k.to_string_lossy(), v.to_string_lossy());
44
45 if k.starts_with("CARGO_TARGET_") && k.ends_with("_RUNNER") && !v.is_empty() {
46 Some((TARGET_RUNNER.to_string(), v.to_string()))
47 } else if k == CROSS_RUNNER && !v.is_empty() {
48 Some((k.to_string(), v.to_string()))
49 } else {
50 None
51 }
52 })
53 .collect::<HashMap<_, _>>();
54
55 if let Some(cross_runner) = env_mapping.get(CROSS_RUNNER) {
56 if cross_runner == "qemu-user" {
57 env_mapping.get(TARGET_RUNNER).map(|target_runner| {
58 format!(
59 "qemu-{}",
60 get_qemu_target(target_runner.split_ascii_whitespace().last().unwrap())
61 )
62 })
63 } else {
64 None
65 }
66 } else {
67 env_mapping.get(TARGET_RUNNER).cloned()
68 }
69 }
70
71 const BTM_EXE_PATH: &str = env!("CARGO_BIN_EXE_btm");
72 const RUNNER_ENV_VARS: [(&str, &str); 1] = [("NO_COLOR", "1")];
73 const DEFAULT_CFG: [&str; 2] = ["-C", "./tests/valid_configs/empty_config.toml"];
74
75 /// Returns the [`Command`] of a binary invocation of bottom, alongside
76 /// any required env variables.
77 pub fn btm_command(args: &[&str]) -> Command {
78 let mut cmd = match cross_runner() {
79 None => Command::new(BTM_EXE_PATH),
80 Some(runner) => {
81 let mut cmd = Command::new(runner);
82 cmd.envs(RUNNER_ENV_VARS);
83 cmd.arg(BTM_EXE_PATH);
84 cmd
85 }
86 };
87
88 let mut prev = "";
89 for arg in args.iter() {
90 if prev == "-C" {
91 // This is the config file; make sure we set it to absolute path!
92 cmd.arg(abs_path(arg));
93 } else {
94 cmd.arg(arg);
95 }
96
97 prev = arg;
98 }
99
100 cmd
101 }
102
103 /// Returns the [`Command`] of a binary invocation of bottom, alongside
104 /// any required env variables, and with the default, empty config file.
105 pub fn no_cfg_btm_command() -> Command {
106 btm_command(&DEFAULT_CFG)
107 }
108
109 /// Spawns `btm` in a pty, returning the pair alongside a handle to the child.
110 #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
111 pub fn spawn_btm_in_pty(args: &[&str]) -> (Box<dyn MasterPty>, Box<dyn Child>) {
112 let native_pty = native_pty_system();
113
114 let pair = native_pty
115 .openpty(PtySize {
116 rows: 100,
117 cols: 100,
118 pixel_width: 1,
119 pixel_height: 1,
120 })
121 .unwrap();
122
123 let btm_exe = BTM_EXE_PATH;
124 let mut cmd = match cross_runner() {
125 None => CommandBuilder::new(btm_exe),
126 Some(runner) => {
127 let mut cmd = CommandBuilder::new(runner);
128 for (env, val) in RUNNER_ENV_VARS {
129 cmd.env(env, val);
130 }
131 cmd.arg(BTM_EXE_PATH);
132
133 cmd
134 }
135 };
136
137 let args = if args.is_empty() { &DEFAULT_CFG } else { args };
138 let mut prev = "";
139 for arg in args.iter() {
140 if prev == "-C" {
141 // This is the config file; make sure we set it to absolute path!
142 cmd.arg(abs_path(arg));
143 } else {
144 cmd.arg(arg);
145 }
146
147 prev = arg;
148 }
149
150 (pair.master, pair.slave.spawn_command(cmd).unwrap())
151 }