diff --git a/.gitignore b/.gitignore index 20dab02e..4b2ee66a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ target/ -**/target **/*.rs.bk -**/pkg tests/.crates.toml tests/bin wasm-pack.log diff --git a/.travis.yml b/.travis.yml index ca59a2dc..5b143b2f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,13 @@ INSTALL_NODE_VIA_NVM: &INSTALL_NODE_VIA_NVM rustup target add wasm32-unknown-unknown curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash source ~/.nvm/nvm.sh - nvm install v10.5 + nvm install lts/carbon + +# Cache `cargo install`ed tools, but don't cache the project's `target` +# directory (which ends up over-caching and filling all disk space!) +cache: + directories: + - /home/travis/.cargo DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB before_deploy: diff --git a/src/binaries.rs b/src/binaries.rs index cb011fd7..a4144055 100644 --- a/src/binaries.rs +++ b/src/binaries.rs @@ -11,27 +11,49 @@ use std::fs; use std::io; use std::path::{Path, PathBuf}; use tar; +use target; use which::which; use zip; +/// Get the path for a crate's directory of locally-installed binaries. +/// +/// This does not check whether or ensure that the directory exists. +pub fn local_bin_dir(crate_path: &Path) -> PathBuf { + crate_path.join("bin") +} + +/// Ensure that the crate's directory for locally-installed binaries exists. +pub fn ensure_local_bin_dir(crate_path: &Path) -> io::Result<()> { + fs::create_dir_all(local_bin_dir(crate_path)) +} + +/// Get the path for where `bin` would be if we have a crate-local install for +/// it. +/// +/// This does *not* check whether there is a file at that path or not. +/// +/// This will automatically add the `.exe` extension for windows. +pub fn local_bin_path(crate_path: &Path, bin: &str) -> PathBuf { + let mut p = local_bin_dir(crate_path).join(bin); + if target::WINDOWS { + p.set_extension("exe"); + } + p +} + /// Get the local (at `$CRATE/bin/$BIN`; preferred) or global (on `$PATH`) path /// for the given binary. +/// +/// If this function returns `Some(path)`, then a file at that path exists (or +/// at least existed when we checked! In general, we aren't really worried about +/// racing with an uninstall of a tool that we rely on.) pub fn bin_path(log: &Logger, crate_path: &Path, bin: &str) -> Option { assert!(!bin.ends_with(".exe")); debug!(log, "Searching for {} binary...", bin); // Return the path to the local binary, if it exists. let local_path = |crate_path: &Path| -> Option { - let mut p = crate_path.to_path_buf(); - p.push("bin"); - if cfg!(target_os = "windows") { - let mut bin = bin.to_string(); - bin.push_str(".exe"); - p.push(bin); - } else { - p.push(bin); - } - + let p = local_bin_path(crate_path, bin); debug!(log, "Checking for local {} binary at {}", bin, p.display()); if p.is_file() { Some(p) @@ -62,40 +84,46 @@ pub fn bin_path(log: &Logger, crate_path: &Path, bin: &str) -> Option { }) } +fn with_url_context(url: &str, r: Result) -> Result +where + Result: failure::ResultExt, +{ + use failure::ResultExt; + r.with_context(|_| format!("when requesting {}", url)) +} + +fn transfer( + url: &str, + easy: &mut curl::easy::Easy, + data: &mut Vec, +) -> Result<(), failure::Error> { + let mut transfer = easy.transfer(); + with_url_context( + url, + transfer.write_function(|part| { + data.extend_from_slice(part); + Ok(part.len()) + }), + )?; + with_url_context(url, transfer.perform())?; + Ok(()) +} + fn curl(url: &str) -> Result, failure::Error> { let mut data = Vec::new(); - fn with_url_context(url: &str, r: Result) -> Result - where - Result: failure::ResultExt, - { - use failure::ResultExt; - r.with_context(|_| format!("when requesting {}", url)) - } - let mut easy = curl::easy::Easy::new(); with_url_context(url, easy.follow_location(true))?; with_url_context(url, easy.url(url))?; + transfer(url, &mut easy, &mut data)?; - { - let mut transfer = easy.transfer(); - with_url_context( - url, - transfer.write_function(|part| { - data.extend_from_slice(part); - Ok(part.len()) - }), - )?; - with_url_context(url, transfer.perform())?; - } - - let code = with_url_context(url, easy.response_code())?; - if 200 <= code && code < 300 { + let status_code = with_url_context(url, easy.response_code())?; + if 200 <= status_code && status_code < 300 { Ok(data) } else { Err(Error::http(&format!( "received a bad HTTP status code ({}) when requesting {}", - code, url + status_code, url )).into()) } } @@ -117,8 +145,8 @@ where let tarball = curl(&url).map_err(|e| Error::http(&e.to_string()))?; let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(&tarball[..])); - let bin = crate_path.join("bin"); - fs::create_dir_all(&bin)?; + ensure_local_bin_dir(crate_path)?; + let bin = local_bin_dir(crate_path); for entry in archive.entries()? { let mut entry = entry?; @@ -166,8 +194,8 @@ where let data = io::Cursor::new(data); let mut zip = zip::ZipArchive::new(data)?; - let bin = crate_path.join("bin"); - fs::create_dir_all(&bin)?; + ensure_local_bin_dir(crate_path)?; + let bin = local_bin_dir(crate_path); for i in 0..zip.len() { let mut entry = zip.by_index(i).unwrap(); diff --git a/src/bindgen.rs b/src/bindgen.rs index 5b227556..2fc5d4ec 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -1,6 +1,6 @@ //! Functionality related to installing and running `wasm-bindgen`. -use binaries::{bin_path, install_binaries_from_targz_at_url}; +use binaries::{self, bin_path, install_binaries_from_targz_at_url}; use emoji; use error::Error; use progressbar::Step; @@ -77,9 +77,9 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result ) } -/// Use `cargo install` to install the `wasm-bindgen` CLI to the given root -/// path. -pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), Error> { +/// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given +/// crate. +pub fn cargo_install_wasm_bindgen(crate_path: &Path, version: &str) -> Result<(), Error> { let output = Command::new("cargo") .arg("install") .arg("--force") @@ -87,7 +87,7 @@ pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), .arg("--version") .arg(version) .arg("--root") - .arg(root_path) + .arg(crate_path) .output()?; if !output.status.success() { let message = "Installing wasm-bindgen failed".to_string(); @@ -97,11 +97,7 @@ pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), stderr: s.to_string(), }) } else { - if cfg!(target_os = "windows") { - assert!(root_path.join("bin").join("wasm-bindgen.exe").is_file()); - } else { - assert!(root_path.join("bin").join("wasm-bindgen").is_file()); - } + assert!(binaries::local_bin_path(crate_path, "wasm-bindgen").is_file()); Ok(()) } } diff --git a/src/command/test.rs b/src/command/test.rs index da57a9c2..5bba1b91 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -12,8 +12,7 @@ use progressbar::Step; use slog::Logger; use std::path::PathBuf; use std::time::Instant; -use test; -use webdriver; +use test::{self, webdriver}; use PBAR; #[derive(Debug, Default, StructOpt)] @@ -129,8 +128,10 @@ impl Test { } if headless && !any_browser { - return Error::crate_config("The `--headless` flag only applies to browser tests") - .map(|_| unreachable!()); + return Error::crate_config( + "The `--headless` flag only applies to browser tests. Node does not provide a UI, \ + so it doesn't make sense to talk about a headless version of Node tests.", + ).map(|_| unreachable!()); } Ok(Test { diff --git a/src/lib.rs b/src/lib.rs index bc1e5228..7db89318 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,6 @@ pub mod progressbar; pub mod readme; pub mod target; pub mod test; -pub mod webdriver; use progressbar::ProgressOutput; diff --git a/src/manifest.rs b/src/manifest.rs index a7ccfd27..332c76ee 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -17,9 +17,36 @@ use PBAR; struct CargoManifest { package: CargoPackage, dependencies: Option>, + #[serde(rename = "dev-dependencies")] + dev_dependencies: Option>, lib: Option, } +fn normalize_dependency_name(dep: &str) -> String { + dep.replace("-", "_") +} + +fn normalize_dependencies( + deps: HashMap, +) -> HashMap { + let mut new_deps = HashMap::with_capacity(deps.len()); + for (key, val) in deps { + new_deps.insert(normalize_dependency_name(&key), val); + } + new_deps +} + +impl CargoManifest { + fn normalize_dependencies(&mut self) { + if let Some(deps) = self.dependencies.take() { + self.dependencies = Some(normalize_dependencies(deps)); + } + if let Some(dev_deps) = self.dev_dependencies.take() { + self.dev_dependencies = Some(normalize_dependencies(dev_deps)); + } + } +} + #[derive(Debug, Deserialize)] struct CargoPackage { name: String, @@ -86,7 +113,10 @@ fn read_cargo_toml(path: &Path) -> Result { let mut cargo_contents = String::new(); cargo_file.read_to_string(&mut cargo_contents)?; - Ok(toml::from_str(&cargo_contents)?) + let mut manifest: CargoManifest = toml::from_str(&cargo_contents)?; + manifest.normalize_dependencies(); + + Ok(manifest) } impl CargoManifest { @@ -192,8 +222,7 @@ pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> { let msg = format!("{}Checking crate configuration...", emoji::WRENCH); PBAR.step(&step, &msg); check_wasm_bindgen(path)?; - // TODO: check that if there is a `wasm-bindgen-test` dependency, then it - // matches `wasm-bindgen`. + check_wasm_bindgen_test(path)?; check_crate_type(path)?; Ok(()) } @@ -203,6 +232,24 @@ fn check_wasm_bindgen(path: &Path) -> Result<(), Error> { Ok(()) } +fn check_wasm_bindgen_test(path: &Path) -> Result<(), Error> { + let expected_version = get_wasm_bindgen_version(path)?; + + // Only do the version check if `wasm-bindgen-test` is actually a + // dependency. Not every crate needs to have tests! + if let Ok(actual_version) = get_wasm_bindgen_test_version(path) { + if expected_version != actual_version { + return Error::crate_config(&format!( + "The `wasm-bindgen-test` dependency version ({}) must match \ + the `wasm-bindgen` dependency version ({}), but it does not.", + actual_version, expected_version + )); + } + } + + Ok(()) +} + fn check_crate_type(path: &Path) -> Result<(), Error> { if read_cargo_toml(path)?.lib.map_or(false, |lib| { lib.crate_type @@ -211,17 +258,22 @@ fn check_crate_type(path: &Path) -> Result<(), Error> { return Ok(()); } Error::crate_config( - "crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]" + "crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \ + Cargo.toml file:\n\n\ + [lib]\n\ + crate-type = [\"cdylib\"]" ) } -/// Get the version of `wasm-bindgen` specified as a dependency. -pub fn get_wasm_bindgen_version(path: &Path) -> Result { - if let Some(deps) = read_cargo_toml(path)?.dependencies { - match deps - .get("wasm-bindgen") - .or_else(|| deps.get("wasm_bindgen")) - { +fn get_dependency_version( + dependencies: Option<&HashMap>, + dependency: &str, + dependencies_section_name: &str, + version_suggestion: &str, +) -> Result { + if let Some(deps) = dependencies { + let dependency = normalize_dependency_name(dependency); + match deps.get(&dependency) { Some(CargoDependency::Simple(version)) | Some(CargoDependency::Detailed(DetailedCargoDependency { version: Some(version), @@ -229,14 +281,20 @@ pub fn get_wasm_bindgen_version(path: &Path) -> Result { Some(CargoDependency::Detailed(DetailedCargoDependency { version: None })) => { let msg = format!( "\"{}\" dependency is missing its version number", - style("wasm-bindgen").bold().dim() + style(&dependency).bold().dim() ); Err(Error::CrateConfig { message: msg }) } None => { let message = format!( - "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"", - style("wasm-bindgen").bold().dim()); + "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ + [{}]\n\ + {} = \"{}\"", + style(&dependency).bold().dim(), + dependencies_section_name, + dependency, + version_suggestion + ); Err(Error::CrateConfig { message }) } } @@ -245,3 +303,25 @@ pub fn get_wasm_bindgen_version(path: &Path) -> Result { Err(Error::CrateConfig { message }) } } + +/// Get the version of `wasm-bindgen` specified as a dependency. +pub fn get_wasm_bindgen_version(path: &Path) -> Result { + let toml = read_cargo_toml(path)?; + get_dependency_version( + toml.dependencies.as_ref(), + "wasm-bindgen", + "dependencies", + "0.2", + ) +} + +/// Get the version of `wasm-bindgen-test` specified as a dependency. +pub fn get_wasm_bindgen_test_version(path: &Path) -> Result { + let toml = read_cargo_toml(path)?; + get_dependency_version( + toml.dev_dependencies.as_ref(), + "wasm-bindgen-test", + "dev-dependencies", + "0.2", + ) +} diff --git a/src/target.rs b/src/target.rs index 0d8179d9..cfaff6af 100644 --- a/src/target.rs +++ b/src/target.rs @@ -1,4 +1,7 @@ -//! Information about the target. +//! Information about the target wasm-pack is currently being compiled for. +//! +//! That is, whether we are building wasm-pack for windows vs linux, and x86 vs +//! x86-64, etc. #![allow(missing_docs)] diff --git a/src/test.rs b/src/test/mod.rs similarity index 98% rename from src/test.rs rename to src/test/mod.rs index d432b4e9..f0ea2332 100644 --- a/src/test.rs +++ b/src/test/mod.rs @@ -1,5 +1,7 @@ //! Testing a Rust crate compiled to wasm. +pub mod webdriver; + use error::Error; use slog::Logger; use std::ffi::OsStr; diff --git a/src/webdriver.rs b/src/test/webdriver.rs similarity index 70% rename from src/webdriver.rs rename to src/test/webdriver.rs index 5f5b25f8..f507775b 100644 --- a/src/webdriver.rs +++ b/src/test/webdriver.rs @@ -1,6 +1,8 @@ //! Getting WebDriver client binaries. -use binaries::{bin_path, install_binaries_from_targz_at_url, install_binaries_from_zip_at_url}; +use binaries::{ + self, bin_path, install_binaries_from_targz_at_url, install_binaries_from_zip_at_url, +}; use command::build::BuildMode; use error::Error; use slog::Logger; @@ -17,15 +19,19 @@ pub fn get_or_install_chromedriver( match (mode, bin_path(log, crate_path, "chromedriver")) { (_, Some(path)) => Ok(path), (BuildMode::Normal, None) => install_chromedriver(crate_path), - (BuildMode::Noinstall, None) => { - Error::crate_config("could not find `chromedriver` on the `$PATH`") - .map(|_| unreachable!()) - } + (BuildMode::Noinstall, None) => Error::crate_config( + "No crate-local `chromedriver` binary found, and could not find a global \ + `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ + mode.", + ).map(|_| unreachable!()), } } -/// Download and install a pre-built `chromedriver` binary. -pub fn install_chromedriver(crate_path: &Path) -> Result { +fn get_local_chromedriver_path(crate_path: &Path) -> PathBuf { + binaries::local_bin_path(crate_path, "chromedriver") +} + +fn get_chromedriver_url() -> Result { let target = if target::LINUX && target::x86_64 { "linux64" } else if target::MACOS && target::x86_64 { @@ -38,17 +44,17 @@ pub fn install_chromedriver(crate_path: &Path) -> Result { )); }; - let url = format!( + Ok(format!( "https://chromedriver.storage.googleapis.com/2.41/chromedriver_{}.zip", target - ); - install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?; + )) +} - let chromedriver = crate_path.join("bin").join(if cfg!(target_os = "windows") { - "chromedriver.exe" - } else { - "chromedriver" - }); +/// Download and install a pre-built `chromedriver` binary. +pub fn install_chromedriver(crate_path: &Path) -> Result { + let url = get_chromedriver_url()?; + install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?; + let chromedriver = get_local_chromedriver_path(crate_path); assert!(chromedriver.is_file()); Ok(chromedriver) } @@ -63,15 +69,14 @@ pub fn get_or_install_geckodriver( match (mode, bin_path(log, crate_path, "geckodriver")) { (_, Some(path)) => Ok(path), (BuildMode::Normal, None) => install_geckodriver(crate_path), - (BuildMode::Noinstall, None) => { - Error::crate_config("could not find `geckodriver` on the `$PATH`") - .map(|_| unreachable!()) - } + (BuildMode::Noinstall, None) => Error::crate_config( + "No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \ + on the `$PATH`. Not installing `geckodriver` because of noinstall mode.", + ).map(|_| unreachable!()), } } -/// Download and install a pre-built `geckodriver` binary. -pub fn install_geckodriver(crate_path: &Path) -> Result { +fn get_geckodriver_url() -> Result { let (target, ext) = if target::LINUX && target::x86 { ("linux32", "tar.gz") } else if target::LINUX && target::x86_64 { @@ -88,24 +93,29 @@ pub fn install_geckodriver(crate_path: &Path) -> Result { )); }; - let url = format!( + Ok(format!( "https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-{}.{}", target, ext, - ); + )) +} + +fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf { + binaries::local_bin_path(crate_path, "geckodriver") +} - if ext == "tar.gz" { +/// Download and install a pre-built `geckodriver` binary. +pub fn install_geckodriver(crate_path: &Path) -> Result { + let url = get_geckodriver_url()?; + + if url.ends_with("tar.gz") { install_binaries_from_targz_at_url(crate_path, &url, Some("geckodriver"))?; } else { - assert_eq!(ext, "zip"); + assert!(url.ends_with("zip")); install_binaries_from_zip_at_url(crate_path, &url, Some("geckodriver"))?; } - let geckodriver = crate_path.join("bin").join(if cfg!(target_os = "windows") { - "geckodriver.exe" - } else { - "geckodriver" - }); + let geckodriver = get_local_geckodriver_path(crate_path); assert!(geckodriver.is_file()); Ok(geckodriver) } diff --git a/tests/all/bindgen.rs b/tests/all/bindgen.rs index add58382..d9a2d6d7 100644 --- a/tests/all/bindgen.rs +++ b/tests/all/bindgen.rs @@ -1,5 +1,5 @@ use tempfile; -use wasm_pack::bindgen; +use wasm_pack::{binaries, bindgen}; #[test] #[cfg(any( @@ -9,13 +9,8 @@ use wasm_pack::bindgen; fn can_download_prebuilt_wasm_bindgen() { let dir = tempfile::TempDir::new().unwrap(); bindgen::download_prebuilt_wasm_bindgen(dir.path(), "0.2.19").unwrap(); - assert!(dir.path().join("bin").join("wasm-bindgen").is_file()); - assert!( - dir.path() - .join("bin") - .join("wasm-bindgen-test-runner") - .is_file() - ); + assert!(binaries::local_bin_path(dir.path(), "wasm-bindgen").is_file()); + assert!(binaries::local_bin_path(dir.path(), "wasm-bindgen-test-runner").is_file()); } #[test] diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 668e112f..d300ea1b 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -250,3 +250,16 @@ fn it_gets_wasm_bindgen_version_with_underscores() { "0.2" ); } + +#[test] +fn the_wasm_bindgen_test_version_should_match_the_wasm_bindgen_version() { + let fixture = fixture::fixture("tests/fixtures/wbg-test-bad-versions"); + let step = wasm_pack::progressbar::Step::new(1); + let result = manifest::check_crate_config(&fixture.path, &step); + assert!(result.is_err()); + let msg = result.unwrap_err().to_string(); + assert!(msg.contains(&format!( + "The `wasm-bindgen-test` dependency version (0.2.19) must match \ + the `wasm-bindgen` dependency version (0.2.21), but it does not." + ))); +} diff --git a/tests/all/test.rs b/tests/all/test.rs index a7003cdb..00d21d08 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -1,4 +1,8 @@ +use std::env; +use std::fs; +use tempfile; use utils::fixture::fixture; +use wasm_pack::binaries; use wasm_pack::command::{self, build, test, Command}; use wasm_pack::logger; @@ -84,26 +88,43 @@ fn it_can_run_failing_tests() { ); } -// #[test] -// fn it_can_find_a_webdriver_on_path() { -// let fixture = fixture("tests/fixtures/wbg-test-browser"); -// fixture.install_local_wasm_bindgen(); - -// let geckodriver = geckodriver(); -// let mut paths: Vec<_> = env::split_paths(&env::var("PATH").unwrap()).collect(); -// paths.insert(0, geckodriver); -// env::set_var("PATH", env::join_paths(paths).unwrap()); - -// let cmd = Command::Test(test::TestOptions { -// path: Some(fixture.path.clone()), -// firefox: true, -// headless: true, -// mode: build::BuildMode::Noinstall, -// ..Default::default() -// }); -// let logger = logger::new(&cmd, 3).unwrap(); -// command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); -// } +#[test] +#[cfg(any( + all(target_os = "linux", target_arch = "x86"), + all(target_os = "linux", target_arch = "x86_64"), + all(target_os = "macos", target_arch = "x86_64"), + all(target_os = "windows", target_arch = "x86"), + all(target_os = "windows", target_arch = "x86_64") +))] +fn it_can_find_a_webdriver_on_path() { + let fixture = fixture("tests/fixtures/wbg-test-browser"); + fixture.install_local_wasm_bindgen(); + fixture.install_local_geckodriver(); + + let geckodriver_dir = tempfile::TempDir::new().unwrap(); + let local_geckodriver = binaries::local_bin_path(&fixture.path, "geckodriver"); + fs::copy( + &local_geckodriver, + geckodriver_dir + .path() + .join(local_geckodriver.file_name().unwrap()), + ).unwrap(); + fs::remove_file(&local_geckodriver).unwrap(); + + let mut paths: Vec<_> = env::split_paths(&env::var("PATH").unwrap()).collect(); + paths.insert(0, geckodriver_dir.path().into()); + env::set_var("PATH", env::join_paths(paths).unwrap()); + + let cmd = Command::Test(test::TestOptions { + path: Some(fixture.path.clone()), + firefox: true, + headless: true, + mode: build::BuildMode::Noinstall, + ..Default::default() + }); + let logger = logger::new(&cmd, 3).unwrap(); + command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); +} #[test] fn it_requires_node_or_a_browser() { diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index 3db25939..2d4c76c0 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -10,6 +10,9 @@ use copy_dir::copy_dir; use tempfile; pub struct Fixture { + // NB: we wrap the fixture's tempdir in a `ManuallyDrop` so that if a test + // fails, its directory isn't deleted, and we have a chance to manually + // inspect its state and figure out what is going on. pub dir: ManuallyDrop, pub path: PathBuf, } @@ -77,7 +80,7 @@ impl Fixture { return; } - const WASM_BINDGEN_VERSION: &str = "0.2.17"; + const WASM_BINDGEN_VERSION: &str = "0.2.21"; wasm_pack::bindgen::download_prebuilt_wasm_bindgen(&tests, WASM_BINDGEN_VERSION) .or_else(|_| { wasm_pack::bindgen::cargo_install_wasm_bindgen(&tests, WASM_BINDGEN_VERSION) @@ -89,73 +92,65 @@ impl Fixture { /// Download `geckodriver` and return its path. /// - /// Takes car to ensure that only one `geckodriver` is downloaded for the whole + /// Takes care to ensure that only one `geckodriver` is downloaded for the whole /// test suite. pub fn install_local_geckodriver(&self) { static FETCH_GECKODRIVER: Once = ONCE_INIT; let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); - let bin = tests.join("bin"); - fs::create_dir_all(&bin).expect("could not create `tests/bin` directory"); + wasm_pack::binaries::ensure_local_bin_dir(&tests) + .expect("could not create fixture's `bin` directory"); - let geckodriver = bin.join(if cfg!(target_os = "windows") { - "geckodriver.exe" - } else { - "geckodriver" - }); + let geckodriver = wasm_pack::binaries::local_bin_path(&tests, "geckodriver"); FETCH_GECKODRIVER.call_once(|| { if geckodriver.is_file() { return; } - wasm_pack::webdriver::install_geckodriver(&tests).unwrap(); + wasm_pack::test::webdriver::install_geckodriver(&tests).unwrap(); assert!(geckodriver.is_file()); }); - let self_bin = self.path.join("bin"); - fs::create_dir_all(&self_bin).expect("could not create fixture's `bin` directory"); + wasm_pack::binaries::ensure_local_bin_dir(&self.path) + .expect("could not create fixture's `bin` directory"); fs::copy( &geckodriver, - self_bin.join(geckodriver.file_name().unwrap()), + wasm_pack::binaries::local_bin_path(&self.path, "geckodriver"), ).expect("could not copy `geckodriver` to fixture directory"); } /// Download `chromedriver` and return its path. /// - /// Takes car to ensure that only one `chromedriver` is downloaded for the whole + /// Takes care to ensure that only one `chromedriver` is downloaded for the whole /// test suite. pub fn install_local_chromedriver(&self) { static FETCH_CHROMEDRIVER: Once = ONCE_INIT; let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); - let bin = tests.join("bin"); - fs::create_dir_all(&bin).expect("could not create `tests/bin` directory"); + wasm_pack::binaries::ensure_local_bin_dir(&tests) + .expect("could not create fixture's `bin` directory"); - let chromedriver = bin.join(if cfg!(target_os = "windows") { - "chromedriver.exe" - } else { - "chromedriver" - }); + let chromedriver = wasm_pack::binaries::local_bin_path(&tests, "chromedriver"); FETCH_CHROMEDRIVER.call_once(|| { if chromedriver.is_file() { return; } - wasm_pack::webdriver::install_chromedriver(&tests).unwrap(); + wasm_pack::test::webdriver::install_chromedriver(&tests).unwrap(); assert!(chromedriver.is_file()); }); - let self_bin = self.path.join("bin"); - fs::create_dir_all(&self_bin).expect("could not create fixture's `bin` directory"); + wasm_pack::binaries::ensure_local_bin_dir(&self.path) + .expect("could not create fixture's `bin` directory"); fs::copy( &chromedriver, - self_bin.join(chromedriver.file_name().unwrap()), + wasm_pack::binaries::local_bin_path(&self.path, "chromedriver"), ).expect("could not copy `chromedriver` to fixture directory"); } } diff --git a/tests/all/webdriver.rs b/tests/all/webdriver.rs index 25abf9f2..21e33415 100644 --- a/tests/all/webdriver.rs +++ b/tests/all/webdriver.rs @@ -1,5 +1,5 @@ use utils::fixture::fixture; -use wasm_pack::webdriver; +use wasm_pack::test::webdriver; #[test] #[cfg(any( diff --git a/tests/fixtures/wbg-test-bad-versions/Cargo.toml b/tests/fixtures/wbg-test-bad-versions/Cargo.toml new file mode 100644 index 00000000..e88d9be1 --- /dev/null +++ b/tests/fixtures/wbg-test-bad-versions/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "wbg-test-node" +version = "0.1.0" +authors = ["Nick Fitzgerald "] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +# We depend on wasm-bindgen 0.2.21 +wasm-bindgen = "0.2.21" + +[dev-dependencies] +# And we depend on wasm-bindgen-test 0.2.19. But this should match the +# wasm-bindgen dependency! +wasm-bindgen-test = "0.2.19" diff --git a/tests/fixtures/wbg-test-bad-versions/src/lib.rs b/tests/fixtures/wbg-test-bad-versions/src/lib.rs new file mode 100644 index 00000000..31e1bb20 --- /dev/null +++ b/tests/fixtures/wbg-test-bad-versions/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/tests/fixtures/wbg-test-bad-versions/tests/node.rs b/tests/fixtures/wbg-test-bad-versions/tests/node.rs new file mode 100644 index 00000000..84e09f5d --- /dev/null +++ b/tests/fixtures/wbg-test-bad-versions/tests/node.rs @@ -0,0 +1,8 @@ +extern crate wasm_bindgen_test; + +use wasm_bindgen_test::*; + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1, 1); +} diff --git a/tests/fixtures/wbg-test-browser/Cargo.toml b/tests/fixtures/wbg-test-browser/Cargo.toml index 16254fce..ea109aad 100644 --- a/tests/fixtures/wbg-test-browser/Cargo.toml +++ b/tests/fixtures/wbg-test-browser/Cargo.toml @@ -7,7 +7,7 @@ authors = ["Nick Fitzgerald "] crate-type = ["cdylib"] [dependencies] -wasm-bindgen = "0.2.17" +wasm-bindgen = "0.2.21" [dev-dependencies] -wasm-bindgen-test = "0.2.17" +wasm-bindgen-test = "0.2.21" diff --git a/tests/fixtures/wbg-test-browser/src/lib.rs b/tests/fixtures/wbg-test-browser/src/lib.rs index 31e1bb20..28599bbe 100644 --- a/tests/fixtures/wbg-test-browser/src/lib.rs +++ b/tests/fixtures/wbg-test-browser/src/lib.rs @@ -1,7 +1,5 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +extern crate wasm_bindgen; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn hello() {} diff --git a/tests/fixtures/wbg-test-fail/Cargo.toml b/tests/fixtures/wbg-test-fail/Cargo.toml index 791969ca..831cf6b2 100644 --- a/tests/fixtures/wbg-test-fail/Cargo.toml +++ b/tests/fixtures/wbg-test-fail/Cargo.toml @@ -7,7 +7,7 @@ authors = ["Nick Fitzgerald "] crate-type = ["cdylib"] [dependencies] -wasm-bindgen = "0.2.17" +wasm-bindgen = "0.2.21" [dev-dependencies] -wasm-bindgen-test = "0.2.17" +wasm-bindgen-test = "0.2.21" diff --git a/tests/fixtures/wbg-test-fail/src/lib.rs b/tests/fixtures/wbg-test-fail/src/lib.rs index 31e1bb20..1659959a 100644 --- a/tests/fixtures/wbg-test-fail/src/lib.rs +++ b/tests/fixtures/wbg-test-fail/src/lib.rs @@ -1,7 +1,5 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +extern crate wasm_bindgen; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn hi() {} diff --git a/tests/fixtures/wbg-test-node/Cargo.toml b/tests/fixtures/wbg-test-node/Cargo.toml index e19ff4c3..75f2391d 100644 --- a/tests/fixtures/wbg-test-node/Cargo.toml +++ b/tests/fixtures/wbg-test-node/Cargo.toml @@ -7,7 +7,7 @@ authors = ["Nick Fitzgerald "] crate-type = ["cdylib"] [dependencies] -wasm-bindgen = "0.2.17" +wasm-bindgen = "0.2.21" -[target.'cfg(target_arch = "wasm32")'.dev-dependencies] -wasm-bindgen-test = "0.2.17" +[dev-dependencies] +wasm-bindgen-test = "0.2.21"