Passes `extra_options` to `cargo build --tests` when running `wasm-pack test`.

Issue #698
master
Azriel Hoh 5 years ago
parent d42d673138
commit 23452046ec
  1. 19
      src/build/mod.rs
  2. 2
      src/command/test.rs
  3. 53
      tests/all/test.rs

@ -112,11 +112,24 @@ pub fn cargo_build_wasm(
Ok(())
}
/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
/// Runs `cargo build --tests` targeting `wasm32-unknown-unknown`.
///
/// This generates the `Cargo.lock` file that we use in order to know which version of
/// wasm-bindgen-cli to use when running tests.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
///
/// Note that the command to build tests and the command to run tests must use the same parameters, i.e. features to be
/// disabled / enabled must be consistent for both `cargo build` and `cargo test`.
///
/// # Parameters
///
/// * `path`: Path to the crate directory to build tests.
/// * `debug`: Whether to build tests in `debug` mode.
/// * `extra_options`: Additional parameters to pass to `cargo` when building tests.
pub fn cargo_build_wasm_tests(
path: &Path,
debug: bool,
extra_options: &[String],
) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
@ -131,6 +144,8 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.args(extra_options);
child::run(cmd, "cargo build").context("Compilation of your program failed")?;
Ok(())
}

@ -243,7 +243,7 @@ impl Test {
fn step_build_tests(&mut self) -> Result<(), Error> {
info!("Compiling tests to wasm...");
build::cargo_build_wasm_tests(&self.crate_path, !self.release)?;
build::cargo_build_wasm_tests(&self.crate_path, !self.release, &self.extra_options)?;
info!("Finished compiling tests to wasm.");
Ok(())

@ -337,3 +337,56 @@ fn test_output_is_printed_once_in_both_stdout_and_failures() {
out.matches("YABBA DABBA DOO").count() == log_cnt * 2
}));
}
#[test]
fn extra_options_is_passed_to_cargo_when_building_tests() {
let fixture = fixture::Fixture::new();
fixture
.readme()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
[dev-dependencies]
wasm-bindgen-test = "0.2"
[features]
default = ["native"]
native = []
"#,
)
.file(
"src/lib.rs",
r#"
pub fn foo() -> u32 {
#[cfg(feature = "native")]
compile_error!("Test should pass through `--no-default-features` for this to pass.");
1
}
"#,
)
.file(
"tests/foo.rs",
r#"
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn smoke() {
foo::foo();
}
"#,
)
.install_local_wasm_bindgen();
let _lock = fixture.lock();
fixture
.wasm_pack()
.args(&["test", "--node", "--", "--no-default-features"])
.assert()
.success();
}

Loading…
Cancel
Save