tests: Make mocked pagers work on Windows

This commit is contained in:
Martin Nordholts 2021-01-04 17:20:02 +01:00
parent df33ed05dd
commit e87c554ccd
5 changed files with 28 additions and 14 deletions

View File

@ -34,6 +34,18 @@ fn get_mocked_pagers_dir() -> PathBuf {
.join("mocked-pagers") .join("mocked-pagers")
} }
/// On Unix: 'most' -> 'most'
/// On Windows: 'most' -> 'most.bat'
fn mocked_pager(base: &str) -> String {
if cfg!(windows) {
let mut str = String::from(base);
str.push_str(".bat");
str
} else {
String::from(base)
}
}
/// Prepends a directory to the PATH environment variable /// Prepends a directory to the PATH environment variable
/// Returns the original value for later restoration /// Returns the original value for later restoration
fn prepend_dir_to_path_env_var(dir: PathBuf) -> String { fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
@ -64,14 +76,14 @@ fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) {
let original_path = prepend_dir_to_path_env_var(get_mocked_pagers_dir()); let original_path = prepend_dir_to_path_env_var(get_mocked_pagers_dir());
// Make sure our own variants of 'more' and 'most' is used // Make sure our own variants of 'more' and 'most' is used
Command::new("more") Command::new(mocked_pager("more"))
.assert() .assert()
.success() .success()
.stdout("I am more\n"); .stdout(predicate::str::contains("I am more"));
Command::new("most") Command::new(mocked_pager("most"))
.assert() .assert()
.success() .success()
.stdout("I am most\n"); .stdout(predicate::str::contains("I am most"));
// Now run the actual test // Now run the actual test
actual_test(); actual_test();
@ -478,9 +490,9 @@ fn pager_value_bat() {
#[serial] // Because of PATH #[serial] // Because of PATH
fn pager_most_from_pager_env_var() { fn pager_most_from_pager_env_var() {
with_mocked_versions_of_more_and_most_in_path(|| { with_mocked_versions_of_more_and_most_in_path(|| {
// If the output is not "I am most\n" then we know 'most' is not used // If the output is not "I am most" then we know 'most' is not used
bat() bat()
.env("PAGER", "most") .env("PAGER", mocked_pager("most"))
.arg("--paging=always") .arg("--paging=always")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
@ -496,12 +508,12 @@ fn pager_most_from_pager_env_var() {
fn pager_most_from_bat_pager_env_var() { fn pager_most_from_bat_pager_env_var() {
with_mocked_versions_of_more_and_most_in_path(|| { with_mocked_versions_of_more_and_most_in_path(|| {
bat() bat()
.env("BAT_PAGER", "most") .env("BAT_PAGER", mocked_pager("most"))
.arg("--paging=always") .arg("--paging=always")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq("I am most\n").normalize()); .stdout(predicate::str::contains("I am most"));
}); });
} }
@ -512,11 +524,11 @@ fn pager_most_from_pager_arg() {
with_mocked_versions_of_more_and_most_in_path(|| { with_mocked_versions_of_more_and_most_in_path(|| {
bat() bat()
.arg("--paging=always") .arg("--paging=always")
.arg("--pager=most") .arg(format!("--pager={}", mocked_pager("most")))
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
.stdout(predicate::eq("I am most\n").normalize()); .stdout(predicate::str::contains("I am most"));
}); });
} }
@ -526,7 +538,7 @@ fn pager_most_from_pager_arg() {
fn pager_most_with_arg() { fn pager_most_with_arg() {
with_mocked_versions_of_more_and_most_in_path(|| { with_mocked_versions_of_more_and_most_in_path(|| {
bat() bat()
.env("PAGER", "most -w") .env("PAGER", format!("{} -w", mocked_pager("most")))
.arg("--paging=always") .arg("--paging=always")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
@ -541,7 +553,7 @@ fn pager_most_with_arg() {
fn pager_more() { fn pager_more() {
with_mocked_versions_of_more_and_most_in_path(|| { with_mocked_versions_of_more_and_most_in_path(|| {
bat() bat()
.env("PAGER", "more") .env("PAGER", mocked_pager("more"))
.arg("--paging=always") .arg("--paging=always")
.arg("test.txt") .arg("test.txt")
.assert() .assert()

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo "I am more" echo I am more

1
tests/mocked-pagers/more.bat Executable file
View File

@ -0,0 +1 @@
ECHO I am more

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo "I am most" echo I am most

1
tests/mocked-pagers/most.bat Executable file
View File

@ -0,0 +1 @@
ECHO I am most