mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-09 16:10:34 -04:00
process: Use a callback instead of real subprocess in tests
This commit is contained in:
parent
1c825797be
commit
c37bd22db0
2 changed files with 22 additions and 0 deletions
|
@ -10,6 +10,7 @@ use std::ffi::OsStr;
|
|||
use tokio::process::Command;
|
||||
use tracing::warn;
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<i32> {
|
||||
// Run given script and return the exit code
|
||||
let mut child = Command::new(executable).args(args).spawn()?;
|
||||
|
@ -17,6 +18,14 @@ pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> R
|
|||
status.code().ok_or(anyhow!("Killed by signal"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub async fn script_exit_code(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<i32> {
|
||||
let test = crate::testing::current();
|
||||
let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect();
|
||||
let cb = &test.process_cb;
|
||||
cb(executable, args.as_ref()).map(|(res, _)| res)
|
||||
}
|
||||
|
||||
pub async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]) -> Result<()> {
|
||||
// Run given script to get exit code and return true on success.
|
||||
// Return Err on failure, but also print an error if needed
|
||||
|
@ -33,6 +42,7 @@ pub async fn run_script(name: &str, executable: &str, args: &[impl AsRef<OsStr>]
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
|
||||
// Run given command and return the output given
|
||||
let output = Command::new(executable).args(args).output();
|
||||
|
@ -42,3 +52,11 @@ pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Resu
|
|||
let s = std::str::from_utf8(&output.stdout)?;
|
||||
Ok(s.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub async fn script_output(executable: &str, args: &[impl AsRef<OsStr>]) -> Result<String> {
|
||||
let test = crate::testing::current();
|
||||
let args: Vec<&OsStr> = args.iter().map(|arg| arg.as_ref()).collect();
|
||||
let cb = &test.process_cb;
|
||||
cb(executable, args.as_ref()).map(|(_, res)| res)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
@ -12,6 +14,7 @@ pub fn start() -> TestHandle {
|
|||
assert!(lock.borrow().as_ref().is_none());
|
||||
let test: Rc<Test> = Rc::new(Test {
|
||||
base: tempdir().expect("Couldn't create test directory"),
|
||||
process_cb: |_, _| Err(anyhow!("No current process_cb")),
|
||||
});
|
||||
*lock.borrow_mut() = Some(test.clone());
|
||||
TestHandle { test }
|
||||
|
@ -28,6 +31,7 @@ pub fn current() -> Rc<Test> {
|
|||
|
||||
pub struct Test {
|
||||
base: TempDir,
|
||||
pub process_cb: fn(&str, &[&OsStr]) -> Result<(i32, String)>,
|
||||
}
|
||||
|
||||
pub struct TestHandle {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue