@@ -1,6 +1,7 @@
1use assert_cmd::Command;
1use assert_cmd::Command;
2use predicates::{prelude::predicate, str::PredicateStrExt};
2use predicates::{prelude::predicate, str::PredicateStrExt};
3−use std::path::Path;
3+use std::env;
4+use std::path::{Path, PathBuf};
4use std::str::from_utf8;
5use std::str::from_utf8;
6const EXAMPLES_DIR: &str = "tests/examples";
7const EXAMPLES_DIR: &str = "tests/examples";
@@ -23,6 +24,61 @@ fn bat() -> Command {
27+/// For some tests we want mocked versions of some pagers
28+/// This fn returns the absolute path to the directory with these mocked pagers
29+fn get_mocked_pagers_dir() -> PathBuf {
30+ let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
31+ Path::new(&cargo_manifest_dir)
33+ .join("mocked-pagers")
36+/// Prepends a directory to the PATH environment variable
37+/// Returns the original value for later restoration
38+fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
40+ let original_path = env::var("PATH").expect("No PATH?!");
42+ // Add the new dir first
43+ let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
44+ split_paths.insert(0, dir);
46+ // Set PATH with the new dir
47+ let new_path = env::join_paths(split_paths).expect("Failed to join paths");
48+ env::set_var("PATH", new_path);
50+ // Return the original value for later restoration of it
54+/// Helper to restore the value of PATH
55+fn restore_path(original_path: String) {
56+ env::set_var("PATH", original_path);
59+/// Allows test to run that require our mocked versions of 'more' and 'most'
60+/// in PATH. Temporarily changes PATH while the test code runs, and then restore it
61+/// to avoid pollution of global state
62+fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) {
63+ let original_path = prepend_dir_to_path_env_var(get_mocked_pagers_dir());
65+ // Make sure our own variants of 'more' and 'most' is used
69+ .stdout("I am more\n");
73+ .stdout("I am most\n");
75+ // Now run the actual test
78+ // Make sure to restore PATH since it is global state
79+ restore_path(original_path);
27fn basic() {
83fn basic() {
@@ -415,28 +471,77 @@ fn pager_value_bat() {
415 .failure();
471 .failure();
474+/// We shall use less instead of most if PAGER is used since PAGER
475+/// is a generic env var
419−fn pager_most() {
477+fn pager_most_from_pager_env_var() {
478+ with_mocked_versions_of_more_and_most_in_path(|| {
479+ // If the output is not "I am most\n" then we know 'most' is not used
421− .env("PAGER", "most")
481+ .env("PAGER", "most")
422− .arg("--paging=always")
482+ .arg("--paging=always")
423− .arg("test.txt")
483+ .arg("test.txt")
424− .assert()
484+ .assert()
425− .success()
485+ .success()
426− .stdout(predicate::eq("hello world\n").normalize());
486+ .stdout(predicate::eq("hello world\n").normalize());
427− // TODO: How to ensure less is used?
487+ });
490+/// If the bat-specific BAT_PAGER is used, obey the wish of the user
493+fn pager_most_from_bat_pager_env_var() {
494+ with_mocked_versions_of_more_and_most_in_path(|| {
496+ .env("BAT_PAGER", "most")
497+ .arg("--paging=always")
501+ .stdout(predicate::eq("I am most\n").normalize());
505+/// Same reasoning with --pager as with BAT_PAGER
507+fn pager_most_from_pager_arg() {
508+ with_mocked_versions_of_more_and_most_in_path(|| {
510+ .arg("--paging=always")
511+ .arg("--pager=most")
515+ .stdout(predicate::eq("I am most\n").normalize());
519+/// Make sure the logic for 'most' applies even if an argument is passed
431fn pager_most_with_arg() {
521fn pager_most_with_arg() {
522+ with_mocked_versions_of_more_and_most_in_path(|| {
433− .env("PAGER", "most -w")
524+ .env("PAGER", "most -w")
434− .arg("--paging=always")
525+ .arg("--paging=always")
435− .arg("test.txt")
526+ .arg("test.txt")
436− .assert()
527+ .assert()
437− .success()
528+ .success()
438− .stdout(predicate::eq("hello world\n").normalize());
529+ .stdout(predicate::eq("hello world\n").normalize());
439− // TODO: How to ensure less is used?
530+ });
533+/// Sanity check that 'more' is treated like 'most'
536+ with_mocked_versions_of_more_and_most_in_path(|| {
538+ .env("PAGER", "more")
539+ .arg("--paging=always")
543+ .stdout(predicate::eq("hello world\n").normalize());