Skip to main content

wasmsh_testkit/
oracle.rs

1//! Oracle comparison: run shell scripts against local reference shells.
2//!
3//! Enabled via `WASMSH_ORACLE=1` environment variable. Requires `bash`
4//! or `busybox ash` to be installed on the host.
5
6use std::process::Command;
7
8/// Result from an oracle shell execution.
9#[derive(Debug)]
10pub struct OracleResult {
11    pub shell: String,
12    pub status: i32,
13    pub stdout: String,
14    pub stderr: String,
15}
16
17/// Run a script against a reference shell. Returns `None` if the shell
18/// is not available or oracle mode is disabled.
19pub fn run_oracle(script: &str, shell: &str) -> Option<OracleResult> {
20    if std::env::var("WASMSH_ORACLE").is_err() {
21        return None;
22    }
23
24    let output = Command::new(shell).arg("-c").arg(script).output().ok()?;
25
26    Some(OracleResult {
27        shell: shell.to_string(),
28        status: output.status.code().unwrap_or(-1),
29        stdout: String::from_utf8_lossy(&output.stdout).to_string(),
30        stderr: String::from_utf8_lossy(&output.stderr).to_string(),
31    })
32}
33
34/// Compare wasmsh output against oracle output.
35pub fn compare_oracle(
36    wasmsh_status: i32,
37    wasmsh_stdout: &str,
38    oracle: &OracleResult,
39    ignore_stderr: bool,
40) -> Vec<String> {
41    let mut diffs = Vec::new();
42
43    if wasmsh_status != oracle.status {
44        diffs.push(format!(
45            "[{}] status: wasmsh={}, oracle={}",
46            oracle.shell, wasmsh_status, oracle.status
47        ));
48    }
49
50    if wasmsh_stdout != oracle.stdout {
51        diffs.push(format!(
52            "[{}] stdout differs:\n  wasmsh: {:?}\n  oracle: {:?}",
53            oracle.shell, wasmsh_stdout, oracle.stdout
54        ));
55    }
56
57    if !ignore_stderr {
58        // stderr comparison is informational, not enforced by default
59    }
60
61    diffs
62}