Skip to content

Commit d6e11b5

Browse files
boyvanduurenrbbcd
andauthored
#2592: Fixes unit tests dependent on echo on windows (#2602)
Co-authored-by: Boy van Duuren <[email protected]>
1 parent f49278c commit d6e11b5

File tree

5 files changed

+153
-84
lines changed

5 files changed

+153
-84
lines changed

.github/workflows/CICD.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,13 @@ jobs:
247247
248248
- name: Run tests
249249
shell: bash
250-
run: $BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}}
250+
run: |
251+
if [[ ${{ matrix.job.os }} = windows-* ]]
252+
then
253+
powershell.exe -command "$BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}}"
254+
else
255+
$BUILD_CMD test --locked --target=${{ matrix.job.target }} ${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}}
256+
fi
251257
252258
- name: Run bat
253259
shell: bash

tests/examples/bat-windows.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Make sure that the pager gets executed
2+
--paging=always
3+
4+
# Output a dummy message for the integration test and system wide config test.
5+
--pager="echo.bat dummy-pager-from-config"

tests/integration_tests.rs

Lines changed: 126 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ use utils::mocked_pagers;
3434

3535
const EXAMPLES_DIR: &str = "tests/examples";
3636

37+
fn get_config() -> &'static str {
38+
if cfg!(windows) {
39+
"bat-windows.conf"
40+
} else {
41+
"bat.conf"
42+
}
43+
}
44+
3745
#[test]
3846
fn basic() {
3947
bat()
@@ -589,37 +597,49 @@ fn do_not_exit_directory() {
589597
}
590598

591599
#[test]
600+
#[serial]
592601
fn pager_basic() {
593-
bat()
594-
.env("PAGER", "echo pager-output")
595-
.arg("--paging=always")
596-
.arg("test.txt")
597-
.assert()
598-
.success()
599-
.stdout(predicate::eq("pager-output\n").normalize());
602+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
603+
bat()
604+
.env("PAGER", mocked_pagers::from("echo pager-output"))
605+
.arg("--paging=always")
606+
.arg("test.txt")
607+
.assert()
608+
.success()
609+
.stdout(predicate::str::contains("pager-output\n").normalize());
610+
});
600611
}
601612

602613
#[test]
614+
#[serial]
603615
fn pager_basic_arg() {
604-
bat()
605-
.arg("--pager=echo pager-output")
606-
.arg("--paging=always")
607-
.arg("test.txt")
608-
.assert()
609-
.success()
610-
.stdout(predicate::eq("pager-output\n").normalize());
616+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
617+
bat()
618+
.arg(format!(
619+
"--pager={}",
620+
mocked_pagers::from("echo pager-output")
621+
))
622+
.arg("--paging=always")
623+
.arg("test.txt")
624+
.assert()
625+
.success()
626+
.stdout(predicate::str::contains("pager-output\n").normalize());
627+
});
611628
}
612629

613630
#[test]
631+
#[serial]
614632
fn pager_overwrite() {
615-
bat()
616-
.env("PAGER", "echo other-pager")
617-
.env("BAT_PAGER", "echo pager-output")
618-
.arg("--paging=always")
619-
.arg("test.txt")
620-
.assert()
621-
.success()
622-
.stdout(predicate::eq("pager-output\n").normalize());
633+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
634+
bat()
635+
.env("PAGER", mocked_pagers::from("echo other-pager"))
636+
.env("BAT_PAGER", mocked_pagers::from("echo pager-output"))
637+
.arg("--paging=always")
638+
.arg("test.txt")
639+
.assert()
640+
.success()
641+
.stdout(predicate::str::contains("pager-output\n").normalize());
642+
});
623643
}
624644

625645
#[test]
@@ -635,55 +655,73 @@ fn pager_disable() {
635655
}
636656

637657
#[test]
658+
#[serial]
638659
fn pager_arg_override_env_withconfig() {
639-
bat_with_config()
640-
.env("BAT_CONFIG_PATH", "bat.conf")
641-
.env("PAGER", "echo another-pager")
642-
.env("BAT_PAGER", "echo other-pager")
643-
.arg("--pager=echo pager-output")
644-
.arg("--paging=always")
645-
.arg("test.txt")
646-
.assert()
647-
.success()
648-
.stdout(predicate::eq("pager-output\n").normalize());
660+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
661+
bat_with_config()
662+
.env("BAT_CONFIG_PATH", get_config())
663+
.env("PAGER", mocked_pagers::from("echo another-pager"))
664+
.env("BAT_PAGER", mocked_pagers::from("echo other-pager"))
665+
.arg(format!(
666+
"--pager={}",
667+
mocked_pagers::from("echo pager-output")
668+
))
669+
.arg("--paging=always")
670+
.arg("test.txt")
671+
.assert()
672+
.success()
673+
.stdout(predicate::str::contains("pager-output\n").normalize());
674+
});
649675
}
650676

651677
#[test]
678+
#[serial]
652679
fn pager_arg_override_env_noconfig() {
653-
bat()
654-
.env("PAGER", "echo another-pager")
655-
.env("BAT_PAGER", "echo other-pager")
656-
.arg("--pager=echo pager-output")
657-
.arg("--paging=always")
658-
.arg("test.txt")
659-
.assert()
660-
.success()
661-
.stdout(predicate::eq("pager-output\n").normalize());
680+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
681+
bat()
682+
.env("PAGER", mocked_pagers::from("echo another-pager"))
683+
.env("BAT_PAGER", mocked_pagers::from("echo other-pager"))
684+
.arg(format!(
685+
"--pager={}",
686+
mocked_pagers::from("echo pager-output")
687+
))
688+
.arg("--paging=always")
689+
.arg("test.txt")
690+
.assert()
691+
.success()
692+
.stdout(predicate::str::contains("pager-output\n").normalize());
693+
});
662694
}
663695

