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 server;
mod shell; mod shell;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum TestMode {
Node,
Browser,
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
env_logger::init(); env_logger::init();
let mut args = env::args_os().skip(1); 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 // That's done on a per-test-binary basis with the
// `wasm_bindgen_test_configure` macro, which emits a custom section for us // `wasm_bindgen_test_configure` macro, which emits a custom section for us
// to read later on. // to read later on.
let mut node = true;
if let Some(section) = wasm.customs.remove_raw("__wasm_bindgen_test_unstable") { let custom_section = wasm.customs.remove_raw("__wasm_bindgen_test_unstable");
node = !section.data.contains(&0x01); 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 headless = env::var("NO_HEADLESS").is_err();
let debug = env::var("WASM_BINDGEN_NO_DEBUG").is_err(); let debug = env::var("WASM_BINDGEN_NO_DEBUG").is_err();
// Gracefully handle requests to execute only node or only web tests. // 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 env::var_os("WASM_BINDGEN_TEST_ONLY_NODE").is_some() {
if !node { if !node {
println!( println!(
@ -131,9 +143,12 @@ integration test.\
// Make the generated bindings available for the tests to execute against. // Make the generated bindings available for the tests to execute against.
shell.status("Executing bindgen..."); shell.status("Executing bindgen...");
let mut b = Bindgen::new(); let mut b = Bindgen::new();
match test_mode {
TestMode::Node => b.nodejs(true)?,
TestMode::Browser => b.web(true)?,
};
b.debug(debug) b.debug(debug)
.nodejs(node)?
.web(!node)?
.input_module(module, wasm) .input_module(module, wasm)
.keep_debug(false) .keep_debug(false)
.emit_start(false) .emit_start(false)
@ -141,44 +156,44 @@ integration test.\
.context("executing `wasm-bindgen` over the wasm file")?; .context("executing `wasm-bindgen` over the wasm file")?;
shell.clear(); shell.clear();
// If we're executing in node.js, that module will take it from here. let args: Vec<_> = args.collect();
if node {
return node::execute(&module, &tmpdir, &args.collect::<Vec<_>>(), &tests); 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()
} else {
"127.0.0.1:8000".parse().unwrap()
},
headless,
&module,
&tmpdir,
&args,
&tests,
)
.context("failed to spawn server")?;
let addr = srv.server_addr();
// TODO: eventually we should provide the ability to exit at some point
// (gracefully) here, but for now this just runs forever.
if !headless {
println!(
"Interactive browsers tests are now available at http://{}",
addr
);
println!("");
println!("Note that interactive mode is enabled because `NO_HEADLESS`");
println!("is specified in the environment of this process. Once you're");
println!("done with testing you'll need to kill this server with");
println!("Ctrl-C.");
return Ok(srv.run());
}
thread::spawn(|| srv.run());
headless::run(&addr, &shell, timeout)?;
}
} }
// Otherwise we're executing in a browser. Spawn a server which serves up
// the local generated files over an HTTP server.
let srv = server::spawn(
&if headless {
"127.0.0.1:0".parse().unwrap()
} else {
"127.0.0.1:8000".parse().unwrap()
},
headless,
&module,
&tmpdir,
&args.collect::<Vec<_>>(),
&tests,
)
.context("failed to spawn server")?;
let addr = srv.server_addr();
// TODO: eventually we should provide the ability to exit at some point
// (gracefully) here, but for now this just runs forever.
if !headless {
println!(
"Interactive browsers tests are now available at http://{}",
addr
);
println!("");
println!("Note that interactive mode is enabled because `NO_HEADLESS`");
println!("is specified in the environment of this process. Once you're");
println!("done with testing you'll need to kill this server with");
println!("Ctrl-C.");
return Ok(srv.run());
}
thread::spawn(|| srv.run());
headless::run(&addr, &shell, timeout)?;
Ok(()) Ok(())
} }