diff options
author | Niklas Mohrin <dev@niklasmohrin.de> | 2021-02-27 15:33:13 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2021-02-28 23:04:49 +0100 |
commit | b8a18d3ebb5b29162c346b74174239ea077f0796 (patch) | |
tree | 53389df10d515164311310dc116065435112ca63 | |
parent | 694b31909a38989fbb153164f0218f1f0377d2c9 (diff) |
Use tempfiles for clircle testsv0.18.0
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | tests/examples/cycle.txt | 0 | ||||
-rw-r--r-- | tests/integration_tests.rs | 125 |
3 files changed, 93 insertions, 35 deletions
@@ -79,6 +79,9 @@ predicates = "1.0.7" wait-timeout = "0.2.0" tempfile = "3.2.0" +[target.'cfg(unix)'.dev-dependencies] +nix = "0.19.1" + [build-dependencies] clap = { version = "2.33", optional = true } diff --git a/tests/examples/cycle.txt b/tests/examples/cycle.txt deleted file mode 100644 index e69de29b..00000000 --- a/tests/examples/cycle.txt +++ /dev/null diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index de673784..3e3d029a 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,28 +1,37 @@ -use assert_cmd::assert::OutputAssertExt; use assert_cmd::cargo::CommandCargoExt; use predicates::{prelude::predicate, str::PredicateStrExt}; use serial_test::serial; -use std::fs::File; use std::path::Path; -use std::process::{Command, Stdio}; +use std::process::Command; use std::str::from_utf8; use tempfile::tempdir; #[cfg(unix)] -use std::time::Duration; +mod unix { + pub use std::fs::File; + pub use std::io::{self, Write}; + pub use std::os::unix::io::FromRawFd; + pub use std::path::PathBuf; + pub use std::process::Stdio; + pub use std::thread; + pub use std::time::Duration; + + pub use assert_cmd::assert::OutputAssertExt; + pub use nix::pty::{openpty, OpenptyResult}; + pub use wait_timeout::ChildExt; + + pub const SAFE_CHILD_PROCESS_CREATION_TIME: Duration = Duration::from_millis(100); + pub const CHILD_WAIT_TIMEOUT: Duration = Duration::from_secs(15); +} +#[cfg(unix)] +use unix::*; mod utils; use utils::mocked_pagers; const EXAMPLES_DIR: &str = "tests/examples"; -#[cfg(unix)] -const SAFE_CHILD_PROCESS_CREATION_TIME: Duration = Duration::from_millis(100); - -#[cfg(unix)] -const CHILD_WAIT_TIMEOUT: Duration = Duration::from_secs(15); - -fn bat_raw_command() -> Command { +fn bat_raw_command_with_config() -> Command { let mut cmd = Command::cargo_bin("bat").unwrap(); cmd.current_dir("tests/examples"); cmd.env_remove("PAGER"); @@ -34,14 +43,18 @@ fn bat_raw_command() -> Command { cmd } +fn bat_raw_command() -> Command { + let mut cmd = bat_raw_command_with_config(); + cmd.arg("--no-config"); + cmd +} + fn bat_with_config() -> assert_cmd::Command { - assert_cmd::Command::from_std(bat_raw_command()) + assert_cmd::Command::from_std(bat_raw_command_with_config()) } fn bat() -> assert_cmd::Command { - let mut cmd = bat_with_config(); - cmd.arg("--no-config"); - cmd + assert_cmd::Command::from_std(bat_raw_command()) } #[test] @@ -210,40 +223,82 @@ fn line_range_multiple() { .stdout("line 1\nline 2\nline 4\n"); } +#[cfg(unix)] +fn setup_temp_file(content: &[u8]) -> io::Result<(PathBuf, tempfile::TempDir)> { + let dir = tempfile::tempdir().expect("Couldn't create tempdir"); + let path = dir.path().join("temp_file"); + File::create(&path)?.write_all(content)?; + Ok((path, dir)) +} + +#[cfg(unix)] #[test] -fn basic_io_cycle() { - let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap()); - bat_raw_command() +fn basic_io_cycle() -> io::Result<()> { + let (filename, dir) = setup_temp_file(b"I am not empty")?; + + let file_out = Stdio::from(File::create(&filename)?); + let res = bat_raw_command() .arg("test.txt") - .arg("cycle.txt") + .arg(&filename) .stdout(file_out) - .assert() - .failure(); + .assert(); + drop(dir); + res.failure(); + Ok(()) } +#[cfg(unix)] #[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) +fn first_file_cyclic_is_ok() -> io::Result<()> { + let (filename, dir) = setup_temp_file(b"I am not empty")?; + + let file_out = Stdio::from(File::create(&filename)?); + let res = bat_raw_command() + .arg(&filename) .arg("test.txt") - .arg("-") .stdout(file_out) - .assert() - .failure(); + .assert(); + drop(dir); + res.success(); + Ok(()) } #[cfg(unix)] #[test] -fn no_args_doesnt_break() { - use std::io::Write; - use std::os::unix::io::FromRawFd; - use std::thread; +fn empty_file_cycle_is_ok() -> io::Result<()> { + let (filename, dir) = setup_temp_file(b"I am not empty")?; + + let file_out = Stdio::from(File::create(&filename)?); + let res = bat_raw_command() + .arg("empty.txt") + .arg(&filename) + .stdout(file_out) + .assert(); + drop(dir); + res.success(); + Ok(()) +} - use clircle::nix::pty::{openpty, OpenptyResult}; - use wait_timeout::ChildExt; +#[cfg(unix)] +#[test] +fn stdin_to_stdout_cycle() -> io::Result<()> { + let (filename, dir) = setup_temp_file(b"I am not empty")?; + let file_in = Stdio::from(File::open(&filename)?); + let file_out = Stdio::from(File::create(&filename)?); + let res = bat_raw_command() + .arg("test.txt") + .arg("-") + .stdin(file_in) + .stdout(file_out) + .assert(); + drop(dir); + res.failure(); + Ok(()) +} +#[cfg(unix)] +#[test] +fn no_args_doesnt_break() { // 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 |