From 616c3f4a7fb199a525bcf148ed799516e13cf739 Mon Sep 17 00:00:00 2001 From: Benjamin Bara Date: Sat, 22 Jul 2023 11:25:39 +0200 Subject: [PATCH] util: extend run_ucmd_as_root for stdin/stdout This enables to give path to files (as str) which are then used as either stdin or stdout. --- tests/common/util.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 0fbc58cd5..6f4e76d42 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -2532,6 +2532,16 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result< pub fn run_ucmd_as_root( ts: &TestScenario, args: &[&str], +) -> std::result::Result { + run_ucmd_as_root_with_stdin_stdout(ts, args, None, None) +} + +#[cfg(unix)] +pub fn run_ucmd_as_root_with_stdin_stdout( + ts: &TestScenario, + args: &[&str], + stdin: Option<&str>, + stdout: Option<&str>, ) -> std::result::Result { if is_ci() { Err(format!("{UUTILS_INFO}: {}", "cannot run inside CI")) @@ -2546,16 +2556,21 @@ pub fn run_ucmd_as_root( Ok(output) if String::from_utf8_lossy(&output.stdout).eq("root\n") => { // we can run sudo and we're root // run ucmd as root: - Ok(ts - .cmd("sudo") - .env("PATH", PATH) + let mut cmd = ts.cmd("sudo"); + cmd.env("PATH", PATH) .envs(DEFAULT_ENV) .arg("-E") .arg("--non-interactive") .arg(&ts.bin_path) .arg(&ts.util_name) - .args(args) - .run()) + .args(args); + if let Some(stdin) = stdin { + cmd.set_stdin(File::open(stdin).unwrap()); + } + if let Some(stdout) = stdout { + cmd.set_stdout(File::open(stdout).unwrap()); + } + Ok(cmd.run()) } Ok(output) if String::from_utf8_lossy(&output.stderr).eq("sudo: a password is required\n") =>