1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-01 19:02:22 +01:00

Merge remote-tracking branch 'origin/master' into fix-1063

This commit is contained in:
Martin Nordholts
2021-01-06 14:30:55 +01:00
6 changed files with 143 additions and 3 deletions

0
tests/examples/cycle.txt vendored Normal file
View File

View File

@@ -1,13 +1,19 @@
use assert_cmd::Command;
use assert_cmd::assert::OutputAssertExt;
use assert_cmd::cargo::CommandCargoExt;
use predicates::{prelude::predicate, str::PredicateStrExt};
use serial_test::serial;
use std::env;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::str::from_utf8;
use std::time::Duration;
const EXAMPLES_DIR: &str = "tests/examples";
const SAFE_CHILD_PROCESS_CREATION_TIME: Duration = Duration::from_millis(100);
const CHILD_WAIT_TIMEOUT: Duration = Duration::from_secs(15);
fn bat_with_config() -> Command {
fn bat_raw_command() -> Command {
let mut cmd = Command::cargo_bin("bat").unwrap();
cmd.current_dir("tests/examples");
cmd.env_remove("PAGER");
@@ -19,7 +25,11 @@ fn bat_with_config() -> Command {
cmd
}
fn bat() -> Command {
fn bat_with_config() -> assert_cmd::Command {
assert_cmd::Command::from_std(bat_raw_command())
}
fn bat() -> assert_cmd::Command {
let mut cmd = bat_with_config();
cmd.arg("--no-config");
cmd
@@ -258,6 +268,80 @@ fn line_range_multiple() {
.stdout("line 1\nline 2\nline 4\n");
}
#[test]
fn basic_io_cycle() {
let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
bat_raw_command()
.arg("test.txt")
.arg("cycle.txt")
.stdout(file_out)
.assert()
.failure();
}
#[test]
fn stdin_to_stdout_cycle() {
let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
let file_in = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
bat_raw_command()
.stdin(file_in)
.arg("test.txt")
.arg("-")
.stdout(file_out)
.assert()
.failure();
}
#[cfg(unix)]
#[test]
fn no_args_doesnt_break() {
use std::io::Write;
use std::os::unix::io::FromRawFd;
use std::thread;
use clircle::nix::pty::{openpty, OpenptyResult};
use wait_timeout::ChildExt;
// To simulate bat getting started from the shell, a process is created with stdin and stdout
// as the slave end of a pseudo terminal. Although both point to the same "file", bat should
// not exit, because in this case it is safe to read and write to the same fd, which is why
// this test exists.
let OpenptyResult { master, slave } = openpty(None, None).expect("Couldn't open pty.");
let mut master = unsafe { File::from_raw_fd(master) };
let stdin = unsafe { Stdio::from_raw_fd(slave) };
let stdout = unsafe { Stdio::from_raw_fd(slave) };
let mut child = bat_raw_command()
.stdin(stdin)
.stdout(stdout)
.spawn()
.expect("Failed to start.");
// Some time for the child process to start and to make sure, that we can poll the exit status.
// Although this waiting period is not necessary, it is best to keep it in and be absolutely
// sure, that the try_wait does not error later.
thread::sleep(SAFE_CHILD_PROCESS_CREATION_TIME);
// The child process should be running and waiting for input,
// therefore no exit status should be available.
let exit_status = child
.try_wait()
.expect("Error polling exit status, this should never happen.");
assert!(exit_status.is_none());
// Write Ctrl-D (end of transmission) to the pty.
master
.write_all(&[0x04])
.expect("Couldn't write EOT character to master end.");
let exit_status = child
.wait_timeout(CHILD_WAIT_TIMEOUT)
.expect("Error polling exit status, this should never happen.")
.expect("Exit status not set, but the child should have exited already.");
assert!(exit_status.success());
}
#[test]
fn tabs_numbers() {
bat()