From f986edf4c7d9770dc112e97c2e364dd71d372375 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 2 Nov 2018 13:09:40 -0700 Subject: [PATCH] Move test synchronization to tests, not in `wasm-pack` In `wasm-pack` it can't do cross-process synchronization, and in tests there's now multiple processes which need to be synchronized. --- src/test/mod.rs | 6 ------ tests/all/build.rs | 4 +++- tests/all/main.rs | 2 ++ tests/all/test.rs | 7 ++++++- tests/all/utils/fixture.rs | 11 ++++++++++- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index db683ff..1b71fed 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -22,12 +22,6 @@ where K: AsRef, V: AsRef, { - use std::sync::Mutex; - lazy_static! { - static ref ONE_TEST_AT_A_TIME: Mutex<()> = Mutex::new(()); - } - let _locked = ONE_TEST_AT_A_TIME.lock().unwrap(); - let output = { let mut cmd = Command::new("cargo"); cmd.envs(envs); diff --git a/tests/all/build.rs b/tests/all/build.rs index 295708d..2d8d56d 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -17,7 +17,9 @@ fn build_in_non_crate_directory_doesnt_panic() { "running wasm-pack in a non-crate directory should fail, but it should not panic" ); let err = result.unwrap_err(); - assert!(err.iter_chain().any(|e| e.to_string().contains("missing a `Cargo.toml`"))); + assert!(err + .iter_chain() + .any(|e| e.to_string().contains("missing a `Cargo.toml`"))); } #[test] diff --git a/tests/all/main.rs b/tests/all/main.rs index 553ce1f..3fa272f 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -1,5 +1,7 @@ extern crate failure; #[macro_use] +extern crate lazy_static; +#[macro_use] extern crate serde_derive; extern crate serde_json; #[macro_use] diff --git a/tests/all/test.rs b/tests/all/test.rs index a4b0f74..66f1022 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -98,7 +98,10 @@ fn it_can_run_failing_tests() { mode: build::BuildMode::Noinstall, ..Default::default() }); - assert_err(fixture.run(cmd), "Running Wasm tests with wasm-bindgen-test failed"); + assert_err( + fixture.run(cmd), + "Running Wasm tests with wasm-bindgen-test failed", + ); } #[test] @@ -121,6 +124,8 @@ fn it_can_find_a_webdriver_on_path() { paths.insert(0, local_wasm_bindgen.parent().unwrap().to_path_buf()); let path = env::join_paths(paths).unwrap(); + let _lock = fixture.lock(); + let mut me = env::current_exe().unwrap(); me.pop(); me.pop(); diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index 64ccbdd..be6484a 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -4,7 +4,7 @@ use std::fs; use std::mem::ManuallyDrop; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -use std::sync::{Once, ONCE_INIT}; +use std::sync::{MutexGuard, Once, ONCE_INIT}; use std::thread; use tempfile::TempDir; use wasm_pack; @@ -208,6 +208,7 @@ impl Fixture { let logger = wasm_pack::logger::new(&cmd, 3)?; match cmd { wasm_pack::command::Command::Test(cmd) => { + let _lock = self.lock(); let mut test = wasm_pack::command::test::Test::try_from_opts(cmd)?; test.set_cache(self.cache()); test.run(&logger) @@ -220,6 +221,14 @@ impl Fixture { _ => unreachable!(), } } + + pub fn lock(&self) -> MutexGuard<'static, ()> { + use std::sync::Mutex; + lazy_static! { + static ref ONE_TEST_AT_A_TIME: Mutex<()> = Mutex::new(()); + } + ONE_TEST_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner()) + } } impl Drop for Fixture {