@@ -2,15 +2,17 @@ use assert_cmd::assert::OutputAssertExt;
2use assert_cmd::cargo::CommandCargoExt;
2use assert_cmd::cargo::CommandCargoExt;
3use predicates::{prelude::predicate, str::PredicateStrExt};
3use predicates::{prelude::predicate, str::PredicateStrExt};
4use serial_test::serial;
4use serial_test::serial;
6use std::fs::File;
5use std::fs::File;
7−use std::path::{Path, PathBuf};
6+use std::path::Path;
8use std::process::{Command, Stdio};
7use std::process::{Command, Stdio};
9use std::str::from_utf8;
8use std::str::from_utf8;
11#[cfg(unix)]
10#[cfg(unix)]
12use std::time::Duration;
11use std::time::Duration;
14+use utils::mocked_pagers;
14const EXAMPLES_DIR: &str = "tests/examples";
16const EXAMPLES_DIR: &str = "tests/examples";
16#[cfg(unix)]
18#[cfg(unix)]
@@ -41,71 +43,6 @@ fn bat() -> assert_cmd::Command {
44−/// For some tests we want mocked versions of some pagers
45−/// This fn returns the absolute path to the directory with these mocked pagers
46−fn get_mocked_pagers_dir() -> PathBuf {
47− let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
48− Path::new(&cargo_manifest_dir)
50− .join("mocked-pagers")
53−/// On Unix: 'most' -> 'most'
54−/// On Windows: 'most' -> 'most.bat'
55−fn mocked_pager(base: &str) -> String {
57− format!("{}.bat", base)
63−/// Prepends a directory to the PATH environment variable
64−/// Returns the original value for later restoration
65−fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
67− let original_path = env::var("PATH").expect("No PATH?!");
69− // Add the new dir first
70− let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
71− split_paths.insert(0, dir);
73− // Set PATH with the new dir
74− let new_path = env::join_paths(split_paths).expect("Failed to join paths");
75− env::set_var("PATH", new_path);
77− // Return the original value for later restoration of it
81−/// Helper to restore the value of PATH
82−fn restore_path(original_path: String) {
83− env::set_var("PATH", original_path);
86−/// Allows test to run that require our mocked versions of 'more' and 'most'
87−/// in PATH. Temporarily changes PATH while the test code runs, and then restore it
88−/// to avoid pollution of global state
89−fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) {
90− let original_path = prepend_dir_to_path_env_var(get_mocked_pagers_dir());
92− // Make sure our own variants of 'more' and 'most' are used
93− Command::new(mocked_pager("more"))
96− .stdout(predicate::str::contains("I am more"));
97− Command::new(mocked_pager("most"))
100− .stdout(predicate::str::contains("I am most"));
102− // Now run the actual test
105− // Make sure to restore PATH since it is global state
106− restore_path(original_path);
110fn basic() {
47fn basic() {
@@ -577,10 +514,10 @@ fn pager_value_bat() {
578#[serial] // Because of PATH
515#[serial] // Because of PATH
579fn pager_most_from_pager_env_var() {
516fn pager_most_from_pager_env_var() {
580− with_mocked_versions_of_more_and_most_in_path(|| {
517+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
581 // If the output is not "I am most" then we know 'most' is not used
518 // If the output is not "I am most" then we know 'most' is not used
583− .env("PAGER", mocked_pager("most"))
520+ .env("PAGER", mocked_pagers::from("most"))
584 .arg("--paging=always")
521 .arg("--paging=always")
585 .arg("test.txt")
522 .arg("test.txt")
586 .assert()
523 .assert()
@@ -594,9 +531,9 @@ fn pager_most_from_pager_env_var() {
595#[serial] // Because of PATH
532#[serial] // Because of PATH
596fn pager_most_from_bat_pager_env_var() {
533fn pager_most_from_bat_pager_env_var() {
597− with_mocked_versions_of_more_and_most_in_path(|| {
534+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
599− .env("BAT_PAGER", mocked_pager("most"))
536+ .env("BAT_PAGER", mocked_pagers::from("most"))
600 .arg("--paging=always")
537 .arg("--paging=always")
601 .arg("test.txt")
538 .arg("test.txt")
602 .assert()
539 .assert()
@@ -609,10 +546,10 @@ fn pager_most_from_bat_pager_env_var() {
610#[serial] // Because of PATH
547#[serial] // Because of PATH
611fn pager_most_from_pager_arg() {
548fn pager_most_from_pager_arg() {
612− with_mocked_versions_of_more_and_most_in_path(|| {
549+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
614 .arg("--paging=always")
551 .arg("--paging=always")
615− .arg(format!("--pager={}", mocked_pager("most")))
552+ .arg(format!("--pager={}", mocked_pagers::from("most")))
616 .arg("test.txt")
553 .arg("test.txt")
617 .assert()
554 .assert()
618 .success()
555 .success()
@@ -624,9 +561,9 @@ fn pager_most_from_pager_arg() {
625#[serial] // Because of PATH
562#[serial] // Because of PATH
626fn pager_most_with_arg() {
563fn pager_most_with_arg() {
627− with_mocked_versions_of_more_and_most_in_path(|| {
564+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
629− .env("PAGER", format!("{} -w", mocked_pager("most")))
566+ .env("PAGER", format!("{} -w", mocked_pagers::from("most")))
630 .arg("--paging=always")
567 .arg("--paging=always")
631 .arg("test.txt")
568 .arg("test.txt")
632 .assert()
569 .assert()
@@ -639,9 +576,9 @@ fn pager_most_with_arg() {
640#[serial] // Because of PATH
577#[serial] // Because of PATH
641fn pager_more() {
578fn pager_more() {
642− with_mocked_versions_of_more_and_most_in_path(|| {
579+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
644− .env("PAGER", mocked_pager("more"))
581+ .env("PAGER", mocked_pagers::from("more"))
645 .arg("--paging=always")
582 .arg("--paging=always")
646 .arg("test.txt")
583 .arg("test.txt")
647 .assert()
584 .assert()