1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +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();
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);

View file

@ -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]

View file

@ -84,26 +84,94 @@ pub struct CmdResult {
}
impl CmdResult {
pub fn new(
pub fn new<T, U>(
bin_path: String,
util_name: Option<String>,
tmpd: Option<Rc<TempDir>>,
code: Option<i32>,
success: bool,
stdout: &[u8],
stderr: &[u8],
) -> Self {
stdout: T,
stderr: U,
) -> Self
where
T: Into<Vec<u8>>,
U: Into<Vec<u8>>,
{
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<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
pub fn stdout(&self) -> &[u8] {
&self.stdout