tests: Make tests run faster by using Fixture builders

Instead of copying directories, just build the fixtures directly in the
temporary directory.
master
Nick Fitzgerald 7 years ago
parent 4df7761156
commit fa61f90909
  1. 20
      Cargo.lock
  2. 1
      Cargo.toml
  3. 2
      tests/all/build.rs
  4. 3
      tests/all/main.rs
  5. 115
      tests/all/manifest.rs
  6. 4
      tests/all/readme.rs
  7. 14
      tests/all/test.rs
  8. 394
      tests/all/utils/fixture.rs
  9. 6
      tests/all/webdriver.rs
  10. 9
      tests/fixtures/bad-cargo-toml/Cargo.toml
  11. 2
      tests/fixtures/bad-cargo-toml/README.md
  12. 15
      tests/fixtures/bad-cargo-toml/src/lib.rs
  13. 13
      tests/fixtures/js-hello-world/Cargo.toml
  14. 2
      tests/fixtures/js-hello-world/README.md
  15. 18
      tests/fixtures/js-hello-world/src/lib.rs
  16. 13
      tests/fixtures/no-compile/Cargo.toml
  17. 2
      tests/fixtures/no-compile/README.md
  18. 15
      tests/fixtures/no-compile/src/lib.rs
  19. 13
      tests/fixtures/no-readme/Cargo.toml
  20. 15
      tests/fixtures/no-readme/src/lib.rs
  21. 1
      tests/fixtures/not-a-crate/README.md
  22. 13
      tests/fixtures/scopes/Cargo.toml
  23. 2
      tests/fixtures/scopes/README.md
  24. 15
      tests/fixtures/scopes/src/lib.rs
  25. 14
      tests/fixtures/serde-feature/Cargo.toml
  26. 2
      tests/fixtures/serde-feature/README.md
  27. 15
      tests/fixtures/serde-feature/src/lib.rs
  28. 16
      tests/fixtures/wbg-test-bad-versions/Cargo.toml
  29. 7
      tests/fixtures/wbg-test-bad-versions/src/lib.rs
  30. 8
      tests/fixtures/wbg-test-bad-versions/tests/node.rs
  31. 13
      tests/fixtures/wbg-test-browser/Cargo.toml
  32. 5
      tests/fixtures/wbg-test-browser/src/lib.rs
  33. 9
      tests/fixtures/wbg-test-browser/tests/browser.rs
  34. 13
      tests/fixtures/wbg-test-fail/Cargo.toml
  35. 5
      tests/fixtures/wbg-test-fail/src/lib.rs
  36. 7
      tests/fixtures/wbg-test-fail/tests/node.rs
  37. 13
      tests/fixtures/wbg-test-node/Cargo.toml
  38. 7
      tests/fixtures/wbg-test-node/src/lib.rs
  39. 8
      tests/fixtures/wbg-test-node/tests/node.rs
  40. 14
      tests/fixtures/with-underscores/Cargo.toml
  41. 2
      tests/fixtures/with-underscores/README.md
  42. 18
      tests/fixtures/with-underscores/src/lib.rs

20
Cargo.lock generated

