on master help sign in
Use tempfiles for clircle tests
commitb8a18d3ebb5b29162c346b74174239ea077f0796 authorNiklas Mohrin <dev@niklasmohrin.de> dateSat 27 Feb 2021, 15:33 (6 years ago) parent694b319
3 files changed+93−35
Cargo.toml+3 tests/examples/cycle.txt tests/integration_tests.rs+90−35
Cargo.toml+3
@@ -79,6 +79,9 @@ predicates = "1.0.7"
7979wait-timeout = "0.2.0"
8080tempfile = "3.2.0"
8181
82+[target.'cfg(unix)'.dev-dependencies]
83+nix = "0.19.1"
84+
8285[build-dependencies]
8386clap = { version = "2.33", optional = true }
8487
tests/examples/cycle.txtdeleted

No changes to the content.

tests/integration_tests.rs+90−35
@@ -1,28 +1,37 @@
1−use assert_cmd::assert::OutputAssertExt;
21use assert_cmd::cargo::CommandCargoExt;
32use predicates::{prelude::predicate, str::PredicateStrExt};
43use serial_test::serial;
5−use std::fs::File;
64use std::path::Path;
7−use std::process::{Command, Stdio};
5+use std::process::Command;
86use std::str::from_utf8;
97use tempfile::tempdir;
108
119#[cfg(unix)]
12−use std::time::Duration;
10+mod unix {
11+ pub use std::fs::File;
12+ pub use std::io::{self, Write};
13+ pub use std::os::unix::io::FromRawFd;
14+ pub use std::path::PathBuf;
15+ pub use std::process::Stdio;
16+ pub use std::thread;
17+ pub use std::time::Duration;
18+
19+ pub use assert_cmd::assert::OutputAssertExt;
20+ pub use nix::pty::{openpty, OpenptyResult};
21+ pub use wait_timeout::ChildExt;
22+
23+ pub const SAFE_CHILD_PROCESS_CREATION_TIME: Duration = Duration::from_millis(100);
24+ pub const CHILD_WAIT_TIMEOUT: Duration = Duration::from_secs(15);
25+}
26+#[cfg(unix)]
27+use unix::*;
1328
1429mod utils;
1530use utils::mocked_pagers;
1631
1732const EXAMPLES_DIR: &str = "tests/examples";
1833
19−#[cfg(unix)]
20−const SAFE_CHILD_PROCESS_CREATION_TIME: Duration = Duration::from_millis(100);
21−
22−#[cfg(unix)]
23−const CHILD_WAIT_TIMEOUT: Duration = Duration::from_secs(15);
24−
25−fn bat_raw_command() -> Command {
34+fn bat_raw_command_with_config() -> Command {
2635 let mut cmd = Command::cargo_bin("bat").unwrap();
2736 cmd.current_dir("tests/examples");
2837 cmd.env_remove("PAGER");
@@ -34,14 +43,18 @@ fn bat_raw_command() -> Command {
3443 cmd
3544}
3645
46+fn bat_raw_command() -> Command {
47+ let mut cmd = bat_raw_command_with_config();
48+ cmd.arg("--no-config");
49+ cmd
50+}
51+
3752fn bat_with_config() -> assert_cmd::Command {
38− assert_cmd::Command::from_std(bat_raw_command())
53+ assert_cmd::Command::from_std(bat_raw_command_with_config())
3954}
4055
4156fn bat() -> assert_cmd::Command {
42− let mut cmd = bat_with_config();
43− cmd.arg("--no-config");
44− cmd
57+ assert_cmd::Command::from_std(bat_raw_command())
4558}
4659
4760#[test]
@@ -210,40 +223,82 @@ fn line_range_multiple() {
210223 .stdout("line 1\nline 2\nline 4\n");
211224}
212225
226+#[cfg(unix)]
227+fn setup_temp_file(content: &[u8]) -> io::Result<(PathBuf, tempfile::TempDir)> {
228+ let dir = tempfile::tempdir().expect("Couldn't create tempdir");
229+ let path = dir.path().join("temp_file");
230+ File::create(&path)?.write_all(content)?;
231+ Ok((path, dir))
232+}
233+
234+#[cfg(unix)]
213235#[test]
214−fn basic_io_cycle() {
215− let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
216− bat_raw_command()
236+fn basic_io_cycle() -> io::Result<()> {
237+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
238+
239+ let file_out = Stdio::from(File::create(&filename)?);
240+ let res = bat_raw_command()
217241 .arg("test.txt")
218− .arg("cycle.txt")
242+ .arg(&filename)
219243 .stdout(file_out)
220− .assert()
221− .failure();
244+ .assert();
245+ drop(dir);
246+ res.failure();
247+ Ok(())
222248}
223249
250+#[cfg(unix)]
224251#[test]
225−fn stdin_to_stdout_cycle() {
226− let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
227− let file_in = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
228− bat_raw_command()
229− .stdin(file_in)
252+fn first_file_cyclic_is_ok() -> io::Result<()> {
253+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
254+
255+ let file_out = Stdio::from(File::create(&filename)?);
256+ let res = bat_raw_command()
257+ .arg(&filename)
230258 .arg("test.txt")
231− .arg("-")
232259 .stdout(file_out)
233− .assert()
234− .failure();
260+ .assert();
261+ drop(dir);
262+ res.success();
263+ Ok(())
235264}
236265
237266#[cfg(unix)]
238267#[test]
239−fn no_args_doesnt_break() {
240− use std::io::Write;
241− use std::os::unix::io::FromRawFd;
242− use std::thread;
268+fn empty_file_cycle_is_ok() -> io::Result<()> {
269+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
270+
271+ let file_out = Stdio::from(File::create(&filename)?);
272+ let res = bat_raw_command()
273+ .arg("empty.txt")
274+ .arg(&filename)
275+ .stdout(file_out)
276+ .assert();
277+ drop(dir);
278+ res.success();
279+ Ok(())
280+}
243281
244− use clircle::nix::pty::{openpty, OpenptyResult};
245− use wait_timeout::ChildExt;
282+#[cfg(unix)]
283+#[test]
284+fn stdin_to_stdout_cycle() -> io::Result<()> {
285+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
286+ let file_in = Stdio::from(File::open(&filename)?);
287+ let file_out = Stdio::from(File::create(&filename)?);
288+ let res = bat_raw_command()
289+ .arg("test.txt")
290+ .arg("-")
291+ .stdin(file_in)
292+ .stdout(file_out)
293+ .assert();
294+ drop(dir);
295+ res.failure();
296+ Ok(())
297+}
246298
299+#[cfg(unix)]
300+#[test]
301+fn no_args_doesnt_break() {
247302 // To simulate bat getting started from the shell, a process is created with stdin and stdout
248303 // as the slave end of a pseudo terminal. Although both point to the same "file", bat should
249304 // not exit, because in this case it is safe to read and write to the same fd, which is why