process: Executable names should be passed as AsRef<OsStr> instead of &str

This commit is contained in:
Vicki Pfau 2024-06-25 20:29:37 -07:00
parent 4cc4c74ff0
commit 50b6fb85b1
2 changed files with 23 additions and 11 deletions

View file

@ -13,7 +13,10 @@ use std::process::Stdio;
use tokio::process::Command; use tokio::process::Command;
#[cfg(not(test))] #[cfg(not(test))]
pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<i32> { pub async fn script_exit_code<P: AsRef<OsStr>>(
executable: P,
args: &[impl AsRef<OsStr>],
) -> Result<i32> {
// Run given script and return the exit code // Run given script and return the exit code
let output = Command::new(executable) let output = Command::new(executable)
.args(args) .args(args)
@ -25,14 +28,17 @@ pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> R
} }
#[cfg(test)] #[cfg(test)]
pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<i32> { pub async fn script_exit_code<P: AsRef<OsStr>>(
executable: P,
args: &[impl AsRef<OsStr>],
) -> Result<i32> {
let test = crate::testing::current(); let test = crate::testing::current();
let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect(); let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect();
let cb = test.process_cb.get(); let cb = test.process_cb.get();
cb(executable, args.as_ref()).map(|(res, _)| res) cb(executable.as_ref(), args.as_ref()).map(|(res, _)| res)
} }
pub async fn run_script(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<()> { pub async fn run_script<P: AsRef<OsStr>>(executable: P, args: &[impl AsRef<OsStr>]) -> Result<()> {
// Run given script to get exit code and return true on success. // Run given script to get exit code and return true on success.
// Return Err on failure, but also print an error if needed // Return Err on failure, but also print an error if needed
match script_exit_code(executable, args).await { match script_exit_code(executable, args).await {
@ -43,7 +49,10 @@ pub async fn run_script(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<
} }
#[cfg(not(test))] #[cfg(not(test))]
pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> { pub async fn script_output<P: AsRef<OsStr>>(
executable: P,
args: &[impl AsRef<OsStr>],
) -> Result<String> {
// Run given command and return the output given // Run given command and return the output given
let output = Command::new(executable).args(args).output(); let output = Command::new(executable).args(args).output();
@ -54,11 +63,14 @@ pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Resu
} }
#[cfg(test)] #[cfg(test)]
pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> { pub async fn script_output<P: AsRef<OsStr>>(
executable: P,
args: &[impl AsRef<OsStr>],
) -> Result<String> {
let test = crate::testing::current(); let test = crate::testing::current();
let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect(); let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect();
let cb = test.process_cb.get(); let cb = test.process_cb.get();
cb(executable, args.as_ref()).map(|(_, res)| res) cb(executable.as_ref(), args.as_ref()).map(|(_, res)| res)
} }
#[cfg(test)] #[cfg(test)]
@ -66,15 +78,15 @@ pub(crate) mod test {
use super::*; use super::*;
use crate::testing; use crate::testing;
pub fn ok(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn ok(_: &OsStr, _: &[&OsStr]) -> Result<(i32, String)> {
Ok((0, String::from("ok"))) Ok((0, String::from("ok")))
} }
pub fn code(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn code(_: &OsStr, _: &[&OsStr]) -> Result<(i32, String)> {
Ok((1, String::from("code"))) Ok((1, String::from("code")))
} }
pub fn exit(_: &str, _: &[&OsStr]) -> Result<(i32, String)> { pub fn exit(_: &OsStr, _: &[&OsStr]) -> Result<(i32, String)> {
Err(anyhow!("oops!")) Err(anyhow!("oops!"))
} }

View file

@ -89,7 +89,7 @@ pub struct MockDBus {
pub struct Test { pub struct Test {
base: TempDir, base: TempDir,
pub process_cb: Cell<fn(&str, &[&OsStr]) -> Result<(i32, String)>>, pub process_cb: Cell<fn(&OsStr, &[&OsStr]) -> Result<(i32, String)>>,
pub mock_dbus: Cell<Option<MockDBus>>, pub mock_dbus: Cell<Option<MockDBus>>,
pub dbus_address: Mutex<Option<Address>>, pub dbus_address: Mutex<Option<Address>>,
} }