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
use colored::Colorize;
use std::{io::ErrorKind, path::Path, process::Command};

const RUNNER_TOOL_NAME: &str = "run-scenarios";
const RUNNER_TOOL_NAME_LEGACY: &str = "mandos-test";

/// Just marks that the tool was not found.
struct ToolNotFound;

/// Runs the Arwen executable,
/// which reads parses and executes one or more mandos tests.
pub fn run_go<P: AsRef<Path>>(relative_path: P) {
    if cfg!(not(feature = "run-go-tests")) {
        return;
    }

    let mut absolute_path = std::env::current_dir().unwrap();
    absolute_path.push(relative_path);

    if run_scenario_tool(RUNNER_TOOL_NAME, absolute_path.as_path()).is_ok() {
        return;
    }

    // fallback - use the old binary
    println!(
        "{}",
        format!("Warning: `{RUNNER_TOOL_NAME}` not found. Using `{RUNNER_TOOL_NAME_LEGACY}` as fallback.").yellow(),
    );
    if run_scenario_tool(RUNNER_TOOL_NAME_LEGACY, absolute_path.as_path()).is_ok() {
        return;
    }

    panic!("Could not find `{RUNNER_TOOL_NAME_LEGACY}`, aborting.");
}

fn run_scenario_tool(tool_name: &str, path: &Path) -> Result<(), ToolNotFound> {
    let result = Command::new(tool_name).arg(path).output();

    if let Err(error) = &result {
        if error.kind() == ErrorKind::NotFound {
            return Err(ToolNotFound);
        }
    }

    let output = result.expect("failed to execute process");

    if output.status.success() {
        println!("{}", String::from_utf8_lossy(output.stdout.as_slice()));
    } else {
        panic!(
            "{} output:\n{}\n{}",
            tool_name,
            String::from_utf8_lossy(output.stdout.as_slice()),
            String::from_utf8_lossy(output.stderr.as_slice())
        );
    }

    Ok(())
}