From 6fc3f9b3493178330635f4f76ae31ad02ee45010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sat, 19 Jan 2019 13:31:02 +0100 Subject: [PATCH 01/24] doc: Fix links and Rust highlightning --- docs/src/prerequisites/index.md | 4 ++-- docs/src/tutorial/index.md | 4 ++-- docs/src/tutorial/template-deep-dive/src-utils-rs.md | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/src/prerequisites/index.md b/docs/src/prerequisites/index.md index 6454a69..bc91b02 100644 --- a/docs/src/prerequisites/index.md +++ b/docs/src/prerequisites/index.md @@ -2,8 +2,8 @@ To run `wasm-pack` you'll need to have both Rust and npm installed and configured. -- [Rust](./rust.html) -- [npm](./npm.html) +- [Rust](./prerequisites/rust.html) +- [npm](./prerequisites/npm.html) In the future, we intend to rewrite the npm registry client bits so that the need for a Node runtime is eliminated. If you're excited about that work- you should diff --git a/docs/src/tutorial/index.md b/docs/src/tutorial/index.md index 21df55c..75637ee 100644 --- a/docs/src/tutorial/index.md +++ b/docs/src/tutorial/index.md @@ -10,8 +10,8 @@ Be sure to have done the following before starting: 1. [Install `wasm-pack`](../../installer) 1. Read and install the [Prerequisites](../prerequisites/index.html). - - You'll need [Rust], version 1.30 or higher. (Currently either `beta` or `nightly` channels). [Learn more](../project-setup/rust.html). - - You'll need [Node.js] and [npm] installed. You'll also need an npm Account. [Learn more](../project-setup/npm.html). + - You'll need [Rust], version 1.30 or higher. (Currently either `beta` or `nightly` channels). [Learn more](../prerequisites/rust.html). + - You'll need [Node.js] and [npm] installed. You'll also need an npm Account. [Learn more](../prerequisites/npm.html). ⚠️ We strongly recommend that you install [Node.js] using a version manager. You can learn more [here](https://npmjs.com/get-npm). diff --git a/docs/src/tutorial/template-deep-dive/src-utils-rs.md b/docs/src/tutorial/template-deep-dive/src-utils-rs.md index 3ea5cb2..0ee271b 100644 --- a/docs/src/tutorial/template-deep-dive/src-utils-rs.md +++ b/docs/src/tutorial/template-deep-dive/src-utils-rs.md @@ -13,13 +13,13 @@ We will discuss: ## 1. Defining `set_panic_hook` -``` +```rust use cfg_if::cfg_if; ``` This allows us to write `cfg_if!` instead of `cfg_if::cfg_if!`, identically to the line in `src/lib.rs`. -``` +```rust cfg_if! { if #[cfg(feature = "console_error_panic_hook")] { extern crate console_error_panic_hook; @@ -35,14 +35,14 @@ As described in the preceding section, the macro `cfg_if!` evaluates the `if` st The entire macro block will either be replaced with the statements in the `if` block or with those in the `else` block. These two cases are now described in turn: -``` +```rust extern crate console_error_panic_hook; pub use self::console_error_panic_hook::set_once as set_panic_hook; ``` Due to the `use` statement, the function `self::console_error_panic_hook::set_once` can now be accessed more conveniently as `set_panic_hook`. Due to `pub`, this function will be publicly accessible outside of the `utils` module as `utils::set_panic_hook`. -``` +```rust #[inline] pub fn set_panic_hook() {} ``` @@ -63,4 +63,4 @@ To do this, a panic hook for WebAssembly is provided that logs panics to the dev Note that although the template sets up the function, your error messages will not automatically be enhanced. To enable the enhanced errors, call the function `utils::set_panic_hook()` in `lib.rs` when your code first runs. The function may be called multiple times if needed. -For more details, see the [`console_error_panic_hook` repository](https://github.com/rustwasm/console_error_panic_hook). \ No newline at end of file +For more details, see the [`console_error_panic_hook` repository](https://github.com/rustwasm/console_error_panic_hook). From ac746ef226794ac58629bb91b633c962e864eb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Sat, 19 Jan 2019 16:10:03 +0100 Subject: [PATCH 02/24] doc: Use absolute url in prerequisites index --- docs/src/prerequisites/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/prerequisites/index.md b/docs/src/prerequisites/index.md index bc91b02..b56901a 100644 --- a/docs/src/prerequisites/index.md +++ b/docs/src/prerequisites/index.md @@ -2,8 +2,8 @@ To run `wasm-pack` you'll need to have both Rust and npm installed and configured. -- [Rust](./prerequisites/rust.html) -- [npm](./prerequisites/npm.html) +- [Rust](/wasm-pack/book/prerequisites/rust.html) +- [npm](/wasm-pack/book/prerequisites/npm.html) In the future, we intend to rewrite the npm registry client bits so that the need for a Node runtime is eliminated. If you're excited about that work- you should From f5f9d40c9172399d0289c1eb76721a791fa83f89 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 23 Jan 2019 14:58:07 -0800 Subject: [PATCH 03/24] Only print `cargo test` output the once Also introduces testing of our CLI's output via `assert_cmd`. Expect some follow ups to get more of our testing infrastructure using this incredible crate! Fixes #511 --- Cargo.lock | 89 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/cache.rs | 14 ++++++ src/command/build.rs | 3 +- src/command/test.rs | 3 +- src/lib.rs | 1 + src/progressbar.rs | 7 +++ src/test/mod.rs | 24 ++++------ tests/all/main.rs | 2 + tests/all/test.rs | 42 ++++++++++++++++++ tests/all/utils/fixture.rs | 19 +++++++- 11 files changed, 187 insertions(+), 19 deletions(-) create mode 100644 src/cache.rs diff --git a/Cargo.lock b/Cargo.lock index 4ef603c..23f52ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,17 @@ dependencies = [ "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "assert_cmd" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "escargot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "atty" version = "0.2.11" @@ -289,6 +300,11 @@ dependencies = [ "tempfile 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dirs" version = "1.0.4" @@ -323,6 +339,15 @@ dependencies = [ "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "escargot" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "failure" version = "0.1.5" @@ -364,6 +389,14 @@ dependencies = [ "miniz_oxide_c_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "float-cmp" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "foreign-types" version = "0.3.2" @@ -562,6 +595,16 @@ name = "nodrop" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "normalize-line-endings" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num-traits" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "openssl" version = "0.10.16" @@ -668,6 +711,32 @@ name = "podio" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "predicates" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "float-cmp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "normalize-line-endings 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "predicates-core" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "predicates-tree" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.4.24" @@ -951,6 +1020,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "serde" version = "1.0.84" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "serde_derive" @@ -1173,6 +1245,11 @@ dependencies = [ "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "treeline" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ucd-util" version = "0.1.3" @@ -1249,6 +1326,7 @@ dependencies = [ name = "wasm-pack" version = "0.6.0" dependencies = [ + "assert_cmd 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "binary-install 0.0.1", "cargo_metadata 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1265,6 +1343,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.16 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", "serde_ignored 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1367,6 +1446,7 @@ dependencies = [ "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" +"checksum assert_cmd 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b7ac5c260f75e4e4ba87b7342be6edcecbcb3eb6741a0507fda7ad115845cc65" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4e5f34df7a019573fb8bdc7e24a2bfebe51a2a1d6bfdbaeccedb3c41fc574727" "checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" @@ -1392,14 +1472,17 @@ dependencies = [ "checksum curl 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "c7c9d851c825e0c033979d4516c9173bc19a78a96eb4d6ae51d4045440eafa16" "checksum curl-sys 0.4.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ca79238a79fb294be6173b4057c95b22a718c94c4e38475d5faa82b8383f3502" "checksum dialoguer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1ad1c29a0368928e78c551354dbff79f103a962ad820519724ef0d74f1c62fa9" +"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" +"checksum escargot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "19db1f7e74438642a5018cdf263bb1325b2e792f02dd0a3ca6d6c0f0d7b1d5a5" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum filetime 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a2df5c1a8c4be27e7707789dc42ae65976e60b394afd293d1419ab915833e646" "checksum flate2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2291c165c8e703ee54ef3055ad6188e3d51108e2ded18e9f2476e774fc5ad3d4" +"checksum float-cmp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "134a8fa843d80a51a5b77d36d42bc2def9edcb0262c914861d08129fd1926600" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" @@ -1425,6 +1508,8 @@ dependencies = [ "checksum miniz_oxide 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5ad30a47319c16cde58d0314f5d98202a80c9083b5f61178457403dfb14e509c" "checksum miniz_oxide_c_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28edaef377517fd9fe3e085c37d892ce7acd1fbeab9239c5a36eec352d8a8b7e" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" +"checksum normalize-line-endings 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2e0a1a39eab95caf4f5556da9289b9e68f0aafac901b2ce80daaf020d3b733a8" +"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum openssl 0.10.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ec7bd7ca4cce6dbdc77e7c1230682740d307d1218a87fb0349a571272be749f9" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" "checksum openssl-src 111.1.0+1.1.1a (registry+https://github.com/rust-lang/crates.io-index)" = "26bb632127731bf4ac49bf86a5dde12d2ca0918c2234fc39d79d4da2ccbc6da7" @@ -1437,6 +1522,9 @@ dependencies = [ "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum podio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "780fb4b6698bbf9cf2444ea5d22411cef2953f0824b98f33cf454ec5615645bd" +"checksum predicates 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa984b7cd021a0bf5315bcce4c4ae61d2a535db2a8d288fc7578638690a7b7c3" +"checksum predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178" +"checksum predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" @@ -1496,6 +1584,7 @@ dependencies = [ "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" +"checksum treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" diff --git a/Cargo.toml b/Cargo.toml index 14bdf5b..a031588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,8 @@ binary-install = { version = "0.0.1", path = "./binary-install" } walkdir = "2" [dev-dependencies] +assert_cmd = "0.10.2" +predicates = "1.0.0" tempfile = "3" [features] diff --git a/src/cache.rs b/src/cache.rs new file mode 100644 index 0000000..4e3c2d0 --- /dev/null +++ b/src/cache.rs @@ -0,0 +1,14 @@ +//! Getting and configuring wasm-pack's binary cache. + +use binary_install::Cache; +use std::env; +use std::path::Path; + +/// Get wasm-pack's binary cache. +pub fn get_wasm_pack_cache() -> Result { + if let Ok(path) = env::var("WASM_PACK_CACHE") { + Ok(Cache::at(Path::new(&path))) + } else { + Cache::new("wasm-pack") + } +} diff --git a/src/command/build.rs b/src/command/build.rs index c6bad24..ee59a75 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -3,6 +3,7 @@ use binary_install::{Cache, Download}; use bindgen; use build; +use cache; use command::utils::{create_pkg_dir, set_crate_path}; use emoji; use failure::Error; @@ -180,7 +181,7 @@ impl Build { mode: build_opts.mode, out_dir, bindgen: None, - cache: Cache::new("wasm_pack")?, + cache: cache::get_wasm_pack_cache()?, extra_options: build_opts.extra_options, }) } diff --git a/src/command/test.rs b/src/command/test.rs index e0837b1..ff2f348 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -4,6 +4,7 @@ use super::build::BuildMode; use binary_install::Cache; use bindgen; use build; +use cache; use command::utils::set_crate_path; use console::style; use emoji; @@ -132,7 +133,7 @@ impl Test { } Ok(Test { - cache: Cache::new("wasm_pack")?, + cache: cache::get_wasm_pack_cache()?, crate_path, crate_data, node, diff --git a/src/lib.rs b/src/lib.rs index 164988e..97b136f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ extern crate walkdir; pub mod bindgen; pub mod build; +pub mod cache; pub mod child; pub mod command; pub mod emoji; diff --git a/src/progressbar.rs b/src/progressbar.rs index 24a8947..f0c0e6a 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -43,6 +43,13 @@ impl ProgressOutput { let mut spinner = self.spinner.write(); *spinner = Self::progressbar(message); + + if !atty::is(atty::Stream::Stderr) { + // `indicatif` won't print any output if `stderr` is not a tty, so + // to ensure that our output is still emitted, we print it manually + // here. + eprintln!("{}", message) + } } fn add_message(&self, msg: &str) { diff --git a/src/test/mod.rs b/src/test/mod.rs index 0ec11e9..77eff43 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -4,7 +4,6 @@ pub mod webdriver; use child; use failure::{self, ResultExt}; -use log::info; use std::ffi::OsStr; use std::path::Path; use std::process::Command; @@ -17,20 +16,15 @@ where K: AsRef, V: AsRef, { - let output = { - let mut cmd = Command::new("cargo"); - cmd.envs(envs); - cmd.current_dir(path).arg("test"); - if release { - cmd.arg("--release"); - } - cmd.arg("--target").arg("wasm32-unknown-unknown"); - child::run(cmd, "cargo test").context("Running Wasm tests with wasm-bindgen-test failed")? - }; - - for line in output.lines() { - info!("test output: {}", line); - println!("{}", line); + let mut cmd = Command::new("cargo"); + cmd.envs(envs); + cmd.current_dir(path).arg("test"); + if release { + cmd.arg("--release"); } + cmd.arg("--target").arg("wasm32-unknown-unknown"); + 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. Ok(()) } diff --git a/tests/all/main.rs b/tests/all/main.rs index 06806cd..a01018b 100644 --- a/tests/all/main.rs +++ b/tests/all/main.rs @@ -1,4 +1,6 @@ +extern crate assert_cmd; extern crate failure; +extern crate predicates; #[macro_use] extern crate lazy_static; #[macro_use] diff --git a/tests/all/test.rs b/tests/all/test.rs index 278959a..b438fcd 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -1,4 +1,6 @@ +use assert_cmd::prelude::*; use failure::Error; +use predicates::prelude::*; use std::env; use utils::fixture; use wasm_pack::command::{build, test, Command}; @@ -334,3 +336,43 @@ fn cdylib_not_required() { }); fixture.run(cmd).unwrap(); } + +#[test] +fn test_output_is_printed_once() { + let fixture = fixture::Fixture::new(); + fixture + .readme() + .cargo_toml("wbg-test-node") + .hello_world_src_lib() + .file( + "tests/node.rs", + r#" + extern crate wasm_bindgen; + extern crate wasm_bindgen_test; + use wasm_bindgen::prelude::*; + use wasm_bindgen_test::*; + + #[wasm_bindgen] + extern { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + } + + #[wasm_bindgen_test] + fn pass() { + log("YABBA DABBA DOO"); + assert_eq!(1, 2); + } + "#, + ); + + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .stderr(predicate::function(|err: &str| { + err.matches("YABBA DABBA DOO").count() == 1 + })) + .failure(); +} diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index def8e8c..3eee609 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -218,9 +218,14 @@ impl Fixture { wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap() } + pub fn cache_dir(&self) -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("target") + .join("test_cache") + } + pub fn cache(&self) -> Cache { - let target_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("target"); - Cache::at(&target_dir.join("test_cache")) + Cache::at(&self.cache_dir()) } /// The `step_install_wasm_bindgen` and `step_run_wasm_bindgen` steps only @@ -257,6 +262,16 @@ impl Fixture { } } + /// Get a `wasm-pack` command configured to run in this fixure's temp + /// directory and using the test cache. + pub fn wasm_pack(&self) -> Command { + use assert_cmd::prelude::*; + let mut cmd = Command::main_binary().unwrap(); + cmd.current_dir(&self.path); + cmd.env("WASM_PACK_CACHE", self.cache_dir()); + cmd + } + pub fn lock(&self) -> MutexGuard<'static, ()> { use std::sync::Mutex; lazy_static! { From fee93bc9651ed6b9d266d928144b97049ec5c599 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 03:15:36 +0900 Subject: [PATCH 04/24] 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 05/24] 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 06/24] 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 6dbcba22e8cfb30cccb1920829014db6d86c2e6d Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 14:25:03 +0900 Subject: [PATCH 07/24] include README.md and license files in 'files' field of package.json --- src/command/build.rs | 10 +++++----- src/manifest/mod.rs | 46 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index ee59a75..76d0a61 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -239,29 +239,29 @@ impl Build { step_add_wasm_target, step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, step_install_wasm_bindgen, step_run_wasm_bindgen, + step_create_json, ], BuildMode::Noinstall => steps![ step_check_rustc_version, step_check_crate_config, step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, - step_run_wasm_bindgen + step_run_wasm_bindgen, + step_create_json, ], BuildMode::Force => steps![ step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, - step_run_wasm_bindgen + step_run_wasm_bindgen, + step_create_json, ], } } diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 92f03cd..9b0762e 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -374,11 +374,11 @@ impl CrateData { PBAR.step(step, &msg); let pkg_file_path = out_dir.join("package.json"); let npm_data = if target == "nodejs" { - self.to_commonjs(scope, disable_dts) + self.to_commonjs(scope, disable_dts, out_dir) } else if target == "no-modules" { - self.to_nomodules(scope, disable_dts) + self.to_nomodules(scope, disable_dts, out_dir) } else { - self.to_esmodules(scope, disable_dts) + self.to_esmodules(scope, disable_dts, out_dir) }; let npm_json = serde_json::to_string_pretty(&npm_data)?; @@ -392,6 +392,7 @@ impl CrateData { scope: &Option, include_commonjs_shim: bool, disable_dts: bool, + out_dir: &Path, ) -> NpmData { let crate_name = self.crate_name(); let wasm_file = format!("{}_bg.wasm", crate_name); @@ -417,6 +418,23 @@ impl CrateData { } else { None }; + + let readme_file = out_dir.join("README.md"); + if readme_file.is_file() { + files.push("README.md".to_string()); + } + + if let Ok(entries) = fs::read_dir(out_dir) { + let file_names = entries + .filter_map(|e| e.ok()) + .filter(|e| e.metadata().map(|m| m.is_file()).unwrap_or(false)) + .filter_map(|e| e.file_name().into_string().ok()) + .filter(|f| f.starts_with("LICENSE")); + for file_name in file_names { + files.push(file_name); + } + } + NpmData { name: npm_name, dts_file, @@ -425,8 +443,8 @@ impl CrateData { } } - fn to_commonjs(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, true, disable_dts); + fn to_commonjs(&self, scope: &Option, disable_dts: bool, out_dir: &Path) -> NpmPackage { + let data = self.npm_data(scope, true, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); @@ -452,8 +470,13 @@ impl CrateData { }) } - fn to_esmodules(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, false, disable_dts); + fn to_esmodules( + &self, + scope: &Option, + disable_dts: bool, + out_dir: &Path, + ) -> NpmPackage { + let data = self.npm_data(scope, false, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); @@ -480,8 +503,13 @@ impl CrateData { }) } - fn to_nomodules(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, false, disable_dts); + fn to_nomodules( + &self, + scope: &Option, + disable_dts: bool, + out_dir: &Path, + ) -> NpmPackage { + let data = self.npm_data(scope, false, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); From e724b0136563d46eed8e5d7dfa52c91ae8d72eac Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 27 Jan 2019 15:14:54 +0900 Subject: [PATCH 08/24] add tests to check README and licenses are in 'files' field --- tests/all/manifest.rs | 63 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 1d631c8..8373113 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use structopt::StructOpt; use utils::{self, fixture}; -use wasm_pack::{self, manifest, Cli}; +use wasm_pack::{self, license, manifest, readme, Cli}; #[test] fn it_gets_the_crate_name_default_path() { @@ -368,3 +368,64 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); } + +#[test] +fn it_lists_license_files_in_files_field_of_package_json() { + let fixture = fixture::dual_license(); + let out_dir = fixture.path.join("pkg"); + + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(3); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + license::copy_from_crate(&crate_data, &fixture.path, &out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .unwrap(); + + let package_json_path = &fixture.path.join("pkg").join("package.json"); + fs::metadata(package_json_path).unwrap(); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + + assert!( + pkg.files.contains(&"LICENSE-WTFPL".to_string()), + "LICENSE-WTFPL is not in files: {:?}", + pkg.files, + ); + + assert!( + pkg.files.contains(&"LICENSE-MIT".to_string()), + "LICENSE-MIT is not in files: {:?}", + pkg.files, + ); +} + +#[test] +fn it_lists_readme_in_files_field_of_package_json() { + let fixture = utils::fixture::Fixture::new(); + fixture + .readme() + .hello_world_src_lib() + .cargo_toml("readme-test-for-package-json"); + + let out_dir = fixture.path.join("pkg"); + + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(3); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + readme::copy_from_crate(&fixture.path, &out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .unwrap(); + + let package_json_path = &fixture.path.join("pkg").join("package.json"); + fs::metadata(package_json_path).unwrap(); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + + assert!( + pkg.files.contains(&"README.md".to_string()), + "README.md is not in files: {:?}", + pkg.files, + ); +} From b72051d385fe178cbb95865942bc42b983665a63 Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Sun, 27 Jan 2019 10:08:56 -0500 Subject: [PATCH 09/24] 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 10/24] 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 11/24] 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 12/24] 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 13/24] 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 14/24] 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 15/24] 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 16/24] 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, From f54de0a06750cfeeb58438cf6a91821d1691030c Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Tue, 29 Jan 2019 07:26:40 -0500 Subject: [PATCH 17/24] Improve test docs --- docs/src/commands/test.md | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/docs/src/commands/test.md b/docs/src/commands/test.md index ec8663f..56b21fd 100644 --- a/docs/src/commands/test.md +++ b/docs/src/commands/test.md @@ -47,10 +47,39 @@ 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". +`cargo test -h` for a list of all options that you can pass through. + +## Running only some tests + +Are your test suite grows it may become unrealistic to need to run all of your tests in a browser when you're +really only debugging one or a few of them. + +Here are a few examples of how to run a subset of your tests. ``` -wasm-pack test --firefox --headless -- --manifest-path=crates/my-workspace-crate/Cargo.toml apple +# Example directory structure +$ tree crates/foo +├── Cargo.toml +├── README.md +├── src +│   ├── diff +│   │   ├── diff_test_case.rs +│   │   └── mod.rs +│   ├── lib.rs +└── tests + ├── diff_patch.rs ``` -`cargo test -h` for a list of all options that you can pass through. +``` +# Run all tests in tests/diff_patch.rs +wasm-pack test crates/foo --firefox --headless -- --tests diff_patch + +# Run all tests in tests/diff_patch.rs that contain the word "replace" +wasm-pack test crates/foo --firefox --headless -- --tests diff_patch replace + +# Run all tests inside of a `tests` module inside of src/lib/diff.rs +wasm-pack test crates/foo --firefox --headless -- --lib diff::tests + +# Same as the above, but only if they contain the word replace +wasm-pack test crates/foo --firefox --headless -- --lib diff::tests::replace +``` From 5757744ffa60b3e9cf9e79881bc8185dab5b4c7e Mon Sep 17 00:00:00 2001 From: Chinedu Francis Nwafili Date: Tue, 29 Jan 2019 09:49:02 -0500 Subject: [PATCH 18/24] Clean up subset of test docs --- docs/src/commands/test.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/src/commands/test.md b/docs/src/commands/test.md index 56b21fd..52913c0 100644 --- a/docs/src/commands/test.md +++ b/docs/src/commands/test.md @@ -51,10 +51,9 @@ end of your command, and all the arguments you want to pass to cargo should go a ## Running only some tests -Are your test suite grows it may become unrealistic to need to run all of your tests in a browser when you're -really only debugging one or a few of them. +When debugging a specific issue, you may find yourself wanting to run a subset of tests, instead of your entire suite of tests. -Here are a few examples of how to run a subset of your tests. +Here are a few examples of how to run a subset of your tests: ``` # Example directory structure From 5637b0757776ae85b8e51b9e23ca0143c95a2544 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 23 Jan 2019 16:19:45 -0800 Subject: [PATCH 19/24] tests: Port all of our tests to use `assert_cmd` --- src/manifest/mod.rs | 2 +- tests/all/build.rs | 123 +++++++------------ tests/all/manifest.rs | 51 ++++---- tests/all/test.rs | 238 +++++++++++++++---------------------- tests/all/utils/fixture.rs | 17 --- 5 files changed, 166 insertions(+), 265 deletions(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index ab5ba74..a06b4ec 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -294,7 +294,7 @@ impl CrateData { pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) { manifest_and_keys.unused_keys.iter().for_each(|path| { PBAR.warn(&format!( - "\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.", + "\"{}\" is an unknown key and will be ignored. Please check your Cargo.toml.", path )); }); diff --git a/tests/all/build.rs b/tests/all/build.rs index d4f4deb..be6e191 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -1,40 +1,24 @@ +use assert_cmd::prelude::*; use std::fs; use std::path::Path; -use structopt::StructOpt; use utils; -use wasm_pack::Cli; #[test] fn build_in_non_crate_directory_doesnt_panic() { let fixture = utils::fixture::not_a_crate(); - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.display().to_string(), - ]) - .unwrap(); - let result = fixture.run(cli.cmd); - assert!( - result.is_err(), - "running wasm-pack in a non-crate directory should fail, but it should not panic" - ); - let err = result.unwrap_err(); - assert!(err - .iter_chain() - .any(|e| e.to_string().contains("missing a `Cargo.toml`"))); + fixture + .wasm_pack() + .arg("build") + .assert() + .failure() + .stderr(predicates::str::contains("missing a `Cargo.toml`")); } #[test] fn it_should_build_js_hello_world_example() { let fixture = utils::fixture::js_hello_world(); fixture.install_local_wasm_bindgen(); - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.display().to_string(), - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + fixture.wasm_pack().arg("build").assert().success(); } #[test] @@ -75,15 +59,14 @@ fn it_should_build_crates_in_a_workspace() { #[wasm_bindgen] pub fn hello() -> u32 { 42 } "#, - ); - fixture.install_local_wasm_bindgen(); - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.join("blah").display().to_string(), - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + ) + .install_local_wasm_bindgen(); + fixture + .wasm_pack() + .current_dir(&fixture.path.join("blah")) + .arg("build") + .assert() + .success(); } #[test] @@ -116,28 +99,21 @@ fn renamed_crate_name_works() { #[wasm_bindgen] pub fn one() -> u32 { 1 } "#, - ); - fixture.install_local_wasm_bindgen(); - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.display().to_string(), - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + ) + .install_local_wasm_bindgen(); + fixture.wasm_pack().arg("build").assert().success(); } #[test] fn it_should_build_nested_project_with_transitive_dependencies() { let fixture = utils::fixture::transitive_dependencies(); fixture.install_local_wasm_bindgen(); - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.join("main").display().to_string(), - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + fixture + .wasm_pack() + .current_dir(fixture.path.join("main")) + .arg("build") + .assert() + .success(); } #[test] @@ -149,14 +125,12 @@ fn build_different_profiles() { .iter() .cloned() { - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - profile, - &fixture.path.display().to_string(), - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + fixture + .wasm_pack() + .arg("build") + .arg(profile) + .assert() + .success(); } } @@ -208,17 +182,15 @@ fn build_with_and_without_wasm_bindgen_debug() { pub fn take(self) {} } "#, - ); - - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - "--dev", - &fixture.path.display().to_string(), - ]) - .unwrap(); + ) + .install_local_wasm_bindgen(); - fixture.run(cli.cmd).unwrap(); + fixture + .wasm_pack() + .arg("build") + .arg("--dev") + .assert() + .success(); let contents = fs::read_to_string(fixture.path.join("pkg/whatever.js")).unwrap(); assert_eq!( @@ -233,14 +205,11 @@ fn build_with_and_without_wasm_bindgen_debug() { fn build_with_arbitrary_cargo_options() { let fixture = utils::fixture::js_hello_world(); fixture.install_local_wasm_bindgen(); - - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - &fixture.path.display().to_string(), - "--", - "--no-default-features", - ]) - .unwrap(); - fixture.run(cli.cmd).unwrap(); + fixture + .wasm_pack() + .arg("build") + .arg("--") + .arg("--no-default-features") + .assert() + .success(); } diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 7105794..30e2547 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -1,11 +1,9 @@ +use assert_cmd::prelude::*; use std::collections::HashSet; use std::fs; use std::path::PathBuf; - -use structopt::StructOpt; - use utils::{self, fixture}; -use wasm_pack::{self, license, manifest, readme, Cli}; +use wasm_pack::{self, license, manifest, readme}; #[test] fn it_gets_the_crate_name_default_path() { @@ -370,22 +368,15 @@ fn configure_wasm_bindgen_debug_incorrectly_is_error() { debug-js-glue = "not a boolean" "#, ); - - let cli = Cli::from_iter_safe(vec![ - "wasm-pack", - "build", - "--dev", - &fixture.path.display().to_string(), - ]) - .unwrap(); - - let result = fixture.run(cli.cmd); - assert!(result.is_err()); - - let err = result.unwrap_err(); - assert!(err.iter_chain().any(|c| c - .to_string() - .contains("package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug"))); + fixture + .wasm_pack() + .arg("build") + .arg("--dev") + .assert() + .failure() + .stderr(predicates::str::contains( + "package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug", + )); } #[test] @@ -415,15 +406,17 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { debug-js-glue = true "#, ) - .hello_world_src_lib(); - - let result = manifest::CrateData::parse_crate_data(&fixture.path.join("Cargo.toml")); - - assert!(result.is_ok()); - - let manifest::ManifestAndUnsedKeys { unused_keys, .. } = result.unwrap(); - - assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); + .hello_world_src_lib() + .install_local_wasm_bindgen(); + fixture + .wasm_pack() + .arg("build") + .assert() + .success() + .stdout(predicates::str::contains( + "[WARN]: \"package.metadata.wasm-pack.profile.production\" is an unknown key and will \ + be ignored. Please check your Cargo.toml." + )); } #[test] diff --git a/tests/all/test.rs b/tests/all/test.rs index b438fcd..dbe9532 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -1,43 +1,32 @@ use assert_cmd::prelude::*; -use failure::Error; use predicates::prelude::*; use std::env; use utils::fixture; -use wasm_pack::command::{build, test, Command}; - -fn assert_err(result: Result, msg: &str) -> Error { - let error = result.err().expect("should have failed"); - for e in error.iter_chain() { - println!("err: {}", e); - } - assert!(error.iter_chain().any(|e| e.to_string().contains(msg))); - error -} #[test] fn it_can_run_node_tests() { let fixture = fixture::wbg_test_node(); fixture.install_local_wasm_bindgen(); - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - fixture.run(cmd).unwrap(); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .success(); } #[test] fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() { let fixture = fixture::wbg_test_diff_versions(); fixture.install_local_wasm_bindgen(); - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - fixture.run(cmd).unwrap(); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .success(); } #[test] @@ -77,33 +66,37 @@ fn it_can_run_browser_tests() { return; } - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - firefox, - chrome, - safari, - headless: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - - fixture.run(cmd).unwrap(); + let mut cmd = fixture.wasm_pack(); + cmd.arg("test").arg("--headless"); + + if firefox { + cmd.arg("--firefox"); + } + if chrome { + cmd.arg("--chrome"); + } + if safari { + cmd.arg("--safari"); + } + + let _lock = fixture.lock(); + cmd.assert().success(); } #[test] fn it_can_run_failing_tests() { let fixture = fixture::wbg_test_fail(); fixture.install_local_wasm_bindgen(); - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - assert_err( - fixture.run(cmd), - "Running Wasm tests with wasm-bindgen-test failed", - ); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .failure() + .stderr(predicates::str::contains( + "Running Wasm tests with wasm-bindgen-test failed", + )); } #[test] @@ -115,8 +108,6 @@ fn it_can_run_failing_tests() { all(target_os = "windows", target_arch = "x86_64") ))] fn it_can_find_a_webdriver_on_path() { - use std::process::Command; - let fixture = fixture::wbg_test_browser(); let local_geckodriver = fixture.install_local_geckodriver(); let local_wasm_bindgen = fixture.install_local_wasm_bindgen(); @@ -127,54 +118,44 @@ fn it_can_find_a_webdriver_on_path() { let path = env::join_paths(paths).unwrap(); let _lock = fixture.lock(); - - let mut me = env::current_exe().unwrap(); - me.pop(); - me.pop(); - me.push("wasm-pack"); - let output = Command::new(&me) + fixture + .wasm_pack() + .env("PATH", &path) .arg("test") .arg("--firefox") .arg("--headless") .arg("--mode") .arg("no-install") - .env("PATH", &path) - .arg(&fixture.path) - .output() - .unwrap(); - println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); - println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); - println!("status: {}", output.status); - assert!(output.status.success()); + .assert() + .success(); } #[test] fn it_requires_node_or_a_browser() { let fixture = fixture::wbg_test_node(); fixture.install_local_wasm_bindgen(); - - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - mode: build::BuildMode::Noinstall, - // Note: not setting node or any browser to true here. - ..Default::default() - }); - assert_err(fixture.run(cmd), "Must specify at least one of"); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .assert() + .failure() + .stderr(predicates::str::contains("Must specify at least one of")); } #[test] fn the_headless_flag_requires_a_browser() { let fixture = fixture::wbg_test_node(); fixture.install_local_wasm_bindgen(); - - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - headless: true, - ..Default::default() - }); - assert_err(fixture.run(cmd), "only applies to browser tests"); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .arg("--headless") + .assert() + .failure() + .stderr(predicates::str::contains("only applies to browser tests")); } #[test] @@ -205,44 +186,18 @@ fn complains_about_missing_wasm_bindgen_test_dependency() { ) .hello_world_src_lib() .install_local_wasm_bindgen(); - - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - let error = assert_err(fixture.run(cmd), "Ensure that you have"); - - // Test that the error message has two occurrences of "wasm-bindgen-test" in - // it. I am surprised to learn there is no `str` method to count - // occurrences, so we find the first and the last and assert that they - // aren't the same occurrence. - // - // This should protect against regresstions where we said: - // - // Ensure that you have "wasm-bindgen" as a dependency in your Cargo.toml file: - // [dev-dependencies] - // wasm-bindgen-test = "0.2" - // - // instead of - // - // Ensure that you have "wasm-bindgen-test" as a dependency in your Cargo.toml file: - // [dev-dependencies] - // wasm-bindgen-test = "0.2" - // - // Note that the whole reason we are doing this string manipulation instead - // of just doing `assert_eq!` is because the first occurrence of the - // dependency name is bolded with terminal escape codes and if I try to pipe - // the output to a text file, then the escape codes go away, so I can't - // figure out which exact escape codes are even used here. - - let err_msg = error.to_string(); - let first = err_msg.find("wasm-bindgen-test"); - assert!(first.is_some()); - let second = err_msg.rfind("wasm-bindgen-test"); - assert!(second.is_some()); - assert_ne!(first, second, "should have found two occurrences"); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .failure() + .stderr(predicates::str::contains( + "Ensure that you have \"wasm-bindgen-test\" as a dependency in your Cargo.toml file", + )) + .stderr(predicates::str::contains("[dev-dependencies]")) + .stderr(predicates::str::contains("wasm-bindgen-test = \"0.2\"")); } #[test] @@ -278,15 +233,15 @@ fn renamed_crate_name_works() { #[wasm_bindgen] pub fn one() -> u32 { 1 } "#, - ); - fixture.install_local_wasm_bindgen(); - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - fixture.run(cmd).unwrap(); + ) + .install_local_wasm_bindgen(); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .success(); } #[test] @@ -326,15 +281,15 @@ fn cdylib_not_required() { foo::foo(); } "#, - ); - fixture.install_local_wasm_bindgen(); - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - node: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - fixture.run(cmd).unwrap(); + ) + .install_local_wasm_bindgen(); + let _lock = fixture.lock(); + fixture + .wasm_pack() + .arg("test") + .arg("--node") + .assert() + .success(); } #[test] @@ -342,7 +297,7 @@ fn test_output_is_printed_once() { let fixture = fixture::Fixture::new(); fixture .readme() - .cargo_toml("wbg-test-node") + .cargo_toml("test-output-printed-once") .hello_world_src_lib() .file( "tests/node.rs", @@ -359,20 +314,21 @@ fn test_output_is_printed_once() { } #[wasm_bindgen_test] - fn pass() { + fn yabba() { log("YABBA DABBA DOO"); assert_eq!(1, 2); } "#, - ); - + ) + .install_local_wasm_bindgen(); + let _lock = fixture.lock(); fixture .wasm_pack() .arg("test") .arg("--node") .assert() + .failure() .stderr(predicate::function(|err: &str| { err.matches("YABBA DABBA DOO").count() == 1 - })) - .failure(); + })); } diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index f157b9b..856fd21 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -278,23 +278,6 @@ impl Fixture { self } - pub fn run(&self, cmd: wasm_pack::command::Command) -> Result<(), failure::Error> { - match cmd { - wasm_pack::command::Command::Test(cmd) => { - let _lock = self.lock(); - let mut test = wasm_pack::command::test::Test::try_from_opts(cmd)?; - test.set_cache(self.cache()); - test.run() - } - wasm_pack::command::Command::Build(cmd) => { - let mut build = wasm_pack::command::build::Build::try_from_opts(cmd)?; - build.set_cache(self.cache()); - build.run() - } - _ => unreachable!(), - } - } - /// Get a `wasm-pack` command configured to run in this fixure's temp /// directory and using the test cache. pub fn wasm_pack(&self) -> Command { From b0f47b960e7a168d7ec9a921cdbd28a86099ab43 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Tue, 29 Jan 2019 10:28:22 -0500 Subject: [PATCH 20/24] fix(style): appease cargo fmt --- tests/all/manifest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 30e2547..7638f7a 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -415,7 +415,7 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { .success() .stdout(predicates::str::contains( "[WARN]: \"package.metadata.wasm-pack.profile.production\" is an unknown key and will \ - be ignored. Please check your Cargo.toml." + be ignored. Please check your Cargo.toml.", )); } From cd8ee977d517dd1b3edcf350682a212763333579 Mon Sep 17 00:00:00 2001 From: Jonas Scheffner Date: Mon, 4 Feb 2019 15:31:04 +0100 Subject: [PATCH 21/24] Use win32 chromedriver binaries for win64 systems, update chromedriver version to 2.45 and fix error message --- src/test/webdriver.rs | 6 +++--- tests/all/webdriver.rs | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index 54e2968..e15ae80 100644 --- a/src/test/webdriver.rs +++ b/src/test/webdriver.rs @@ -31,14 +31,14 @@ pub fn install_chromedriver( "linux64" } else if target::MACOS && target::x86_64 { "mac64" - } else if target::WINDOWS && target::x86 { + } else if target::WINDOWS { "win32" } else { - bail!("geckodriver binaries are unavailable for this target") + bail!("chromedriver binaries are unavailable for this target") }; let url = format!( - "https://chromedriver.storage.googleapis.com/2.41/chromedriver_{}.zip", + "https://chromedriver.storage.googleapis.com/2.45/chromedriver_{}.zip", target ); match cache.download( diff --git a/tests/all/webdriver.rs b/tests/all/webdriver.rs index d5e8cf6..6223290 100644 --- a/tests/all/webdriver.rs +++ b/tests/all/webdriver.rs @@ -6,7 +6,8 @@ use wasm_pack::test::webdriver; #[cfg(any( 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"), + all(target_os = "windows", target_arch = "x86_64") ))] fn can_install_chromedriver() { let fixture = fixture::js_hello_world(); From d80406173b20ad768bd39630fee9bd9477b71d15 Mon Sep 17 00:00:00 2001 From: Jonas Scheffner Date: Mon, 4 Feb 2019 17:33:06 +0100 Subject: [PATCH 22/24] Update chromedriver to v0.46 and geckodriver to v0.24.0 --- src/test/webdriver.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index e15ae80..1dfe1a1 100644 --- a/src/test/webdriver.rs +++ b/src/test/webdriver.rs @@ -38,7 +38,7 @@ pub fn install_chromedriver( }; let url = format!( - "https://chromedriver.storage.googleapis.com/2.45/chromedriver_{}.zip", + "https://chromedriver.storage.googleapis.com/2.46/chromedriver_{}.zip", target ); match cache.download( @@ -92,7 +92,7 @@ pub fn install_geckodriver( }; let url = format!( - "https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-{}.{}", + "https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-{}.{}", target, ext, ); From bec91efbeaa32b2040f9406b4f06c4e585d1e1dd Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Tue, 5 Feb 2019 14:57:21 +0100 Subject: [PATCH 23/24] Clarify what kind of account `login` adds --- src/command/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command/mod.rs b/src/command/mod.rs index 86a8235..5a56e93 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -50,7 +50,7 @@ pub enum Command { }, #[structopt(name = "login", alias = "adduser", alias = "add-user")] - /// 👤 Add a registry user account! (aliases: adduser, add-user) + /// 👤 Add a npm registry user account! (aliases: adduser, add-user) Login { #[structopt(long = "registry", short = "r")] /// Default: 'https://registry.npmjs.org/'. From 73ed2b83f7b750499b1d0c4ad5fd3fbe27e88aaa Mon Sep 17 00:00:00 2001 From: ashley williams Date: Tue, 5 Feb 2019 15:29:07 +0100 Subject: [PATCH 24/24] Update mod.rs --- src/command/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command/mod.rs b/src/command/mod.rs index 5a56e93..c239b09 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -50,7 +50,7 @@ pub enum Command { }, #[structopt(name = "login", alias = "adduser", alias = "add-user")] - /// 👤 Add a npm registry user account! (aliases: adduser, add-user) + /// 👤 Add an npm registry user account! (aliases: adduser, add-user) Login { #[structopt(long = "registry", short = "r")] /// Default: 'https://registry.npmjs.org/'.