on master help sign in
Use tempfiles for clircle tests
b8a18d3Niklas Mohrin
Cargo.toml+3
@@ -79,6 +79,9 @@ predicates = "1.0.7"
79wait-timeout = "0.2.0"
79wait-timeout = "0.2.0"
80tempfile = "3.2.0"
80tempfile = "3.2.0"
81
81
82+[target.'cfg(unix)'.dev-dependencies]
83+nix = "0.19.1"
84+
82[build-dependencies]
85[build-dependencies]
83clap = { version = "2.33", optional = true }
86clap = { version = "2.33", optional = true }
84
87
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;
2use assert_cmd::cargo::CommandCargoExt;
1use assert_cmd::cargo::CommandCargoExt;
3use predicates::{prelude::predicate, str::PredicateStrExt};
2use predicates::{prelude::predicate, str::PredicateStrExt};
4use serial_test::serial;
3use serial_test::serial;
5−use std::fs::File;
6use std::path::Path;
4use std::path::Path;
7−use std::process::{Command, Stdio};
5+use std::process::Command;
8use std::str::from_utf8;
6use std::str::from_utf8;
9use tempfile::tempdir;
7use tempfile::tempdir;
10
8
11#[cfg(unix)]
9#[cfg(unix)]
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;
12−use std::time::Duration;
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::*;
13
28
14mod utils;
29mod utils;
15use utils::mocked_pagers;
30use utils::mocked_pagers;
16
31
17const EXAMPLES_DIR: &str = "tests/examples";
32const EXAMPLES_DIR: &str = "tests/examples";
18
33
19−#[cfg(unix)]
34+fn bat_raw_command_with_config() -> Command {
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 {
26 let mut cmd = Command::cargo_bin("bat").unwrap();
35 let mut cmd = Command::cargo_bin("bat").unwrap();
27 cmd.current_dir("tests/examples");
36 cmd.current_dir("tests/examples");
28 cmd.env_remove("PAGER");
37 cmd.env_remove("PAGER");
@@ -34,14 +43,18 @@ fn bat_raw_command() -> Command {
34 cmd
43 cmd
35}
44}
36
45
46+fn bat_raw_command() -> Command {
47+ let mut cmd = bat_raw_command_with_config();
48+ cmd.arg("--no-config");
49+ cmd
50+}
51+
37fn bat_with_config() -> assert_cmd::Command {
52fn 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())
39}
54}
40
55
41fn bat() -> assert_cmd::Command {
56fn bat() -> assert_cmd::Command {
42− let mut cmd = bat_with_config();
57+ assert_cmd::Command::from_std(bat_raw_command())
43− cmd.arg("--no-config");
44− cmd
45}
58}
46
59
47#[test]
60#[test]
@@ -210,40 +223,82 @@ fn line_range_multiple() {
210 .stdout("line 1\nline 2\nline 4\n");
223 .stdout("line 1\nline 2\nline 4\n");
211}
224}
212
225
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)]
213#[test]
235#[test]
214−fn basic_io_cycle() {
236+fn basic_io_cycle() -> io::Result<()> {
237+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
238+
215− let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
239+ let file_out = Stdio::from(File::create(&filename)?);
216− bat_raw_command()
240+ let res = bat_raw_command()
217 .arg("test.txt")
241 .arg("test.txt")
218− .arg("cycle.txt")
242+ .arg(&filename)
219 .stdout(file_out)
243 .stdout(file_out)
220− .assert()
244+ .assert();
245+ drop(dir);
221− .failure();
246+ res.failure();
247+ Ok(())
222}
248}
223
249
250+#[cfg(unix)]
224#[test]
251#[test]
225−fn stdin_to_stdout_cycle() {
252+fn first_file_cyclic_is_ok() -> io::Result<()> {
253+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
254+
226− let file_out = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
255+ let file_out = Stdio::from(File::create(&filename)?);
227− let file_in = Stdio::from(File::open("tests/examples/cycle.txt").unwrap());
228− bat_raw_command()
256+ let res = bat_raw_command()
229− .stdin(file_in)
257+ .arg(&filename)
230 .arg("test.txt")
258 .arg("test.txt")
231− .arg("-")
232 .stdout(file_out)
259 .stdout(file_out)
233− .assert()
260+ .assert();
234− .failure();
261+ drop(dir);
262+ res.success();
263+ Ok(())
235}
264}
236
265
237#[cfg(unix)]
266#[cfg(unix)]
238#[test]
267#[test]
239−fn no_args_doesnt_break() {
268+fn empty_file_cycle_is_ok() -> io::Result<()> {
240− use std::io::Write;
269+ let (filename, dir) = setup_temp_file(b"I am not empty")?;
241− use std::os::unix::io::FromRawFd;
270+
242− use std::thread;
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+}
243
281
244− use clircle::nix::pty::{openpty, OpenptyResult};
282+#[cfg(unix)]
245− use wait_timeout::ChildExt;
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+}
246
298
299+#[cfg(unix)]
300+#[test]
301+fn no_args_doesnt_break() {
247 // To simulate bat getting started from the shell, a process is created with stdin and stdout
302 // To simulate bat getting started from the shell, a process is created with stdin and stdout
248 // as the slave end of a pseudo terminal. Although both point to the same "file", bat should
303 // as the slave end of a pseudo terminal. Although both point to the same "file", bat should
249 // not exit, because in this case it is safe to read and write to the same fd, which is why
304 // not exit, because in this case it is safe to read and write to the same fd, which is why