From 4ca68b621d26c61e1c4b4b6b3522ce5ac245b8ba Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Mon, 27 Jun 2022 01:36:45 -0400 Subject: [PATCH] ci: fix missing target in tests (#757) Fix missing target parameter for primary test targets (e.g. aarch64), and adjust integration tests to work with cross. --- .github/workflows/ci.yml | 6 +-- tests/arg_tests.rs | 37 ++++++++---------- tests/invalid_config_tests.rs | 34 ++++++++-------- tests/util.rs | 73 +++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 42 deletions(-) create mode 100644 tests/util.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9b28cd0a..b260f8c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,7 +94,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --no-run --locked ${{ matrix.features }} + args: --no-run --locked ${{ matrix.features }} --target=${{ matrix.info.target }} use-cross: ${{ matrix.info.cross }} env: RUST_BACKTRACE: full @@ -104,7 +104,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: --no-fail-fast ${{ matrix.features }} -- --nocapture --quiet + args: --no-fail-fast ${{ matrix.features }} --target=${{ matrix.info.target }} -- --nocapture --quiet use-cross: ${{ matrix.info.cross }} env: RUST_BACKTRACE: full @@ -114,7 +114,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: clippy - args: ${{ matrix.features }} --all-targets --workspace -- -D warnings + args: ${{ matrix.features }} --all-targets --workspace --target=${{ matrix.info.target }} -- -D warnings use-cross: ${{ matrix.info.cross }} env: RUST_BACKTRACE: full diff --git a/tests/arg_tests.rs b/tests/arg_tests.rs index 95afb635..372955da 100644 --- a/tests/arg_tests.rs +++ b/tests/arg_tests.rs @@ -1,18 +1,13 @@ +//! These tests are mostly here just to ensure that invalid results will be caught when passing arguments. use assert_cmd::prelude::*; use predicates::prelude::*; -use std::process::Command; -// These tests are mostly here just to ensure that invalid results will be caught when passing arguments... - -//======================RATES======================// - -fn get_binary_location() -> String { - env!("CARGO_BIN_EXE_btm").to_string() -} +mod util; +use util::*; #[test] fn test_small_rate() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-r") @@ -26,7 +21,7 @@ fn test_small_rate() { #[test] fn test_large_default_time() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-t") @@ -38,7 +33,7 @@ fn test_large_default_time() { #[test] fn test_small_default_time() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-t") @@ -52,7 +47,7 @@ fn test_small_default_time() { #[test] fn test_large_delta_time() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-d") @@ -64,7 +59,7 @@ fn test_large_delta_time() { #[test] fn test_small_delta_time() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-d") @@ -78,7 +73,7 @@ fn test_small_delta_time() { #[test] fn test_large_rate() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-r") @@ -91,7 +86,7 @@ fn test_large_rate() { #[test] fn test_negative_rate() { // This test should auto fail due to how clap works - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-r") @@ -105,7 +100,7 @@ fn test_negative_rate() { #[test] fn test_invalid_rate() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-r") @@ -117,7 +112,7 @@ fn test_invalid_rate() { #[test] fn test_conflicting_temps() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("-c") @@ -129,7 +124,7 @@ fn test_conflicting_temps() { #[test] fn test_invalid_default_widget_1() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("--default_widget_type") @@ -141,7 +136,7 @@ fn test_invalid_default_widget_1() { #[test] fn test_invalid_default_widget_2() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("--default_widget_type") @@ -157,7 +152,7 @@ fn test_invalid_default_widget_2() { #[test] fn test_missing_default_widget_type() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/empty_config.toml") .arg("--default_widget_count") @@ -172,7 +167,7 @@ fn test_missing_default_widget_type() { #[test] fn test_battery_flag() { if !cfg!(feature = "battery") { - Command::new(get_binary_location()) + btm_command() .arg("--battery") .assert() .failure() diff --git a/tests/invalid_config_tests.rs b/tests/invalid_config_tests.rs index ca3f7e24..65bdaa1f 100644 --- a/tests/invalid_config_tests.rs +++ b/tests/invalid_config_tests.rs @@ -1,16 +1,14 @@ use assert_cmd::prelude::*; use predicates::prelude::*; -use std::process::Command; + +mod util; +use util::*; // These tests are for testing some config file-specific options. -fn get_binary_location() -> String { - env!("CARGO_BIN_EXE_btm").to_string() -} - #[test] fn test_toml_mismatch_type() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/toml_mismatch_type.toml") .assert() @@ -20,7 +18,7 @@ fn test_toml_mismatch_type() { #[test] fn test_empty_layout() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/empty_layout.toml") .assert() @@ -30,7 +28,7 @@ fn test_empty_layout() { #[test] fn test_invalid_layout_widget_type() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_layout_widget_type.toml") .assert() @@ -42,7 +40,7 @@ fn test_invalid_layout_widget_type() { /// However, I feel like it's worth checking anyways - not like it takes long. #[test] fn test_duplicate_temp_type() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/duplicate_temp_type.toml") .assert() @@ -53,7 +51,7 @@ fn test_duplicate_temp_type() { /// Checks for if a hex is valid #[test] fn test_invalid_colour_hex() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_hex.toml") .assert() @@ -64,7 +62,7 @@ fn test_invalid_colour_hex() { /// Checks for if a hex is too long #[test] fn test_invalid_colour_hex_2() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_hex_2.toml") .assert() @@ -76,7 +74,7 @@ fn test_invalid_colour_hex_2() { /// boundary errors! #[test] fn test_invalid_colour_hex_3() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_hex_3.toml") .assert() @@ -86,7 +84,7 @@ fn test_invalid_colour_hex_3() { #[test] fn test_invalid_colour_name() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_name.toml") .assert() @@ -96,7 +94,7 @@ fn test_invalid_colour_name() { #[test] fn test_invalid_colour_rgb() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_rgb.toml") .assert() @@ -106,7 +104,7 @@ fn test_invalid_colour_rgb() { #[test] fn test_invalid_colour_rgb_2() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_rgb_2.toml") .assert() @@ -116,7 +114,7 @@ fn test_invalid_colour_rgb_2() { #[test] fn test_invalid_colour_string() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_colour_string.toml") .assert() @@ -126,7 +124,7 @@ fn test_invalid_colour_string() { #[test] fn test_lone_default_widget_count() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/lone_default_widget_count.toml") .assert() @@ -136,7 +134,7 @@ fn test_lone_default_widget_count() { #[test] fn test_invalid_default_widget_count() { - Command::new(get_binary_location()) + btm_command() .arg("-C") .arg("./tests/invalid_configs/invalid_default_widget_count.toml") .assert() diff --git a/tests/util.rs b/tests/util.rs new file mode 100644 index 00000000..0e74eafa --- /dev/null +++ b/tests/util.rs @@ -0,0 +1,73 @@ +use std::{collections::HashMap, env, process::Command}; + +/// Returns a QEMU runner target given an architecture. +fn get_qemu_target(arch: &str) -> &str { + match arch { + "armv7" => "arm", + "i686" => "i386", + "powerpc" => "ppc", + "powerpc64le" => "ppc64le", + _ => arch, + } +} + +/// This is required since running binary tests via cross can cause be tricky! We need to basically "magically" grab +/// the correct runner in some cases, which can be done by inspecting env variables that should only show up while +/// using cross. +/// +/// Originally inspired by [ripgrep's test files](https://cs.github.com/BurntSushi/ripgrep/blob/9f0e88bcb14e02da1b88872435b17d74786640b5/tests/util.rs#L470), +/// but adapted to work more generally with the architectures supported by bottom after looking through cross' +/// [linux-runner](https://github.com/cross-rs/cross/blob/main/docker/linux-runner) file. +fn cross_runner() -> Option { + const TARGET_RUNNER: &str = "CARGO_TARGET_RUNNER"; + const CROSS_RUNNER: &str = "CROSS_RUNNER"; + + let env_mapping = std::env::vars_os() + .filter_map(|(k, v)| { + let (k, v) = (k.to_string_lossy(), v.to_string_lossy()); + + if k.starts_with("CARGO_TARGET_") && k.ends_with("_RUNNER") && !v.is_empty() { + Some((TARGET_RUNNER.to_string(), v.to_string())) + } else if k == CROSS_RUNNER && !v.is_empty() { + Some((k.to_string(), v.to_string())) + } else { + None + } + }) + .collect::>(); + + if let Some(cross_runner) = env_mapping.get(CROSS_RUNNER) { + if cross_runner == "qemu-user" { + env_mapping.get(TARGET_RUNNER).map(|target_runner| { + format!( + "qemu-{}", + get_qemu_target( + target_runner + .split_ascii_whitespace() + .collect::>() + .last() + .unwrap() + ) + ) + }) + } else { + None + } + } else { + env_mapping.get(TARGET_RUNNER).cloned() + } +} + +/// Returns the [`Command`] of a binary invocation of bottom. +/// +pub fn btm_command() -> Command { + let btm_exe = env!("CARGO_BIN_EXE_btm"); + match cross_runner() { + None => Command::new(btm_exe), + Some(runner) => { + let mut cmd = Command::new(runner); + cmd.arg(btm_exe); + cmd + } + } +}