aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common.rs
blob: 210b92a293d4d4e0f366027368616078af3c5f83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#[cfg(test)]
pub fn test_case(_name: &str, script: &[u8], expected_output: &[u8]) {
    use pish::parse::Parse;
    use pish::rw::*;
    use std::io::Read;
    use std::sync::{Arc, Mutex};

    let session = Arc::new(Mutex::new(pish::Session::new_noninteractive()));
    let script = pish::parse::Script::parse_from_bytes(script).unwrap();
    let (stdin, _c1) = InputReader::new(Input::Null);
    let (mut pr, pw) = std::io::pipe().unwrap();
    let (stdout, _c2) = OutputWriter::new(Output::Pipe(pw));
    let mut exec = pish::run::Executor::new(session.clone());
    let mut spawned = exec.execute_script(script, stdin, stdout);
    let _res = spawned.join_timeout(1000);
    let mut actual_output = Vec::new();
    pr.read_to_end(&mut actual_output).unwrap();
    assert_eq!(expected_output, actual_output);
}