Merge branch 'master' into refactor-binary-installation

master
Jesper Håkansson 7 years ago
commit b15f062906
  1. 18
      .travis.yml
  2. 128
      Cargo.lock
  3. 3
      Cargo.toml
  4. 4
      binary-install/Cargo.toml
  5. 344
      binary-install/src/lib.rs
  6. 216
      src/bindgen.rs
  7. 2
      src/build.rs
  8. 3
      src/child.rs
  9. 44
      src/command/build.rs
  10. 49
      src/command/test.rs
  11. 13
      src/error.rs
  12. 3
      src/lib.rs
  13. 40
      src/lockfile.rs
  14. 2
      src/main.rs
  15. 436
      src/manifest/mod.rs
  16. 6
      src/test/mod.rs
  17. 138
      src/test/webdriver.rs
  18. 33
      tests/all/bindgen.rs
  19. 106
      tests/all/build.rs
  20. 13
      tests/all/lockfile.rs
  21. 2
      tests/all/main.rs
  22. 107
      tests/all/manifest.rs
  23. 151
      tests/all/test.rs
  24. 164
      tests/all/utils/fixture.rs
  25. 7
      tests/all/webdriver.rs

@ -38,8 +38,8 @@ DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB
matrix:
include:
# tests pass
- env: JOB=test RUST_BACKTRACE=1
- name: Tests
env: RUST_BACKTRACE=1
rust: nightly
addons:
firefox: latest
@ -49,14 +49,14 @@ matrix:
script:
- cargo test --locked
- rustup component add rustfmt-preview
- cargo fmt --version
- cargo fmt --all -- --check
# book
- env: JOB=book
- name: Book
rust: stable
before_script:
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
- (test -x $HOME/.cargo/bin/mdbook || cargo install --vers "^0.1" mdbook)
- (test -x $HOME/.cargo/bin/mdbook || cargo install --vers "^0.2" mdbook)
- cargo install-update -a
script:
- (cd docs && mdbook build)
@ -71,8 +71,8 @@ matrix:
on:
branch: master
# dist linux binary
- env: JOB=dist-linux TARGET=x86_64-unknown-linux-musl
- name: Linux Binary
env: TARGET=x86_64-unknown-linux-musl
rust: nightly
before_script: rustup target add $TARGET
script: cargo build --release --target $TARGET --locked --features vendored-openssl
@ -82,8 +82,8 @@ matrix:
- musl-tools
<<: *DEPLOY_TO_GITHUB
# dist OSX binary
- env: JOB=dist-osx MACOSX_DEPLOYMENT_TARGET=10.7 TARGET=x86_64-apple-darwin
- name: macOS Binary
env: MACOSX_DEPLOYMENT_TARGET=10.7 TARGET=x86_64-apple-darwin
os: osx
rust: nightly
script: cargo build --release --target $TARGET --locked

128
Cargo.lock generated

