git_mob_tool/
helpers.rs

1use crate::Result;
2use std::process::Command;
3
4pub struct CmdOutput {
5    pub stdout: Vec<u8>,
6    pub stderr: Vec<u8>,
7    pub status_code: Option<i32>,
8}
9
10#[cfg(test)]
11use mockall::{automock, predicate::*};
12#[cfg_attr(test, automock)]
13pub trait CommandRunner {
14    #[allow(clippy::needless_lifetimes)] // explicit lifetime is needed for automock
15    fn execute<'a>(&self, program: &str, args: &[&'a str]) -> Result<CmdOutput>;
16}
17
18pub struct StdCommandRunner;
19
20impl CommandRunner for StdCommandRunner {
21    fn execute(&self, program: &str, args: &[&str]) -> Result<CmdOutput> {
22        let output = Command::new(program).args(args).output()?;
23
24        Ok(CmdOutput {
25            stdout: output.stdout,
26            stderr: output.stderr,
27            status_code: output.status.code(),
28        })
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_execute_success() -> Result<()> {
38        let result = StdCommandRunner.execute("git", &["--version"])?;
39
40        assert_eq!(result.status_code, Some(0));
41        assert!(result.stdout.starts_with(b"git version "));
42        assert!(result.stderr.is_empty());
43
44        Ok(())
45    }
46
47    #[test]
48    fn test_execute_failure() -> Result<()> {
49        let result = StdCommandRunner.execute("git", &["--invalid_option"])?;
50
51        assert!(result.status_code.is_some_and(|x| x != 0));
52        assert!(result.stdout.is_empty());
53        assert!(result
54            .stderr
55            .starts_with(b"unknown option: --invalid_option"));
56
57        Ok(())
58    }
59
60    #[test]
61    fn test_execute_error() -> Result<()> {
62        let result = StdCommandRunner.execute("non_existent_program", &[]);
63
64        assert!(result.is_err());
65
66        Ok(())
67    }
68}