From f610f33aa73b501a8a9da3ac45320d1e54f1f6dd Mon Sep 17 00:00:00 2001 From: Joining7943 <111500881+Joining7943@users.noreply.github.com> Date: Thu, 16 Feb 2023 15:33:33 +0100 Subject: [PATCH] `tests/util`: Don't trim output in `CmdResult::stdout_matches` and `stdout_does_not_match` (#4304) * tests/util: Fix documentation of UCommand::stderr_only and usage_error * tests/util: Remove trimming from CmdResult::stdout_matches and stdout_does_not_match. Fix tests. The tests are fixed to match the trailing newline instead of ignoring it. --- tests/by-util/test_date.rs | 10 +++++----- tests/by-util/test_dir.rs | 2 +- tests/by-util/test_ls.rs | 2 +- tests/by-util/test_realpath.rs | 6 +++--- tests/by-util/test_vdir.rs | 2 +- tests/common/util.rs | 8 ++++---- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/by-util/test_date.rs b/tests/by-util/test_date.rs index 4442f2df4..a1064a8fa 100644 --- a/tests/by-util/test_date.rs +++ b/tests/by-util/test_date.rs @@ -68,10 +68,10 @@ fn test_date_utc() { fn test_date_format_y() { let scene = TestScenario::new(util_name!()); - let mut re = Regex::new(r"^\d{4}$").unwrap(); + let mut re = Regex::new(r"^\d{4}\n$").unwrap(); scene.ucmd().arg("+%Y").succeeds().stdout_matches(&re); - re = Regex::new(r"^\d{2}$").unwrap(); + re = Regex::new(r"^\d{2}\n$").unwrap(); scene.ucmd().arg("+%y").succeeds().stdout_matches(&re); } @@ -82,7 +82,7 @@ fn test_date_format_m() { let mut re = Regex::new(r"\S+").unwrap(); scene.ucmd().arg("+%b").succeeds().stdout_matches(&re); - re = Regex::new(r"^\d{2}$").unwrap(); + re = Regex::new(r"^\d{2}\n$").unwrap(); scene.ucmd().arg("+%m").succeeds().stdout_matches(&re); } @@ -96,7 +96,7 @@ fn test_date_format_day() { re = Regex::new(r"\S+").unwrap(); scene.ucmd().arg("+%A").succeeds().stdout_matches(&re); - re = Regex::new(r"^\d{1}$").unwrap(); + re = Regex::new(r"^\d{1}\n$").unwrap(); scene.ucmd().arg("+%u").succeeds().stdout_matches(&re); } @@ -117,7 +117,7 @@ fn test_date_issue_3780() { #[test] fn test_date_nano_seconds() { // %N nanoseconds (000000000..999999999) - let re = Regex::new(r"^\d{1,9}$").unwrap(); + let re = Regex::new(r"^\d{1,9}\n$").unwrap(); new_ucmd!().arg("+%N").succeeds().stdout_matches(&re); } diff --git a/tests/by-util/test_dir.rs b/tests/by-util/test_dir.rs index 4d20b8e8e..068ffbbcc 100644 --- a/tests/by-util/test_dir.rs +++ b/tests/by-util/test_dir.rs @@ -49,5 +49,5 @@ fn test_long_output() { .ucmd() .arg("-l") .succeeds() - .stdout_matches(&Regex::new("[rwx-]{10}.*some-file1$").unwrap()); + .stdout_matches(&Regex::new("[rwx-]{10}.*some-file1\n$").unwrap()); } diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index f1ab3eea4..339c78c12 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -950,7 +950,7 @@ fn test_ls_commas_trailing() { .arg("./test-commas-trailing-1") .arg("./test-commas-trailing-2") .succeeds() - .stdout_matches(&Regex::new(r"\S$").unwrap()); // matches if there is no whitespace at the end of stdout. + .stdout_matches(&Regex::new(r"\S\n$").unwrap()); } #[test] diff --git a/tests/by-util/test_realpath.rs b/tests/by-util/test_realpath.rs index 5ff98c78d..2993d8dbb 100644 --- a/tests/by-util/test_realpath.rs +++ b/tests/by-util/test_realpath.rs @@ -300,7 +300,7 @@ fn test_relative_base_not_prefix_of_relative_to() { .succeeds(); #[cfg(windows)] - result.stdout_matches(&Regex::new(r"^.*:\\usr\n.*:\\usr\\local$").unwrap()); + result.stdout_matches(&Regex::new(r"^.*:\\usr\n.*:\\usr\\local\n$").unwrap()); #[cfg(not(windows))] result.stdout_is("/usr\n/usr/local\n"); @@ -344,7 +344,7 @@ fn test_relative() { #[cfg(not(windows))] result.stdout_is("/tmp\n.\n"); #[cfg(windows)] - result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.$").unwrap()); + result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.\n$").unwrap()); new_ucmd!() .args(&["-sm", "--relative-base=/", "--relative-to=/", "/", "/usr"]) @@ -357,7 +357,7 @@ fn test_relative() { #[cfg(not(windows))] result.stdout_is("/tmp\n.\n"); #[cfg(windows)] - result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.$").unwrap()); + result.stdout_matches(&Regex::new(r"^.*:\\tmp\n\.\n$").unwrap()); new_ucmd!() .args(&["-sm", "--relative-base=/", "/", "/usr"]) diff --git a/tests/by-util/test_vdir.rs b/tests/by-util/test_vdir.rs index dc5e9f5ec..0781256b9 100644 --- a/tests/by-util/test_vdir.rs +++ b/tests/by-util/test_vdir.rs @@ -29,7 +29,7 @@ fn test_default_output() { scene .ucmd() .succeeds() - .stdout_matches(&Regex::new("[rwx-]{10}.*some-file1$").unwrap()); + .stdout_matches(&Regex::new("[rwx-]{10}.*some-file1\n$").unwrap()); } #[test] diff --git a/tests/common/util.rs b/tests/common/util.rs index f7b62a695..ed4ecf8e9 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -598,7 +598,7 @@ impl CmdResult { /// asserts that /// 1. the command resulted in stderr stream output that equals the - /// passed in value, when both are trimmed of trailing whitespace + /// passed in value /// 2. the command resulted in empty (zero-length) stdout stream output #[track_caller] pub fn stderr_only>(&self, msg: T) -> &Self { @@ -623,7 +623,7 @@ impl CmdResult { /// asserts that /// 1. the command resulted in stderr stream output that equals the - /// the following format when both are trimmed of trailing whitespace + /// the following format /// `"{util_name}: {msg}\nTry '{bin_path} {util_name} --help' for more information."` /// This the expected format when a `UUsageError` is returned or when `show_error!` is called /// `msg` should be the same as the one provided to `UUsageError::new` or `show_error!` @@ -681,7 +681,7 @@ impl CmdResult { #[track_caller] pub fn stdout_matches(&self, regex: ®ex::Regex) -> &Self { assert!( - regex.is_match(self.stdout_str().trim()), + regex.is_match(self.stdout_str()), "Stdout does not match regex:\n{}", self.stdout_str() ); @@ -691,7 +691,7 @@ impl CmdResult { #[track_caller] pub fn stdout_does_not_match(&self, regex: ®ex::Regex) -> &Self { assert!( - !regex.is_match(self.stdout_str().trim()), + !regex.is_match(self.stdout_str()), "Stdout matches regex:\n{}", self.stdout_str() );