wasmer_integration_tests_cli/
util.rs

1use anyhow::bail;
2use std::path::Path;
3use std::process::Command;
4
5#[derive(Debug, Copy, Clone)]
6pub enum Compiler {
7    Cranelift,
8    LLVM,
9    Singlepass,
10}
11
12impl Compiler {
13    pub const fn to_flag(self) -> &'static str {
14        match self {
15            Compiler::Cranelift => "--cranelift",
16            Compiler::LLVM => "--llvm",
17            Compiler::Singlepass => "--singlepass",
18        }
19    }
20}
21
22pub fn run_code(
23    operating_dir: &Path,
24    executable_path: &Path,
25    args: &[String],
26    stderr: bool,
27) -> anyhow::Result<String> {
28    let output = Command::new(executable_path.canonicalize()?)
29        .current_dir(operating_dir)
30        .args(args)
31        .output()?;
32
33    if !output.status.success() && !stderr {
34        bail!(
35            "running executable failed: stdout: {}\n\nstderr: {}",
36            std::str::from_utf8(&output.stdout)
37                .expect("stdout is not utf8! need to handle arbitrary bytes"),
38            std::str::from_utf8(&output.stderr)
39                .expect("stderr is not utf8! need to handle arbitrary bytes")
40        );
41    }
42    let output = std::str::from_utf8(if stderr {
43        &output.stderr
44    } else {
45        &output.stdout
46    })
47    .expect("output from running executable is not utf-8");
48
49    Ok(output.to_owned())
50}