From 37e06edadcd32186da7a4041f08e7de755dfa30c Mon Sep 17 00:00:00 2001 From: Joining7943 <111500881+Joining7943@users.noreply.github.com> Date: Wed, 30 Nov 2022 07:35:52 +0100 Subject: [PATCH] tests/util: Introduce CmdResult::stdout_str_apply ... to preprocess stdout, stderr before asserting --- tests/by-util/test_rm.rs | 9 ++--- tests/by-util/test_uname.rs | 7 +++- tests/common/util.rs | 80 ++++++++++++++++++++++++++++++++++--- 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/tests/by-util/test_rm.rs b/tests/by-util/test_rm.rs index d11a6e379..598ae5c3f 100644 --- a/tests/by-util/test_rm.rs +++ b/tests/by-util/test_rm.rs @@ -503,11 +503,10 @@ fn test_rm_force_prompts_order() { child.try_write_in(yes.as_bytes()).unwrap(); let result = child.wait().unwrap(); - let string_output = result.stderr_str(); - assert_eq!( - string_output.trim(), - "rm: remove regular empty file 'empty'?" - ); + result + .stderr_str_apply(str::trim) + .stderr_only("rm: remove regular empty file 'empty'?"); + assert!(!at.file_exists(empty_file)); at.touch(empty_file); diff --git a/tests/by-util/test_uname.rs b/tests/by-util/test_uname.rs index a0d20cab6..c446da6c5 100644 --- a/tests/by-util/test_uname.rs +++ b/tests/by-util/test_uname.rs @@ -28,8 +28,11 @@ fn test_uname_processor() { #[test] fn test_uname_hardware_platform() { - let result = new_ucmd!().arg("-i").succeeds(); - assert_eq!(result.stdout_str().trim_end(), "unknown"); + new_ucmd!() + .arg("-i") + .succeeds() + .stdout_str_apply(str::trim_end) + .stdout_only("unknown"); } #[test] diff --git a/tests/common/util.rs b/tests/common/util.rs index c60a3637e..b7edb2839 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -84,26 +84,94 @@ pub struct CmdResult { } impl CmdResult { - pub fn new( + pub fn new( bin_path: String, util_name: Option, tmpd: Option>, code: Option, success: bool, - stdout: &[u8], - stderr: &[u8], - ) -> Self { + stdout: T, + stderr: U, + ) -> Self + where + T: Into>, + U: Into>, + { Self { bin_path, util_name, tmpd, code, success, - stdout: stdout.to_vec(), - stderr: stderr.to_vec(), + stdout: stdout.into(), + stderr: stderr.into(), } } + pub fn stdout_apply<'a, F, R>(&'a self, function: F) -> Self + where + F: Fn(&'a [u8]) -> R, + R: Into>, + { + Self::new( + self.bin_path.clone(), + self.util_name.clone(), + self.tmpd.clone(), + self.code, + self.success, + function(&self.stdout), + self.stderr.as_slice(), + ) + } + + pub fn stdout_str_apply<'a, F, R>(&'a self, function: F) -> Self + where + F: Fn(&'a str) -> R, + R: Into>, + { + Self::new( + self.bin_path.clone(), + self.util_name.clone(), + self.tmpd.clone(), + self.code, + self.success, + function(self.stdout_str()), + self.stderr.as_slice(), + ) + } + + pub fn stderr_apply<'a, F, R>(&'a self, function: F) -> Self + where + F: Fn(&'a [u8]) -> R, + R: Into>, + { + Self::new( + self.bin_path.clone(), + self.util_name.clone(), + self.tmpd.clone(), + self.code, + self.success, + self.stdout.as_slice(), + function(&self.stderr), + ) + } + + pub fn stderr_str_apply<'a, F, R>(&'a self, function: F) -> Self + where + F: Fn(&'a str) -> R, + R: Into>, + { + Self::new( + self.bin_path.clone(), + self.util_name.clone(), + self.tmpd.clone(), + self.code, + self.success, + self.stdout.as_slice(), + function(self.stderr_str()), + ) + } + /// Returns a reference to the program's standard output as a slice of bytes pub fn stdout(&self) -> &[u8] { &self.stdout