664696
#[test]
697+
#[serial]
665698
fn pager_env_bat_pager_override_config() {
666-
bat_with_config()
667-
.env("BAT_CONFIG_PATH", "bat.conf")
668-
.env("PAGER", "echo other-pager")
669-
.env("BAT_PAGER", "echo pager-output")
670-
.arg("--paging=always")
671-
.arg("test.txt")
672-
.assert()
673-
.success()
674-
.stdout(predicate::eq("pager-output\n").normalize());
699+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
700+
bat_with_config()
701+
.env("BAT_CONFIG_PATH", get_config())
702+
.env("PAGER", mocked_pagers::from("echo other-pager"))
703+
.env("BAT_PAGER", mocked_pagers::from("echo pager-output"))
704+
.arg("--paging=always")
705+
.arg("test.txt")
706+
.assert()
707+
.success()
708+
.stdout(predicate::str::contains("pager-output\n").normalize());
709+
});
675710
}
676711

677712
#[test]
713+
#[serial]
678714
fn pager_env_pager_nooverride_config() {
679-
bat_with_config()
680-
.env("BAT_CONFIG_PATH", "bat.conf")
681-
.env("PAGER", "echo other-pager")
682-
.arg("--paging=always")
683-
.arg("test.txt")
684-
.assert()
685-
.success()
686-
.stdout(predicate::eq("dummy-pager-from-config\n").normalize());
715+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
716+
bat_with_config()
717+
.env("BAT_CONFIG_PATH", get_config())
718+
.env("PAGER", mocked_pagers::from("echo other-pager"))
719+
.arg("--paging=always")
720+
.arg("test.txt")
721+
.assert()
722+
.success()
723+
.stdout(predicate::str::contains("dummy-pager-from-config\n").normalize());
724+
});
687725
}
688726

689727
#[test]
@@ -809,15 +847,18 @@ fn alias_pager_disable() {
809847
}
810848

811849
#[test]
850+
#[serial]
812851
fn alias_pager_disable_long_overrides_short() {
813-
bat()
814-
.env("PAGER", "echo pager-output")
815-
.arg("-P")
816-
.arg("--paging=always")
817-
.arg("test.txt")
818-
.assert()
819-
.success()
820-
.stdout(predicate::eq("pager-output\n").normalize());
852+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
853+
bat()
854+
.env("PAGER", mocked_pagers::from("echo pager-output"))
855+
.arg("-P")
856+
.arg("--paging=always")
857+
.arg("test.txt")
858+
.assert()
859+
.success()
860+
.stdout(predicate::str::contains("pager-output\n").normalize());
861+
});
821862
}
822863

823864
#[test]
@@ -844,14 +885,17 @@ fn pager_failed_to_parse() {
844885
}
845886

846887
#[test]
888+
#[serial]
847889
fn env_var_bat_paging() {
848-
bat()
849-
.env("BAT_PAGER", "echo pager-output")
850-
.env("BAT_PAGING", "always")
851-
.arg("test.txt")
852-
.assert()
853-
.success()
854-
.stdout(predicate::eq("pager-output\n"));
890+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
891+
bat()
892+
.env("BAT_PAGER", mocked_pagers::from("echo pager-output"))
893+
.env("BAT_PAGING", "always")
894+
.arg("test.txt")
895+
.assert()
896+
.success()
897+
.stdout(predicate::str::contains("pager-output\n").normalize());
898+
});
855899
}
856900

857901
#[test]
@@ -912,13 +956,16 @@ fn config_location_from_bat_config_dir_variable() {
912956
}
913957

914958
#[test]
959+
#[serial]
915960
fn config_read_arguments_from_file() {
916-
bat_with_config()
917-
.env("BAT_CONFIG_PATH", "bat.conf")
918-
.arg("test.txt")
919-
.assert()
920-
.success()
921-
.stdout(predicate::eq("dummy-pager-from-config\n").normalize());
961+
mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
962+
bat_with_config()
963+
.env("BAT_CONFIG_PATH", get_config())
964+
.arg("test.txt")
965+
.assert()
966+
.success()
967+
.stdout(predicate::str::contains("dummy-pager-from-config\n").normalize());
968+
});
922969
}
923970

924971
#[cfg(unix)]

tests/mocked-pagers/echo.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ECHO %*

tests/utils/mocked_pagers.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ fn get_mocked_pagers_dir() -> PathBuf {
1717
/// On Unix: 'most' -> 'most'
1818
/// On Windows: 'most' -> 'most.bat'
1919
pub fn from(base: &str) -> String {
20-
if cfg!(windows) {
21-
format!("{}.bat", base)
22-
} else {
23-
String::from(base)
20+
let mut cmd_and_args = shell_words::split(base).unwrap();
21+
let suffix = if cfg!(windows) { ".bat" } else { "" };
22+
let mut out_cmd = format!("{}{}", cmd_and_args.first().unwrap(), suffix);
23+
24+
if (cmd_and_args.len() > 1) {
25+
out_cmd.push(' ');
26+
out_cmd.push_str(cmd_and_args[1..].to_vec().join(" ").as_str());
2427
}
28+
29+
out_cmd
2530
}
2631

2732
/// Prepends a directory to the PATH environment variable
@@ -62,6 +67,11 @@ pub fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) {
6267
.assert()
6368
.success()
6469
.stdout(predicate::str::contains("I am most"));
70+
Command::new(from("echo"))
71+
.arg("foobar")
72+
.assert()
73+
.success()
74+
.stdout(predicate::str::contains("foobar"));
6575

6676
// Now run the actual test
6777
actual_test();

0 commit comments

Comments
 (0)