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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::str;

use crate::{command::BashCommand, error::RashError, process::Process};

type Out = (i32, String, String);

#[cfg(unix)]
pub fn __command<S: AsRef<str>>(c: S) -> Result<Out, RashError> {
    let command = BashCommand::new(c)?;
    let mut process = Process::new();
    Ok(unsafe {
        process.open(command)?;
        let ret = process.close()?;
        let stdout = process.stdout()?;
        let stderr = process.stderr()?;
        (ret, stdout, stderr)
    })
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;

    lazy_static! {
        static ref EMPTY_STRING: String = String::default();
    }

    #[test]
    fn test_commands_return_zero() {
        [
            "ls",
            "ls -l | cat -",
            "ls | cat $(echo '-')",
            "[[ 5 -eq $((3 + 2)) ]]",
            "/bin/sh -c 'echo hi'",
            "exit 0",
        ]
        .iter()
        .for_each(|c| {
            let (r, _, _) = __command(c).unwrap();
            assert_eq!(r, 0);
        });
    }

    #[test]
    fn test_commands_return_non_zero() {
        [("i_am_not_a_valid_executable", 127), ("echo hi | grep 'bye'", 1), ("exit 54;", 54)]
            .iter()
            .for_each(move |(c, ret)| {
                let (r, _, _) = __command(c).unwrap();
                assert_eq!(r, *ret);
            });
    }

    #[test]
    fn test_commands_stdout() {
        [
            ("", ""),
            ("echo hi", "hi\n"),
            ("echo hi >/dev/null", ""),
            ("echo -n $((3 + 2 - 1))", "4"),
        ]
        .iter()
        .for_each(move |(c, out)| {
            default_assertions(__command(c).unwrap(), out);
        });
    }

    #[test]
    fn test_commands_stderr() -> Result<(), RashError> {
        Ok(assert_eq!(__command("echo -n 'hi' >&2")?, (0, EMPTY_STRING.clone(), "hi".to_string())))
    }

    #[test]
    fn test_combined_stdout() -> Result<(), RashError> {
        Ok(default_assertions(__command("echo -n hi; echo -n bye 2>&1;")?, "hibye"))
    }

    #[test]
    fn test_redirect_to_and_read_from_file() -> anyhow::Result<()> {
        let temp_dir = TempDir::new()?;
        let path = temp_dir.path().to_str().unwrap();
        let script = format!("cd {path}; echo -n 'foo' > bar.txt; cat bar.txt;");

        default_assertions(__command(script)?, "foo");

        Ok(temp_dir.close()?)
    }

    #[test]
    fn test_run_script() -> anyhow::Result<()> {
        let temp_dir = TempDir::new()?;
        let path = temp_dir.path().to_str().unwrap();
        let message = "hi from within the script!";
        let script = format!(
            "cd {path}; echo -n \"echo -n '{message}'\" > bar.sh; chmod u+x bar.sh; ./bar.sh;"
        );

        default_assertions(__command(script)?, message);

        Ok(temp_dir.close()?)
    }

    #[test]
    fn test_quotes() -> Result<(), RashError> {
        let message = "a new line \n a day keeps the doctors away";
        default_assertions(__command(format!("echo -n '{}'", message))?, message);

        let message = "\"\"'blah' \'blah\' 'blah'''";
        let expected = "blah blah blah";
        default_assertions(__command(format!("echo -n {}", message))?, expected);

        let message = "hello world";
        let expected = "hello world\n";
        default_assertions(__command(format!("echo {}", message))?, expected);
        Ok(())
    }

    #[test]
    fn test_comments() -> Result<(), RashError> {
        Ok(default_assertions(__command("#echo 'i am silent'")?, EMPTY_STRING.as_ref()))
    }

    #[test]
    fn test_backslashes() -> Result<(), RashError> {
        let c = "echo \
        -n \
        hi \
        there";

        Ok(default_assertions(__command(c)?, "hi there"))
    }

    fn default_assertions(o: Out, expected_stdout: &str) -> () {
        assert_eq!(o, (0, expected_stdout.to_string(), EMPTY_STRING.clone()))
    }
}