on master help sign in
tests: Move 'mocked pagers' utils to separate file
commiteda72c31b27584d61166c48b36c642cd3a51c3a3 authorMartin Nordholts <enselic@gmail.com> dateSun 10 Jan 2021, 14:05 (6 years ago) parent7809008
3 files changed+84−77
tests/integration_tests.rs+14−77 tests/utils/mocked_pagers.rs+69 tests/utils/mod.rs+1
tests/integration_tests.rs+14−77
@@ -2,15 +2,17 @@ use assert_cmd::assert::OutputAssertExt;
22use assert_cmd::cargo::CommandCargoExt;
33use predicates::{prelude::predicate, str::PredicateStrExt};
44use serial_test::serial;
5−use std::env;
65use std::fs::File;
7−use std::path::{Path, PathBuf};
6+use std::path::Path;
87use std::process::{Command, Stdio};
98use std::str::from_utf8;
109
1110#[cfg(unix)]
1211use std::time::Duration;
1312
13+mod utils;
14+use utils::mocked_pagers;
15+
1416const EXAMPLES_DIR: &str = "tests/examples";
1517
1618#[cfg(unix)]
@@ -41,71 +43,6 @@ fn bat() -> assert_cmd::Command {
4143 cmd
4244}
4345
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)
49− .join("tests")
50− .join("mocked-pagers")
51−}
52−
53−/// On Unix: 'most' -> 'most'
54−/// On Windows: 'most' -> 'most.bat'
55−fn mocked_pager(base: &str) -> String {
56− if cfg!(windows) {
57− format!("{}.bat", base)
58− } else {
59− String::from(base)
60− }
61−}
62−
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 {
66− // Get current PATH
67− let original_path = env::var("PATH").expect("No PATH?!");
68−
69− // Add the new dir first
70− let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
71− split_paths.insert(0, dir);
72−
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);
76−
77− // Return the original value for later restoration of it
78− original_path
79−}
80−
81−/// Helper to restore the value of PATH
82−fn restore_path(original_path: String) {
83− env::set_var("PATH", original_path);
84−}
85−
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());
91−
92− // Make sure our own variants of 'more' and 'most' are used
93− Command::new(mocked_pager("more"))
94− .assert()
95− .success()
96− .stdout(predicate::str::contains("I am more"));
97− Command::new(mocked_pager("most"))
98− .assert()
99− .success()
100− .stdout(predicate::str::contains("I am most"));
101−
102− // Now run the actual test
103− actual_test();
104−
105− // Make sure to restore PATH since it is global state
106− restore_path(original_path);
107−}
108−
10946#[test]
11047fn basic() {
11148 bat()
@@ -577,10 +514,10 @@ fn pager_value_bat() {
577514#[test]
578515#[serial] // Because of PATH
579516fn 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(|| {
581518 // If the output is not "I am most" then we know 'most' is not used
582519 bat()
583− .env("PAGER", mocked_pager("most"))
520+ .env("PAGER", mocked_pagers::from("most"))
584521 .arg("--paging=always")
585522 .arg("test.txt")
586523 .assert()
@@ -594,9 +531,9 @@ fn pager_most_from_pager_env_var() {
594531#[test]
595532#[serial] // Because of PATH
596533fn 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(|| {
598535 bat()
599− .env("BAT_PAGER", mocked_pager("most"))
536+ .env("BAT_PAGER", mocked_pagers::from("most"))
600537 .arg("--paging=always")
601538 .arg("test.txt")
602539 .assert()
@@ -609,10 +546,10 @@ fn pager_most_from_bat_pager_env_var() {
609546#[test]
610547#[serial] // Because of PATH
611548fn 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(|| {
613550 bat()
614551 .arg("--paging=always")
615− .arg(format!("--pager={}", mocked_pager("most")))
552+ .arg(format!("--pager={}", mocked_pagers::from("most")))
616553 .arg("test.txt")
617554 .assert()
618555 .success()
@@ -624,9 +561,9 @@ fn pager_most_from_pager_arg() {
624561#[test]
625562#[serial] // Because of PATH
626563fn 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(|| {
628565 bat()
629− .env("PAGER", format!("{} -w", mocked_pager("most")))
566+ .env("PAGER", format!("{} -w", mocked_pagers::from("most")))
630567 .arg("--paging=always")
631568 .arg("test.txt")
632569 .assert()
@@ -639,9 +576,9 @@ fn pager_most_with_arg() {
639576#[test]
640577#[serial] // Because of PATH
641578fn pager_more() {
642− with_mocked_versions_of_more_and_most_in_path(|| {
579+ mocked_pagers::with_mocked_versions_of_more_and_most_in_path(|| {
643580 bat()
644− .env("PAGER", mocked_pager("more"))
581+ .env("PAGER", mocked_pagers::from("more"))
645582 .arg("--paging=always")
646583 .arg("test.txt")
647584 .assert()
tests/utils/mocked_pagers.rsnew file+69
@@ -0,0 +1,69 @@
1+use assert_cmd::Command;
2+use predicates::prelude::predicate;
3+use std::env;
4+use std::path::{Path, PathBuf};
5+
6+/// For some tests we want mocked versions of some pagers
7+/// This fn returns the absolute path to the directory with these mocked pagers
8+fn get_mocked_pagers_dir() -> PathBuf {
9+ let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
10+ Path::new(&cargo_manifest_dir)
11+ .join("tests")
12+ .join("mocked-pagers")
13+}
14+
15+/// On Unix: 'most' -> 'most'
16+/// On Windows: 'most' -> 'most.bat'
17+pub fn from(base: &str) -> String {
18+ if cfg!(windows) {
19+ format!("{}.bat", base)
20+ } else {
21+ String::from(base)
22+ }
23+}
24+
25+/// Prepends a directory to the PATH environment variable
26+/// Returns the original value for later restoration
27+fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
28+ // Get current PATH
29+ let original_path = env::var("PATH").expect("No PATH?!");
30+
31+ // Add the new dir first
32+ let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
33+ split_paths.insert(0, dir);
34+
35+ // Set PATH with the new dir
36+ let new_path = env::join_paths(split_paths).expect("Failed to join paths");
37+ env::set_var("PATH", new_path);
38+
39+ // Return the original value for later restoration of it
40+ original_path
41+}
42+
43+/// Helper to restore the value of PATH
44+fn restore_path(original_path: String) {
45+ env::set_var("PATH", original_path);
46+}
47+
48+/// Allows test to run that require our mocked versions of 'more' and 'most'
49+/// in PATH. Temporarily changes PATH while the test code runs, and then restore it
50+/// to avoid pollution of global state
51+pub fn with_mocked_versions_of_more_and_most_in_path(actual_test: fn()) {
52+ let original_path = prepend_dir_to_path_env_var(get_mocked_pagers_dir());
53+
54+ // Make sure our own variants of 'more' and 'most' are used
55+ Command::new(from("more"))
56+ .assert()
57+ .success()
58+ .stdout(predicate::str::contains("I am more"));
59+ Command::new(from("most"))
60+ .assert()
61+ .success()
62+ .stdout(predicate::str::contains("I am most"));
63+
64+ // Now run the actual test
65+ actual_test();
66+
67+ // Make sure to restore PATH since it is global state
68+ restore_path(original_path);
69+}
tests/utils/mod.rsnew file+1
@@ -0,0 +1 @@
1+pub mod mocked_pagers;