@ -19,6 +19,23 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "argon2rs"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "arrayvec"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.11"
@ -55,8 +72,11 @@ name = "binary-install"
version = "0.1.0"
dependencies = [
"curl 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -70,6 +90,15 @@ name = "bitflags"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "blake2-rfc"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "build_const"
version = "0.2.1"
@ -179,6 +208,11 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "constant_time_eq"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "crc"
version = "1.8.1"
@ -216,6 +250,16 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dirs"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "error-chain"
version = "0.12.0"
@ -240,7 +284,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.16 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)",
"synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -291,6 +335,19 @@ name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "heck"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hex"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "human-panic"
version = "1.0.1"
@ -428,6 +485,11 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nodrop"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num-integer"
version = "0.1.39"
@ -590,6 +652,17 @@ dependencies = [
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_users"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.2.11"
@ -665,6 +738,11 @@ dependencies = [
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scoped_threadpool"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "scopeguard"
version = "0.3.3"
@ -696,7 +774,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.16 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -709,6 +787,11 @@ dependencies = [
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "siphasher"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "slog"
version = "2.4.1"
@ -767,26 +850,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "structopt"
version = "0.2.12"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt-derive 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt-derive 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "structopt-derive"
version = "0.2.12"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.16 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.15.16"
version = "0.15.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
@ -801,7 +885,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.16 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -917,6 +1001,11 @@ name = "ucd-util"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-segmentation"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.5"
@ -978,8 +1067,10 @@ dependencies = [
"cargo_metadata 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"curl 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"human-panic 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -988,10 +1079,11 @@ dependencies = [
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
"siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"tar 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1069,10 +1161,13 @@ dependencies = [
"checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c"
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"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.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a"
"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400"
"checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39"
"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d"
"checksum bzip2 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "42b7c3cbf0fa9c1b82308d57191728ca0256cb821220f4e2fd410a72ade26e3b"
@ -1085,9 +1180,11 @@ dependencies = [
"checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd48adf136733979b49e15bc3b4c43cc0d3c85ece7bd08e6daa414c6fcb13e6"
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
"checksum curl 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a9e5285b49b44401518c947d3b808d14d99a538a6c9ffb3ec0205c11f9fc4389"
"checksum curl-sys 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "08459503c415173da1ce6b41036a37b8bfdd86af46d45abb9964d4c61fe670ef"
"checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a"
"checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02"
"checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7"
"checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596"
@ -1097,6 +1194,8 @@ dependencies = [
"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"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82"
"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77"
"checksum human-panic 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "21638c5955a6daf3ecc42cae702335fc37a72a4abcc6959ce457b31a7d43bbdd"
"checksum indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a29b2fa6f00010c268bface64c18bb0310aaa70d46a195d5382d288c477fb016"
"checksum isatty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc"
@ -1112,6 +1211,7 @@ 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 msdos_time 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "aad9dfe950c057b1bfe9c1f2aa51583a8468ef2a5baba2ebbe06d775efeb7729"
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
"checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613"
@ -1132,6 +1232,7 @@ dependencies = [
"checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "214a97e49be64fd2c86f568dd0cb2c757d2cc53de95b273b6ad0a1c908482f26"
"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
"checksum regex 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2069749032ea3ec200ca51e4a31df41759190a88edca0d2d86ee8bedf7073341"
"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
@ -1141,12 +1242,14 @@ dependencies = [
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7"
"checksum schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0e1a231dc10abf6749cfa5d7767f25888d484201accbd919b66ab5413c502d56"
"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8"
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "15c141fc7027dd265a47c090bf864cf62b42c4d228bbcf4e51a0c9e2b0d3f7ef"
"checksum serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "225de307c6302bec3898c51ca302fc94a7a1697ef0845fcee6448f33c032249c"
"checksum serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "43344e7ce05d0d8280c5940cabb4964bea626aa58b1ec0e8c73fa2a8512a38ce"
"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
"checksum slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e1a2eec401952cd7b12a84ea120e2d57281329940c3f93c2bf04f462539508e"
"checksum slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e544d16c6b230d84c866662fe55e31aacfca6ae71e6fc49ae9a311cb379bfc2f"
"checksum slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5951a808c40f419922ee014c15b6ae1cd34d963538b57d8a4778b9ca3fff1e0b"
@ -1154,9 +1257,9 @@ dependencies = [
"checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum structopt 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "d77af7242f18c40fd19cb270985930f239ee1646cfb482050bbae9da1d18743b"
"checksum structopt-derive 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "17ff01fe96de9d16e7372ae5f19dd7ece2c703b51043c3db9ea27f9e393ea311"
"checksum syn 0.15.16 (registry+https://github.com/rust-lang/crates.io-index)" = "0b78d53b5e1b6e63129140b1336877c3bddbae398c7956150396bdad0e28676c"
"checksum structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "41c4a2479a078509940d82773d90ff824a8c89533ab3b59cd3ce8b0c0e369c02"
"checksum structopt-derive 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5352090cfae7a2c85e1a31146268b53396106c88ca5d6ccee2e3fae83b6e35c2"
"checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
"checksum tar 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)" = "83b0d14b53dbfd62681933fadd651e815f99e6084b649e049ab99296e05ab3de"
@ -1171,6 +1274,7 @@ dependencies = [
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"
"checksum toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4a2ecc31b0351ea18b3fe11274b8db6e4d82bce861bbb22e6dbed40417902c65"
"checksum ucd-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d0f8bfa9ff0cadcd210129ad9d2c5f145c13e9ced3d3e5d948a6213487d52444"
"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"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"

@ -18,8 +18,10 @@ atty = "0.2.11"
cargo_metadata = "0.6.0"
console = "0.6.1"
curl = "0.4.13"
dirs = "1.0.4"
failure = "0.1.2"
flate2 = "1.0.2"
hex = "0.3"
human-panic = "1.0.1"
indicatif = "0.9.0"
lazy_static = "1.1.0"
@ -28,6 +30,7 @@ parking_lot = "0.6"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_json = "1.0.26"
siphasher = "0.2.3"
slog = "2.3"
slog-term = "2.4"
slog-async = "2.3"

@ -4,12 +4,14 @@ version = "0.1.0"
[dependencies]
curl = "0.4.13"
dirs = "1.0.4"
failure = "0.1.2"
flate2 = "1.0.2"
hex = "0.3"
siphasher = "0.2.3"
slog = "2.3"
slog-term = "2.4"
slog-async = "2.3"
tar = "0.4.16"
which = "2.0.0"
zip = "0.4.2"

@ -4,142 +4,260 @@ extern crate curl;
#[macro_use]
extern crate failure;
extern crate flate2;
#[macro_use]
extern crate slog;
extern crate dirs;
extern crate hex;
extern crate siphasher;
extern crate tar;
extern crate which;
extern crate zip;
pub mod error;
pub mod path;
mod fetch;
mod target;
use error::Error;
use fetch::curl;
use path::{ensure_local_bin_dir, local_bin_dir};
use failure::{Error, ResultExt};
use siphasher::sip::SipHasher13;
use std::collections::HashSet;
use std::env;
use std::ffi;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io;
use std::path::Path;
/// Download the `.tar.gz` file at the given URL and unpack the given binaries
/// from it into the given crate.
///
/// Upon success, every `$BIN` in `binaries` will be at `$CRATE/bin/$BIN`.
pub fn install_binaries_from_targz_at_url<'a, I>(
crate_path: &Path,
url: &str,
binaries: I,
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = &'a str>,
{
let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect();
let tarball = curl(&url).map_err(|e| Error::http(&e.to_string()))?;
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(&tarball[..]));
ensure_local_bin_dir(crate_path)?;
let bin = local_bin_dir(crate_path);
for entry in archive.entries()? {
let mut entry = entry.map_err(|err| {
Error::archive(&format!("Invalid tarball at {}. Inner error: {}", url, err))
})?;
use std::path::{Path, PathBuf};
let dest = match entry.path()?.file_stem() {
Some(f) if binaries.contains(f) => {
binaries.remove(f);
bin.join(entry.path()?.file_name().unwrap())
}
_ => continue,
};
/// Global cache for wasm-pack, currently containing binaries downloaded from
/// urls like wasm-bindgen and such.
pub struct Cache {
destination: PathBuf,
}
entry.unpack(dest)?;
/// Representation of a downloaded tarball/zip
pub struct Download {
root: PathBuf,
}
impl Cache {
/// Returns the global cache directory, as inferred from env vars and such.
///
/// This function may return an error if a cache directory cannot be
/// determined.
pub fn new() -> Result<Cache, Error> {
let destination = dirs::cache_dir()
.map(|p| p.join("wasm-pack"))
.or_else(|| {
let home = dirs::home_dir()?;
Some(home.join(".wasm-pack"))
}).ok_or_else(|| format_err!("couldn't find your home directory, is $HOME not set?"))?;
Ok(Cache::at(&destination))
}
if binaries.is_empty() {
Ok(())
} else {
Err(Error::archive(&format!(
"the tarball at {} was missing expected executables: {}",
url,
binaries
.into_iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
))
.into())
/// Creates a new cache specifically at a particular directory, useful in
/// testing and such.
pub fn at(path: &Path) -> Cache {
Cache {
destination: path.to_path_buf(),
}
}
}
/// Install binaries from within the given zip at the given URL.
///
/// Upon success, the binaries will be at the `$CRATE/bin/$BIN` path.
pub fn install_binaries_from_zip_at_url<'a, I>(
crate_path: &Path,
url: &str,
binaries: I,
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = &'a str>,
{
let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect();
let data = curl(&url).map_err(|e| Error::http(&e.to_string()))?;
let data = io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(data)?;
ensure_local_bin_dir(crate_path)?;
let bin = local_bin_dir(crate_path);
for i in 0..zip.len() {
let mut entry = zip.by_index(i).unwrap();
let entry_path = entry.sanitized_name();
match entry_path.file_stem() {
Some(f) if binaries.contains(f) => {
binaries.remove(f);
let mut dest = bin_open_options()
.write(true)
.create_new(true)
.open(bin.join(entry_path.file_name().unwrap()))?;
io::copy(&mut entry, &mut dest)?;
}
_ => continue,
};
/// Joins a path to the destination of this cache, returning the result
pub fn join(&self, path: &Path) -> PathBuf {
self.destination.join(path)
}
if binaries.is_empty() {
/// Downloads a tarball or zip file from the specified url, extracting it
/// locally and returning the directory that the contents were extracted
/// into.
///
/// Note that this function requries that the contents of `url` never change
/// as the contents of the url are globally cached on the system and never
/// invalidated.
///
/// The `name` is a human-readable name used to go into the folder name of
/// the destination, and `binaries` is a list of binaries expected to be at
/// the url. If the URL's extraction doesn't contain all the binaries this
/// function will return an error.
pub fn download(
&self,
install_permitted: bool,
name: &str,
binaries: &[&str],
url: &str,
) -> Result<Option<Download>, Error> {
let mut hasher = SipHasher13::new();
url.hash(&mut hasher);
let result = hasher.finish();
let hex = hex::encode(&[
(result >> 0) as u8,
(result >> 8) as u8,
(result >> 16) as u8,
(result >> 24) as u8,
(result >> 32) as u8,
(result >> 40) as u8,
(result >> 48) as u8,
(result >> 56) as u8,
]);
let dirname = format!("{}-{}", name, hex);
let destination = self.destination.join(&dirname);
if destination.exists() {
return Ok(Some(Download { root: destination }));
}
if !install_permitted {
return Ok(None);
}
let data = curl(&url).with_context(|_| format!("failed to download from {}", url))?;
// Extract everything in a temporary directory in case we're ctrl-c'd.
// Don't want to leave around corrupted data!
let temp = self.destination.join(&format!(".{}", dirname));
drop(fs::remove_dir_all(&temp));
fs::create_dir_all(&temp)?;
if url.ends_with(".tar.gz") {
self.extract_tarball(&data, &temp, binaries)
.with_context(|_| format!("failed to extract tarball from {}", url))?;
} else if url.ends_with(".zip") {
self.extract_zip(&data, &temp, binaries)
.with_context(|_| format!("failed to extract zip from {}", url))?;
} else {
// panic instead of runtime error as it's a static violation to
// download a different kind of url, all urls should be encoded into
// the binary anyway
panic!("don't know how to extract {}", url)
}
// Now that everything is ready move this over to our destination and
// we're good to go.
fs::rename(&temp, &destination)?;
Ok(Some(Download { root: destination }))
}
fn extract_tarball(&self, tarball: &[u8], dst: &Path, binaries: &[&str]) -> Result<(), Error> {
let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect();
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(tarball));
for entry in archive.entries()? {
let mut entry = entry?;
let dest = match entry.path()?.file_stem() {
Some(f) if binaries.contains(f) => {
binaries.remove(f);
dst.join(entry.path()?.file_name().unwrap())
}
_ => continue,
};
entry.unpack(dest)?;
}
if !binaries.is_empty() {
bail!(
"the tarball was missing expected executables: {}",
binaries
.into_iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
)
}
Ok(())
} else {
Err(Error::archive(&format!(
"the zip at {} was missing expected executables: {}",
url,
binaries
.into_iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
))
.into())
}
fn extract_zip(&self, zip: &[u8], dst: &Path, binaries: &[&str]) -> Result<(), Error> {
let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect();
let data = io::Cursor::new(zip);
let mut zip = zip::ZipArchive::new(data)?;
for i in 0..zip.len() {
let mut entry = zip.by_index(i).unwrap();
let entry_path = entry.sanitized_name();
match entry_path.file_stem() {
Some(f) if binaries.contains(f) => {
binaries.remove(f);
let mut dest = bin_open_options()
.write(true)
.create_new(true)
.open(dst.join(entry_path.file_name().unwrap()))?;
io::copy(&mut entry, &mut dest)?;
}
_ => continue,
};
}
if !binaries.is_empty() {
bail!(
"the zip was missing expected executables: {}",
binaries
.into_iter()
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
)
}
return Ok(());
#[cfg(unix)]
fn bin_open_options() -> fs::OpenOptions {
use std::os::unix::fs::OpenOptionsExt;
let mut opts = fs::OpenOptions::new();
opts.mode(0o755);
opts
}
#[cfg(not(unix))]
fn bin_open_options() -> fs::OpenOptions {
fs::OpenOptions::new()
}
}
}
#[cfg(unix)]
fn bin_open_options() -> fs::OpenOptions {
use std::os::unix::fs::OpenOptionsExt;
impl Download {
/// Manually constructs a download at the specified path
pub fn at(path: &Path) -> Download {
Download {
root: path.to_path_buf(),
}
}
let mut opts = fs::OpenOptions::new();
opts.mode(0o755);
opts
/// Returns the path to the binary `name` within this download
pub fn binary(&self, name: &str) -> PathBuf {
let ret = self
.root
.join(name)
.with_extension(env::consts::EXE_EXTENSION);
assert!(ret.exists(), "binary {} doesn't exist", ret.display());
return ret;
}
}
#[cfg(not(unix))]
fn bin_open_options() -> fs::OpenOptions {
fs::OpenOptions::new()
fn curl(url: &str) -> Result<Vec<u8>, Error> {
let mut data = Vec::new();
let mut easy = curl::easy::Easy::new();
easy.follow_location(true)?;
easy.url(url)?;
easy.get(true)?;
{
let mut transfer = easy.transfer();
transfer.write_function(|part| {
data.extend_from_slice(part);
Ok(part.len())
})?;
transfer.perform()?;
}
let status_code = easy.response_code()?;
if 200 <= status_code && status_code < 300 {
Ok(data)
} else {
bail!(
"received a bad HTTP status code ({}) when requesting {}",
status_code,
url
)
}
}

@ -1,19 +1,17 @@
//! Functionality related to installing and running `wasm-bindgen`.
use binary_install::{
install_binaries_from_targz_at_url,
path::{bin_path, local_bin_path},
};
use cargo_metadata;
use binary_install::{Cache, Download};
use child;
use emoji;
use error::Error;
use failure::{self, ResultExt};
use manifest::CrateData;
use progressbar::Step;
use slog::Logger;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use target;
use which::which;
use PBAR;
/// Install the `wasm-bindgen` CLI.
@ -23,46 +21,66 @@ use PBAR;
/// tarball from the GitHub releases page, if this target has prebuilt
/// binaries. Finally, falls back to `cargo install`.
pub fn install_wasm_bindgen(
root_path: &Path,
cache: &Cache,
version: &str,
install_permitted: bool,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
// If the `wasm-bindgen` dependency is already met, print a message and return.
if wasm_bindgen_path(log, root_path)
.map(|bindgen_path| wasm_bindgen_version_check(&bindgen_path, version, log))
.unwrap_or(false)
{
let msg = format!("{}wasm-bindgen already installed...", emoji::DOWN_ARROW);
PBAR.step(step, &msg);
return Ok(());
}
// If the `wasm-bindgen` dependency was not met, and installs are not
// permitted, return a configuration error.
if !install_permitted {
let msg = format!("wasm-bindgen v{} is not installed!", version);
return Err(Error::crate_config(&msg).into());
) -> Result<Download, failure::Error> {
// If `wasm-bindgen` is installed globally and it has the right version, use
// that. Assume that other tools are installed next to it.
//
// This situation can arise if `wasm-bindgen` is already installed via
// `cargo install`, for example.
if let Ok(path) = which("wasm-bindgen") {
debug!(
log,
"found global wasm-bindgen binary at: {}",
path.display()
);
if wasm_bindgen_version_check(&path, version, log) {
return Ok(Download::at(path.parent().unwrap()));
}
}
let msg = format!("{}Installing wasm-bindgen...", emoji::DOWN_ARROW);
PBAR.step(step, &msg);
download_prebuilt_wasm_bindgen(root_path, version).or_else(|e| {
warn!(
log,
"could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.", e
);
cargo_install_wasm_bindgen(log, root_path, version)
})
let dl = download_prebuilt_wasm_bindgen(&cache, version, install_permitted);
match dl {
Ok(dl) => return Ok(dl),
Err(e) => {
warn!(
log,
"could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.",
e
);
}
}
cargo_install_wasm_bindgen(log, &cache, version, install_permitted)
}
/// Download a tarball containing a pre-built `wasm-bindgen` binary.
/// Downloads a precompiled copy of wasm-bindgen, if available.
pub fn download_prebuilt_wasm_bindgen(
root_path: &Path,
cache: &Cache,
version: &str,
) -> Result<(), failure::Error> {
install_permitted: bool,
) -> Result<Download, failure::Error> {
let url = match prebuilt_url(version) {
Some(url) => url,
None => bail!("no prebuilt wasm-bindgen binaries are available for this platform"),
};
let binaries = &["wasm-bindgen", "wasm-bindgen-test-runner"];
match cache.download(install_permitted, "wasm-bindgen", binaries, &url)? {
Some(download) => Ok(download),
None => bail!("wasm-bindgen v{} is not installed!", version),
}
}
/// Returns the URL of a precompiled version of wasm-bindgen, if we have one
/// available for our host platform.
fn prebuilt_url(version: &str) -> Option<String> {
let target = if target::LINUX && target::x86_64 {
"x86_64-unknown-linux-musl"
} else if target::MACOS && target::x86_64 {
@ -70,32 +88,40 @@ pub fn download_prebuilt_wasm_bindgen(
} else if target::WINDOWS && target::x86_64 {
"x86_64-pc-windows-msvc"
} else {
return Err(Error::unsupported(
"there are no pre-built `wasm-bindgen` binaries for this target",
)
.into());
return None;
};
let url = format!(
Some(format!(
"https://github.com/rustwasm/wasm-bindgen/releases/download/{0}/wasm-bindgen-{0}-{1}.tar.gz",
version,
target
);
install_binaries_from_targz_at_url(
root_path,
&url,
vec!["wasm-bindgen", "wasm-bindgen-test-runner"],
)
))
}
/// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given
/// crate.
pub fn cargo_install_wasm_bindgen(
logger: &Logger,
crate_path: &Path,
cache: &Cache,
version: &str,
) -> Result<(), failure::Error> {
install_permitted: bool,
) -> Result<Download, failure::Error> {
let dirname = format!("wasm-bindgen-cargo-install-{}", version);
let destination = cache.join(dirname.as_ref());
if destination.exists() {
return Ok(Download::at(&destination));
}
if !install_permitted {
bail!("wasm-bindgen v{} is not installed!", version)
}
// Run `cargo install` to a temporary location to handle ctrl-c gracefully
// and ensure we don't accidentally use stale files in the future
let tmp = cache.join(format!(".{}", dirname).as_ref());
drop(fs::remove_dir_all(&tmp));
fs::create_dir_all(&tmp)?;
let mut cmd = Command::new("cargo");
cmd.arg("install")
.arg("--force")
@ -103,19 +129,20 @@ pub fn cargo_install_wasm_bindgen(
.arg("--version")
.arg(version)
.arg("--root")
.arg(crate_path);
.arg(&tmp);
child::run(logger, cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;
assert!(local_bin_path(crate_path, "wasm-bindgen").is_file());
Ok(())
fs::rename(&tmp, &destination)?;
Ok(Download::at(&destination))
}
/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
/// `.wasm`.
pub fn wasm_bindgen_build(
path: &Path,
data: &CrateData,
bindgen: &Download,
out_dir: &Path,
name: &str,
disable_dts: bool,
target: &str,
debug: bool,
@ -125,51 +152,41 @@ pub fn wasm_bindgen_build(
let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER);
PBAR.step(step, &msg);
let binary_name = name.replace("-", "_");
let release_or_debug = if debug { "debug" } else { "release" };
let out_dir = out_dir.to_str().unwrap();
if let Some(wasm_bindgen_path) = wasm_bindgen_path(log, path) {
let manifest = path.join("Cargo.toml");
let target_path = cargo_metadata::metadata(Some(&manifest))
.unwrap()
.target_directory;
let mut wasm_path = PathBuf::from(&target_path)
.join("wasm32-unknown-unknown")
.join(release_or_debug)
.join(binary_name);
wasm_path.set_extension("wasm");
let wasm_path = wasm_path.display().to_string();
let dts_arg = if disable_dts {
"--no-typescript"
} else {
"--typescript"
};
let target_arg = match target {
"nodejs" => "--nodejs",
"no-modules" => "--no-modules",
_ => "--browser",
};
let bindgen_path = Path::new(&wasm_bindgen_path);
let mut cmd = Command::new(bindgen_path);
cmd.current_dir(path)
.arg(&wasm_path)
.arg("--out-dir")
.arg(out_dir)
.arg(dts_arg)
.arg(target_arg);
if debug {
cmd.arg("--debug");
}
let wasm_path = data
.target_directory()
.join("wasm32-unknown-unknown")
.join(release_or_debug)
.join(data.crate_name())
.with_extension("wasm");
child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
let dts_arg = if disable_dts {
"--no-typescript"
} else {
Err(Error::crate_config("Could not find `wasm-bindgen`").into())
"--typescript"
};
let target_arg = match target {
"nodejs" => "--nodejs",
"no-modules" => "--no-modules",
_ => "--browser",
};
let bindgen_path = bindgen.binary("wasm-bindgen");
let mut cmd = Command::new(bindgen_path);
cmd.arg(&wasm_path)
.arg("--out-dir")
.arg(out_dir)
.arg(dts_arg)
.arg(target_arg);
if debug {
cmd.arg("--debug");
}
child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}
/// Check if the `wasm-bindgen` dependency is locally satisfied.
@ -190,21 +207,6 @@ fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &L
dep_version
);
v == dep_version
})
.unwrap_or(false)
})
.unwrap_or(false)
}
/// Return a `PathBuf` containing the path to either the local wasm-bindgen
/// version, or the globally installed version if there is no local version.
fn wasm_bindgen_path(log: &Logger, crate_path: &Path) -> Option<PathBuf> {
bin_path(log, crate_path, "wasm-bindgen")
}
/// Return a `PathBuf` containing the path to either the local
/// wasm-bindgen-test-runner version, or the globally installed version if there
/// is no local version.
pub fn wasm_bindgen_test_runner_path(log: &Logger, crate_path: &Path) -> Option<PathBuf> {
bin_path(log, crate_path, "wasm-bindgen-test-runner")
}).unwrap_or(false)
}).unwrap_or(false)
}

@ -82,7 +82,7 @@ pub fn cargo_build_wasm(
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly")?;
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly failed")?;
Ok(())
}

@ -171,7 +171,6 @@ pub fn run(
if exit.success() {
return Ok(stdout);
} else {
let msg = format!("`{}` did not exit successfully", command_name);
return Err(Error::cli(&msg, stdout.into(), stderr.into(), exit).into());
return Err(Error::cli(command_name, stdout.into(), stderr.into(), exit).into());
}
}

@ -1,5 +1,6 @@
//! Implementation of the `wasm-pack build` command.
use binary_install::{Cache, Download};
use bindgen;
use build;
use command::utils::{create_pkg_dir, set_crate_path};
@ -17,16 +18,19 @@ use std::time::Instant;
use PBAR;
/// Everything required to configure and run the `wasm-pack init` command.
pub(crate) struct Build {
#[allow(missing_docs)]
pub struct Build {
pub crate_path: PathBuf,
pub crate_data: manifest::CrateData,
pub scope: Option<String>,
pub disable_dts: bool,
pub target: String,
pub debug: bool,
pub mode: BuildMode,
// build_config: Option<BuildConfig>,
pub crate_name: String,
pub out_dir: PathBuf,
pub bindgen: Option<Download>,
pub cache: Cache,
}
/// The `BuildMode` determines which mode of initialization we are running, and
@ -100,22 +104,29 @@ impl Build {
/// Construct a build command from the given options.
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, failure::Error> {
let crate_path = set_crate_path(build_opts.path)?;
let crate_name = manifest::get_crate_name(&crate_path)?;
let crate_data = manifest::CrateData::new(&crate_path)?;
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir));
// let build_config = manifest::xxx(&crate_path).xxx();
Ok(Build {
crate_path,
crate_data,
scope: build_opts.scope,
disable_dts: build_opts.disable_dts,
target: build_opts.target,
debug: build_opts.debug,
mode: build_opts.mode,
// build_config,
crate_name,
out_dir,
bindgen: None,
cache: Cache::new()?,
})
}
/// Configures the global binary cache used for this build
pub fn set_cache(&mut self, cache: Cache) {
self.cache = cache;
}
/// Execute this `Build` command.
pub fn run(&mut self, log: &Logger) -> Result<(), failure::Error> {
let process_steps = Build::get_process_steps(&self.mode);
@ -202,7 +213,7 @@ impl Build {
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(&log, "Checking crate configuration...");
manifest::check_crate_config(&self.crate_path, step)?;
self.crate_data.check_crate_config(step)?;
info!(&log, "Crate is correctly configured.");
Ok(())
}
@ -239,8 +250,7 @@ impl Build {
fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(&log, "Writing a package.json...");
manifest::write_package_json(
&self.crate_path,
self.crate_data.write_package_json(
&self.out_dir,
&self.scope,
self.disable_dts,
@ -268,7 +278,7 @@ impl Build {
log: &Logger,
) -> Result<(), failure::Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_path)?;
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
info!(&log, "Installing wasm-bindgen-cli...");
let install_permitted = match self.mode {
@ -276,32 +286,24 @@ impl Build {
BuildMode::Force => true,
BuildMode::Noinstall => false,
};
bindgen::install_wasm_bindgen(
&self.crate_path,
let bindgen = bindgen::install_wasm_bindgen(
&self.cache,
&bindgen_version,
install_permitted,
step,
log,
)?;
self.bindgen = Some(bindgen);
info!(&log, "Installing wasm-bindgen-cli was successful.");
info!(&log, "Getting the crate name from the manifest...");
self.crate_name = manifest::get_crate_name(&self.crate_path)?;
info!(
&log,
"Got crate name {:#?} from the manifest at {:#?}.",
&self.crate_name,
&self.crate_path.join("Cargo.toml")
);
Ok(())
}
fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(&log, "Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_path,
&self.crate_data,
self.bindgen.as_ref().unwrap(),
&self.out_dir,
&self.crate_name,
self.disable_dts,
&self.target,
self.debug,

@ -1,6 +1,7 @@
//! Implementation of the `wasm-pack test` command.
use super::build::BuildMode;
use binary_install::Cache;
use bindgen;
use build;
use command::utils::set_crate_path;
@ -81,6 +82,8 @@ pub struct TestOptions {
/// A configured `wasm-pack test` command.
pub struct Test {
crate_path: PathBuf,
crate_data: manifest::CrateData,
cache: Cache,
node: bool,
mode: BuildMode,
firefox: bool,
@ -114,32 +117,26 @@ impl Test {
} = test_opts;
let crate_path = set_crate_path(path)?;
// let geckodriver = get_web_driver("geckodriver", test_opts.geckodriver, test_opts.firefox)?;
// let chromedriver =
// get_web_driver("chromedriver", test_opts.chromedriver, test_opts.chrome)?;
// let safaridriver =
// get_web_driver("safaridriver", test_opts.safaridriver, test_opts.safari)?;
let crate_data = manifest::CrateData::new(&crate_path)?;
let any_browser = chrome || firefox || safari;
if !node && !any_browser {
return Err(Error::crate_config(
"Must specify at least one of `--node`, `--chrome`, `--firefox`, or `--safari`",
)
.into());
).into());
}
if headless && !any_browser {
return Err(Error::crate_config(
"The `--headless` flag only applies to browser tests. Node does not provide a UI, \
so it doesn't make sense to talk about a headless version of Node tests.",
)
.into());
).into());
}
Ok(Test {
cache: Cache::new()?,
crate_path,
crate_data,
node,
mode,
chrome,
@ -154,6 +151,11 @@ impl Test {
})
}
/// Configures the cache that this test command uses
pub fn set_cache(&mut self, cache: Cache) {
self.cache = cache;
}
/// Execute this test command.
pub fn run(mut self, log: &Logger) -> Result<(), failure::Error> {
let process_steps = self.get_process_steps();
@ -240,7 +242,7 @@ impl Test {
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(log, "Checking crate configuration...");
manifest::check_crate_config(&self.crate_path, step)?;
self.crate_data.check_crate_config(step)?;
info!(log, "Crate is correctly configured.");
Ok(())
}
@ -270,7 +272,7 @@ impl Test {
log: &Logger,
) -> Result<(), failure::Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_path)?;
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
// Unlike `wasm-bindgen` and `wasm-bindgen-cli`, `wasm-bindgen-test`
@ -303,16 +305,15 @@ impl Test {
}
};
bindgen::install_wasm_bindgen(
&self.crate_path,
let dl = bindgen::install_wasm_bindgen(
&self.cache,
&bindgen_version,
install_permitted,
step,
log,
)?;
self.test_runner_path = Some(bindgen::wasm_bindgen_test_runner_path(log, &self.crate_path)
.expect("if installing wasm-bindgen succeeded, then we should have wasm-bindgen-test-runner too"));
self.test_runner_path = Some(dl.binary("wasm-bindgen-test-runner"));
info!(&log, "Getting wasm-bindgen-cli was successful.");
Ok(())
@ -335,13 +336,12 @@ impl Test {
Ok(())
}
fn step_get_chromedriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_get_chromedriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> {
PBAR.step(step, "Getting chromedriver...");
assert!(self.chrome && self.chromedriver.is_none());
self.chromedriver = Some(webdriver::get_or_install_chromedriver(
log,
&self.crate_path,
&self.cache,
self.mode,
)?);
Ok(())
@ -378,13 +378,12 @@ impl Test {
Ok(())
}
fn step_get_geckodriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_get_geckodriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> {
PBAR.step(step, "Getting geckodriver...");
assert!(self.firefox && self.geckodriver.is_none());
self.geckodriver = Some(webdriver::get_or_install_geckodriver(
log,
&self.crate_path,
&self.cache,
self.mode,
)?);
Ok(())
@ -421,11 +420,11 @@ impl Test {
Ok(())
}
fn step_get_safaridriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_get_safaridriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> {
PBAR.step(step, "Getting safaridriver...");
assert!(self.safari && self.safaridriver.is_none());
self.safaridriver = Some(webdriver::get_safaridriver(log, &self.crate_path)?);
self.safaridriver = Some(webdriver::get_safaridriver()?);
Ok(())
}

@ -47,13 +47,10 @@ pub enum Error {
},
/// An error invoking another CLI tool.
#[fail(
display = "Process exited with {}: {}.\n\nstdout:{}\n\nstderr:\n\n{}",
exit_status, message, stdout, stderr
)]
#[fail(display = "`{}` exited with {}", tool, exit_status)]
Cli {
/// Error message.
message: String,
tool: String,
/// The underlying CLI's `stdout` output.
stdout: String,
/// The underlying CLI's `stderr` output.
@ -101,9 +98,9 @@ pub enum Error {
impl Error {
/// Construct a CLI error.
pub fn cli(message: &str, stdout: Cow<str>, stderr: Cow<str>, exit_status: ExitStatus) -> Self {
pub fn cli(tool: &str, stdout: Cow<str>, stderr: Cow<str>, exit_status: ExitStatus) -> Self {
Error::Cli {
message: message.to_string(),
tool: tool.to_string(),
stdout: stdout.to_string(),
stderr: stderr.to_string(),
exit_status,
@ -161,7 +158,7 @@ impl Error {
local_minor_version: _,
} => "Your rustc version is not supported. Please install version 1.30.0 or higher.",
Error::Cli {
message: _,
tool: _,
stdout: _,
stderr: _,
exit_status: _,

@ -5,9 +5,11 @@
extern crate cargo_metadata;
extern crate console;
extern crate curl;
extern crate dirs;
#[macro_use]
extern crate failure;
extern crate flate2;
extern crate hex;
extern crate indicatif;
#[macro_use]
extern crate lazy_static;
@ -15,6 +17,7 @@ extern crate parking_lot;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate siphasher;
#[macro_use]
extern crate structopt;
#[macro_use]

@ -1,11 +1,11 @@
//! Reading Cargo.lock lock file.
use std::fs;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use cargo_metadata;
use console::style;
use error::Error;
use failure::{Error, ResultExt};
use manifest::CrateData;
use toml;
/// This struct represents the contents of `Cargo.lock`.
@ -23,10 +23,13 @@ struct Package {
impl Lockfile {
/// Read the `Cargo.lock` file for the crate at the given path.
pub fn new(crate_path: &Path) -> Result<Lockfile, failure::Error> {
let lock_path = get_lockfile_path(crate_path)?;
let lockfile = fs::read_to_string(lock_path)?;
toml::from_str(&lockfile).map_err(|err| Error::from(err).into())
pub fn new(crate_data: &CrateData) -> Result<Lockfile, Error> {
let lock_path = get_lockfile_path(crate_data)?;
let lockfile = fs::read_to_string(&lock_path)
.with_context(|_| format!("failed to read: {}", lock_path.display()))?;
let lockfile = toml::from_str(&lockfile)
.with_context(|_| format!("failed to parse: {}", lock_path.display()))?;
Ok(lockfile)
}
/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
@ -36,15 +39,14 @@ impl Lockfile {
/// Like `wasm_bindgen_version`, except it returns an error instead of
/// `None`.
pub fn require_wasm_bindgen(&self) -> Result<&str, failure::Error> {
pub fn require_wasm_bindgen(&self) -> Result<&str, Error> {
self.wasm_bindgen_version().ok_or_else(|| {
let message = format!(
format_err!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
[dependencies]\n\
wasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim(),
);
Error::CrateConfig { message }.into()
)
})
}
@ -63,22 +65,12 @@ impl Lockfile {
/// Given the path to the crate that we are buliding, return a `PathBuf`
/// containing the location of the lock file, by finding the workspace root.
fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, failure::Error> {
// Identify the crate's root directory, or return an error.
let manifest = crate_path.join("Cargo.toml");
let crate_root = cargo_metadata::metadata(Some(&manifest))
.map_err(|_| Error::CrateConfig {
message: String::from("Error while processing crate metadata"),
})?
.workspace_root;
fn get_lockfile_path(crate_data: &CrateData) -> Result<PathBuf, Error> {
// Check that a lock file can be found in the directory. Return an error
// if it cannot, otherwise return the path buffer.
let lockfile_path = Path::new(&crate_root).join("Cargo.lock");
let lockfile_path = crate_data.workspace_root().join("Cargo.lock");
if !lockfile_path.is_file() {
Err(Error::CrateConfig {
message: format!("Could not find lockfile at {:?}", lockfile_path),
}
.into())
bail!("Could not find lockfile at {:?}", lockfile_path)
} else {
Ok(lockfile_path)
}

@ -16,7 +16,7 @@ mod installer;
fn main() {
setup_panic!();
if let Err(e) = run() {
eprintln!("{}", e);
eprintln!("Error: {}", e);
for cause in e.iter_causes() {
eprintln!("Caused by: {}", cause);
}

@ -2,264 +2,306 @@
mod npm;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::fs;
use std::path::Path;
use self::npm::{
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
};
use cargo_metadata::Metadata;
use emoji;
use error::Error;
use failure;
use failure::{Error, ResultExt};
use progressbar::Step;
use serde_json;
use toml;
use PBAR;
#[derive(Debug, Deserialize)]
/// Store for metadata learned about a crate
pub struct CrateData {
data: Metadata,
current_idx: usize,
manifest: CargoManifest,
}
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
dependencies: Option<HashMap<String, CargoDependency>>,
#[serde(rename = "dev-dependencies")]
dev_dependencies: Option<HashMap<String, CargoDependency>>,
lib: Option<CargoLib>,
}
#[derive(Debug, Deserialize)]
#[derive(Deserialize)]
struct CargoPackage {
name: String,
authors: Vec<String>,
description: Option<String>,
version: String,
license: Option<String>,
repository: Option<String>,
}
impl CargoPackage {
fn check_optional_fields(&self) {
let mut messages = vec![];
if self.description.is_none() {
messages.push("description");
struct NpmData {
name: String,
files: Vec<String>,
dts_file: Option<String>,
main: String,
}
impl CrateData {
/// Reads all metadata for the crate whose manifest is inside the directory
/// specified by `path`.
pub fn new(crate_path: &Path) -> Result<CrateData, Error> {
let manifest_path = crate_path.join("Cargo.toml");
if !manifest_path.is_file() {
bail!(
"crate directory is missing a `Cargo.toml` file; is `{}` the \
wrong directory?",
crate_path.display()
)
}
if self.repository.is_none() {
messages.push("repository");
}
if self.license.is_none() {
messages.push("license");
let manifest = fs::read_to_string(&manifest_path)
.with_context(|_| format!("failed to read: {}", manifest_path.display()))?;
let manifest: CargoManifest = toml::from_str(&manifest)
.with_context(|_| format!("failed to parse manifest: {}", manifest_path.display()))?;
let data =
cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?;
let current_idx = data
.packages
.iter()
.position(|pkg| pkg.name == manifest.package.name)
.ok_or_else(|| format_err!("failed to find package in metadata"))?;
return Ok(CrateData {
data,
manifest,
current_idx,
});
fn error_chain_to_failure(err: cargo_metadata::Error) -> Error {
let errors = err.iter().collect::<Vec<_>>();
let mut err: Error = match errors.last() {
Some(e) => format_err!("{}", e),
None => return format_err!("{}", err),
};
for e in errors[..errors.len() - 1].iter().rev() {
err = err.context(e.to_string()).into();
}
return err;
}
}
match messages.len() {
1 => PBAR.info(&format!("Optional field missing from Cargo.toml: '{}'. This is not necessary, but recommended", messages[0])),
2 => PBAR.info(&format!("Optional fields missing from Cargo.toml: '{}', '{}'. These are not necessary, but recommended", messages[0], messages[1])),
3 => PBAR.info(&format!("Optional fields missing from Cargo.toml: '{}', '{}', and '{}'. These are not necessary, but recommended", messages[0], messages[1], messages[2])),
_ => ()
};
/// Check that the crate the given path is properly configured.
pub fn check_crate_config(&self, step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
self.check_crate_type()?;
Ok(())
}
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum CargoDependency {
Simple(String),
Detailed(DetailedCargoDependency),
}
fn check_crate_type(&self) -> Result<(), Error> {
let pkg = &self.data.packages[self.current_idx];
let any_cdylib = pkg
.targets
.iter()
.filter(|target| target.kind.iter().any(|k| k == "cdylib"))
.any(|target| target.crate_types.iter().any(|s| s == "cdylib"));
if any_cdylib {
return Ok(());
}
bail!(
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \
Cargo.toml file:\n\n\
[lib]\n\
crate-type = [\"cdylib\", \"rlib\"]"
)
}
#[derive(Debug, Deserialize)]
struct DetailedCargoDependency {
version: Option<String>,
}
/// Get the crate name for the crate at the given path.
pub fn crate_name(&self) -> String {
let pkg = &self.data.packages[self.current_idx];
match pkg
.targets
.iter()
.find(|t| t.kind.iter().any(|k| k == "cdylib"))
{
Some(lib) => lib.name.replace("-", "_"),
None => pkg.name.replace("-", "_"),
}
}
#[derive(Debug, Deserialize)]
struct CargoLib {
#[serde(rename = "crate-type")]
crate_type: Option<Vec<String>>,
}
/// Returns the path to this project's target directory where artifacts are
/// located after a cargo build.
pub fn target_directory(&self) -> &Path {
Path::new(&self.data.target_directory)
}
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, failure::Error> {
let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() {
return Err(Error::crate_config(&format!(
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
path.display()
))
.into());
/// Returns the path to this project's root cargo workspace directory
pub fn workspace_root(&self) -> &Path {
Path::new(&self.data.workspace_root)
}
let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;
let manifest: CargoManifest = toml::from_str(&cargo_contents)?;
Ok(manifest)
}
/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(
&self,
out_dir: &Path,
scope: &Option<String>,
disable_dts: bool,
target: &str,
step: &Step,
) -> Result<(), Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);
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)
} else if target == "no-modules" {
self.to_nomodules(scope, disable_dts)
} else {
self.to_esmodules(scope, disable_dts)
};
impl CargoManifest {
fn into_commonjs(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
let mut files = vec![wasm_file];
let npm_json = serde_json::to_string_pretty(&npm_data)?;
fs::write(&pkg_file_path, npm_json)
.with_context(|_| format!("failed to write: {}", pkg_file_path.display()))?;
Ok(())
}
let js_bg_file = format!("{}_bg.js", filename);
files.push(js_bg_file.to_string());
fn npm_data(
&self,
scope: &Option<String>,
include_commonjs_shim: bool,
disable_dts: bool,
) -> NpmData {
let crate_name = self.crate_name();
let wasm_file = format!("{}_bg.wasm", crate_name);
let js_file = format!("{}.js", crate_name);
let mut files = vec![wasm_file];
if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
files.push(js_file.clone());
if include_commonjs_shim {
let js_bg_file = format!("{}_bg.js", crate_name);
files.push(js_bg_file.to_string());
}
let dts_file = if disable_dts == false {
let file = format!("{}.d.ts", filename);
let pkg = &self.data.packages[self.current_idx];
let npm_name = match scope {
Some(s) => format!("@{}/{}", s, pkg.name),
None => pkg.name.clone(),
};
let dts_file = if !disable_dts {
let file = format!("{}.d.ts", crate_name);
files.push(file.to_string());
Some(file)
} else {
None
};
NpmData {
name: npm_name,
dts_file,
files,
main: js_file,
}
}
&self.package.check_optional_fields();
fn to_commonjs(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, true, disable_dts);
let pkg = &self.data.packages[self.current_idx];
self.check_optional_fields();
NpmPackage::CommonJSPackage(CommonJSPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
main: js_file,
types: dts_file,
name: data.name,
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
repository: self
.manifest
.package
.repository
.clone()
.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: data.files,
main: data.main,
types: data.dts_file,
})
}
fn into_esmodules(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
let mut files = vec![wasm_file, js_file.clone()];
let dts_file = if disable_dts == false {
let file = format!("{}.d.ts", filename);
files.push(file.to_string());
Some(file)
} else {
None
};
if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
}
fn to_esmodules(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts);
let pkg = &self.data.packages[self.current_idx];
&self.package.check_optional_fields();
self.check_optional_fields();
NpmPackage::ESModulesPackage(ESModulesPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
module: js_file,
types: dts_file,
name: data.name,
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
repository: self
.manifest
.package
.repository
.clone()
.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: data.files,
module: data.main,
types: data.dts_file,
side_effects: "false".to_string(),
})
}
fn into_nomodules(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
let mut files = vec![wasm_file, js_file.clone()];
fn to_nomodules(&self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts);
let pkg = &self.data.packages[self.current_idx];
let dts_file = if disable_dts == false {
let file = format!("{}.d.ts", filename);
files.push(file.to_string());
Some(file)
} else {
None
};
if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
}
&self.package.check_optional_fields();
self.check_optional_fields();
NpmPackage::NoModulesPackage(NoModulesPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
browser: js_file,
types: dts_file,
name: data.name,
collaborators: pkg.authors.clone(),
description: self.manifest.package.description.clone(),
version: pkg.version.clone(),
license: self.manifest.package.license.clone(),
repository: self
.manifest
.package
.repository
.clone()
.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: data.files,
browser: data.main,
types: data.dts_file,
})
}
}
/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(
path: &Path,
out_dir: &Path,
scope: &Option<String>,
disable_dts: bool,
target: &str,
step: &Step,
) -> Result<(), failure::Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);
PBAR.step(step, &msg);
let pkg_file_path = out_dir.join("package.json");
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = if target == "nodejs" {
crate_data.into_commonjs(scope, disable_dts)
} else if target == "no-modules" {
crate_data.into_nomodules(scope, disable_dts)
} else {
crate_data.into_esmodules(scope, disable_dts)
};
let npm_json = serde_json::to_string_pretty(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
Ok(())
}
/// Get the crate name for the crate at the given path.
pub fn get_crate_name(path: &Path) -> Result<String, failure::Error> {
Ok(read_cargo_toml(path)?.package.name)
}
/// Check that the crate the given path is properly configured.
pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
check_crate_type(path)?;
Ok(())
}
fn check_optional_fields(&self) {
let mut messages = vec![];
if self.manifest.package.description.is_none() {
messages.push("description");
}
if self.manifest.package.repository.is_none() {
messages.push("repository");
}
if self.manifest.package.license.is_none() {
messages.push("license");
}
fn check_crate_type(path: &Path) -> Result<(), failure::Error> {
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
}) {
return Ok(());
match messages.len() {
1 => PBAR.info(&format!("Optional field missing from Cargo.toml: '{}'. This is not necessary, but recommended", messages[0])),
2 => PBAR.info(&format!("Optional fields missing from Cargo.toml: '{}', '{}'. These are not necessary, but recommended", messages[0], messages[1])),
3 => PBAR.info(&format!("Optional fields missing from Cargo.toml: '{}', '{}', and '{}'. These are not necessary, but recommended", messages[0], messages[1], messages[2])),
_ => ()
};
}
Err(Error::crate_config(
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \
Cargo.toml file:\n\n\
[lib]\n\
crate-type = [\"cdylib\", \"rlib\"]"
).into())
}

@ -22,12 +22,6 @@ where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
use std::sync::Mutex;
lazy_static! {
static ref ONE_TEST_AT_A_TIME: Mutex<()> = Mutex::new(());
}
let _locked = ONE_TEST_AT_A_TIME.lock().unwrap();
let output = {
let mut cmd = Command::new("cargo");
cmd.envs(envs);

@ -1,41 +1,32 @@
//! Getting WebDriver client binaries.
use binary_install::{
install_binaries_from_targz_at_url, install_binaries_from_zip_at_url,
path::{bin_path, local_bin_path},
};
use binary_install::Cache;
use command::build::BuildMode;
use error::Error;
use failure;
use slog::Logger;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use target;
/// Get the path to an existing `chromedriver`, or install it if no existing
/// binary is found.
pub fn get_or_install_chromedriver(
log: &Logger,
crate_path: &Path,
cache: &Cache,
mode: BuildMode,
) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "chromedriver")) {
(_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_chromedriver(crate_path),
(BuildMode::Force, None) => install_chromedriver(crate_path),
(BuildMode::Noinstall, None) => Err(Error::crate_config(
"No crate-local `chromedriver` binary found, and could not find a global \
`chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \
mode.",
)
.into()),
if let Ok(path) = which::which("chromedriver") {
return Ok(path);
}
let installation_allowed = match mode {
BuildMode::Noinstall => false,
_ => true,
};
install_chromedriver(cache, installation_allowed)
}
fn get_local_chromedriver_path(crate_path: &Path) -> PathBuf {
local_bin_path(crate_path, "chromedriver")
}
fn get_chromedriver_url() -> Result<String, Error> {
/// Download and install a pre-built `chromedriver` binary.
pub fn install_chromedriver(
cache: &Cache,
installation_allowed: bool,
) -> Result<PathBuf, failure::Error> {
let target = if target::LINUX && target::x86_64 {
"linux64"
} else if target::MACOS && target::x86_64 {
@ -43,46 +34,49 @@ fn get_chromedriver_url() -> Result<String, Error> {
} else if target::WINDOWS && target::x86 {
"win32"
} else {
return Err(Error::unsupported(
"geckodriver binaries are unavailable for this target",
));
bail!("geckodriver binaries are unavailable for this target")
};
Ok(format!(
let url = format!(
"https://chromedriver.storage.googleapis.com/2.41/chromedriver_{}.zip",
target
))
}
/// Download and install a pre-built `chromedriver` binary.
pub fn install_chromedriver(crate_path: &Path) -> Result<PathBuf, failure::Error> {
let url = get_chromedriver_url()?;
install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?;
let chromedriver = get_local_chromedriver_path(crate_path);
assert!(chromedriver.is_file());
Ok(chromedriver)
);
match cache.download(
installation_allowed,
"chromedriver",
&["chromedriver"],
&url,
)? {
Some(dl) => Ok(dl.binary("chromedriver")),
None => bail!(
"No cached `chromedriver` binary found, and could not find a global \
`chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \
mode."
),
}
}
/// Get the path to an existing `geckodriver`, or install it if no existing
/// binary is found.
pub fn get_or_install_geckodriver(
log: &Logger,
crate_path: &Path,
cache: &Cache,
mode: BuildMode,
) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "geckodriver")) {
(_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_geckodriver(crate_path),
(BuildMode::Force, None) => install_geckodriver(crate_path),
(BuildMode::Noinstall, None) => Err(Error::crate_config(
"No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \
on the `$PATH`. Not installing `geckodriver` because of noinstall mode.",
)
.into()),
if let Ok(path) = which::which("geckodriver") {
return Ok(path);
}
let installation_allowed = match mode {
BuildMode::Noinstall => false,
_ => true,
};
install_geckodriver(cache, installation_allowed)
}
fn get_geckodriver_url() -> Result<String, Error> {
/// Download and install a pre-built `geckodriver` binary.
pub fn install_geckodriver(
cache: &Cache,
installation_allowed: bool,
) -> Result<PathBuf, failure::Error> {
let (target, ext) = if target::LINUX && target::x86 {
("linux32", "tar.gz")
} else if target::LINUX && target::x86_64 {
@ -94,36 +88,21 @@ fn get_geckodriver_url() -> Result<String, Error> {
} else if target::WINDOWS && target::x86_64 {
("win64", "zip")
} else {
return Err(Error::unsupported(
"geckodriver binaries are unavailable for this target",
));
bail!("geckodriver binaries are unavailable for this target")
};
Ok(format!(
let url = format!(
"https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-{}.{}",
target,
ext,
))
}
fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf {
local_bin_path(crate_path, "geckodriver")
}
/// Download and install a pre-built `geckodriver` binary.
pub fn install_geckodriver(crate_path: &Path) -> Result<PathBuf, failure::Error> {
let url = get_geckodriver_url()?;
if url.ends_with("tar.gz") {
install_binaries_from_targz_at_url(crate_path, &url, Some("geckodriver"))?;
} else {
assert!(url.ends_with("zip"));
install_binaries_from_zip_at_url(crate_path, &url, Some("geckodriver"))?;
);
match cache.download(installation_allowed, "geckodriver", &["geckodriver"], &url)? {
Some(dl) => Ok(dl.binary("geckodriver")),
None => bail!(
"No cached `geckodriver` binary found, and could not find a global `geckodriver` \
on the `$PATH`. Not installing `geckodriver` because of noinstall mode."
),
}
let geckodriver = get_local_geckodriver_path(crate_path);
assert!(geckodriver.is_file());
Ok(geckodriver)
}
/// Get the path to an existing `safaridriver`.
@ -131,12 +110,9 @@ pub fn install_geckodriver(crate_path: &Path) -> Result<PathBuf, failure::Error>
/// We can't install `safaridriver` if an existing one is not found because
/// Apple does not provide pre-built binaries. However, `safaridriver` *should*
/// be present by default.
pub fn get_safaridriver(log: &Logger, crate_path: &Path) -> Result<PathBuf, Error> {
if let Some(p) = bin_path(log, crate_path, "safaridriver") {
Ok(p)
} else {
Err(Error::crate_config(
"could not find `safaridriver` on the `$PATH`",
))
pub fn get_safaridriver() -> Result<PathBuf, failure::Error> {
match which::which("safaridriver") {
Ok(p) => Ok(p),
Err(_) => bail!("could not find `safaridriver` on the `$PATH`"),
}
}

@ -1,3 +1,4 @@
use binary_install::Cache;
use tempfile;
use wasm_pack::bindgen;
@ -8,21 +9,11 @@ use wasm_pack::bindgen;
all(windows, target_arch = "x86_64"),
))]
fn can_download_prebuilt_wasm_bindgen() {
use std::env;
let dir = tempfile::TempDir::new().unwrap();
bindgen::download_prebuilt_wasm_bindgen(dir.path(), "0.2.21").unwrap();
assert!(dir
.path()
.join("bin")
.join("wasm-bindgen")
.with_extension(env::consts::EXE_EXTENSION)
.is_file());
assert!(dir
.path()
.join("bin")
.join("wasm-bindgen-test-runner")
.with_extension(env::consts::EXE_EXTENSION)
.is_file());
let cache = Cache::at(dir.path());
let dl = bindgen::download_prebuilt_wasm_bindgen(&cache, "0.2.21", true).unwrap();
assert!(dl.binary("wasm-bindgen").is_file());
assert!(dl.binary("wasm-bindgen-test-runner").is_file())
}
#[test]
@ -34,9 +25,15 @@ fn can_download_prebuilt_wasm_bindgen() {
fn downloading_prebuilt_wasm_bindgen_handles_http_errors() {
let dir = tempfile::TempDir::new().unwrap();
let bad_version = "0.2.21-some-trailing-version-stuff-that-does-not-exist";
let result = bindgen::download_prebuilt_wasm_bindgen(dir.path(), bad_version);
let cache = Cache::at(dir.path());
let result = bindgen::download_prebuilt_wasm_bindgen(&cache, bad_version, true);
assert!(result.is_err());
let error_msg = result.unwrap_err().to_string();
assert!(error_msg.contains("404"));
assert!(error_msg.contains(bad_version));
let error = result.err().unwrap();
assert!(error.iter_chain().any(|e| e.to_string().contains("404")));
assert!(
error
.iter_chain()
.any(|e| e.to_string().contains(bad_version))
);
}

@ -1,6 +1,7 @@
use std::path::Path;
use structopt::StructOpt;
use utils;
use wasm_pack::{command, logger, Cli};
use wasm_pack::Cli;
#[test]
fn build_in_non_crate_directory_doesnt_panic() {
@ -11,14 +12,15 @@ fn build_in_non_crate_directory_doesnt_panic() {
&fixture.path.display().to_string(),
])
.unwrap();
let logger = logger::new(&cli.cmd, cli.verbosity).unwrap();
let result = command::run_wasm_pack(cli.cmd, &logger);
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_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("missing a `Cargo.toml`"));
let err = result.unwrap_err();
assert!(err
.iter_chain()
.any(|e| e.to_string().contains("missing a `Cargo.toml`")));
}
#[test]
@ -31,7 +33,95 @@ fn it_should_build_js_hello_world_example() {
&fixture.path.display().to_string(),
])
.unwrap();
let logger = logger::new(&cli.cmd, cli.verbosity).unwrap();
command::run_wasm_pack(cli.cmd, &logger)
.expect("running wasm-pack in a js-hello-world directory should succeed.");
fixture.run(cli.cmd).unwrap();
}
#[test]
fn it_should_build_crates_in_a_workspace() {
let fixture = utils::fixture::Fixture::new();
fixture
.file(
"Cargo.toml",
r#"
[workspace]
members = ["blah"]
"#,
)
.file(
Path::new("blah").join("Cargo.toml"),
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "blah"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
"#,
)
.file(
Path::new("blah").join("src").join("lib.rs"),
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[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();
}
#[test]
fn renamed_crate_name_works() {
let fixture = utils::fixture::Fixture::new();
fixture
.readme()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
[lib]
crate-type = ["cdylib"]
name = 'bar'
[dependencies]
wasm-bindgen = "=0.2.21"
"#,
)
.file(
"src/lib.rs",
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[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();
}

@ -1,11 +1,13 @@
use utils::fixture;
use wasm_pack::lockfile::Lockfile;
use wasm_pack::manifest::CrateData;
#[test]
fn it_gets_wasm_bindgen_version() {
let fixture = fixture::js_hello_world();
fixture.cargo_check();
let lock = Lockfile::new(&fixture.path).unwrap();
let data = CrateData::new(&fixture.path).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
}
@ -13,7 +15,8 @@ fn it_gets_wasm_bindgen_version() {
fn it_gets_wasm_bindgen_test_version() {
let fixture = fixture::wbg_test_node();
fixture.cargo_check();
let lock = Lockfile::new(&fixture.path).unwrap();
let data = CrateData::new(&fixture.path).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.21"),);
}
@ -57,7 +60,8 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() {
"#,
);
fixture.cargo_check();
let lock = Lockfile::new(&fixture.path.join("blah")).unwrap();
let data = CrateData::new(&fixture.path.join("blah")).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
}
@ -124,6 +128,7 @@ fn it_gets_wasm_bindgen_version_from_dependencies() {
"#,
);
fixture.cargo_check();
let lock = Lockfile::new(&fixture.path.join("parent")).unwrap();
let data = CrateData::new(&fixture.path.join("parent")).unwrap();
let lock = Lockfile::new(&data).unwrap();
assert_eq!(lock.wasm_bindgen_version(), Some("0.2.21"),);
}

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

@ -8,18 +8,16 @@ use wasm_pack::{self, manifest};
#[test]
fn it_gets_the_crate_name_default_path() {
let path = &PathBuf::from(".");
assert!(manifest::get_crate_name(path).is_ok());
assert_eq!(manifest::get_crate_name(path).unwrap(), "wasm-pack");
let crate_data = manifest::CrateData::new(&path).unwrap();
let name = crate_data.crate_name();
assert_eq!(name, "wasm_pack");
}
#[test]
fn it_gets_the_crate_name_provided_path() {
let fixture = fixture::js_hello_world();
assert!(manifest::get_crate_name(&fixture.path).is_ok());
assert_eq!(
manifest::get_crate_name(&fixture.path).unwrap(),
"js-hello-world"
);
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
assert_eq!(crate_data.crate_name(), "js_hello_world");
}
#[test]
@ -27,8 +25,9 @@ fn it_checks_has_cdylib_default_path() {
let fixture = fixture::no_cdylib();
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
assert!(crate_data.check_crate_config(&step).is_err());
}
#[test]
@ -36,15 +35,17 @@ fn it_checks_has_cdylib_provided_path() {
let fixture = fixture::js_hello_world();
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
crate_data.check_crate_config(&step).unwrap();
}
#[test]
fn it_checks_has_cdylib_wrong_crate_type() {
let fixture = fixture::bad_cargo_toml();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
assert!(crate_data.check_crate_config(&step).is_err());
}
#[test]
@ -52,20 +53,24 @@ fn it_recognizes_a_map_during_depcheck() {
let fixture = fixture::serde_feature();
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
crate_data.check_crate_config(&step).unwrap();
}
#[test]
fn it_creates_a_package_json_default_path() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
@ -93,11 +98,14 @@ fn it_creates_a_package_json_default_path() {
fn it_creates_a_package_json_provided_path() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
@ -119,20 +127,15 @@ fn it_creates_a_package_json_provided_path() {
fn it_creates_a_package_json_provided_path_with_scope() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(
&fixture.path,
&out_dir,
&Some("test".to_string()),
false,
"",
&step
)
.is_ok());
assert!(crate_data
.write_package_json(&out_dir, &Some("test".to_string()), false, "", &step)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "@test/js-hello-world");
assert_eq!(pkg.module, "js_hello_world.js");
@ -153,14 +156,14 @@ fn it_creates_a_package_json_provided_path_with_scope() {
fn it_creates_a_pkg_json_with_correct_files_on_node() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(
manifest::write_package_json(&fixture.path, &out_dir, &None, false, "nodejs", &step)
.is_ok()
);
assert!(crate_data
.write_package_json(&out_dir, &None, false, "nodejs", &step)
.is_ok());
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
@ -177,6 +180,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
"js_hello_world_bg.wasm",
"js_hello_world_bg.js",
"js_hello_world.d.ts",
"js_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
@ -188,19 +192,14 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(
&fixture.path,
&out_dir,
&None,
false,
"no-modules",
&step
)
.is_ok());
assert!(crate_data
.write_package_json(&out_dir, &None, false, "no-modules", &step)
.is_ok());
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
@ -228,25 +227,31 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
fn it_creates_a_pkg_json_in_out_dir() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("./custom/out");
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
assert!(crate_data
.write_package_json(&out_dir, &None, false, "", &step)
.is_ok());
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
}
#[test]
fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
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(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, true, "", &step).is_ok());
assert!(crate_data
.write_package_json(&out_dir, &None, true, "", &step)
.is_ok());
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
@ -267,8 +272,9 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
#[test]
fn it_errors_when_wasm_bindgen_is_not_declared() {
let fixture = fixture::bad_cargo_toml();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
assert!(crate_data.check_crate_config(&step).is_err());
}
#[test]
@ -276,6 +282,7 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() {
let fixture = fixture::js_hello_world();
// Ensure that there is a `Cargo.lock`.
fixture.cargo_check();
let crate_data = manifest::CrateData::new(&fixture.path).unwrap();
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
crate_data.check_crate_config(&step).unwrap();
}

@ -1,10 +1,16 @@
use binary_install;
use failure::Error;
use std::env;
use std::fs;
use tempfile;
use utils::fixture;
use wasm_pack::command::{self, build, test, Command};
use wasm_pack::logger;
use wasm_pack::command::{build, test, Command};
fn assert_err<T>(result: Result<T, Error>, 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() {
@ -16,8 +22,7 @@ fn it_can_run_node_tests() {
mode: build::BuildMode::Noinstall,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
command::run_wasm_pack(cmd, &logger).expect("should run test command OK");
fixture.run(cmd).unwrap();
}
#[test]
@ -30,8 +35,7 @@ fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() {
mode: build::BuildMode::Noinstall,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
command::run_wasm_pack(cmd, &logger).expect("should run test command OK");
fixture.run(cmd).unwrap();
}
#[test]
@ -81,8 +85,7 @@ fn it_can_run_browser_tests() {
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
command::run_wasm_pack(cmd, &logger).expect("should run test command OK");
fixture.run(cmd).unwrap();
}
#[test]
@ -95,10 +98,9 @@ fn it_can_run_failing_tests() {
mode: build::BuildMode::Noinstall,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
assert!(
command::run_wasm_pack(cmd, &logger).is_err(),
"failing tests should return Err"
assert_err(
fixture.run(cmd),
"Running Wasm tests with wasm-bindgen-test failed",
);
}
@ -111,34 +113,37 @@ 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();
fixture.install_local_wasm_bindgen();
fixture.install_local_geckodriver();
let geckodriver_dir = tempfile::TempDir::new().unwrap();
let local_geckodriver = binary_install::path::local_bin_path(&fixture.path, "geckodriver");
fs::copy(
&local_geckodriver,
geckodriver_dir
.path()
.join(local_geckodriver.file_name().unwrap()),
)
.unwrap();
fs::remove_file(&local_geckodriver).unwrap();
let local_geckodriver = fixture.install_local_geckodriver();
let local_wasm_bindgen = fixture.install_local_wasm_bindgen();
let mut paths: Vec<_> = env::split_paths(&env::var("PATH").unwrap()).collect();
paths.insert(0, geckodriver_dir.path().into());
env::set_var("PATH", env::join_paths(paths).unwrap());
paths.insert(0, local_geckodriver.parent().unwrap().to_path_buf());
paths.insert(0, local_wasm_bindgen.parent().unwrap().to_path_buf());
let path = env::join_paths(paths).unwrap();
let cmd = Command::Test(test::TestOptions {
path: Some(fixture.path.clone()),
firefox: true,
headless: true,
mode: build::BuildMode::Noinstall,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
command::run_wasm_pack(cmd, &logger).expect("should run test command OK");
let _lock = fixture.lock();
let mut me = env::current_exe().unwrap();
me.pop();
me.pop();
me.push("wasm-pack");
let output = Command::new(&me)
.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());
}
#[test]
@ -152,11 +157,7 @@ fn it_requires_node_or_a_browser() {
// Note: not setting node or any browser to true here.
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
assert!(
command::run_wasm_pack(cmd, &logger).is_err(),
"need to enable node or browser testing"
);
assert_err(fixture.run(cmd), "Must specify at least one of");
}
#[test]
@ -171,11 +172,7 @@ fn the_headless_flag_requires_a_browser() {
headless: true,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
assert!(
command::run_wasm_pack(cmd, &logger).is_err(),
"running headless tests in node doesn't make sense"
);
assert_err(fixture.run(cmd), "only applies to browser tests");
}
#[test]
@ -203,8 +200,7 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
[dev-dependencies]
# no wasm-bindgen-test dep here!
"#,
)
.hello_world_src_lib()
).hello_world_src_lib()
.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions {
@ -213,13 +209,7 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
mode: build::BuildMode::Noinstall,
..Default::default()
});
let logger = logger::new(&cmd, 3).unwrap();
let result = command::run_wasm_pack(cmd, &logger);
assert!(
result.is_err(),
"running tests without wasm-bindgen-test won't work"
);
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
@ -244,10 +234,53 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
// 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 = result.unwrap_err().to_string();
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");
}
#[test]
fn renamed_crate_name_works() {
let fixture = fixture::Fixture::new();
fixture
.readme()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
[lib]
crate-type = ["cdylib"]
name = 'bar'
[dependencies]
wasm-bindgen = "=0.2.21"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
).file(
"src/lib.rs",
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[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();
}

@ -1,22 +1,15 @@
use super::logger::null_logger;
use binary_install;
use binary_install::Cache;
use std::env;
use std::fs;
use std::io;
use std::mem::ManuallyDrop;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::{Once, ONCE_INIT};
use std::sync::{MutexGuard, Once, ONCE_INIT};
use std::thread;
use tempfile::TempDir;
use wasm_pack;
fn hard_link_or_copy<P1: AsRef<Path>, P2: AsRef<Path>>(from: P1, to: P2) -> io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
}
/// A test fixture in a temporary directory.
pub struct Fixture {
// NB: we wrap the fixture's tempdir in a `ManuallyDrop` so that if a test
@ -135,120 +128,63 @@ impl Fixture {
///
/// Takes care not to re-install for every fixture, but only the one time
/// for the whole test suite.
pub fn install_local_wasm_bindgen(&self) -> &Self {
pub fn install_local_wasm_bindgen(&self) -> PathBuf {
static INSTALL_WASM_BINDGEN: Once = ONCE_INIT;
let cache = self.cache();
let version = "0.2.21";
let log = &null_logger();
let download = || {
if let Ok(download) =
wasm_pack::bindgen::download_prebuilt_wasm_bindgen(&cache, version, true)
{
return Ok(download);
}
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
let shared_wasm_bindgen = binary_install::path::local_bin_path(&tests, "wasm-bindgen");
let shared_wasm_bindgen_test_runner =
binary_install::path::local_bin_path(&tests, "wasm-bindgen-test-runner");
wasm_pack::bindgen::cargo_install_wasm_bindgen(log, &cache, version, true)
};
// Only one thread can perform the actual download, and then afterwards
// everything will hit the cache so we can run the same path.
INSTALL_WASM_BINDGEN.call_once(|| {
if shared_wasm_bindgen.is_file() {
assert!(shared_wasm_bindgen_test_runner.is_file());
return;
}
const WASM_BINDGEN_VERSION: &str = "0.2.21";
wasm_pack::bindgen::download_prebuilt_wasm_bindgen(&tests, WASM_BINDGEN_VERSION)
.or_else(|_| {
wasm_pack::bindgen::cargo_install_wasm_bindgen(
&null_logger(),
&tests,
WASM_BINDGEN_VERSION,
)
})
.unwrap();
download().unwrap();
});
assert!(shared_wasm_bindgen.is_file());
assert!(shared_wasm_bindgen_test_runner.is_file());
binary_install::path::ensure_local_bin_dir(&self.path).unwrap();
hard_link_or_copy(
&shared_wasm_bindgen,
binary_install::path::local_bin_path(&self.path, "wasm-bindgen"),
)
.expect("could not copy `wasm-bindgen` to fixture directory");
hard_link_or_copy(
&shared_wasm_bindgen_test_runner,
binary_install::path::local_bin_path(&self.path, "wasm-bindgen-test-runner"),
)
.expect("could not copy `wasm-bindgen-test` to fixture directory");
self
download().unwrap().binary("wasm-bindgen")
}
/// Download `geckodriver` and return its path.
///
/// Takes care to ensure that only one `geckodriver` is downloaded for the whole
/// test suite.
pub fn install_local_geckodriver(&self) -> &Self {
pub fn install_local_geckodriver(&self) -> PathBuf {
static FETCH_GECKODRIVER: Once = ONCE_INIT;
let cache = self.cache();
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
binary_install::path::ensure_local_bin_dir(&tests)
.expect("could not create fixture's `bin` directory");
let geckodriver = binary_install::path::local_bin_path(&tests, "geckodriver");
// like above for synchronization
FETCH_GECKODRIVER.call_once(|| {
if geckodriver.is_file() {
return;
}
wasm_pack::test::webdriver::install_geckodriver(&tests).unwrap();
assert!(geckodriver.is_file());
wasm_pack::test::webdriver::install_geckodriver(&cache, true).unwrap();
});
binary_install::path::ensure_local_bin_dir(&self.path)
.expect("could not create fixture's `bin` directory");
hard_link_or_copy(
&geckodriver,
binary_install::path::local_bin_path(&self.path, "geckodriver"),
)
.expect("could not copy `geckodriver` to fixture directory");
self
wasm_pack::test::webdriver::install_geckodriver(&cache, true).unwrap()
}
/// Download `chromedriver` and return its path.
///
/// Takes care to ensure that only one `chromedriver` is downloaded for the whole
/// test suite.
pub fn install_local_chromedriver(&self) -> &Self {
pub fn install_local_chromedriver(&self) -> PathBuf {
static FETCH_CHROMEDRIVER: Once = ONCE_INIT;
let cache = self.cache();
let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
binary_install::path::ensure_local_bin_dir(&tests)
.expect("could not create fixture's `bin` directory");
let chromedriver = binary_install::path::local_bin_path(&tests, "chromedriver");
// like above for synchronization
FETCH_CHROMEDRIVER.call_once(|| {
if chromedriver.is_file() {
return;
}
wasm_pack::test::webdriver::install_chromedriver(&tests).unwrap();
assert!(chromedriver.is_file());
wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap();
});
wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap()
}
binary_install::path::ensure_local_bin_dir(&self.path)
.expect("could not create fixture's `bin` directory");
hard_link_or_copy(
&chromedriver,
binary_install::path::local_bin_path(&self.path, "chromedriver"),
)
.expect("could not copy `chromedriver` to fixture directory");
self
pub fn cache(&self) -> Cache {
let target_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("target");
Cache::at(&target_dir.join("test_cache"))
}
/// The `step_install_wasm_bindgen` and `step_run_wasm_bindgen` steps only
@ -267,6 +203,32 @@ impl Fixture {
.unwrap();
self
}
pub fn run(&self, cmd: wasm_pack::command::Command) -> Result<(), failure::Error> {
let logger = wasm_pack::logger::new(&cmd, 3)?;
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(&logger)
}
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(&logger)
}
_ => unreachable!(),
}
}
pub fn lock(&self) -> MutexGuard<'static, ()> {
use std::sync::Mutex;
lazy_static! {
static ref ONE_TEST_AT_A_TIME: Mutex<()> = Mutex::new(());
}
ONE_TEST_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner())
}
}
impl Drop for Fixture {
@ -315,7 +277,7 @@ pub fn no_cdylib() -> Fixture {
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "{}"
name = "foo"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
@ -384,8 +346,7 @@ pub fn wbg_test_diff_versions() -> Fixture {
# wasm-bindgen-test at 0.2.19, and everything should still work.
wasm-bindgen-test = "0.2.19"
"#,
)
.file(
).file(
"src/lib.rs",
r#"
extern crate wasm_bindgen;
@ -394,8 +355,7 @@ pub fn wbg_test_diff_versions() -> Fixture {
#[wasm_bindgen]
pub fn one() -> u32 { 1 }
"#,
)
.file(
).file(
"tests/node.rs",
r#"
extern crate wbg_test_diff_versions;

@ -1,3 +1,4 @@
use binary_install::Cache;
use utils::fixture;
use wasm_pack::test::webdriver;
@ -9,7 +10,8 @@ use wasm_pack::test::webdriver;
))]
fn can_install_chromedriver() {
let fixture = fixture::js_hello_world();
assert!(webdriver::install_chromedriver(&fixture.path).is_ok());
let cache = Cache::at(&fixture.path);
assert!(webdriver::install_chromedriver(&cache, true).is_ok());
}
#[test]
@ -22,5 +24,6 @@ fn can_install_chromedriver() {
))]
fn can_install_geckodriver() {
let fixture = fixture::js_hello_world();
assert!(webdriver::install_geckodriver(&fixture.path).is_ok());
let cache = Cache::at(&fixture.path);
assert!(webdriver::install_geckodriver(&cache, true).is_ok());
}

Loading…
Cancel
Save