@ -152,14 +152,6 @@ dependencies = [
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "copy_dir"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crc"
version = "1.8.1"
@ -944,22 +936,12 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "walkdir"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wasm-pack"
version = "0.4.2"
dependencies = [
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"copy_dir 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"curl 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1067,7 +1049,6 @@ dependencies = [
"checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd48adf136733979b49e15bc3b4c43cc0d3c85ece7bd08e6daa414c6fcb13e6"
"checksum copy_dir 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e4281031634644843bd2f5aa9c48cf98fc48d6b083bd90bb11becf10deaf8b0"
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
"checksum curl 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)" = "c8172e96ecfb1a2bfe3843d9d7154099a15130cf4a2f658259c7aa9cc2b5d4ff"
"checksum curl-sys 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "78800a6de442f65dab6ce26c6f369c14fc585686432bf4b77119d2d384216c31"
@ -1162,7 +1143,6 @@ dependencies = [
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c66c0b9792f0a765345452775f3adbd28dde9d33f30d13e5dcc5ae17cf6f3780"
"checksum which 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49c4f580e93079b70ac522e7bdebbe1568c8afa7d8d05ee534ee737ca37d2f51"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"

@ -32,7 +32,6 @@ which = "2.0.0"
zip = "0.4.2"
[dev-dependencies]
copy_dir = "0.1.2"
tempfile = "3"
[features]

@ -4,7 +4,7 @@ use wasm_pack::{command, logger, Cli};
#[test]
fn build_in_non_crate_directory_doesnt_panic() {
let fixture = utils::fixture::fixture("tests/fixtures/not-a-crate");
let fixture = utils::fixture::not_a_crate();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",

@ -1,8 +1,5 @@
extern crate copy_dir;
extern crate failure;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate structopt;

@ -14,46 +14,45 @@ fn it_gets_the_crate_name_default_path() {
#[test]
fn it_gets_the_crate_name_provided_path() {
let path = &PathBuf::from("tests/fixtures/js-hello-world");
assert!(manifest::get_crate_name(path).is_ok());
assert_eq!(manifest::get_crate_name(path).unwrap(), "js-hello-world");
let fixture = fixture::js_hello_world();
assert!(manifest::get_crate_name(&fixture.path).is_ok());
assert_eq!(
manifest::get_crate_name(&fixture.path).unwrap(),
"js-hello-world"
);
}
#[test]
fn it_checks_has_cdylib_default_path() {
let fixture = fixture::no_cdylib();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&PathBuf::from("."), &step).is_err());
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
}
#[test]
fn it_checks_has_cdylib_provided_path() {
let fixture = fixture::js_hello_world();
let step = wasm_pack::progressbar::Step::new(1);
assert!(
manifest::check_crate_config(&PathBuf::from("tests/fixtures/js-hello-world"), &step)
.is_ok()
);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
}
#[test]
fn it_checks_has_cdylib_wrong_crate_type() {
let fixture = fixture::bad_cargo_toml();
let step = wasm_pack::progressbar::Step::new(1);
assert!(
manifest::check_crate_config(&PathBuf::from("tests/fixtures/bad-cargo-toml"), &step)
.is_err()
);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
}
#[test]
fn it_recognizes_a_map_during_depcheck() {
let fixture = fixture::serde_feature();
let step = wasm_pack::progressbar::Step::new(1);
assert!(
manifest::check_crate_config(&PathBuf::from("tests/fixtures/serde-feature"), &step).is_ok()
);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
}
#[test]
fn it_creates_a_package_json_default_path() {
let fixture = fixture::fixture(".");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
@ -62,18 +61,22 @@ fn it_creates_a_package_json_default_path() {
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "wasm_pack.js");
assert_eq!(pkg.types, "wasm_pack.d.ts");
assert_eq!(pkg.module, "js_hello_world.js");
assert_eq!(pkg.types, "js_hello_world.d.ts");
assert_eq!(pkg.side_effects, "false");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.d.ts", "wasm_pack.js"]
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
.collect();
@ -82,14 +85,14 @@ fn it_creates_a_package_json_default_path() {
#[test]
fn it_creates_a_package_json_provided_path() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.module, "js_hello_world.js");
@ -108,7 +111,7 @@ fn it_creates_a_package_json_provided_path() {
#[test]
fn it_creates_a_package_json_provided_path_with_scope() {
let fixture = fixture::fixture("tests/fixtures/scopes");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
@ -126,14 +129,14 @@ fn it_creates_a_package_json_provided_path_with_scope() {
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(pkg.module, "scopes_hello_world.js");
assert_eq!(pkg.name, "@test/js-hello-world");
assert_eq!(pkg.module, "js_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"scopes_hello_world_bg.wasm",
"scopes_hello_world.d.ts",
"scopes_hello_world.js",
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
@ -143,7 +146,7 @@ fn it_creates_a_package_json_provided_path_with_scope() {
#[test]
fn it_creates_a_pkg_json_with_correct_files_on_node() {
let fixture = fixture::fixture(".");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
@ -153,29 +156,32 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
);
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
assert_eq!(pkg.types, "wasm_pack.d.ts");
assert_eq!(pkg.main, "js_hello_world.js");
assert_eq!(pkg.types, "js_hello_world.d.ts");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> =
["wasm_pack_bg.wasm", "wasm_pack_bg.js", "wasm_pack.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world_bg.js",
"js_hello_world.d.ts",
]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}
#[test]
fn it_creates_a_pkg_json_in_out_dir() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("./custom/out");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
@ -188,7 +194,7 @@ fn it_creates_a_pkg_json_in_out_dir() {
#[test]
fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
let fixture = fixture::fixture(".");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
@ -197,16 +203,16 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "wasm_pack.js");
assert_eq!(pkg.module, "js_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.js"]
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.js"]
.iter()
.map(|&s| String::from(s))
.collect();
@ -215,30 +221,30 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
#[test]
fn it_errors_when_wasm_bindgen_is_not_declared() {
let fixture = fixture::fixture("tests/fixtures/bad-cargo-toml");
let fixture = fixture::bad_cargo_toml();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
}
#[test]
fn it_does_not_error_when_wasm_bindgen_is_declared() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
}
#[test]
fn it_gets_wasm_bindgen_version() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
assert_eq!(
manifest::get_wasm_bindgen_version(&fixture.path).unwrap(),
"0.2"
"0.2.21"
);
}
#[test]
fn it_gets_wasm_bindgen_version_with_underscores() {
let fixture = fixture::fixture("tests/fixtures/with-underscores");
let fixture = fixture::with_underscores();
assert_eq!(
manifest::get_wasm_bindgen_version(&fixture.path).unwrap(),
"0.2"
@ -247,13 +253,14 @@ fn it_gets_wasm_bindgen_version_with_underscores() {
#[test]
fn the_wasm_bindgen_test_version_should_match_the_wasm_bindgen_version() {
let fixture = fixture::fixture("tests/fixtures/wbg-test-bad-versions");
let fixture = fixture::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();
println!("{}", msg);
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."
"The `wasm-bindgen-test` dependency version (=0.2.19) must match \
the `wasm-bindgen` dependency version (=0.2.21), but it does not."
)));
}

@ -8,7 +8,7 @@ use wasm_pack::readme;
#[test]
fn it_copies_a_readme_default_path() {
let fixture = fixture::fixture(".");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
@ -33,7 +33,7 @@ fn it_copies_a_readme_default_path() {
#[test]
fn it_creates_a_package_json_provided_path() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");

@ -1,14 +1,14 @@
use std::env;
use std::fs;
use tempfile;
use utils::fixture::fixture;
use utils::fixture;
use wasm_pack::binaries;
use wasm_pack::command::{self, build, test, Command};
use wasm_pack::logger;
#[test]
fn it_can_run_node_tests() {
let fixture = fixture("tests/fixtures/wbg-test-node");
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
@ -28,7 +28,7 @@ fn it_can_run_node_tests() {
all(target_os = "windows", target_arch = "x86_64")
))]
fn it_can_run_browser_tests() {
let fixture = fixture("tests/fixtures/wbg-test-browser");
let fixture = fixture::wbg_test_browser();
fixture.install_local_wasm_bindgen();
let firefox = cfg!(any(
@ -73,7 +73,7 @@ fn it_can_run_browser_tests() {
#[test]
fn it_can_run_failing_tests() {
let fixture = fixture("tests/fixtures/wbg-test-fail");
let fixture = fixture::wbg_test_fail();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
@ -97,7 +97,7 @@ fn it_can_run_failing_tests() {
all(target_os = "windows", target_arch = "x86_64")
))]
fn it_can_find_a_webdriver_on_path() {
let fixture = fixture("tests/fixtures/wbg-test-browser");
let fixture = fixture::wbg_test_browser();
fixture.install_local_wasm_bindgen();
fixture.install_local_geckodriver();
@ -128,7 +128,7 @@ fn it_can_find_a_webdriver_on_path() {
#[test]
fn it_requires_node_or_a_browser() {
let fixture = fixture("tests/fixtures/wbg-test-node");
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
@ -146,7 +146,7 @@ fn it_requires_node_or_a_browser() {
#[test]
fn the_headless_flag_requires_a_browser() {
let fixture = fixture("tests/fixtures/wbg-test-node");
let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {

@ -1,14 +1,21 @@
use std::env;
use std::fs;
use std::io;
use std::mem::ManuallyDrop;
use std::path::{Path, PathBuf};
use std::sync::{Once, ONCE_INIT};
use std::thread;
use wasm_pack;
use copy_dir::copy_dir;
use tempfile;
fn hard_link_or_copy<P1: AsRef<Path>, P2: AsRef<Path>>(from: P1, to: P2) -> io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
}
/// A test fixture in a temporary directory.
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
@ -17,66 +24,123 @@ pub struct Fixture {
pub path: PathBuf,
}
/// Copy the given fixture into a unique temporary directory. This allows the
/// test to mutate the copied fixture without messing up other tests that are
/// also trying to read from or write to that fixture. The given path should be
/// relative from the root of the repository, eg
/// "tests/fixtures/im-from-brooklyn-the-place-where-stars-are-born".
pub fn fixture<P>(fixture: P) -> Fixture
where
P: AsRef<Path>,
{
// Make sure that all fixtures end up sharing a target dir, and we don't
// recompile wasm-bindgen and friends many times over.
static SET_TARGET_DIR: Once = ONCE_INIT;
SET_TARGET_DIR.call_once(|| {
env::set_var(
"CARGO_TARGET_DIR",
Path::new(env!("CARGO_MANIFEST_DIR")).join("target"),
);
});
let fixture = fixture
.as_ref()
.canonicalize()
.expect("should canonicalize fixture path OK");
let dir = ManuallyDrop::new(tempfile::tempdir().expect("should create temporary directory OK"));
let path = dir.path().join("wasm-pack");
println!(
"wasm-pack: copying test fixture '{}' to temporary directory '{}'",
fixture.display(),
path.display()
);
impl Fixture {
/// Create a new test fixture in a temporary directory.
pub fn new() -> Fixture {
// Make sure that all fixtures end up sharing a target dir, and we don't
// recompile wasm-bindgen and friends many times over.
static SET_TARGET_DIR: Once = ONCE_INIT;
SET_TARGET_DIR.call_once(|| {
env::set_var(
"CARGO_TARGET_DIR",
Path::new(env!("CARGO_MANIFEST_DIR")).join("target"),
);
});
let dir =
ManuallyDrop::new(tempfile::tempdir().expect("should create temporary directory OK"));
let path = dir.path().join("wasm-pack");
Fixture { dir, path }
}
{
// Copying too many things in parallel totally kills my machine(??!!?!),
// so make sure we are only doing one `copy_dir` at a time...
use std::sync::Mutex;
lazy_static! {
static ref ONE_AT_A_TIME: Mutex<()> = Mutex::new(());
/// Create a file within this fixture.
///
/// `path` should be a relative path to the file (relative within this
/// fixture's path).
///
/// The `contents` are written to the file.
pub fn file<P: AsRef<Path>, C: AsRef<[u8]>>(&self, path: P, contents: C) -> &Self {
assert!(path.as_ref().is_relative());
let path = self.path.join(path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
let _locked = ONE_AT_A_TIME.lock();
fs::write(path, contents).unwrap();
self
}
copy_dir(fixture, &path)
.expect("should copy fixture directory into temporary directory OK");
/// Add a generic `README.md` file to the fixture.
pub fn readme(&self) -> &Self {
self.file(
"README.md",
r#"
# Fixture!
> an example rust -> wasm project
"#,
)
}
Fixture { dir, path }
}
/// Add a `Cargo.toml` with a correctly configured `wasm-bindgen`
/// dependency, `wasm-bindgen-test` dev-dependency, and `crate-type =
/// ["cdylib"]`.
///
/// `name` is the crate's name.
pub fn cargo_toml(&self, name: &str) -> &Self {
self.file(
"Cargo.toml",
&format!(
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "{}"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.21"
[dev-dependencies]
wasm-bindgen-test = "0.2.21"
"#,
name
),
)
}
/// Add a `src/lib.rs` file that contains a "hello world" program.
pub fn hello_world_src_lib(&self) -> &Self {
self.file(
"src/lib.rs",
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
"#,
)
}
impl Fixture {
/// Install a local wasm-bindgen for this fixture.
///
/// Takes care not to re-install for every fixture, but only the one time
/// for the whole test suite.
pub fn install_local_wasm_bindgen(&self) {
pub fn install_local_wasm_bindgen(&self) -> &Self {
static INSTALL_WASM_BINDGEN: Once = ONCE_INIT;
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
let bin = tests.join("bin");
let shared_wasm_bindgen = wasm_pack::binaries::local_bin_path(&tests, "wasm-bindgen");
let shared_wasm_bindgen_test_runner =
wasm_pack::binaries::local_bin_path(&tests, "wasm-bindgen-test-runner");
INSTALL_WASM_BINDGEN.call_once(|| {
if bin.join("wasm-bindgen").is_file() {
if shared_wasm_bindgen.is_file() {
assert!(shared_wasm_bindgen_test_runner.is_file());
return;
}
@ -87,14 +151,29 @@ impl Fixture {
}).unwrap();
});
copy_dir(bin, self.path.join("bin")).expect("could not copy `bin` directory into temp dir");
assert!(shared_wasm_bindgen.is_file());
assert!(shared_wasm_bindgen_test_runner.is_file());
wasm_pack::binaries::ensure_local_bin_dir(&self.path).unwrap();
hard_link_or_copy(
&shared_wasm_bindgen,
wasm_pack::binaries::local_bin_path(&self.path, "wasm-bindgen"),
).expect("could not copy `wasm-bindgen` to fixture directory");
hard_link_or_copy(
&shared_wasm_bindgen_test_runner,
wasm_pack::binaries::local_bin_path(&self.path, "wasm-bindgen-test-runner"),
).expect("could not copy `wasm-bindgen-test` to fixture directory");
self
}
/// Download `geckodriver` and return its path.
///
/// Takes care to ensure that only one `geckodriver` is downloaded for the whole
/// test suite.
pub fn install_local_geckodriver(&self) {
pub fn install_local_geckodriver(&self) -> &Self {
static FETCH_GECKODRIVER: Once = ONCE_INIT;
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
@ -116,17 +195,19 @@ impl Fixture {
wasm_pack::binaries::ensure_local_bin_dir(&self.path)
.expect("could not create fixture's `bin` directory");
fs::copy(
hard_link_or_copy(
&geckodriver,
wasm_pack::binaries::local_bin_path(&self.path, "geckodriver"),
).expect("could not copy `geckodriver` to fixture directory");
self
}
/// Download `chromedriver` and return its path.
///
/// Takes care to ensure that only one `chromedriver` is downloaded for the whole
/// test suite.
pub fn install_local_chromedriver(&self) {
pub fn install_local_chromedriver(&self) -> &Self {
static FETCH_CHROMEDRIVER: Once = ONCE_INIT;
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
@ -148,10 +229,12 @@ impl Fixture {
wasm_pack::binaries::ensure_local_bin_dir(&self.path)
.expect("could not create fixture's `bin` directory");
fs::copy(
hard_link_or_copy(
&chromedriver,
wasm_pack::binaries::local_bin_path(&self.path, "chromedriver"),
).expect("could not copy `chromedriver` to fixture directory");
self
}
}
@ -162,3 +245,212 @@ impl Drop for Fixture {
}
}
}
pub fn bad_cargo_toml() -> Fixture {
let fixture = Fixture::new();
fixture.readme().hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
name = "bad-cargo-toml"
version = "0.1.0"
authors = ["The wasm-pack developers"]
[lib]
crate-type = ["foo"]
[dependencies]
# Note: no wasm-bindgen dependency!
"#,
);
fixture
}
pub fn js_hello_world() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("js-hello-world")
.hello_world_src_lib();
fixture
}
pub fn no_cdylib() -> Fixture {
let fixture = Fixture::new();
fixture.readme().hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "{}"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
# [lib]
# crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.21"
[dev-dependencies]
wasm-bindgen-test = "0.2.21"
"#,
);
fixture
}
pub fn not_a_crate() -> Fixture {
let fixture = Fixture::new();
fixture.file("README.md", "This is not a Rust crate!");
fixture
}
pub fn serde_feature() -> Fixture {
let fixture = Fixture::new();
fixture.readme().hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
name = "serde-serialize"
version = "0.1.0"
authors = ["The wasm-pack developers"]
[lib]
crate-type = ["cdylib"]
[dependencies.wasm-bindgen]
version = "^0.2"
features = ["serde-serialize"]
"#,
);
fixture
}
pub fn wbg_test_bad_versions() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.hello_world_src_lib()
.file(
"Cargo.toml",
r#"
[package]
name = "wbg-test-node"
version = "0.1.0"
authors = ["The wasm-pack developers"]
[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"
"#,
).file(
"tests/node.rs",
r#"
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}
"#,
);
fixture
}
pub fn wbg_test_browser() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("wbg-test-browser")
.hello_world_src_lib()
.file(
"tests/browser.rs",
r#"
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}
"#,
);
fixture
}
pub fn wbg_test_fail() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("wbg-test-fail")
.hello_world_src_lib()
.file(
"tests/node.rs",
r#"
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 2);
}
"#,
);
fixture
}
pub fn wbg_test_node() -> Fixture {
let fixture = Fixture::new();
fixture
.readme()
.cargo_toml("wbg-test-node")
.hello_world_src_lib()
.file(
"tests/node.rs",
r#"
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}
"#,
);
fixture
}
pub fn with_underscores() -> Fixture {
let fixture = Fixture::new();
fixture.readme().hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
name = "with-underscores"
version = "0.1.0"
authors = ["The wasm-pack developers"]
[lib]
crate-type = ["cdylib"]
[dependencies]
# Cargo will normalize "wasm-bindgen" and "wasm_bindgen" and that shouldn't
# break wasm-pack.
wasm_bindgen = "0.2"
"#,
);
fixture
}

@ -1,4 +1,4 @@
use utils::fixture::fixture;
use utils::fixture;
use wasm_pack::test::webdriver;
#[test]
@ -8,7 +8,7 @@ use wasm_pack::test::webdriver;
all(target_os = "windows", target_arch = "x86")
))]
fn can_install_chromedriver() {
let fixture = fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
assert!(webdriver::install_chromedriver(&fixture.path).is_ok());
}
@ -21,6 +21,6 @@ fn can_install_chromedriver() {
all(target_os = "windows", target_arch = "x86_64")
))]
fn can_install_geckodriver() {
let fixture = fixture("tests/fixtures/js-hello-world");
let fixture = fixture::js_hello_world();
assert!(webdriver::install_geckodriver(&fixture.path).is_ok());
}

@ -1,9 +0,0 @@
[package]
name = "bad-cargo-toml"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
[lib]
crate-type = ["foo"]
[dependencies]

@ -1,2 +0,0 @@
# bad-cargo-toml
> an example rust -> wasm project

@ -1,15 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1,13 +0,0 @@
[package]
name = "js-hello-world"
description = "an example rust->wasm crate"
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"

@ -1,2 +0,0 @@
# js-hello-world
> an example rust -> wasm project

@ -1,18 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1,13 +0,0 @@
[package]
name = "no-compile"
description = "an example rust->wasm crate that can't compile"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"

@ -1,2 +0,0 @@
# no-compile
> an example rust -> wasm project that won't compile!

@ -1,15 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1,13 +0,0 @@
[package]
name = "no-readme"
description = "an example rust->wasm crate with no readme"
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"

@ -1,15 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1 +0,0 @@
This is not a Rust crate!

@ -1,13 +0,0 @@
[package]
name = "scopes-hello-world"
description = "an example rust->wasm crate"
version = "0.1.0"
authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"

@ -1,2 +0,0 @@
# scopes-hello-world
> an example rust -> wasm project

@ -1,15 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1,14 +0,0 @@
[package]
name = "serde-serialize"
description = "an example rust->wasm crate"
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies.wasm-bindgen]
version = "^0.2"
features = ["serde-serialize"]

@ -1,2 +0,0 @@
# js-hello-world
> an example rust -> wasm project

@ -1,15 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

@ -1,16 +0,0 @@
[package]
name = "wbg-test-node"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
[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"

@ -1,7 +0,0 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

@ -1,8 +0,0 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}

@ -1,13 +0,0 @@
[package]
name = "wbg-test-browser"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.21"
[dev-dependencies]
wasm-bindgen-test = "0.2.21"

@ -1,5 +0,0 @@
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn hello() {}

@ -1,9 +0,0 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}

@ -1,13 +0,0 @@
[package]
name = "wbg-test-ok"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.21"
[dev-dependencies]
wasm-bindgen-test = "0.2.21"

@ -1,5 +0,0 @@
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn hi() {}

@ -1,7 +0,0 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn fail() {
assert_eq!(1, 2);
}

@ -1,13 +0,0 @@
[package]
name = "wbg-test-node"
version = "0.1.0"
authors = ["Nick Fitzgerald <fitzgen@gmail.com>"]
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.21"
[dev-dependencies]
wasm-bindgen-test = "0.2.21"

@ -1,7 +0,0 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

@ -1,8 +0,0 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1, 1);
}

@ -1,14 +0,0 @@
[package]
name = "with-underscores"
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"
[lib]
crate-type = ["cdylib"]
[dependencies]
# Cargo will normalize "wasm-bindgen" and "wasm_bindgen" and that shouldn't
# break wasm-pack.
wasm_bindgen = "0.2"

@ -1,2 +0,0 @@
# js-hello-world
> an example rust -> wasm project

@ -1,18 +0,0 @@
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
Loading…
Cancel
Save