use std::process::Command;
fn wasmrt() -> Command {
Command::new(env!("CARGO_BIN_EXE_wasmrt"))
}
fn fixture() -> std::path::PathBuf {
use std::sync::atomic::{AtomicUsize, Ordering};
static NEXT: AtomicUsize = AtomicUsize::new(0);
let n = NEXT.fetch_add(1, Ordering::Relaxed);
let p = std::env::temp_dir().join(format!("wasmrt_flagpos_{}_{n}.wat", std::process::id()));
std::fs::write(&p, br#"(module (func (export "f")))"#).expect("write fixture");
p
}
#[test]
fn no_two_fixtures_share_a_path() {
assert_ne!(fixture(), fixture(), "parallel tests must not write the same fixture file");
}
fn missing_dir() -> String {
std::env::temp_dir()
.join("wasmrt_no_such_dir_flagpos")
.to_string_lossy()
.into_owned()
}
fn run(args: &[&str]) -> (bool, String) {
let out = wasmrt().args(args).output().expect("run wasmrt");
let text = String::from_utf8_lossy(&out.stderr).into_owned()
+ &String::from_utf8_lossy(&out.stdout);
(out.status.success(), text)
}
#[test]
fn a_trailing_preopen_flag_is_applied_not_donated_to_the_guest() {
let m = fixture();
let (_, after) = run(&["wasi", &m.to_string_lossy(), "--dir", &missing_dir()]);
assert!(
after.contains("cannot preopen"),
"a trailing --dir must be APPLIED (wazmrt's spelling); got: {after}"
);
}
#[test]
fn both_flag_positions_behave_identically() {
let m = fixture();
let d = missing_dir();
let (_, before) = run(&["wasi", "--dir", &d, &m.to_string_lossy()]);
let (_, after) = run(&["wasi", &m.to_string_lossy(), "--dir", &d]);
assert_eq!(
before.contains("cannot preopen"),
after.contains("cannot preopen"),
"leading and trailing --dir must do the same thing\nbefore: {before}\nafter: {after}"
);
}
#[test]
fn an_unknown_leading_option_is_refused_not_treated_as_a_path() {
let (ok, text) = run(&["wasi", "--typo", "x.wasm"]);
assert!(!ok, "an unknown flag must fail");
assert!(
text.contains("unknown flag"),
"must name it as an unknown FLAG, not as an unreadable file; got: {text}"
);
}
#[test]
fn a_host_flag_stranded_in_guest_argv_is_refused() {
let m = fixture();
let (ok, text) = run(&["wasi", &m.to_string_lossy(), "somearg", "--dir", &missing_dir()]);
assert!(!ok, "a stranded host flag must FAIL the run; got: {text}");
assert!(text.contains("passed to the GUEST"), "and say why; got: {text}");
}
#[test]
fn an_explicit_double_dash_silences_the_warning() {
let m = fixture();
let (_, text) = run(&["wasi", &m.to_string_lossy(), "--", "--dir", &missing_dir()]);
assert!(
!text.contains("passed to the GUEST"),
"after `--` the flag is deliberately the guest's; must not warn. got: {text}"
);
}