mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-22 04:48:48 +00:00
Add integration tests for 'more' and 'most' used as pagers
This commit is contained in:
parent
bfa5342331
commit
c9efdd68ed
@ -1,6 +1,7 @@
|
|||||||
use assert_cmd::Command;
|
use assert_cmd::Command;
|
||||||
use predicates::{prelude::predicate, str::PredicateStrExt};
|
use predicates::{prelude::predicate, str::PredicateStrExt};
|
||||||
use std::path::Path;
|
use std::env;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
const EXAMPLES_DIR: &str = "tests/examples";
|
const EXAMPLES_DIR: &str = "tests/examples";
|
||||||
@ -23,6 +24,61 @@ fn bat() -> Command {
|
|||||||
cmd
|
cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For some tests we want mocked versions of some pagers
|
||||||
|
/// This fn returns the absolute path to the directory with these mocked pagers
|
||||||
|
fn get_mocked_pagers_dir() -> PathBuf {
|
||||||
|
let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
|
||||||
|
Path::new(&cargo_manifest_dir)
|
||||||
|
.join("tests")
|
||||||
|
.join("mocked-pagers")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prepends a directory to the PATH environment variable
|
||||||
|
/// Returns the original value for later restoration
|
||||||
|
fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
|
||||||
|
// Get current PATH
|
||||||
|
let original_path = env::var("PATH").expect("No PATH?!");
|
||||||
|
|
||||||
|
// Add the new dir first
|
||||||
|
let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
|
||||||
|
split_paths.insert(0, dir);
|
||||||
|
|
||||||
|
// Set PATH with the new dir
|
||||||
|
let new_path = env::join_paths(split_paths).expect("Failed to join paths");
|
||||||
|
env::set_var("PATH", new_path);
|
||||||
|
|
||||||
|
// Return the original value for later restoration of it
|
||||||
|
original_path
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper to restore the value of PATH
|
||||||
|
fn restore_path(original_path: String) {
|
||||||
|
env::set_var("PATH", original_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allows test to run that require our mocked versions of 'more' and 'most'
|
||||||
|
/// in PATH. Temporarily changes PATH while the test code runs, and then restore it
|
||||||
|
/// to avoid pollution of global state
|
||||||
|
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());
|
||||||
|
|
||||||
|
// Make sure our own variants of 'more' and 'most' is used
|
||||||
|
Command::new("more")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout("I am more\n");
|
||||||
|
Command::new("most")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout("I am most\n");
|
||||||
|
|
||||||
|
// Now run the actual test
|
||||||
|
actual_test();
|
||||||
|
|
||||||
|
// Make sure to restore PATH since it is global state
|
||||||
|
restore_path(original_path);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic() {
|
fn basic() {
|
||||||
bat()
|
bat()
|
||||||
@ -415,8 +471,12 @@ fn pager_value_bat() {
|
|||||||
.failure();
|
.failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// We shall use less instead of most if PAGER is used since PAGER
|
||||||
|
/// is a generic env var
|
||||||
#[test]
|
#[test]
|
||||||
fn pager_most() {
|
fn pager_most_from_pager_env_var() {
|
||||||
|
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
|
||||||
bat()
|
bat()
|
||||||
.env("PAGER", "most")
|
.env("PAGER", "most")
|
||||||
.arg("--paging=always")
|
.arg("--paging=always")
|
||||||
@ -424,11 +484,42 @@ fn pager_most() {
|
|||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq("hello world\n").normalize());
|
||||||
// TODO: How to ensure less is used?
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If the bat-specific BAT_PAGER is used, obey the wish of the user
|
||||||
|
/// and allow 'most'
|
||||||
|
#[test]
|
||||||
|
fn pager_most_from_bat_pager_env_var() {
|
||||||
|
with_mocked_versions_of_more_and_most_in_path(|| {
|
||||||
|
bat()
|
||||||
|
.env("BAT_PAGER", "most")
|
||||||
|
.arg("--paging=always")
|
||||||
|
.arg("test.txt")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::eq("I am most\n").normalize());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same reasoning with --pager as with BAT_PAGER
|
||||||
|
#[test]
|
||||||
|
fn pager_most_from_pager_arg() {
|
||||||
|
with_mocked_versions_of_more_and_most_in_path(|| {
|
||||||
|
bat()
|
||||||
|
.arg("--paging=always")
|
||||||
|
.arg("--pager=most")
|
||||||
|
.arg("test.txt")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::eq("I am most\n").normalize());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Make sure the logic for 'most' applies even if an argument is passed
|
||||||
#[test]
|
#[test]
|
||||||
fn pager_most_with_arg() {
|
fn pager_most_with_arg() {
|
||||||
|
with_mocked_versions_of_more_and_most_in_path(|| {
|
||||||
bat()
|
bat()
|
||||||
.env("PAGER", "most -w")
|
.env("PAGER", "most -w")
|
||||||
.arg("--paging=always")
|
.arg("--paging=always")
|
||||||
@ -436,7 +527,21 @@ fn pager_most_with_arg() {
|
|||||||
.assert()
|
.assert()
|
||||||
.success()
|
.success()
|
||||||
.stdout(predicate::eq("hello world\n").normalize());
|
.stdout(predicate::eq("hello world\n").normalize());
|
||||||
// TODO: How to ensure less is used?
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sanity check that 'more' is treated like 'most'
|
||||||
|
#[test]
|
||||||
|
fn pager_more() {
|
||||||
|
with_mocked_versions_of_more_and_most_in_path(|| {
|
||||||
|
bat()
|
||||||
|
.env("PAGER", "more")
|
||||||
|
.arg("--paging=always")
|
||||||
|
.arg("test.txt")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(predicate::eq("hello world\n").normalize());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
2
tests/mocked-pagers/more
Executable file
2
tests/mocked-pagers/more
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
echo "I am more"
|
2
tests/mocked-pagers/most
Executable file
2
tests/mocked-pagers/most
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
echo "I am most"
|
Loading…
x
Reference in New Issue
Block a user