| 1 |
//! A test suite to test the `wasm-tools` CLI itself. |
| 2 |
//! |
| 3 |
//! This test suite will look for `*.wat` and `*.wit` files in the |
| 4 |
//! `tests/cli/**` directory, recursively. For more information about supported |
| 5 |
//! directives and features of this test suite see the `tests/cli/readme.wat` |
| 6 |
//! file which has an explanatory comment at the top for what's going on. |
| 7 |
|
| 8 |
use anyhow::{Context, Result, bail}; |
| 9 |
use indexmap::IndexMap; |
| 10 |
use libtest_mimic::{Arguments, Trial}; |
| 11 |
use pretty_assertions::StrComparison; |
| 12 |
use std::env; |
| 13 |
use std::io::Write; |
| 14 |
use std::path::{Path, PathBuf}; |
| 15 |
use std::process::{Command, Output, Stdio}; |
| 16 |
use tempfile::TempDir; |
| 17 |
|
| 18 |
fn main() { |
| 19 |
let mut tests = Vec::new(); |
| 20 |
find_tests("tests/cli".as_ref(), &mut tests); |
| 21 |
let bless = env::var("BLESS").is_ok(); |
| 22 |
|
| 23 |
let mut trials = Vec::new(); |
| 24 |
for test in tests { |
| 25 |
let trial = Trial::test(format!("{test:?}"), move || { |
| 26 |
run_test(&test, bless) |
| 27 |
.with_context(|| format!("failed test {test:?}")) |
| 28 |
.map_err(|e| format!("{e:?}").into()) |
| 29 |
}) |
| 30 |
// This test suite can't run on wasm since it involves spawning |
| 31 |
// subprocesses. |
| 32 |
.with_ignored_flag(cfg!(target_family = "wasm")); |
| 33 |
trials.push(trial); |
| 34 |
} |
| 35 |
|
| 36 |
let mut args = Arguments::from_args(); |
| 37 |
if cfg!(target_family = "wasm") && !cfg!(target_feature = "atomics") { |
| 38 |
args.test_threads = Some(1); |
| 39 |
} |
| 40 |
libtest_mimic::run(&args, trials).exit(); |
| 41 |
} |
| 42 |
|
| 43 |
fn run_test(test: &Path, bless: bool) -> Result<()> { |
| 44 |
let contents = std::fs::read_to_string(test)?; |
| 45 |
|
| 46 |
let mut directives = contents |
| 47 |
.lines() |
| 48 |
.enumerate() |
| 49 |
.filter(|(_, l)| !l.is_empty()) |
| 50 |
.filter_map(|(i, l)| { |
| 51 |
l.strip_prefix("// ") |
| 52 |
.or(l.strip_prefix(";; ")) |
| 53 |
.map(|l| (i + 1, l)) |
| 54 |
}); |
| 55 |
|
| 56 |
let mut commands = IndexMap::new(); |
| 57 |
|
| 58 |
while let Some((i, line)) = directives.next() { |
| 59 |
let run = line.strip_prefix("RUN"); |
| 60 |
let fail = line.strip_prefix("FAIL"); |
| 61 |
let (directive, should_fail) = match run.map(|l| (l, false)).or(fail.map(|l| (l, true))) { |
| 62 |
Some(pair) => pair, |
| 63 |
None => continue, |
| 64 |
}; |
| 65 |
let (cmd, name) = match directive.strip_prefix("[") { |
| 66 |
Some(prefix) => match prefix.find("]:") { |
| 67 |
Some(i) => (&prefix[i + 2..], &prefix[..i]), |
| 68 |
None => bail!("line {i}: failed to find `]:` after `[`"), |
| 69 |
}, |
| 70 |
None => match directive.strip_prefix(":") { |
| 71 |
Some(cmd) => (cmd, ""), |
| 72 |
None => bail!("line {i}: failed to find `:` after `RUN` or `FAIL`"), |
| 73 |
}, |
| 74 |
}; |
| 75 |
let mut cmd = cmd.to_string(); |
| 76 |
while cmd.ends_with("\\") { |
| 77 |
cmd.pop(); |
| 78 |
match directives.next() { |
| 79 |
Some((_, line)) => cmd.push_str(line), |
| 80 |
None => bail!("line {i}: directive ends in `\\` but nothing on next line"), |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
match commands.insert(name, (cmd, should_fail, i)) { |
| 85 |
Some(_) => bail!("line {i}: duplicate directive named {name:?}"), |
| 86 |
None => {} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if commands.is_empty() { |
| 91 |
bail!("failed to find `// RUN: ...` or `// FAIL: ...` at the top of this file"); |
| 92 |
} |
| 93 |
let exe = Path::new(env!("CARGO_BIN_EXE_wasm-tools")); |
| 94 |
let tempdir = TempDir::new_in(exe.parent().unwrap())?; |
| 95 |
for (name, (line, should_fail, i)) in commands { |
| 96 |
run_test_directive(test, &name, &line, bless, should_fail, exe, &tempdir).with_context( |
| 97 |
|| { |
| 98 |
let kind = if should_fail { "FAIL" } else { "RUN" }; |
| 99 |
format!("failed {kind} directive `{name}` on line {i}") |
| 100 |
}, |
| 101 |
)?; |
| 102 |
} |
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
|
| 106 |
fn run_test_directive( |
| 107 |
test: &Path, |
| 108 |
name: &str, |
| 109 |
line: &str, |
| 110 |
bless: bool, |
| 111 |
should_fail: bool, |
| 112 |
exe: &Path, |
| 113 |
tempdir: &TempDir, |
| 114 |
) -> Result<()> { |
| 115 |
let mut cmd = Command::new(exe); |
| 116 |
let mut stdin = None; |
| 117 |
for arg in line.split_whitespace() { |
| 118 |
let arg = arg.replace("%tmpdir", tempdir.path().to_str().unwrap()); |
| 119 |
if arg == "|" { |
| 120 |
let output = execute(&mut cmd, stdin.as_deref(), false)?; |
| 121 |
stdin = Some(output.stdout); |
| 122 |
cmd = Command::new(exe); |
| 123 |
} else if arg == "%" { |
| 124 |
cmd.arg(test); |
| 125 |
} else { |
| 126 |
cmd.arg(arg); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
let output = execute(&mut cmd, stdin.as_deref(), should_fail)?; |
| 131 |
let extension = test.extension().unwrap().to_str().unwrap(); |
| 132 |
let extension = if name.is_empty() { |
| 133 |
extension.to_string() |
| 134 |
} else { |
| 135 |
format!("{extension}.{name}") |
| 136 |
}; |
| 137 |
assert_output( |
| 138 |
bless, |
| 139 |
&output.stdout, |
| 140 |
&test.with_extension(&format!("{extension}.stdout")), |
| 141 |
&tempdir, |
| 142 |
) |
| 143 |
.context("failed to check stdout expectation (auto-update with BLESS=1)")?; |
| 144 |
assert_output( |
| 145 |
bless, |
| 146 |
&output.stderr, |
| 147 |
&test.with_extension(&format!("{extension}.stderr")), |
| 148 |
&tempdir, |
| 149 |
) |
| 150 |
.context("failed to check stderr expectation (auto-update with BLESS=1)")?; |
| 151 |
Ok(()) |
| 152 |
} |
| 153 |
|
| 154 |
fn execute(cmd: &mut Command, stdin: Option<&[u8]>, should_fail: bool) -> Result<Output> { |
| 155 |
cmd.stdin(Stdio::piped()); |
| 156 |
cmd.stdout(Stdio::piped()); |
| 157 |
cmd.stderr(Stdio::piped()); |
| 158 |
let mut p = cmd |
| 159 |
.spawn() |
| 160 |
.with_context(|| format!("failed to spawn {cmd:?}"))?; |
| 161 |
|
| 162 |
let mut io = p.stdin.take().unwrap(); |
| 163 |
if let Some(stdin) = stdin { |
| 164 |
io.write_all(stdin).context("failed to write to stdin")?; |
| 165 |
} |
| 166 |
drop(io); |
| 167 |
|
| 168 |
let output = p |
| 169 |
.wait_with_output() |
| 170 |
.context("failed to wait for process exit")?; |
| 171 |
|
| 172 |
let mut failure = None; |
| 173 |
match output.status.code() { |
| 174 |
Some(0) => { |
| 175 |
if should_fail { |
| 176 |
failure = Some("succeeded instead of failed"); |
| 177 |
} |
| 178 |
} |
| 179 |
Some(1) | Some(2) => { |
| 180 |
if !should_fail { |
| 181 |
failure = Some("failed"); |
| 182 |
} |
| 183 |
} |
| 184 |
_ => failure = Some("unknown exit code"), |
| 185 |
} |
| 186 |
if let Some(msg) = failure { |
| 187 |
bail!( |
| 188 |
"{cmd:?} {msg}: |
| 189 |
status: {} |
| 190 |
stdout: {} |
| 191 |
stderr: {}", |
| 192 |
output.status, |
| 193 |
String::from_utf8_lossy(&output.stdout), |
| 194 |
String::from_utf8_lossy(&output.stderr) |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
Ok(output) |
| 199 |
} |
| 200 |
|
| 201 |
fn assert_output(bless: bool, output: &[u8], path: &Path, tempdir: &TempDir) -> Result<()> { |
| 202 |
let tempdir = tempdir.path().to_str().unwrap(); |
| 203 |
// sanitize the output to be consistent across platforms and handle per-test |
| 204 |
// differences such as `%tmpdir`, as well as the version number of the crate being |
| 205 |
// tested in the producers custom section. |
| 206 |
let mut output = String::from_utf8_lossy(output) |
| 207 |
.replace(tempdir, "%tmpdir") |
| 208 |
.replace("\\", "/") |
| 209 |
.lines() |
| 210 |
.map(|line| { |
| 211 |
if let Some(start) = line.find("(processed-by \"wit-component\"") { |
| 212 |
let (before, _) = line.split_at(start); |
| 213 |
format!("{before}(processed-by \"wit-component\" \"%version\")") |
| 214 |
} else { |
| 215 |
line.to_owned() |
| 216 |
} |
| 217 |
}) |
| 218 |
.collect::<Vec<String>>() |
| 219 |
.join("\n") |
| 220 |
.trim_end() |
| 221 |
.to_string(); |
| 222 |
|
| 223 |
// Leave a single trailing newline on all test outputs |
| 224 |
if !output.is_empty() { |
| 225 |
output.push_str("\n"); |
| 226 |
} |
| 227 |
|
| 228 |
if bless { |
| 229 |
if output.is_empty() { |
| 230 |
drop(std::fs::remove_file(path)); |
| 231 |
} else { |
| 232 |
std::fs::write(path, output).with_context(|| format!("failed to write {path:?}"))?; |
| 233 |
} |
| 234 |
return Ok(()); |
| 235 |
} |
| 236 |
|
| 237 |
if output.is_empty() { |
| 238 |
if path.exists() { |
| 239 |
bail!("command had no output but {path:?} exists"); |
| 240 |
} else { |
| 241 |
Ok(()) |
| 242 |
} |
| 243 |
} else { |
| 244 |
let contents = std::fs::read_to_string(path) |
| 245 |
.with_context(|| format!("failed to read {path:?}"))? |
| 246 |
.replace("\r\n", "\n"); |
| 247 |
if output != contents { |
| 248 |
bail!( |
| 249 |
"failed test: result is not as expected:{}", |
| 250 |
StrComparison::new(&contents, &output), |
| 251 |
); |
| 252 |
} |
| 253 |
Ok(()) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
fn find_tests(path: &Path, tests: &mut Vec<PathBuf>) { |
| 258 |
for f in path.read_dir().unwrap() { |
| 259 |
let f = f.unwrap(); |
| 260 |
if f.file_type().unwrap().is_dir() { |
| 261 |
find_tests(&f.path(), tests); |
| 262 |
continue; |
| 263 |
} |
| 264 |
match f.path().extension().and_then(|s| s.to_str()) { |
| 265 |
Some("wat") | Some("wit") | Some("wast") => {} |
| 266 |
_ => continue, |
| 267 |
} |
| 268 |
tests.push(f.path()); |
| 269 |
} |
| 270 |
} |