| 1 |
//! A test suite to parse everything in `parse-fail` and assert that it matches |
| 2 |
//! the `*.err` file it generates. |
| 3 |
//! |
| 4 |
//! Use `BLESS=1` in the environment to auto-update `*.err` files. Be sure to |
| 5 |
//! look at the diff! |
| 6 |
|
| 7 |
use libtest_mimic::{Arguments, Trial}; |
| 8 |
use std::env; |
| 9 |
use std::path::{Path, PathBuf}; |
| 10 |
|
| 11 |
fn main() { |
| 12 |
let mut tests = Vec::new(); |
| 13 |
find_tests("tests/parse-fail".as_ref(), &mut tests); |
| 14 |
let bless = env::var("BLESS").is_ok(); |
| 15 |
|
| 16 |
let mut trials = Vec::new(); |
| 17 |
for test in tests { |
| 18 |
let trial = Trial::test(format!("{test:?}"), move || { |
| 19 |
run_test(&test, bless).map_err(|e| format!("{e:?}").into()) |
| 20 |
}); |
| 21 |
trials.push(trial); |
| 22 |
} |
| 23 |
|
| 24 |
let mut args = Arguments::from_args(); |
| 25 |
if cfg!(target_family = "wasm") && !cfg!(target_feature = "atomics") { |
| 26 |
args.test_threads = Some(1); |
| 27 |
} |
| 28 |
libtest_mimic::run(&args, trials).exit(); |
| 29 |
} |
| 30 |
|
| 31 |
fn run_test(test: &Path, bless: bool) -> anyhow::Result<()> { |
| 32 |
let err = match wat::parse_file(test) { |
| 33 |
Ok(_) => anyhow::bail!("{} parsed successfully", test.display()), |
| 34 |
Err(e) => e.to_string() + "\n", |
| 35 |
}; |
| 36 |
let assert = test.with_extension("wat.err"); |
| 37 |
if bless { |
| 38 |
std::fs::write(assert, err)?; |
| 39 |
return Ok(()); |
| 40 |
} |
| 41 |
|
| 42 |
// Ignore CRLF line ending and force always `\n` |
| 43 |
let assert = std::fs::read_to_string(assert) |
| 44 |
.unwrap_or(String::new()) |
| 45 |
.replace("\r\n", "\n"); |
| 46 |
|
| 47 |
// Compare normalize verisons which handles weirdness like path differences |
| 48 |
if normalize(&assert) == normalize(&err) { |
| 49 |
return Ok(()); |
| 50 |
} |
| 51 |
|
| 52 |
anyhow::bail!( |
| 53 |
"errors did not match:\n\nexpected:\n\t{}\nactual:\n\t{}\n", |
| 54 |
tab(&assert), |
| 55 |
tab(&err), |
| 56 |
); |
| 57 |
|
| 58 |
fn normalize(s: &str) -> String { |
| 59 |
s.replace("\\", "/") |
| 60 |
} |
| 61 |
|
| 62 |
fn tab(s: &str) -> String { |
| 63 |
s.replace("\n", "\n\t") |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
fn find_tests(path: &Path, tests: &mut Vec<PathBuf>) { |
| 68 |
for f in path.read_dir().unwrap() { |
| 69 |
let f = f.unwrap(); |
| 70 |
if f.file_type().unwrap().is_dir() { |
| 71 |
find_tests(&f.path(), tests); |
| 72 |
continue; |
| 73 |
} |
| 74 |
match f.path().extension().and_then(|s| s.to_str()) { |
| 75 |
Some("wat") => {} |
| 76 |
_ => continue, |
| 77 |
} |
| 78 |
tests.push(f.path()); |
| 79 |
} |
| 80 |
} |