1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::Command;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::vec::Vec;

#[derive(Serialize, Deserialize, Clone, Debug, Hash, Default, PartialEq, Eq)]
pub struct Commands {
    pub history: Vec<Command>,
}

impl Commands {
    pub fn new() -> Self {
        Commands {
            history: Vec::new(),
        }
    }
    pub fn exec(&mut self, command_str: &str) -> Command {
        self.exec_in(command_str, std::env::temp_dir())
    }

    pub fn exec_in<P: AsRef<Path>>(&mut self, command_str: &str, path: P) -> Command {
        let command = Command::new(command_str).exec_in(&path);
        self.history.insert(0, command.clone());
        command
    }
}

#[cfg(test)]
use crate::Status;

#[test]
fn usage() {
    let mut commands = Commands::new();
    let git_version = commands.exec("git --version");
    assert_eq!(Status::Ok, git_version.status);
}