| 1 |
use anyhow::{Context, Result}; |
| 2 |
use libtest_mimic::{Arguments, Trial}; |
| 3 |
use pretty_assertions::assert_eq; |
| 4 |
use std::fs; |
| 5 |
use std::path::Path; |
| 6 |
use wit_component::WitPrinter; |
| 7 |
use wit_parser::{PackageId, Resolve, UnresolvedPackageGroup}; |
| 8 |
|
| 9 |
/// Tests the encoding of a WIT package as a WebAssembly binary. |
| 10 |
/// |
| 11 |
/// This test looks in the `interfaces/` directory for test cases. Each test |
| 12 |
/// case is a `*.wit` file or a folder which contains `*.wit` files as part of a |
| 13 |
/// multi-file package. Each tests `foo.wit` is accompanied with a `foo.wat` for |
| 14 |
/// the WebAssembly text format encoding of the package. Additionally each test |
| 15 |
/// has a `foo.print.wit` which is the machine-printed version of the WIT |
| 16 |
/// document as decoded from the binary encoded interface. |
| 17 |
/// |
| 18 |
/// Run the test with the environment variable `BLESS` set to update |
| 19 |
/// the baseline files. |
| 20 |
fn main() -> Result<()> { |
| 21 |
env_logger::init(); |
| 22 |
|
| 23 |
let mut trials = Vec::new(); |
| 24 |
for entry in fs::read_dir("tests/interfaces")? { |
| 25 |
let path = entry?.path(); |
| 26 |
let name = match path.file_name().and_then(|s| s.to_str()) { |
| 27 |
Some(s) => s, |
| 28 |
None => continue, |
| 29 |
}; |
| 30 |
let is_dir = path.is_dir(); |
| 31 |
let is_test = is_dir || name.ends_with(".wit"); |
| 32 |
if is_test { |
| 33 |
trials.push(Trial::test(name.to_string(), move || { |
| 34 |
run_test(&path, is_dir) |
| 35 |
.context(format!("failed test `{}`", path.display())) |
| 36 |
.map_err(|e| format!("{e:?}").into()) |
| 37 |
})); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
let mut args = Arguments::from_args(); |
| 42 |
if cfg!(target_family = "wasm") && !cfg!(target_feature = "atomics") { |
| 43 |
args.test_threads = Some(1); |
| 44 |
} |
| 45 |
libtest_mimic::run(&args, trials).exit(); |
| 46 |
} |
| 47 |
|
| 48 |
fn run_test(path: &Path, is_dir: bool) -> Result<()> { |
| 49 |
let mut resolve = Resolve::new(); |
| 50 |
let package = if is_dir { |
| 51 |
resolve.push_dir(path)?.0 |
| 52 |
} else { |
| 53 |
resolve.push_file(path)? |
| 54 |
}; |
| 55 |
|
| 56 |
assert_print(&resolve, package, path, is_dir)?; |
| 57 |
|
| 58 |
// First convert the WIT package to a binary WebAssembly output, then |
| 59 |
// convert that binary wasm to textual wasm, then assert it matches the |
| 60 |
// expectation. |
| 61 |
let wasm = wit_component::encode(&resolve, package)?; |
| 62 |
let wat = wasmprinter::print_bytes(&wasm)?; |
| 63 |
assert_output(&path.with_extension("wat"), &wat)?; |
| 64 |
wasmparser::Validator::new() |
| 65 |
.validate_all(&wasm) |
| 66 |
.context("failed to validate wasm output")?; |
| 67 |
|
| 68 |
// Next decode a fresh WIT package from the WebAssembly generated. Print |
| 69 |
// this package's documents and assert they all match the expectations. |
| 70 |
let decoded = wit_component::decode(&wasm)?; |
| 71 |
|
| 72 |
let decoded_package = decoded.package(); |
| 73 |
let resolve = decoded.resolve(); |
| 74 |
|
| 75 |
assert_print(resolve, decoded.package(), path, is_dir)?; |
| 76 |
|
| 77 |
// Finally convert the decoded package to wasm again and make sure it |
| 78 |
// matches the prior wasm. |
| 79 |
let wasm2 = wit_component::encode(resolve, decoded_package)?; |
| 80 |
if wasm != wasm2 { |
| 81 |
let wat2 = wasmprinter::print_bytes(&wasm)?; |
| 82 |
assert_eq!(wat, wat2, "document did not roundtrip correctly"); |
| 83 |
} |
| 84 |
|
| 85 |
Ok(()) |
| 86 |
} |
| 87 |
|
| 88 |
fn assert_print(resolve: &Resolve, pkg_id: PackageId, path: &Path, is_dir: bool) -> Result<()> { |
| 89 |
let mut printer = WitPrinter::default(); |
| 90 |
printer.print(resolve, pkg_id, &[])?; |
| 91 |
let output = printer.output.to_string(); |
| 92 |
let pkg = &resolve.packages[pkg_id]; |
| 93 |
let expected = if is_dir { |
| 94 |
path.join(format!("{}.wit.print", &pkg.name.name)) |
| 95 |
} else { |
| 96 |
path.with_extension("wit.print") |
| 97 |
}; |
| 98 |
assert_output(&expected, &output)?; |
| 99 |
|
| 100 |
UnresolvedPackageGroup::parse("foo.wit", &output).context("failed to parse printed output")?; |
| 101 |
Ok(()) |
| 102 |
} |
| 103 |
|
| 104 |
fn assert_output(expected: &Path, actual: &str) -> Result<()> { |
| 105 |
let actual = actual.replace( |
| 106 |
concat!("\"", env!("CARGO_PKG_VERSION"), "\""), |
| 107 |
"\"$CARGO_PKG_VERSION\"", |
| 108 |
); |
| 109 |
if std::env::var_os("BLESS").is_some() { |
| 110 |
fs::write(expected, actual).with_context(|| format!("failed to write {expected:?}"))?; |
| 111 |
} else { |
| 112 |
assert_eq!( |
| 113 |
fs::read_to_string(expected) |
| 114 |
.with_context(|| format!("failed to read {expected:?}"))? |
| 115 |
.replace("\r\n", "\n"), |
| 116 |
actual, |
| 117 |
"expectation `{}` did not match actual", |
| 118 |
expected.display(), |
| 119 |
); |
| 120 |
} |
| 121 |
Ok(()) |
| 122 |
} |