multiversx_sc_scenario/
vm_go_tool.rs

1use colored::Colorize;
2use std::{
3    io::{Error, ErrorKind},
4    path::Path,
5    process::{Command, Output},
6};
7
8const RUNNER_TOOL_NAME: &str = "mx-scenario-go";
9const RUNNER_TOOL_NAME_LEGACY: &str = "run-scenarios";
10
11/// Just marks that the tool was not found.
12struct ToolNotFound;
13
14/// Runs the VM executable,
15/// which reads parses and executes one or more mandos tests.
16pub fn run_mx_scenario_go(absolute_path: &Path) {
17    if cfg!(not(feature = "compiled-sc-tests")) {
18        return;
19    }
20
21    let output = Command::new(RUNNER_TOOL_NAME)
22        .arg("run")
23        .arg(absolute_path)
24        .output();
25    if run_scenario_tool(RUNNER_TOOL_NAME, output).is_ok() {
26        return;
27    }
28
29    // fallback - use the old binary
30    println!(
31        "{}",
32        format!("Warning: `{RUNNER_TOOL_NAME}` not found. Using `{RUNNER_TOOL_NAME_LEGACY}` as fallback.").yellow(),
33    );
34    let output = Command::new(RUNNER_TOOL_NAME_LEGACY)
35        .arg(absolute_path)
36        .output();
37    if run_scenario_tool(RUNNER_TOOL_NAME_LEGACY, output).is_ok() {
38        return;
39    }
40
41    panic!("Could not find `{RUNNER_TOOL_NAME_LEGACY}`, aborting.");
42}
43
44fn run_scenario_tool(tool_name: &str, output: Result<Output, Error>) -> Result<(), ToolNotFound> {
45    if let Err(error) = &output {
46        if error.kind() == ErrorKind::NotFound {
47            return Err(ToolNotFound);
48        }
49    }
50
51    let output = output.expect("failed to execute process");
52
53    if output.status.success() {
54        println!("{}", String::from_utf8_lossy(output.stdout.as_slice()));
55    } else {
56        panic!(
57            "{} output:\n{}\n{}",
58            tool_name,
59            String::from_utf8_lossy(output.stdout.as_slice()),
60            String::from_utf8_lossy(output.stderr.as_slice())
61        );
62    }
63
64    Ok(())
65}