Skip to content

Commit

Permalink
Use tempfiles for clircle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmohrin authored and sharkdp committed Feb 28, 2021
1 parent 694b319 commit b8a18d3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Empty file removed tests/examples/cycle.txt
Empty file.
125 changes: 90 additions & 35 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b8a18d3

Please sign in to comment.