From 0d1946a5d27e4ff79196d18cc51835a1836da8f9 Mon Sep 17 00:00:00 2001 From: Gilad Naaman Date: Sat, 17 Apr 2021 15:01:52 +0300 Subject: [PATCH] cksum: Remove direct usage of CmdResult fields in tests --- tests/by-util/test_cksum.rs | 54 ++++++++++++++++++------------------- tests/common/util.rs | 36 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 1a0915cd5..c8e60f8a9 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -31,7 +31,10 @@ fn test_empty() { at.touch("a"); - ucmd.arg("a").succeeds().stdout.ends_with("0 a"); + ucmd.arg("a") + .succeeds() + .no_stderr() + .normalized_newlines_stdout_is("4294967295 0 a\n"); } #[test] @@ -41,36 +44,35 @@ fn test_arg_overrides_stdin() { at.touch("a"); - let result = ucmd - .arg("a") + ucmd.arg("a") .pipe_in(input.as_bytes()) // the command might have exited before all bytes have been pipe in. // in that case, we don't care about the error (broken pipe) .ignore_stdin_write_error() - .run(); - - println!("{}, {}", result.stdout, result.stderr); - - assert!(result.stdout.ends_with("0 a\n")) + .succeeds() + .no_stderr() + .normalized_newlines_stdout_is("4294967295 0 a\n"); } #[test] fn test_invalid_file() { - let (_, mut ucmd) = at_and_ucmd!(); + let ts = TestScenario::new(util_name!()); + let at = ts.fixtures.clone(); - let ls = TestScenario::new("ls"); - let files = ls.cmd("ls").arg("-l").run(); - println!("{:?}", files.stdout); - println!("{:?}", files.stderr); + let folder_name = "asdf"; - let folder_name = "asdf".to_string(); - - let result = ucmd.arg(&folder_name).run(); - - println!("stdout: {:?}", result.stdout); - println!("stderr: {:?}", result.stderr); - assert!(result.stderr.contains("cksum: error: 'asdf'")); - assert!(!result.success); + // First check when file doesn't exist + ts.ucmd().arg(folder_name) + .fails() + .no_stdout() + .stderr_contains("cksum: error: 'asdf' No such file or directory"); + + // Then check when the file is of an invalid type + at.mkdir(folder_name); + ts.ucmd().arg(folder_name) + .fails() + .no_stdout() + .stderr_contains("cksum: error: 'asdf' Is a directory"); } // Make sure crc is correct for files larger than 32 bytes @@ -79,14 +81,13 @@ fn test_invalid_file() { fn test_crc_for_bigger_than_32_bytes() { let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("chars.txt").run(); + let result = ucmd.arg("chars.txt").succeeds(); - let mut stdout_splitted = result.stdout.split(" "); + let mut stdout_splitted = result.stdout_str().split(" "); let cksum: i64 = stdout_splitted.next().unwrap().parse().unwrap(); let bytes_cnt: i64 = stdout_splitted.next().unwrap().parse().unwrap(); - assert!(result.success); assert_eq!(cksum, 586047089); assert_eq!(bytes_cnt, 16); } @@ -95,14 +96,13 @@ fn test_crc_for_bigger_than_32_bytes() { fn test_stdin_larger_than_128_bytes() { let (_, mut ucmd) = at_and_ucmd!(); - let result = ucmd.arg("larger_than_2056_bytes.txt").run(); + let result = ucmd.arg("larger_than_2056_bytes.txt").succeeds(); - let mut stdout_splitted = result.stdout.split(" "); + let mut stdout_splitted = result.stdout_str().split(" "); let cksum: i64 = stdout_splitted.next().unwrap().parse().unwrap(); let bytes_cnt: i64 = stdout_splitted.next().unwrap().parse().unwrap(); - assert!(result.success); assert_eq!(cksum, 945881979); assert_eq!(bytes_cnt, 2058); } diff --git a/tests/common/util.rs b/tests/common/util.rs index c7f46c2a9..b54c8ee39 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -220,6 +220,13 @@ impl CmdResult { self } + /// Like `stdout_is` but newlines are normalized to `\n`. + pub fn normalized_newlines_stdout_is>(&self, msg: T) -> &CmdResult { + let msg = msg.as_ref().replace("\r\n", "\n"); + assert_eq!(self.stdout.replace("\r\n", "\n"), msg); + self + } + /// asserts that the command resulted in stdout stream output, /// whose bytes equal those of the passed in slice pub fn stdout_is_bytes>(&self, msg: T) -> &CmdResult { @@ -1096,4 +1103,33 @@ mod tests { res.stdout_does_not_match(&positive); } + + #[test] + fn test_normalized_newlines_stdout_is() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "A\r\nB\nC".into(), + stderr: "".into(), + }; + + res.normalized_newlines_stdout_is("A\r\nB\nC"); + res.normalized_newlines_stdout_is("A\nB\nC"); + res.normalized_newlines_stdout_is("A\nB\r\nC"); + } + + #[test] + #[should_panic] + fn test_normalized_newlines_stdout_is_fail() { + let res = CmdResult { + tmpd: None, + code: None, + success: true, + stdout: "A\r\nB\nC".into(), + stderr: "".into(), + }; + + res.normalized_newlines_stdout_is("A\r\nB\nC\n"); + } }