rust/tests/run-make/wasm-symbols-different-module/rmake.rs
Alex Crichton 7141379559 Convert some WebAssembly run-make tests to Rust
This commit rewrites a number of `run-make` tests centered around wasm
to instead use `rmake.rs` and additionally use the `wasm32-wasip1`
target instead of `wasm32-unknown-unknown`. Testing no longer requires
Node.js and additionally uses the `wasmparser` crate from crates.io to
parse outputs and power assertions.
2024-03-11 09:36:35 -07:00

53 lines
1.7 KiB
Rust

extern crate run_make_support;
use run_make_support::{out_dir, rustc, wasmparser};
use std::collections::{HashMap, HashSet};
fn main() {
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
return;
}
test_file("foo.rs", &[("a", &["foo"]), ("b", &["foo"])]);
test_file("bar.rs", &[("m1", &["f", "g"]), ("m2", &["f"])]);
test_file("baz.rs", &[("sqlite", &["allocate", "deallocate"])]);
test_file("log.rs", &[("test", &["log"])]);
}
fn test_file(file: &str, expected_imports: &[(&str, &[&str])]) {
test(file, &[], expected_imports);
test(file, &["-Clto"], expected_imports);
test(file, &["-O"], expected_imports);
test(file, &["-Clto", "-O"], expected_imports);
}
fn test(file: &str, args: &[&str], expected_imports: &[(&str, &[&str])]) {
println!("test {file:?} {args:?} for {expected_imports:?}");
rustc().arg(file).arg("--target=wasm32-wasip1").args(args).run();
let file = std::fs::read(&out_dir().join(file).with_extension("wasm")).unwrap();
let mut imports = HashMap::new();
for payload in wasmparser::Parser::new(0).parse_all(&file) {
let payload = payload.unwrap();
if let wasmparser::Payload::ImportSection(s) = payload {
for i in s {
let i = i.unwrap();
imports.entry(i.module).or_insert(HashSet::new()).insert(i.name);
}
}
}
eprintln!("imports {imports:?}");
for (expected_module, expected_names) in expected_imports {
let names = imports.remove(expected_module).unwrap();
assert_eq!(names.len(), expected_names.len());
for name in *expected_names {
assert!(names.contains(name));
}
}
assert!(imports.is_empty());
}