2018-07-26 18:49:20 -07:00
|
|
|
//! A "wrapper binary" used to execute wasm files as tests
|
|
|
|
//!
|
|
|
|
//! This binary is intended to be used as a "test runner" for wasm binaries,
|
|
|
|
//! being compatible with `cargo test` for the wasm target. It will
|
|
|
|
//! automatically execute `wasm-bindgen` (or the equivalent thereof) and then
|
|
|
|
//! execute either Node.js over the tests or start a server which a browser can
|
|
|
|
//! be used to run against to execute tests. In a browser mode if `CI` is in the
|
|
|
|
//! environment then it'll also attempt headless testing, spawning the server in
|
|
|
|
//! the background and then using the WebDriver protocol to execute tests.
|
|
|
|
//!
|
|
|
|
//! For more documentation about this see the `wasm-bindgen-test` crate README
|
|
|
|
//! and source code.
|
|
|
|
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use failure::{bail, format_err, Error, ResultExt};
|
2018-07-20 13:47:49 -05:00
|
|
|
use std::env;
|
2018-07-25 16:06:47 -07:00
|
|
|
use std::fs;
|
2018-07-20 13:47:49 -05:00
|
|
|
use std::path::PathBuf;
|
2018-07-24 11:27:06 -07:00
|
|
|
use std::process;
|
2018-07-25 03:34:35 -07:00
|
|
|
use std::thread;
|
2018-07-20 13:47:49 -05:00
|
|
|
use wasm_bindgen_cli_support::Bindgen;
|
|
|
|
|
2018-10-17 19:15:09 -07:00
|
|
|
// no need for jemalloc bloat in this binary (and we don't need speed)
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: std::alloc::System = std::alloc::System;
|
|
|
|
|
2018-07-25 03:34:35 -07:00
|
|
|
mod headless;
|
2018-07-24 11:27:06 -07:00
|
|
|
mod node;
|
|
|
|
mod server;
|
2018-07-25 03:34:35 -07:00
|
|
|
mod shell;
|
2018-07-24 11:27:06 -07:00
|
|
|
|
2018-07-20 13:47:49 -05:00
|
|
|
fn main() {
|
2018-07-25 03:34:35 -07:00
|
|
|
env_logger::init();
|
2018-07-20 13:47:49 -05:00
|
|
|
let err = match rmain() {
|
|
|
|
Ok(()) => return,
|
|
|
|
Err(e) => e,
|
|
|
|
};
|
|
|
|
eprintln!("error: {}", err);
|
2018-08-01 16:15:09 -05:00
|
|
|
for cause in err.iter_causes() {
|
2018-07-20 13:47:49 -05:00
|
|
|
eprintln!("\tcaused by: {}", cause);
|
|
|
|
}
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rmain() -> Result<(), Error> {
|
|
|
|
let mut args = env::args_os().skip(1);
|
2018-07-25 03:34:35 -07:00
|
|
|
let shell = shell::Shell::new();
|
2018-07-20 13:47:49 -05:00
|
|
|
|
|
|
|
// Currently no flags are supported, and assume there's only one argument
|
|
|
|
// which is the wasm file to test. This'll want to improve over time!
|
|
|
|
let wasm_file_to_test = match args.next() {
|
|
|
|
Some(file) => PathBuf::from(file),
|
|
|
|
None => bail!("must have a file to test as first argument"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assume a cargo-like directory layout and generate output at
|
|
|
|
// `target/wasm32-unknown-unknown/wbg-tmp/...`
|
2018-09-26 08:26:00 -07:00
|
|
|
let tmpdir = wasm_file_to_test
|
|
|
|
.parent() // chop off file name
|
|
|
|
.and_then(|p| p.parent()) // chop off `deps`
|
|
|
|
.and_then(|p| p.parent()) // chop off `debug`
|
2018-07-20 13:47:49 -05:00
|
|
|
.map(|p| p.join("wbg-tmp"))
|
2018-09-26 08:26:00 -07:00
|
|
|
.ok_or_else(|| format_err!("file to test doesn't follow the expected Cargo conventions"))?;
|
2018-07-20 13:47:49 -05:00
|
|
|
|
|
|
|
// Make sure there's no stale state from before
|
|
|
|
drop(fs::remove_dir_all(&tmpdir));
|
2018-09-26 08:26:00 -07:00
|
|
|
fs::create_dir(&tmpdir).context("creating temporary directory")?;
|
2018-07-20 13:47:49 -05:00
|
|
|
|
2018-08-01 14:19:19 -05:00
|
|
|
let module = "wasm-bindgen-test";
|
2018-07-20 13:47:49 -05:00
|
|
|
|
|
|
|
// Collect all tests that the test harness is supposed to run. We assume
|
|
|
|
// that any exported function with the prefix `__wbg_test` is a test we need
|
|
|
|
// to execute.
|
2018-09-26 08:26:00 -07:00
|
|
|
let wasm = fs::read(&wasm_file_to_test).context("failed to read wasm file")?;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
let wasm = walrus::Module::from_buffer(&wasm).context("failed to deserialize wasm module")?;
|
2018-07-24 11:27:06 -07:00
|
|
|
let mut tests = Vec::new();
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
|
|
|
|
for export in wasm.exports.iter() {
|
|
|
|
if !export.name.starts_with("__wbg_test") {
|
|
|
|
continue;
|
2018-07-20 13:47:49 -05:00
|
|
|
}
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
tests.push(export.name.to_string());
|
2018-07-20 13:47:49 -05:00
|
|
|
}
|
2018-07-25 03:34:35 -07:00
|
|
|
|
|
|
|
// Right now there's a bug where if no tests are present then the
|
|
|
|
// `wasm-bindgen-test` runtime support isn't linked in, so just bail out
|
|
|
|
// early saying everything is ok.
|
2018-07-24 11:27:06 -07:00
|
|
|
if tests.len() == 0 {
|
|
|
|
println!("no tests to run!");
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
2018-07-24 11:27:06 -07:00
|
|
|
}
|
2018-07-20 13:47:49 -05:00
|
|
|
|
2018-07-25 03:34:35 -07:00
|
|
|
// Figure out if this tests is supposed to execute in node.js or a browser.
|
|
|
|
// 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;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
for custom in wasm.custom.iter() {
|
|
|
|
if custom.name != "__wasm_bindgen_test_unstable" {
|
2018-09-26 08:26:00 -07:00
|
|
|
continue;
|
2018-07-25 03:34:35 -07:00
|
|
|
}
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
node = !custom.value.contains(&0x01);
|
2018-07-25 03:34:35 -07:00
|
|
|
}
|
2018-08-02 10:30:07 -05:00
|
|
|
let headless = env::var("NO_HEADLESS").is_err();
|
2018-08-06 09:57:41 -07:00
|
|
|
let debug = env::var("WASM_BINDGEN_NO_DEBUG").is_err();
|
2018-07-20 13:47:49 -05:00
|
|
|
|
2018-07-25 03:34:35 -07:00
|
|
|
// Make the generated bindings available for the tests to execute against.
|
|
|
|
shell.status("Executing bindgen...");
|
2018-07-25 15:49:50 -07:00
|
|
|
let mut b = Bindgen::new();
|
2018-08-06 09:57:41 -07:00
|
|
|
b.debug(debug)
|
2019-02-25 11:11:30 -08:00
|
|
|
.nodejs(node)?
|
2019-03-07 07:33:40 -08:00
|
|
|
.web(!node)?
|
2018-10-08 10:01:53 -07:00
|
|
|
.input_module(module, wasm)
|
2018-07-25 15:49:50 -07:00
|
|
|
.keep_debug(false)
|
2018-11-28 09:25:51 -08:00
|
|
|
.emit_start(false)
|
2018-07-25 15:49:50 -07:00
|
|
|
.generate(&tmpdir)
|
|
|
|
.context("executing `wasm-bindgen` over the wasm file")?;
|
2018-07-25 03:34:35 -07:00
|
|
|
shell.clear();
|
2018-07-25 15:49:50 -07:00
|
|
|
|
2018-07-25 03:34:35 -07:00
|
|
|
// If we're executing in node.js, that module will take it from here.
|
2018-07-24 11:27:06 -07:00
|
|
|
if node {
|
2018-09-26 08:26:00 -07:00
|
|
|
return node::execute(&module, &tmpdir, &args.collect::<Vec<_>>(), &tests);
|
2018-07-24 11:27:06 -07:00
|
|
|
}
|
2018-07-20 13:47:49 -05:00
|
|
|
|
2018-07-25 03:34:35 -07:00
|
|
|
// 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,
|
2018-11-30 13:04:05 -08:00
|
|
|
)
|
|
|
|
.context("failed to spawn server")?;
|
2018-07-25 03:34:35 -07:00
|
|
|
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 {
|
2018-09-26 08:26:00 -07:00
|
|
|
println!(
|
|
|
|
"Interactive browsers tests are now available at http://{}",
|
|
|
|
addr
|
|
|
|
);
|
2018-08-02 10:30:07 -05:00
|
|
|
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.");
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(srv.run());
|
2018-07-25 03:34:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
thread::spawn(|| srv.run());
|
|
|
|
headless::run(&addr, &shell)?;
|
|
|
|
Ok(())
|
2018-07-20 13:47:49 -05:00
|
|
|
}
|