aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common.rs
blob: acf562ef8dcc518a2bb6580dae786cd24c4ea435 (plain) (blame)
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
#![allow(unused)]

use pish::parse::{
    Parse,
    regex::{CompiledPattern, Pattern},
};

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,
        "\nexpected: {}\n  actual: {}",
        expected_output.escape_ascii(),
        actual_output.escape_ascii()
    );
}

pub fn regex(pat: &str) -> CompiledPattern {
    Pattern::parse_from_bytes(pat.as_bytes())
        .expect(&format!("pattern {pat} does not parse"))
        .try_compile()
        .expect(&format!("pattern {pat} does not compile"))
}