| 1 |
//! You can run this test suite with: |
| 2 |
//! |
| 3 |
//! cargo test --test all |
| 4 |
//! |
| 5 |
//! An argument can be passed as well to filter, based on filename, which test |
| 6 |
//! to run |
| 7 |
//! |
| 8 |
//! cargo test --test all foo.wit |
| 9 |
|
| 10 |
use anyhow::{Context, Result, bail}; |
| 11 |
use libtest_mimic::{Arguments, Trial}; |
| 12 |
use pretty_assertions::StrComparison; |
| 13 |
use std::env; |
| 14 |
use std::ffi::OsStr; |
| 15 |
use std::fs; |
| 16 |
use std::io; |
| 17 |
use std::path::{Path, PathBuf}; |
| 18 |
use std::str; |
| 19 |
use wit_parser::*; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
env_logger::init(); |
| 23 |
let tests = find_tests(); |
| 24 |
|
| 25 |
let mut trials = Vec::new(); |
| 26 |
for test in tests { |
| 27 |
let trial = Trial::test(format!("{test:?}"), move || { |
| 28 |
Runner {} |
| 29 |
.run(&test) |
| 30 |
.context(format!("test {test:?} failed")) |
| 31 |
.map_err(|e| format!("{e:?}").into()) |
| 32 |
}); |
| 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 |
/// Recursively finds all tests in a whitelisted set of directories which we |
| 44 |
/// then load up and test in parallel. |
| 45 |
fn find_tests() -> Vec<PathBuf> { |
| 46 |
let mut tests = Vec::new(); |
| 47 |
find_tests("tests/ui".as_ref(), &mut tests); |
| 48 |
find_tests("tests/ui/parse-fail".as_ref(), &mut tests); |
| 49 |
tests.sort(); |
| 50 |
return tests; |
| 51 |
|
| 52 |
fn find_tests(path: &Path, tests: &mut Vec<PathBuf>) { |
| 53 |
for f in path.read_dir().unwrap() { |
| 54 |
let f = f.unwrap(); |
| 55 |
let path = f.path(); |
| 56 |
if path.file_name().unwrap().to_str().unwrap() == "parse-fail" { |
| 57 |
continue; |
| 58 |
} |
| 59 |
if f.file_type().unwrap().is_dir() { |
| 60 |
tests.push(path); |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
match path.extension().and_then(|s| s.to_str()) { |
| 65 |
Some("md") | Some("wit") | Some("wat") | Some("wasm") => {} |
| 66 |
_ => continue, |
| 67 |
} |
| 68 |
tests.push(path); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
struct Runner {} |
| 74 |
|
| 75 |
impl Runner { |
| 76 |
fn run(&mut self, test: &Path) -> Result<()> { |
| 77 |
let mut resolve = Resolve::new(); |
| 78 |
resolve.features.insert("active".to_string()); |
| 79 |
let result = resolve.push_path(test); |
| 80 |
let result = if test.iter().any(|s| s == "parse-fail") { |
| 81 |
match result { |
| 82 |
Ok(_) => bail!("expected test to not parse but it did"), |
| 83 |
Err(mut e) => { |
| 84 |
if let Some(err) = e.downcast_mut::<io::Error>() { |
| 85 |
*err = io::Error::new( |
| 86 |
io::ErrorKind::Other, |
| 87 |
"some generic platform-agnostic error message", |
| 88 |
); |
| 89 |
} |
| 90 |
format!("{e:#}") |
| 91 |
} |
| 92 |
} |
| 93 |
} else { |
| 94 |
result?; |
| 95 |
// format json string to human readable |
| 96 |
let json_result = serde_json::to_string_pretty(&resolve)?; |
| 97 |
// "foo.wit" => "foo.wit.json" |
| 98 |
self.read_or_write_to_file(test, &json_result, "json")?; |
| 99 |
return Ok(()); |
| 100 |
}; |
| 101 |
|
| 102 |
// "foo.wit" => "foo.wit.result" |
| 103 |
// "foo.wit.md" => "foo.wit.md.result" |
| 104 |
self.read_or_write_to_file(test, &result, "result")?; |
| 105 |
return Ok(()); |
| 106 |
} |
| 107 |
|
| 108 |
fn read_or_write_to_file( |
| 109 |
&mut self, |
| 110 |
test: &Path, |
| 111 |
result: &str, |
| 112 |
extension: &str, |
| 113 |
) -> Result<(), anyhow::Error> { |
| 114 |
let result_file = if test.extension() == Some(OsStr::new("md")) |
| 115 |
&& test |
| 116 |
.file_stem() |
| 117 |
.and_then(|path| Path::new(path).extension()) |
| 118 |
== Some(OsStr::new("wit")) |
| 119 |
{ |
| 120 |
test.with_extension(format!("md.{extension}")) |
| 121 |
} else { |
| 122 |
test.with_extension(format!("wit.{extension}")) |
| 123 |
}; |
| 124 |
if env::var_os("BLESS").is_some() { |
| 125 |
let normalized = normalize(&result, extension); |
| 126 |
fs::write(&result_file, normalized)?; |
| 127 |
} else { |
| 128 |
let expected = fs::read_to_string(&result_file).context(format!( |
| 129 |
"failed to read test expectation file {result_file:?}\nthis can be fixed with BLESS=1" |
| 130 |
))?; |
| 131 |
let expected = normalize(&expected, extension); |
| 132 |
let result = normalize(&result, extension); |
| 133 |
if expected != result { |
| 134 |
bail!( |
| 135 |
"failed test: result is not as expected:{}", |
| 136 |
StrComparison::new(&expected, &result), |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
Ok(()) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
fn normalize(s: &str, extension: &str) -> String { |
| 145 |
let s = s.trim(); |
| 146 |
match extension { |
| 147 |
// .result files have error messages with paths, so normalize Windows \ separators to / |
| 148 |
"result" => s.replace("\\", "/").replace("\r\n", "\n"), |
| 149 |
|
| 150 |
// .json files escape strings with \, so leave them alone |
| 151 |
_ => s.replace("\r\n", "\n"), |
| 152 |
} |
| 153 |
} |