Skip to main content

wasmer_integration_tests_cli/
util.rs

1use anyhow::bail;
2use std::path::Path;
3use std::process::Command;
4
5use crate::assets::get_wasmer_path;
6
7pub const DEFAULT_WASMER_REGISTRY: &str = "https://registry.wasmer.io/graphql";
8
9pub fn wasmer_command() -> Command {
10    let mut cmd = Command::new(get_wasmer_path());
11    set_default_wasmer_registry(&mut cmd);
12    cmd
13}
14
15pub fn set_default_wasmer_registry(cmd: &mut Command) {
16    cmd.env(
17        "WASMER_REGISTRY",
18        std::env::var("DEFAULT_WASMER_REGISTRY").unwrap_or(DEFAULT_WASMER_REGISTRY.to_string()),
19    );
20}
21
22#[derive(Debug, Copy, Clone)]
23pub enum Compiler {
24    Cranelift,
25    LLVM,
26    Singlepass,
27}
28
29impl Compiler {
30    pub const fn to_flag(self) -> &'static str {
31        match self {
32            Compiler::Cranelift => "--cranelift",
33            Compiler::LLVM => "--llvm",
34            Compiler::Singlepass => "--singlepass",
35        }
36    }
37}
38
39pub fn run_code(
40    operating_dir: &Path,
41    executable_path: &Path,
42    args: &[String],
43    stderr: bool,
44) -> anyhow::Result<String> {
45    let output = Command::new(executable_path.canonicalize()?)
46        .current_dir(operating_dir)
47        .args(args)
48        .output()?;
49
50    if !output.status.success() && !stderr {
51        bail!(
52            "running executable failed: stdout: {}\n\nstderr: {}",
53            std::str::from_utf8(&output.stdout)
54                .expect("stdout is not utf8! need to handle arbitrary bytes"),
55            std::str::from_utf8(&output.stderr)
56                .expect("stderr is not utf8! need to handle arbitrary bytes")
57        );
58    }
59    let output = std::str::from_utf8(if stderr {
60        &output.stderr
61    } else {
62        &output.stdout
63    })
64    .expect("output from running executable is not utf-8");
65
66    Ok(output.to_owned())
67}