mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
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.
This commit is contained in:
parent
e2e42ac265
commit
616c3f4a7f
1 changed files with 20 additions and 5 deletions
|
@ -2532,6 +2532,16 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result<
|
||||||
pub fn run_ucmd_as_root(
|
pub fn run_ucmd_as_root(
|
||||||
ts: &TestScenario,
|
ts: &TestScenario,
|
||||||
args: &[&str],
|
args: &[&str],
|
||||||
|
) -> std::result::Result<CmdResult, String> {
|
||||||
|
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<CmdResult, String> {
|
) -> std::result::Result<CmdResult, String> {
|
||||||
if is_ci() {
|
if is_ci() {
|
||||||
Err(format!("{UUTILS_INFO}: {}", "cannot run inside 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") => {
|
Ok(output) if String::from_utf8_lossy(&output.stdout).eq("root\n") => {
|
||||||
// we can run sudo and we're root
|
// we can run sudo and we're root
|
||||||
// run ucmd as root:
|
// run ucmd as root:
|
||||||
Ok(ts
|
let mut cmd = ts.cmd("sudo");
|
||||||
.cmd("sudo")
|
cmd.env("PATH", PATH)
|
||||||
.env("PATH", PATH)
|
|
||||||
.envs(DEFAULT_ENV)
|
.envs(DEFAULT_ENV)
|
||||||
.arg("-E")
|
.arg("-E")
|
||||||
.arg("--non-interactive")
|
.arg("--non-interactive")
|
||||||
.arg(&ts.bin_path)
|
.arg(&ts.bin_path)
|
||||||
.arg(&ts.util_name)
|
.arg(&ts.util_name)
|
||||||
.args(args)
|
.args(args);
|
||||||
.run())
|
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)
|
Ok(output)
|
||||||
if String::from_utf8_lossy(&output.stderr).eq("sudo: a password is required\n") =>
|
if String::from_utf8_lossy(&output.stderr).eq("sudo: a password is required\n") =>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue