From ea668f090e7a024ca128848794d7c386f602f67a Mon Sep 17 00:00:00 2001 From: "B.C. van Duuren" Date: Thu, 22 Jun 2023 21:25:16 +0200 Subject: [PATCH 1/3] #2592: Fixes unit tests dependent on echo on windows --- tests/examples/bat-windows.conf | 5 + tests/integration_tests.rs | 188 ++++++++++++++++++++------------ tests/mocked-pagers/echo.bat | 1 + tests/utils/mocked_pagers.rs | 23 +++- 4 files changed, 141 insertions(+), 76 deletions(-) create mode 100644 tests/examples/bat-windows.conf create mode 100644 tests/mocked-pagers/echo.bat diff --git a/tests/examples/bat-windows.conf b/tests/examples/bat-windows.conf new file mode 100644 index 0000000000..68122f4f31 --- /dev/null +++ b/tests/examples/bat-windows.conf @@ -0,0 +1,5 @@ +# Make sure that the pager gets executed +--paging=always + +# Output a dummy message for the integration test and system wide config test. +--pager="echo.bat dummy-pager-from-config" diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 8fc2c30c95..ddb19f6ca3 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -34,6 +34,14 @@ use utils::mocked_pagers; const EXAMPLES_DIR: &str = "tests/examples"; +fn get_config() -> &'static str { + if cfg!(windows) { + "bat-windows.conf" + } else { + "bat.conf" + } +} + #[test] fn basic() { bat() @@ -589,37 +597,49 @@ fn do_not_exit_directory() { } #[test] +#[serial] fn pager_basic() { - bat() - .env("PAGER", "echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .env("PAGER", mocked_pagers::from("echo pager-output")) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] +#[serial] fn pager_basic_arg() { - bat() - .arg("--pager=echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .arg(format!( + "--pager={}", + mocked_pagers::from("echo pager-output") + )) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] +#[serial] fn pager_overwrite() { - bat() - .env("PAGER", "echo other-pager") - .env("BAT_PAGER", "echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .env("PAGER", mocked_pagers::from("echo other-pager")) + .env("BAT_PAGER", mocked_pagers::from("echo pager-output")) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] @@ -635,55 +655,73 @@ fn pager_disable() { } #[test] +#[serial] fn pager_arg_override_env_withconfig() { - bat_with_config() - .env("BAT_CONFIG_PATH", "bat.conf") - .env("PAGER", "echo another-pager") - .env("BAT_PAGER", "echo other-pager") - .arg("--pager=echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat_with_config() + .env("BAT_CONFIG_PATH", get_config()) + .env("PAGER", mocked_pagers::from("echo another-pager")) + .env("BAT_PAGER", mocked_pagers::from("echo other-pager")) + .arg(format!( + "--pager={}", + mocked_pagers::from("echo pager-output") + )) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] +#[serial] fn pager_arg_override_env_noconfig() { - bat() - .env("PAGER", "echo another-pager") - .env("BAT_PAGER", "echo other-pager") - .arg("--pager=echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .env("PAGER", mocked_pagers::from("echo another-pager")) + .env("BAT_PAGER", mocked_pagers::from("echo other-pager")) + .arg(format!( + "--pager={}", + mocked_pagers::from("echo pager-output") + )) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] +#[serial] fn pager_env_bat_pager_override_config() { - bat_with_config() - .env("BAT_CONFIG_PATH", "bat.conf") - .env("PAGER", "echo other-pager") - .env("BAT_PAGER", "echo pager-output") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat_with_config() + .env("BAT_CONFIG_PATH", get_config()) + .env("PAGER", mocked_pagers::from("echo other-pager")) + .env("BAT_PAGER", mocked_pagers::from("echo pager-output")) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] +#[serial] fn pager_env_pager_nooverride_config() { - bat_with_config() - .env("BAT_CONFIG_PATH", "bat.conf") - .env("PAGER", "echo other-pager") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("dummy-pager-from-config\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat_with_config() + .env("BAT_CONFIG_PATH", get_config()) + .env("PAGER", mocked_pagers::from("echo other-pager")) + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("dummy-pager-from-config\n").normalize()); + }); } #[test] @@ -809,15 +847,18 @@ fn alias_pager_disable() { } #[test] +#[serial] fn alias_pager_disable_long_overrides_short() { - bat() - .env("PAGER", "echo pager-output") - .arg("-P") - .arg("--paging=always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .env("PAGER", mocked_pagers::from("echo pager-output")) + .arg("-P") + .arg("--paging=always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] @@ -912,13 +953,16 @@ fn config_location_from_bat_config_dir_variable() { } #[test] +#[serial] fn config_read_arguments_from_file() { - bat_with_config() - .env("BAT_CONFIG_PATH", "bat.conf") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("dummy-pager-from-config\n").normalize()); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat_with_config() + .env("BAT_CONFIG_PATH", get_config()) + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("dummy-pager-from-config\n").normalize()); + }); } #[cfg(unix)] diff --git a/tests/mocked-pagers/echo.bat b/tests/mocked-pagers/echo.bat new file mode 100644 index 0000000000..62fa94235e --- /dev/null +++ b/tests/mocked-pagers/echo.bat @@ -0,0 +1 @@ +ECHO %* diff --git a/tests/utils/mocked_pagers.rs b/tests/utils/mocked_pagers.rs index 07ae6fcddc..d791de589e 100644 --- a/tests/utils/mocked_pagers.rs +++ b/tests/utils/mocked_pagers.rs @@ -17,10 +17,20 @@ fn get_mocked_pagers_dir() -> PathBuf { /// On Unix: 'most' -> 'most' /// On Windows: 'most' -> 'most.bat' pub fn from(base: &str) -> String { - if cfg!(windows) { - format!("{}.bat", base) - } else { - String::from(base) + let mut cmd_and_args = base.split(" "); + let suffix = if cfg!(windows) { ".bat" } else { "" }; + match cmd_and_args.next() { + Some(s) => { + let mut out_cmd = format!("{}{}", s, suffix); + let remainder: Vec<&str> = cmd_and_args.collect(); + if (remainder.len() > 0) { + out_cmd.push_str(" "); + out_cmd.push_str(remainder.join(" ").as_str()); + } + + out_cmd + } + None => String::from(base), } } @@ -62,6 +72,11 @@ pub fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) { .assert() .success() .stdout(predicate::str::contains("I am most")); + Command::new(from("echo")) + .arg("foobar") + .assert() + .success() + .stdout(predicate::str::contains("foobar")); // Now run the actual test actual_test(); From d9869ff4fd9b9c5ff3774e5e625b9bb7d225e8f3 Mon Sep 17 00:00:00 2001 From: "B.C. van Duuren" Date: Sun, 3 Sep 2023 16:55:45 +0200 Subject: [PATCH 2/3] Run cargo test on powershell for windows targets --- .github/workflows/CICD.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 4882505c80..95a5c9ab8c 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -247,7 +247,13 @@ jobs: - name: Run tests shell: bash - run: $BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}} + run: | + if [[ ${{ matrix.job.os }} = windows-* ]] + then + powershell.exe -command "$BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}}" + else + $BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}} + fi - name: Run bat shell: bash From e8267278d61dfd227cf81485496fe1c6941ae270 Mon Sep 17 00:00:00 2001 From: Boy van Duuren Date: Mon, 4 Sep 2023 07:57:25 +0200 Subject: [PATCH 3/3] Improvements after review --- tests/integration_tests.rs | 17 ++++++++++------- tests/utils/mocked_pagers.rs | 19 +++++++------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index ddb19f6ca3..e55076283e 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -885,14 +885,17 @@ fn pager_failed_to_parse() { } #[test] +#[serial] fn env_var_bat_paging() { - bat() - .env("BAT_PAGER", "echo pager-output") - .env("BAT_PAGING", "always") - .arg("test.txt") - .assert() - .success() - .stdout(predicate::eq("pager-output\n")); + mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| { + bat() + .env("BAT_PAGER", mocked_pagers::from("echo pager-output")) + .env("BAT_PAGING", "always") + .arg("test.txt") + .assert() + .success() + .stdout(predicate::str::contains("pager-output\n").normalize()); + }); } #[test] diff --git a/tests/utils/mocked_pagers.rs b/tests/utils/mocked_pagers.rs index d791de589e..98055e3832 100644 --- a/tests/utils/mocked_pagers.rs +++ b/tests/utils/mocked_pagers.rs @@ -17,21 +17,16 @@ fn get_mocked_pagers_dir() -> PathBuf { /// On Unix: 'most' -> 'most' /// On Windows: 'most' -> 'most.bat' pub fn from(base: &str) -> String { - let mut cmd_and_args = base.split(" "); + let mut cmd_and_args = shell_words::split(base).unwrap(); let suffix = if cfg!(windows) { ".bat" } else { "" }; - match cmd_and_args.next() { - Some(s) => { - let mut out_cmd = format!("{}{}", s, suffix); - let remainder: Vec<&str> = cmd_and_args.collect(); - if (remainder.len() > 0) { - out_cmd.push_str(" "); - out_cmd.push_str(remainder.join(" ").as_str()); - } + let mut out_cmd = format!("{}{}", cmd_and_args.first().unwrap(), suffix); - out_cmd - } - None => String::from(base), + if (cmd_and_args.len() > 1) { + out_cmd.push(' '); + out_cmd.push_str(cmd_and_args[1..].to_vec().join(" ").as_str()); } + + out_cmd } /// Prepends a directory to the PATH environment variable