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/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..52913c0 --- /dev/null +++ b/docs/src/commands/test.md @@ -0,0 +1,84 @@ +# 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. + +`cargo test -h` for a list of all options that you can pass through. + +## Running only some tests + +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: + +``` +# Example directory structure +$ tree crates/foo +├── Cargo.toml +├── README.md +├── src +│   ├── diff +│   │   ├── diff_test_case.rs +│   │   └── mod.rs +│   ├── lib.rs +└── tests + ├── diff_patch.rs +``` + +``` +# 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 +``` diff --git a/docs/src/prerequisites/index.md b/docs/src/prerequisites/index.md index 6454a69..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](./rust.html) -- [npm](./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 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). diff --git a/src/build.rs b/src/build.rs index 9e29c6d..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."), } } @@ -95,6 +95,9 @@ pub fn cargo_build_wasm( } /// 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"); 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..e683030 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; @@ -18,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, @@ -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, }) } @@ -238,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/command/mod.rs b/src/command/mod.rs index 86a8235..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 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/'. diff --git a/src/command/test.rs b/src/command/test.rs index e0837b1..f07d4a9 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; @@ -77,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. @@ -95,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>; @@ -114,6 +120,7 @@ impl Test { geckodriver, safari, safaridriver, + extra_options, } = test_opts; let crate_path = set_crate_path(path)?; @@ -132,7 +139,7 @@ impl Test { } Ok(Test { - cache: Cache::new("wasm_pack")?, + cache: cache::get_wasm_pack_cache()?, crate_path, crate_data, node, @@ -146,6 +153,7 @@ impl Test { headless, release, test_runner_path: None, + extra_options, }) } @@ -305,6 +313,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(()) @@ -348,7 +357,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(()) } @@ -390,7 +399,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(()) } @@ -429,7 +438,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/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/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 92f03cd..a06b4ec 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -40,7 +40,10 @@ struct CargoPackage { name: String, description: Option, license: Option, + #[serde(rename = "license-file")] + license_file: Option, repository: Option, + homepage: Option, #[serde(default)] metadata: CargoMetadata, @@ -199,6 +202,7 @@ struct NpmData { files: Vec, dts_file: Option, main: String, + homepage: Option, // https://docs.npmjs.com/files/package.json#homepage } #[doc(hidden)] @@ -290,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 )); }); @@ -349,6 +353,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 { @@ -374,11 +383,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 +401,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,16 +427,43 @@ 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, files, main: js_file, + homepage: self.manifest.package.homepage.clone(), } } - fn to_commonjs(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, true, disable_dts); + 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, 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(); @@ -436,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 @@ -448,12 +485,18 @@ impl CrateData { }), files: data.files, main: data.main, + homepage: data.homepage, types: data.dts_file, }) } - 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(); @@ -463,7 +506,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 @@ -475,13 +518,19 @@ impl CrateData { }), files: data.files, module: data.main, + homepage: data.homepage, types: data.dts_file, side_effects: "false".to_string(), }) } - 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(); @@ -491,7 +540,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 @@ -503,6 +552,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, } 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..8edecf0 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -4,33 +4,33 @@ pub mod webdriver; use child; use failure::{self, ResultExt}; -use log::info; use std::ffi::OsStr; use std::path::Path; 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: &[String], +) -> Result<(), failure::Error> where I: IntoIterator, 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"); + 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. Ok(()) } diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index 54e2968..1dfe1a1 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.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, ); 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/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/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/manifest.rs b/tests/all/manifest.rs index 1d631c8..7638f7a 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, manifest, Cli}; +use wasm_pack::{self, license, manifest, readme}; #[test] fn it_gets_the_crate_name_default_path() { @@ -279,6 +277,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(); @@ -313,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] @@ -358,13 +406,76 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { debug-js-glue = true "#, ) - .hello_world_src_lib(); + .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.", + )); +} - let result = manifest::CrateData::parse_crate_data(&fixture.path.join("Cargo.toml")); +#[test] +fn it_lists_license_files_in_files_field_of_package_json() { + let fixture = fixture::dual_license(); + let out_dir = fixture.path.join("pkg"); - assert!(result.is_ok()); + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); - let manifest::ManifestAndUnsedKeys { unused_keys, .. } = result.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!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); + 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, + ); } diff --git a/tests/all/test.rs b/tests/all/test.rs index 278959a..dbe9532 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -1,41 +1,32 @@ -use failure::Error; +use assert_cmd::prelude::*; +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] @@ -75,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] @@ -113,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(); @@ -125,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] @@ -203,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] @@ -276,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] @@ -324,13 +281,54 @@ 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] +fn test_output_is_printed_once() { + let fixture = fixture::Fixture::new(); + fixture + .readme() + .cargo_toml("test-output-printed-once") + .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 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 + })); } diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index def8e8c..856fd21 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( @@ -218,9 +251,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 @@ -240,21 +278,14 @@ 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 { + 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, ()> { @@ -646,3 +677,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 +} 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 { 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();