Debian Codegraph @

tests/integration/util.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/, ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#};
2
3 #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
4 use portable_ptyrust-analyzer cargo portable-pty 0.9.0 crate/::{Childrust-analyzer cargo portable-pty 0.9.0 Child#, CommandBuilderrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/CommandBuilder#, MasterPtyrust-analyzer cargo portable-pty 0.9.0 MasterPty#, PtySizerust-analyzer cargo portable-pty 0.9.0 PtySize#, native_pty_systemrust-analyzer cargo portable-pty 0.9.0 native_pty_system().};
5 use rustc_hashrust-analyzer cargo rustc-hash 2.1.3 crate/::FxHashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap# as HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#;
6
7 pub fn abs_pathrust-analyzer cargo bottom 0.14.9 util/abs_path().(path: &str) -> OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString# {
8 let path = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(path);
9
10 if path.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
11 path.canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]canonicalize().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]into_os_string().()
12 } else {
13 // We are going to trust that the path given is valid...
14 path.to_ownedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path][ToOwned]to_owned().().into_os_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]into_os_string().()
15 }
16 }
17
18 /// Returns a QEMU runner target given an architecture.
19 fn get_qemu_targetrust-analyzer cargo bottom 0.14.9 util/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_runnerrust-analyzer cargo bottom 0.14.9 util/cross_runner().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
38 const TARGET_RUNNERrust-analyzer cargo bottom 0.14.9 util/TARGET_RUNNER.: &str = "CARGO_TARGET_RUNNER";
39 const CROSS_RUNNERrust-analyzer cargo bottom 0.14.9 util/CROSS_RUNNER.: &str = "CROSS_RUNNER";
40
41 let env_mapping = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::vars_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/vars_os().()
42 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|(k, v)| {
43 let (k, v) = (k.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(), v.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().());
44
45 if k.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("CARGO_TARGET_") && k.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().("_RUNNER") && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().v.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
46 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((TARGET_RUNNERrust-analyzer cargo bottom 0.14.9 util/TARGET_RUNNER..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), v.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))
47 } else if k rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'_, str>`][`PartialEq<&str>`]eq().CROSS_RUNNERrust-analyzer cargo bottom 0.14.9 util/CROSS_RUNNER. && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().v.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
48 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((k.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), v.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))
49 } else {
50 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
51 }
52 })
53 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashMaprust-analyzer cargo rustc-hash 2.1.3 FxHashMap#<_, _>>();
54
55 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cross_runner) = env_mapping.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(CROSS_RUNNERrust-analyzer cargo bottom 0.14.9 util/CROSS_RUNNER.) {
56 if cross_runner rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."qemu-user" {
57 env_mapping.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(TARGET_RUNNERrust-analyzer cargo bottom 0.14.9 util/TARGET_RUNNER.).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|target_runner| {
58 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
59 "qemu-{}",
60 get_qemu_targetrust-analyzer cargo bottom 0.14.9 util/get_qemu_target().(target_runner.split_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_ascii_whitespace().().lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitAsciiWhitespace<'a>`][Iterator]last().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().())
61 )
62 })
63 } else {
64 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
65 }
66 } else {
67 env_mapping.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(TARGET_RUNNERrust-analyzer cargo bottom 0.14.9 util/TARGET_RUNNER.).clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().()
68 }
69 }
70
71 const BTM_EXE_PATHrust-analyzer cargo bottom 0.14.9 util/BTM_EXE_PATH.: &str = envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_BIN_EXE_btm");
72 const RUNNER_ENV_VARSrust-analyzer cargo bottom 0.14.9 util/RUNNER_ENV_VARS.: [(&str, &str); 1] = [("NO_COLOR", "1")];
73 const DEFAULT_CFGrust-analyzer cargo bottom 0.14.9 util/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_commandrust-analyzer cargo bottom 0.14.9 util/btm_command().(args: &[&str]) -> Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command# {
78 let mut cmd = match cross_runnerrust-analyzer cargo bottom 0.14.9 util/cross_runner().() {
79 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(BTM_EXE_PATHrust-analyzer cargo bottom 0.14.9 util/BTM_EXE_PATH.),
80 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(runner) => {
81 let mut cmd = Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(runner);
82 cmd.envsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]envs().(RUNNER_ENV_VARSrust-analyzer cargo bottom 0.14.9 util/RUNNER_ENV_VARS.);
83 cmd.argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(BTM_EXE_PATHrust-analyzer cargo bottom 0.14.9 util/BTM_EXE_PATH.);
84 cmd
85 }
86 };
87
88 let mut prev = "";
89 for arg in args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
90 if prev rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."-C" {
91 // This is the config file; make sure we set it to absolute path!
92 cmd.argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(abs_pathrust-analyzer cargo bottom 0.14.9 util/abs_path().(arg));
93 } else {
94 cmd.argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]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_commandrust-analyzer cargo bottom 0.14.9 util/no_cfg_btm_command().() -> Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command# {
106 btm_commandrust-analyzer cargo bottom 0.14.9 util/btm_command().(&DEFAULT_CFGrust-analyzer cargo bottom 0.14.9 util/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_ptyrust-analyzer cargo bottom 0.14.9 util/spawn_btm_in_pty().(args: &[&str]) -> (Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn MasterPtyrust-analyzer cargo portable-pty 0.9.0 MasterPty#>, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Childrust-analyzer cargo portable-pty 0.9.0 Child#>) {
112 let native_pty = native_pty_systemrust-analyzer cargo portable-pty 0.9.0 native_pty_system().();
113
114 let pair = native_pty
115 .openptyrust-analyzer cargo portable-pty 0.9.0 PtySystem#openpty().(PtySizerust-analyzer cargo portable-pty 0.9.0 PtySize# {
116 rowsrust-analyzer cargo portable-pty 0.9.0 PtySize#rows.: 100,
117 colsrust-analyzer cargo portable-pty 0.9.0 PtySize#cols.: 100,
118 pixel_widthrust-analyzer cargo portable-pty 0.9.0 PtySize#pixel_width.: 1,
119 pixel_heightrust-analyzer cargo portable-pty 0.9.0 PtySize#pixel_height.: 1,
120 })
121 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
122
123 let btm_exe = BTM_EXE_PATHrust-analyzer cargo bottom 0.14.9 util/BTM_EXE_PATH.;
124 let mut cmd = match cross_runnerrust-analyzer cargo bottom 0.14.9 util/cross_runner().() {
125 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => CommandBuilderrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/CommandBuilder#::newrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]new().(btm_exe),
126 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(runner) => {
127 let mut cmd = CommandBuilderrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/CommandBuilder#::newrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]new().(runner);
128 for (env, val) in RUNNER_ENV_VARSrust-analyzer cargo bottom 0.14.9 util/RUNNER_ENV_VARS. {
129 cmd.envrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]env().(env, val);
130 }
131 cmd.argrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]arg().(BTM_EXE_PATHrust-analyzer cargo bottom 0.14.9 util/BTM_EXE_PATH.);
132
133 cmd
134 }
135 };
136
137 let args = if args.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { &DEFAULT_CFGrust-analyzer cargo bottom 0.14.9 util/DEFAULT_CFG. } else { args };
138 let mut prev = "";
139 for arg in args.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
140 if prev rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq()."-C" {
141 // This is the config file; make sure we set it to absolute path!
142 cmd.argrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]arg().(abs_pathrust-analyzer cargo bottom 0.14.9 util/abs_path().(arg));
143 } else {
144 cmd.argrust-analyzer cargo portable-pty 0.9.0 cmdbuilder/impl#[CommandBuilder]arg().(arg);
145 }
146
147 prev = arg;
148 }
149
150 (pair.masterrust-analyzer cargo portable-pty 0.9.0 PtyPair#master., pair.slaverust-analyzer cargo portable-pty 0.9.0 PtyPair#slave..spawn_commandrust-analyzer cargo portable-pty 0.9.0 SlavePty#spawn_command().(cmd).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
151 }