From fee93bc9651ed6b9d266d928144b97049ec5c599 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 03:15:36 +0900 Subject: [PATCH 01/11] use license-file field for license field of package.json when no license field in Cargo.toml In 'license' field of package.json, it's ok to say 'license is written in some file' as follows: ``` { "license" : "SEE LICENSE IN " } ``` This notation is useful when 'license' field is missing in `Cargo.toml` but `license-file` field is provided. Ref: https://docs.npmjs.com/files/package.json#license --- src/manifest/mod.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 92f03cd..4bc7f7c 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -40,6 +40,7 @@ struct CargoPackage { name: String, description: Option, license: Option, + license_file: Option, repository: Option, #[serde(default)] @@ -425,6 +426,15 @@ impl CrateData { } } + fn license(&self) -> Option { + self.manifest.package.license.clone().or_else(|| { + self.manifest.package.license_file.clone().map(|file| { + // When license is written in file: https://docs.npmjs.com/files/package.json#license + format!("SEE LICENSE IN {}", file) + }) + }) + } + fn to_commonjs(&self, scope: &Option, disable_dts: bool) -> NpmPackage { let data = self.npm_data(scope, true, disable_dts); let pkg = &self.data.packages[self.current_idx]; @@ -436,7 +446,7 @@ impl CrateData { collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.clone(), - license: self.manifest.package.license.clone(), + license: self.license(), repository: self .manifest .package @@ -463,7 +473,7 @@ impl CrateData { collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.clone(), - license: self.manifest.package.license.clone(), + license: self.license(), repository: self .manifest .package @@ -491,7 +501,7 @@ impl CrateData { collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.clone(), - license: self.manifest.package.license.clone(), + license: self.license(), repository: self .manifest .package From 9fa5139caf1783ecda8159037e18f7e2abb08d3d Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 03:46:19 +0900 Subject: [PATCH 02/11] be aware of license-file field as source of license 'license-file' field is provided in Cargo.toml for providing path of license file. https://doc.rust-lang.org/cargo/reference/manifest.html --- src/license.rs | 13 ++++++++++--- src/manifest/mod.rs | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/license.rs b/src/license.rs index 2099c64..ee5f20b 100644 --- a/src/license.rs +++ b/src/license.rs @@ -57,8 +57,8 @@ pub fn copy_from_crate( "crate's pkg directory should exist" ); - match crate_data.crate_license() { - Some(_) => { + match (crate_data.crate_license(), crate_data.crate_license_file()) { + (Some(_), _) => { let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS); PBAR.step(step, &msg); let license_files = glob_license_files(path); @@ -80,7 +80,14 @@ pub fn copy_from_crate( Err(_) => PBAR.info("origin crate has no LICENSE"), } } - None => { + (None, Some(license_file)) => { + let crate_license_path = path.join(&license_file); + let new_license_path = out_dir.join(&license_file); + if fs::copy(&crate_license_path, &new_license_path).is_err() { + PBAR.info("origin crate has no LICENSE"); + } + } + (None, None) => { PBAR.step(step, "No LICENSE found in Cargo.toml, skipping..."); } }; diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 4bc7f7c..7d76ba8 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -350,6 +350,11 @@ impl CrateData { &self.manifest.package.license } + /// Get the license file path for the crate at the given path. + pub fn crate_license_file(&self) -> &Option { + &self.manifest.package.license_file + } + /// Returns the path to this project's target directory where artifacts are /// located after a cargo build. pub fn target_directory(&self) -> &Path { From 5f515dcf68ec26eb7755f4f6f1e3b79c372b2cb6 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 04:29:59 +0900 Subject: [PATCH 03/11] add a test case for non-standard license file field --- src/manifest/mod.rs | 1 + tests/all/license.rs | 29 ++++++++++++++++++++++++- tests/all/utils/fixture.rs | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 7d76ba8..7b07986 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -40,6 +40,7 @@ struct CargoPackage { name: String, description: Option, license: Option, + #[serde(rename = "license-file")] license_file: Option, repository: Option, diff --git a/tests/all/license.rs b/tests/all/license.rs index ac7d377..5b7cd28 100644 --- a/tests/all/license.rs +++ b/tests/all/license.rs @@ -34,7 +34,7 @@ fn it_copies_a_license_default_path() { } #[test] -fn it_copies_a_license_provied_path() { +fn it_copies_a_license_provided_path() { let fixture = fixture::single_license(); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); @@ -128,3 +128,30 @@ fn it_copies_all_licenses_provided_path() { let pkg_license_2 = utils::file::read_file(&pkg_license_path_2).unwrap(); assert_eq!(crate_license_2, pkg_license_2); } + +#[test] +fn it_copies_a_non_standard_license_provided_path() { + let license_file = "NON-STANDARD-LICENSE"; + let fixture = fixture::non_standard_license(license_file); + let out_dir = fixture.path.join("pkg"); + fs::create_dir(&out_dir).expect("should create pkg directory OK"); + let crate_data = CrateData::new(&fixture.path); + + let step = wasm_pack::progressbar::Step::new(1); + assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir, &step).is_ok()); + + let crate_license_path = fixture.path.join(license_file); + let pkg_license_path = out_dir.join(license_file); + println!( + "wasm-pack: should have copied LICENSE from '{}' to '{}'", + crate_license_path.display(), + pkg_license_path.display() + ); + assert!(fs::metadata(&crate_license_path).is_ok()); + + assert!(fs::metadata(&pkg_license_path).is_ok()); + + let crate_license = utils::file::read_file(&crate_license_path).unwrap(); + let pkg_license = utils::file::read_file(&pkg_license_path).unwrap(); + assert_eq!(crate_license, pkg_license); +} diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index 3eee609..f157b9b 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -137,6 +137,39 @@ impl Fixture { ) } + /// 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. + /// `license_file` is license file path + pub fn cargo_toml_with_license_file(&self, name: &str, license_file: &str) -> &Self { + self.file( + "Cargo.toml", + &format!( + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + name = "{}" + license-file = "{}" + 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, license_file + ), + ) + } + /// Add a `src/lib.rs` file that contains a "hello world" program. pub fn hello_world_src_lib(&self) -> &Self { self.file( @@ -661,3 +694,13 @@ pub fn dual_license() -> Fixture { .hello_world_src_lib(); fixture } + +pub fn non_standard_license(license_file: &str) -> Fixture { + let fixture = Fixture::new(); + fixture + .readme() + .cargo_toml_with_license_file("dual_license", license_file) + .file(license_file, "license file for test") + .hello_world_src_lib(); + fixture +} From b72051d385fe178cbb95865942bc42b983665a63 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 10:08:56 -0500 Subject: [PATCH 04/11] Pass arguments through to cargo test --- docs/src/commands/build.md | 2 +- docs/src/commands/test.md | 54 ++++++++++++++++++++++++++++++++++++++ src/build.rs | 12 --------- src/command/build.rs | 2 +- src/command/test.rs | 27 ++++++++----------- src/test/mod.rs | 8 +++++- 6 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 docs/src/commands/test.md diff --git a/docs/src/commands/build.md b/docs/src/commands/build.md index 96371fc..49b4540 100644 --- a/docs/src/commands/build.md +++ b/docs/src/commands/build.md @@ -59,7 +59,7 @@ wasm-pack build --target nodejs | Option | Description | |-----------|-----------------------------------------------------------------------------------------------------------------| | `nodejs` | Outputs JS that uses CommonJS modules, for use with a `require` statement. `main` key in `package.json`. | -| `nomodules` | Outputs JS that use no modules. `browser` key in `package.json`. | +| `no-modules` | Outputs JS that use no modules. `browser` key in `package.json`. | | `browser` | Outputs JS that uses ES6 modules, primarily for use with `import` statements and/or bundlers such as `webpack`. `module` key in `package.json`. `sideEffects: false` by default. | ## Scope diff --git a/docs/src/commands/test.md b/docs/src/commands/test.md new file mode 100644 index 0000000..5f679ce --- /dev/null +++ b/docs/src/commands/test.md @@ -0,0 +1,54 @@ +# wasm-pack test + +The `wasm-pack test` command wraps the [wasm-bindgen-test-runner](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html) +CLI allowing you to run wasm tests in different browsers without needing to install the different +webdrivers yourself. + +``` +wasm-pack test --help +``` + +## Path + +The `wasm-pack test` command can be given an optional path argument. + +This path should point to a directory that contains a `Cargo.toml` file. If no +path is given, the `test` command will run in the current directory. + +``` +# Run tests for the current directory's crate +wasm-pack test + +# Run tests for a specified crate +wasm-pack test crates/crate-in-my-workspace +``` + +## Profile + +The `test` command accepts an optional profile argument: `--release`. + +If none is supplied, then a debug test build will be used. + +## Test environment + +Choose where to run your tests by passing in any combination of testing environment flags. + +`--headless` is useful for running browser tests in a headless browser as part of a CI process. + +``` +wasm-pack test --node --firefox --chrome --safari --headless +``` + +## Extra options + +The `test` command can pass extra options straight to `cargo test` even if they are not +supported in wasm-pack. + +To use them you should add standalone `--` argument at the very +end of your command, and all the arguments you want to pass to cargo should go after. + +Here's an example of running only tests that contain the world "apple". + +``` +wasm-pack test --firefox --headless -- --manifest-path=crates/my-workspace-crate/Cargo.toml apple +``` diff --git a/src/build.rs b/src/build.rs index 9e29c6d..e0fb1ca 100644 --- a/src/build.rs +++ b/src/build.rs @@ -93,15 +93,3 @@ pub fn cargo_build_wasm( child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; Ok(()) } - -/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`. -pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> { - let mut cmd = Command::new("cargo"); - cmd.current_dir(path).arg("build").arg("--tests"); - if !debug { - cmd.arg("--release"); - } - cmd.arg("--target").arg("wasm32-unknown-unknown"); - child::run(cmd, "cargo build").context("Compilation of your program failed")?; - Ok(()) -} diff --git a/src/command/build.rs b/src/command/build.rs index ee59a75..4dd5d79 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -19,7 +19,7 @@ use std::str::FromStr; use std::time::Instant; use PBAR; -/// Everything required to configure and run the `wasm-pack init` command. +/// Everything required to configure and run the `wasm-pack build` command. #[allow(missing_docs)] pub struct Build { pub crate_path: PathBuf, diff --git a/src/command/test.rs b/src/command/test.rs index ff2f348..7c492b4 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -78,6 +78,10 @@ pub struct TestOptions { #[structopt(long = "release", short = "r")] /// Build with the release profile. pub release: bool, + + #[structopt(last = true)] + /// List of extra options to pass to `cargo test` + pub extra_options: Vec, } /// A configured `wasm-pack test` command. @@ -96,6 +100,7 @@ pub struct Test { headless: bool, release: bool, test_runner_path: Option, + extra_options: Vec } type TestStep = fn(&mut Test, &Step) -> Result<(), Error>; @@ -115,6 +120,7 @@ impl Test { geckodriver, safari, safaridriver, + extra_options, } = test_opts; let crate_path = set_crate_path(path)?; @@ -147,6 +153,7 @@ impl Test { headless, release, test_runner_path: None, + extra_options, }) } @@ -213,7 +220,6 @@ impl Test { step_test_safari if self.safari, ], BuildMode::Noinstall => steps![ - step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -240,18 +246,6 @@ impl Test { Ok(()) } - fn step_build_tests(&mut self, step: &Step) -> Result<(), Error> { - info!("Compiling tests to wasm..."); - - let msg = format!("{}Compiling tests to WASM...", emoji::CYCLONE); - PBAR.step(step, &msg); - - build::cargo_build_wasm_tests(&self.crate_path, !self.release)?; - - info!("Finished compiling tests to wasm."); - Ok(()) - } - fn step_install_wasm_bindgen(&mut self, step: &Step) -> Result<(), Error> { info!("Identifying wasm-bindgen dependency..."); let lockfile = Lockfile::new(&self.crate_data)?; @@ -306,6 +300,7 @@ impl Test { "CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", &self.test_runner_path.as_ref().unwrap(), )), + &self.extra_options )?; info!("Finished running tests in node."); Ok(()) @@ -349,7 +344,7 @@ impl Test { envs.push(("NO_HEADLESS", "1")); } - test::cargo_test_wasm(&self.crate_path, self.release, envs)?; + test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?; Ok(()) } @@ -391,7 +386,7 @@ impl Test { envs.push(("NO_HEADLESS", "1")); } - test::cargo_test_wasm(&self.crate_path, self.release, envs)?; + test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?; Ok(()) } @@ -430,7 +425,7 @@ impl Test { envs.push(("NO_HEADLESS", "1")); } - test::cargo_test_wasm(&self.crate_path, self.release, envs)?; + test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?; Ok(()) } } diff --git a/src/test/mod.rs b/src/test/mod.rs index 77eff43..7448567 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -10,7 +10,12 @@ use std::process::Command; /// Run `cargo test` with the `nightly` toolchain and targeting /// `wasm32-unknown-unknown`. -pub fn cargo_test_wasm(path: &Path, release: bool, envs: I) -> Result<(), failure::Error> +pub fn cargo_test_wasm( + path: &Path, + release: bool, + envs: I, + extra_options: &Vec, +) -> Result<(), failure::Error> where I: IntoIterator, K: AsRef, @@ -23,6 +28,7 @@ where cmd.arg("--release"); } cmd.arg("--target").arg("wasm32-unknown-unknown"); + cmd.args(extra_options); child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")?; // NB: `child::run` took care of ensuring that test output gets printed. From 42413acb493d8d939143d0db14e6ddc4d6197ea4 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 10:29:48 -0500 Subject: [PATCH 05/11] cargo fmt --- src/command/test.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/command/test.rs b/src/command/test.rs index 7c492b4..829f3d3 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -100,7 +100,7 @@ pub struct Test { headless: bool, release: bool, test_runner_path: Option, - extra_options: Vec + extra_options: Vec, } type TestStep = fn(&mut Test, &Step) -> Result<(), Error>; @@ -197,7 +197,6 @@ impl Test { BuildMode::Normal => steps![ step_check_rustc_version, step_add_wasm_target, - step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -209,7 +208,6 @@ impl Test { ], BuildMode::Force => steps![ step_add_wasm_target, - step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -300,7 +298,7 @@ impl Test { "CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", &self.test_runner_path.as_ref().unwrap(), )), - &self.extra_options + &self.extra_options, )?; info!("Finished running tests in node."); Ok(()) From f7ad0e7c5a133fbd154864a0de1855ee3b8af6f0 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 10:31:15 -0500 Subject: [PATCH 06/11] Add instructions for options --- docs/src/commands/test.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/src/commands/test.md b/docs/src/commands/test.md index 5f679ce..ec8663f 100644 --- a/docs/src/commands/test.md +++ b/docs/src/commands/test.md @@ -52,3 +52,5 @@ Here's an example of running only tests that contain the world "apple". ``` wasm-pack test --firefox --headless -- --manifest-path=crates/my-workspace-crate/Cargo.toml apple ``` + +`cargo test -h` for a list of all options that you can pass through. From 879e3aa7a68a793d6c871967380137b42c1fddfc Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 11:44:01 -0500 Subject: [PATCH 07/11] Add back building tests --- src/build.rs | 17 ++++++++++++++++- src/command/test.rs | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/build.rs b/src/build.rs index e0fb1ca..e706b6a 100644 --- a/src/build.rs +++ b/src/build.rs @@ -25,7 +25,7 @@ pub fn check_rustc_version(step: &Step) -> Result { } else { Ok(mv.to_string()) } - }, + } None => bail!("We can't figure out what your Rust version is- which means you might not have Rust installed. Please install Rust version 1.30.0 or higher."), } } @@ -93,3 +93,18 @@ pub fn cargo_build_wasm( child::run(cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?; Ok(()) } + +/// Run `cargo build --tests` targetting `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> { + let mut cmd = Command::new("cargo"); + cmd.current_dir(path).arg("build").arg("--tests"); + if !debug { + cmd.arg("--release"); + } + cmd.arg("--target").arg("wasm32-unknown-unknown"); + child::run(cmd, "cargo build").context("Compilation of your program failed")?; + Ok(()) +} diff --git a/src/command/test.rs b/src/command/test.rs index 829f3d3..45950fc 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -197,6 +197,7 @@ impl Test { BuildMode::Normal => steps![ step_check_rustc_version, step_add_wasm_target, + step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -208,6 +209,7 @@ impl Test { ], BuildMode::Force => steps![ step_add_wasm_target, + step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -218,6 +220,7 @@ impl Test { step_test_safari if self.safari, ], BuildMode::Noinstall => steps![ + step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -244,6 +247,18 @@ impl Test { Ok(()) } + fn step_build_tests(&mut self, step: &Step) -> Result<(), Error> { + info!("Compiling tests to wasm..."); + + let msg = format!("{}Compiling tests to WASM...", emoji::CYCLONE); + PBAR.step(step, &msg); + + build::cargo_build_wasm_tests(&self.crate_path, !self.release)?; + + info!("Finished compiling tests to wasm."); + Ok(()) + } + fn step_install_wasm_bindgen(&mut self, step: &Step) -> Result<(), Error> { info!("Identifying wasm-bindgen dependency..."); let lockfile = Lockfile::new(&self.crate_data)?; From 3f06d2d959d7e51dabd7410cababd629cff3217e Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 11:44:59 -0500 Subject: [PATCH 08/11] Fix formatting --- src/command/test.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/command/test.rs b/src/command/test.rs index 45950fc..f07d4a9 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -197,7 +197,7 @@ impl Test { BuildMode::Normal => steps![ step_check_rustc_version, step_add_wasm_target, - step_build_tests, + step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), @@ -220,7 +220,7 @@ impl Test { step_test_safari if self.safari, ], BuildMode::Noinstall => steps![ - step_build_tests, + step_build_tests, step_install_wasm_bindgen, step_test_node if self.node, step_get_chromedriver if self.chrome && self.chromedriver.is_none(), From bae98ef92a893522fa4e69b965a3f70e3b8a676d Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 28 Jan 2019 03:39:22 +0900 Subject: [PATCH 09/11] copy 'homepage' field from Cargo.toml to package.json - Cargo.toml - https://doc.rust-lang.org/cargo/reference/manifest.html - package.json - https://docs.npmjs.com/files/package.json#homepage --- src/manifest/mod.rs | 6 ++++++ src/manifest/npm/commonjs.rs | 2 ++ src/manifest/npm/esmodules.rs | 2 ++ src/manifest/npm/nomodules.rs | 2 ++ 4 files changed, 12 insertions(+) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 92f03cd..2f7b964 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -41,6 +41,7 @@ struct CargoPackage { description: Option, license: Option, repository: Option, + homepage: Option, #[serde(default)] metadata: CargoMetadata, @@ -199,6 +200,7 @@ struct NpmData { files: Vec, dts_file: Option, main: String, + homepage: Option, // https://docs.npmjs.com/files/package.json#homepage } #[doc(hidden)] @@ -422,6 +424,7 @@ impl CrateData { dts_file, files, main: js_file, + homepage: self.manifest.package.homepage.clone(), } } @@ -448,6 +451,7 @@ impl CrateData { }), files: data.files, main: data.main, + homepage: data.homepage, types: data.dts_file, }) } @@ -475,6 +479,7 @@ impl CrateData { }), files: data.files, module: data.main, + homepage: data.homepage, types: data.dts_file, side_effects: "false".to_string(), }) @@ -503,6 +508,7 @@ impl CrateData { }), files: data.files, browser: data.main, + homepage: data.homepage, types: data.dts_file, }) } diff --git a/src/manifest/npm/commonjs.rs b/src/manifest/npm/commonjs.rs index ed8b8f9..8d8ce8b 100644 --- a/src/manifest/npm/commonjs.rs +++ b/src/manifest/npm/commonjs.rs @@ -16,5 +16,7 @@ pub struct CommonJSPackage { pub files: Vec, pub main: String, #[serde(skip_serializing_if = "Option::is_none")] + pub homepage: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub types: Option, } diff --git a/src/manifest/npm/esmodules.rs b/src/manifest/npm/esmodules.rs index df73964..cd715ca 100644 --- a/src/manifest/npm/esmodules.rs +++ b/src/manifest/npm/esmodules.rs @@ -16,6 +16,8 @@ pub struct ESModulesPackage { pub files: Vec, pub module: String, #[serde(skip_serializing_if = "Option::is_none")] + pub homepage: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub types: Option, #[serde(rename = "sideEffects")] pub side_effects: String, diff --git a/src/manifest/npm/nomodules.rs b/src/manifest/npm/nomodules.rs index 696a74b..913e97e 100644 --- a/src/manifest/npm/nomodules.rs +++ b/src/manifest/npm/nomodules.rs @@ -16,5 +16,7 @@ pub struct NoModulesPackage { pub files: Vec, pub browser: String, #[serde(skip_serializing_if = "Option::is_none")] + pub homepage: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub types: Option, } From 835ce11023843859cd7439686bd8d7e535e5bf90 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 28 Jan 2019 03:53:50 +0900 Subject: [PATCH 10/11] add test case for copying 'homepage' field from Cargo.toml to package.json --- tests/all/manifest.rs | 57 +++++++++++++++++++++++++++++++++++++ tests/all/utils/manifest.rs | 1 + 2 files changed, 58 insertions(+) diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 1d631c8..b47174b 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -279,6 +279,63 @@ fn it_errors_when_wasm_bindgen_is_not_declared() { assert!(crate_data.check_crate_config(&step).is_err()); } +#[test] +fn it_sets_homepage_field_if_available_in_cargo_toml() { + // When 'homepage' is available + let fixture = utils::fixture::Fixture::new(); + fixture.hello_world_src_lib().file( + "Cargo.toml", + r#" + [package] + authors = ["The wasm-pack developers"] + description = "so awesome rust+wasm package" + license = "WTFPL" + name = "homepage-field-test" + repository = "https://github.com/rustwasm/wasm-pack.git" + version = "0.1.0" + homepage = "https://rustwasm.github.io/wasm-pack/" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "=0.2" + + [dev-dependencies] + wasm-bindgen-test = "=0.2" + "#, + ); + + let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(2); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, true, "", &step) + .unwrap(); + + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + assert_eq!( + pkg.homepage, + Some("https://rustwasm.github.io/wasm-pack/".to_string()), + ); + + // When 'homepage' is unavailable + let fixture = fixture::js_hello_world(); + let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(2); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, true, "", &step) + .unwrap(); + + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + assert_eq!(pkg.homepage, None); +} + #[test] fn it_does_not_error_when_wasm_bindgen_is_declared() { let fixture = fixture::js_hello_world(); diff --git a/tests/all/utils/manifest.rs b/tests/all/utils/manifest.rs index 2f811ec..2fce9d5 100644 --- a/tests/all/utils/manifest.rs +++ b/tests/all/utils/manifest.rs @@ -23,6 +23,7 @@ pub struct NpmPackage { pub types: String, #[serde(default = "default_none", rename = "sideEffects")] pub side_effects: String, + pub homepage: Option, } fn default_none() -> String { From 1e4ae144fd68a441aca304e091da1adae172639e Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 28 Jan 2019 08:22:22 -0500 Subject: [PATCH 11/11] Apply suggestions from code review Thanks! Co-Authored-By: chinedufn --- src/test/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index 7448567..8edecf0 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -14,7 +14,7 @@ pub fn cargo_test_wasm( path: &Path, release: bool, envs: I, - extra_options: &Vec, + extra_options: &[String], ) -> Result<(), failure::Error> where I: IntoIterator,