1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

tests/util: Introduce CmdResult::stdout_str_apply ... to preprocess stdout, stderr before asserting

This commit is contained in:
Joining7943 2022-11-30 07:35:52 +01:00
parent 3977ef608e
commit 37e06edadc
3 changed files with 83 additions and 13 deletions

View file

@ -503,11 +503,10 @@ fn test_rm_force_prompts_order() {
child.try_write_in(yes.as_bytes()).unwrap(); child.try_write_in(yes.as_bytes()).unwrap();
let result = child.wait().unwrap(); let result = child.wait().unwrap();
let string_output = result.stderr_str(); result
assert_eq!( .stderr_str_apply(str::trim)
string_output.trim(), .stderr_only("rm: remove regular empty file 'empty'?");
"rm: remove regular empty file 'empty'?"
);
assert!(!at.file_exists(empty_file)); assert!(!at.file_exists(empty_file));
at.touch(empty_file); at.touch(empty_file);

View file

@ -28,8 +28,11 @@ fn test_uname_processor() {
#[test] #[test]
fn test_uname_hardware_platform() { fn test_uname_hardware_platform() {
let result = new_ucmd!().arg("-i").succeeds(); new_ucmd!()
assert_eq!(result.stdout_str().trim_end(), "unknown"); .arg("-i")
.succeeds()
.stdout_str_apply(str::trim_end)
.stdout_only("unknown");
} }
#[test] #[test]

View file

@ -84,26 +84,94 @@ pub struct CmdResult {
} }
impl CmdResult { impl CmdResult {
pub fn new( pub fn new<T, U>(
bin_path: String, bin_path: String,
util_name: Option<String>, util_name: Option<String>,
tmpd: Option<Rc<TempDir>>, tmpd: Option<Rc<TempDir>>,
code: Option<i32>, code: Option<i32>,
success: bool, success: bool,
stdout: &[u8], stdout: T,
stderr: &[u8], stderr: U,
) -> Self { ) -> Self
where
T: Into<Vec<u8>>,
U: Into<Vec<u8>>,
{
Self { Self {
bin_path, bin_path,
util_name, util_name,
tmpd, tmpd,
code, code,
success, success,
stdout: stdout.to_vec(), stdout: stdout.into(),
stderr: stderr.to_vec(), stderr: stderr.into(),
} }
} }
pub fn stdout_apply<'a, F, R>(&'a self, function: F) -> Self
where
F: Fn(&'a [u8]) -> R,
R: Into<Vec<u8>>,
{
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<Vec<u8>>,
{
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<Vec<u8>>,
{
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<Vec<u8>>,
{
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 /// Returns a reference to the program's standard output as a slice of bytes
pub fn stdout(&self) -> &[u8] { pub fn stdout(&self) -> &[u8] {
&self.stdout &self.stdout