make wasm-bindgen-test-runner easier to expand

This commit is contained in:
Jakob Hellermann 2020-06-03 16:54:39 +02:00
parent addb0824d1
commit 77bf0e9e6b

View File

@ -27,6 +27,12 @@ mod node;
mod server;
mod shell;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum TestMode {
Node,
Browser,
}
fn main() -> anyhow::Result<()> {
env_logger::init();
let mut args = env::args_os().skip(1);
@ -81,14 +87,20 @@ fn main() -> anyhow::Result<()> {
// That's done on a per-test-binary basis with the
// `wasm_bindgen_test_configure` macro, which emits a custom section for us
// to read later on.
let mut node = true;
if let Some(section) = wasm.customs.remove_raw("__wasm_bindgen_test_unstable") {
node = !section.data.contains(&0x01);
}
let custom_section = wasm.customs.remove_raw("__wasm_bindgen_test_unstable");
let test_mode = match custom_section {
Some(section) if section.data.contains(&0x01) => TestMode::Browser,
Some(_) => bail!("invalid __wasm_bingen_test_unstable value"),
None => TestMode::Node,
};
let headless = env::var("NO_HEADLESS").is_err();
let debug = env::var("WASM_BINDGEN_NO_DEBUG").is_err();
// Gracefully handle requests to execute only node or only web tests.
let node = test_mode == TestMode::Node;
if env::var_os("WASM_BINDGEN_TEST_ONLY_NODE").is_some() {
if !node {
println!(
@ -131,9 +143,12 @@ integration test.\
// Make the generated bindings available for the tests to execute against.
shell.status("Executing bindgen...");
let mut b = Bindgen::new();
match test_mode {
TestMode::Node => b.nodejs(true)?,
TestMode::Browser => b.web(true)?,
};
b.debug(debug)
.nodejs(node)?
.web(!node)?
.input_module(module, wasm)
.keep_debug(false)
.emit_start(false)
@ -141,13 +156,11 @@ integration test.\
.context("executing `wasm-bindgen` over the wasm file")?;
shell.clear();
// If we're executing in node.js, that module will take it from here.
if node {
return node::execute(&module, &tmpdir, &args.collect::<Vec<_>>(), &tests);
}
let args: Vec<_> = args.collect();
// Otherwise we're executing in a browser. Spawn a server which serves up
// the local generated files over an HTTP server.
match test_mode {
TestMode::Node => node::execute(&module, &tmpdir, &args, &tests)?,
TestMode::Browser => {
let srv = server::spawn(
&if headless {
"127.0.0.1:0".parse().unwrap()
@ -157,7 +170,7 @@ integration test.\
headless,
&module,
&tmpdir,
&args.collect::<Vec<_>>(),
&args,
&tests,
)
.context("failed to spawn server")?;
@ -180,5 +193,7 @@ integration test.\
thread::spawn(|| srv.run());
headless::run(&addr, &shell, timeout)?;
}
}
Ok(())
}