use std::process::Command;
fn write_temp(name: &str, bytes: &[u8]) -> std::path::PathBuf {
let d = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("wasmrt_tests_{}", std::process::id()));
std::fs::create_dir_all(&d).expect("scratch dir");
let p = d.join(name);
std::fs::write(&p, bytes).expect("write fixture");
p
}
fn code(args: &[&str]) -> Option<i32> {
Command::new(env!("CARGO_BIN_EXE_wasmrt"))
.args(args)
.output()
.expect("spawn wasmrt")
.status
.code()
}
const VALID: &[u8] = br#"(module (func (export "f") (result i32) (i32.const 7)))"#;
const INVALID: &[u8] = br#"(module (func (export "f") (result i32) (i64.const 7)))"#;
#[test]
fn summarize_exits_zero_on_a_valid_module() {
let p = write_temp("wasmrt_exit_valid.wat", VALID);
assert_eq!(code(&[p.to_str().unwrap()]), Some(0));
}
#[test]
fn summarize_exits_nonzero_on_an_invalid_module() {
let p = write_temp("wasmrt_exit_invalid.wat", INVALID);
assert_eq!(code(&[p.to_str().unwrap()]), Some(1), "an invalid module must not exit 0");
let bin = wasmrt_core::wat::assemble(INVALID).expect("the assembler does not type-check");
let p = write_temp("wasmrt_exit_invalid.wasm", &bin);
assert_eq!(code(&[p.to_str().unwrap()]), Some(1));
}
#[test]
fn the_summary_never_calls_an_invalid_module_valid() {
let p = write_temp("wasmrt_exit_z3.wat", INVALID);
let out = Command::new(env!("CARGO_BIN_EXE_wasmrt")).arg(&p).output().expect("spawn");
let text = String::from_utf8_lossy(&out.stdout);
assert_eq!(out.status.code(), Some(1));
assert!(text.contains("validation FAILED"), "the verdict must be stated; got: {text}");
for line in text.lines() {
let l = line.to_ascii_lowercase();
let claims_valid = l.contains("validation ok") || (l.contains("valid") && !l.contains("invalid") && !l.contains("validation"));
assert!(!claims_valid, "a line claims validity for an invalid module: {line:?}");
}
let ok = write_temp("wasmrt_exit_z3_ok.wat", VALID);
let out = Command::new(env!("CARGO_BIN_EXE_wasmrt")).arg(&ok).output().expect("spawn");
assert!(String::from_utf8_lossy(&out.stdout).contains("validation OK"));
}
#[test]
fn no_arguments_is_a_usage_error() {
assert_eq!(code(&[]), Some(1));
assert_eq!(code(&["--version"]), Some(0));
assert_eq!(code(&["--help"]), Some(0));
}
#[test]
fn wast_exits_zero_only_when_every_assertion_passed() {
let pass = write_temp(
"wasmrt_exit_pass.wast",
br#"(module (func (export "f") (result i32) (i32.const 1)))
(assert_return (invoke "f") (i32.const 1))"#,
);
let fail = write_temp(
"wasmrt_exit_fail.wast",
br#"(module (func (export "f") (result i32) (i32.const 1)))
(assert_return (invoke "f") (i32.const 2))"#,
);
let unparseable = write_temp("wasmrt_exit_unparseable.wast", b"(module (func");
let missing = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join("wasmrt_exit_no_such_file.wast");
let _ = std::fs::remove_file(&missing);
assert_eq!(code(&["wast", pass.to_str().unwrap()]), Some(0));
assert_eq!(code(&["wast", fail.to_str().unwrap()]), Some(1), "a failed assertion");
assert_eq!(code(&["wast", unparseable.to_str().unwrap()]), Some(1), "an unparseable script");
assert_eq!(code(&["wast", missing.to_str().unwrap()]), Some(1), "an unreadable file");
assert_eq!(
code(&["wast", pass.to_str().unwrap(), fail.to_str().unwrap()]),
Some(1)
);
}