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

use pish::parse::Parse;
use pish::regex::{AllEngines, Pattern};
pub use pish::regex::RegexEngine;

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) -> impl RegexEngine {
    let parsed = Pattern::parse_from_bytes(pat.as_bytes()).expect(&format!("pattern {pat} does not parse"));
    AllEngines::compile(parsed).unwrap()
}