mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-01 19:02:22 +01:00
Add $LESSOPEN
and $LESSCLOSE
support (#2444)
This commit is contained in:
@@ -2025,3 +2025,200 @@ fn acknowledgements() {
|
||||
)
|
||||
.stderr("");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn lessopen_file_piped() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|echo File is %s")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("File is test.txt\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn lessopen_stdin_piped() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|cat")
|
||||
.write_stdin("hello world\n")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
#[serial] // Randomly fails otherwise
|
||||
fn lessopen_and_lessclose_file_temp() {
|
||||
// This is mainly to test that $LESSCLOSE gets passed the correct file paths
|
||||
// In this case, the original file and the temporary file returned by $LESSOPEN
|
||||
bat()
|
||||
.env("LESSOPEN", "echo empty.txt")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("lessclose: test.txt empty.txt\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
#[serial] // Randomly fails otherwise
|
||||
fn lessopen_and_lessclose_file_piped() {
|
||||
// This is mainly to test that $LESSCLOSE gets passed the correct file paths
|
||||
// In these cases, the original file and a dash
|
||||
bat()
|
||||
// This test will not work properly if $LESSOPEN does not output anything
|
||||
.env("LESSOPEN", "|cat test.txt ")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.arg("empty.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\nlessclose: empty.txt -\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||cat empty.txt")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.arg("empty.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("lessclose: empty.txt -\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
#[serial] // Randomly fails otherwise
|
||||
fn lessopen_and_lessclose_stdin_temp() {
|
||||
// This is mainly to test that $LESSCLOSE gets passed the correct file paths
|
||||
// In this case, a dash and the temporary file returned by $LESSOPEN
|
||||
bat()
|
||||
.env("LESSOPEN", "-echo empty.txt")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.write_stdin("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("lessclose: - empty.txt\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
#[serial] // Randomly fails otherwise
|
||||
fn lessopen_and_lessclose_stdin_piped() {
|
||||
// This is mainly to test that $LESSCLOSE gets passed the correct file paths
|
||||
// In these cases, two dashes
|
||||
bat()
|
||||
// This test will not work properly if $LESSOPEN does not output anything
|
||||
.env("LESSOPEN", "|-cat test.txt")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.write_stdin("empty.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\nlessclose: - -\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||-cat empty.txt")
|
||||
.env("LESSCLOSE", "echo lessclose: %s %s")
|
||||
.write_stdin("empty.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("lessclose: - -\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn lessopen_handling_empty_output_file() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|cat empty.txt")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "|cat nonexistent.txt")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||cat empty.txt")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||cat nonexistent.txt")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn lessopen_handling_empty_output_stdin() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|-cat empty.txt")
|
||||
.write_stdin("hello world\n")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "|-cat nonexistent.txt")
|
||||
.write_stdin("hello world\n")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||-cat empty.txt")
|
||||
.write_stdin("hello world\n")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("");
|
||||
|
||||
bat()
|
||||
.env("LESSOPEN", "||-cat nonexistent.txt")
|
||||
.write_stdin("hello world\n")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)] // Expected output assumed that tests are run on a Unix-like system
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn lessopen_uses_shell() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|cat < %s")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[cfg(feature = "lessopen")]
|
||||
#[test]
|
||||
fn do_not_use_lessopen() {
|
||||
bat()
|
||||
.env("LESSOPEN", "|echo File is %s")
|
||||
.arg("--no-lessopen")
|
||||
.arg("test.txt")
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("hello world\n");
|
||||
}
|
||||
|
@@ -17,6 +17,9 @@ pub fn bat_raw_command_with_config() -> Command {
|
||||
cmd.env_remove("COLORTERM");
|
||||
cmd.env_remove("NO_COLOR");
|
||||
cmd.env_remove("PAGER");
|
||||
cmd.env_remove("LESSOPEN");
|
||||
cmd.env_remove("LESSCLOSE");
|
||||
cmd.env_remove("SHELL");
|
||||
cmd
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user