Skip to content

Commit

Permalink
feature: Implement a wasm-pack test subcommand
Browse files Browse the repository at this point in the history
This is a wrapper over `cargo test --target wasm32-unknown-unknown` and the
`wasm-bindgen-test` crate.

Fixes #248
  • Loading branch information
fitzgen committed Aug 28, 2018
1 parent d5bf5a3 commit a6ad544
Show file tree
Hide file tree
Showing 24 changed files with 771 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ target/
**/*.rs.bk
**/pkg
tests/fixtures/**/Cargo.lock
tests/.crates.toml
tests/bin
wasm-pack.log
50 changes: 36 additions & 14 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use emoji;
use error::Error;
use progressbar::Step;
use slog::Logger;
use std::path::{Path, PathBuf};
use std::process::Command;
use which::which;
Expand All @@ -14,10 +15,11 @@ pub fn cargo_install_wasm_bindgen(
version: &str,
install_permitted: bool,
step: &Step,
log: &Logger,
) -> Result<(), Error> {
// If the `wasm-bindgen` dependency is already met, print a message and return.
if wasm_bindgen_path(path)
.map(|bindgen_path| wasm_bindgen_version_check(&bindgen_path, version))
.map(|bindgen_path| wasm_bindgen_version_check(&bindgen_path, version, log))
.unwrap_or(false)
{
let msg = format!("{}wasm-bindgen already installed...", emoji::DOWN_ARROW);
Expand Down Expand Up @@ -105,7 +107,7 @@ pub fn wasm_bindgen_build(
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool {
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &Logger) -> bool {
Command::new(bindgen_path)
.arg("--version")
.output()
Expand All @@ -116,34 +118,54 @@ fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool
.trim()
.split_whitespace()
.nth(1)
.map(|v| v == dep_version)
.unwrap_or(false)
.map(|v| {
info!(
log,
"Checking installed `wasm-bindgen` version == expected version: {} == {}",
v,
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(crate_path: &Path) -> Option<PathBuf> {
// Return the path to the local `wasm-bindgen`, if it exists.
let local_bindgen_path = |crate_path: &Path| -> Option<PathBuf> {
fn local_or_global(crate_path: &Path, bin: &str) -> Option<PathBuf> {
// Return the path to the local binary, if it exists.
let local_path = |crate_path: &Path| -> Option<PathBuf> {
let mut p = crate_path.to_path_buf();
p.push("bin");
p.push("wasm-bindgen");
p.push(bin);
if p.is_file() {
Some(p)
} else {
None
}
};

// Return the path to the global `wasm-bindgen`, if it exists.
let global_bindgen_path = || -> Option<PathBuf> {
if let Ok(p) = which("wasm-bindgen") {
// Return the path to the global binary, if it exists.
let global_path = || -> Option<PathBuf> {
if let Ok(p) = which(bin) {
Some(p)
} else {
None
}
};

local_bindgen_path(crate_path).or_else(global_bindgen_path)
local_path(crate_path)
.or_else(global_path)
.map(|p| p.canonicalize().unwrap_or(p))
}

/// 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(crate_path: &Path) -> Option<PathBuf> {
local_or_global(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(crate_path: &Path) -> Option<PathBuf> {
local_or_global(crate_path, "wasm-bindgen-test-runner")
}
32 changes: 25 additions & 7 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Implementation of the `wasm-pack build` command.

use bindgen;
use build;
use command::utils::{create_pkg_dir, set_crate_path};
Expand All @@ -9,6 +11,7 @@ use progressbar::Step;
use readme;
use slog::Logger;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Instant;
use PBAR;

Expand All @@ -26,6 +29,7 @@ pub(crate) struct Build {

/// The `BuildMode` determines which mode of initialization we are running, and
/// what build and install steps we perform.
#[derive(Clone, Copy, Debug)]
pub enum BuildMode {
/// Perform all the build and install steps.
Normal,
Expand All @@ -34,6 +38,23 @@ pub enum BuildMode {
Noinstall,
}

impl Default for BuildMode {
fn default() -> BuildMode {
BuildMode::Normal
}
}

impl FromStr for BuildMode {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
match s {
"no-install" => Ok(BuildMode::Noinstall),
"normal" => Ok(BuildMode::Normal),
_ => Error::crate_config(&format!("Unknown build mode: {}", s)).map(|_| unreachable!()),
}
}
}

/// Everything required to configure and run the `wasm-pack build` command.
#[derive(Debug, StructOpt)]
pub struct BuildOptions {
Expand All @@ -47,7 +68,7 @@ pub struct BuildOptions {

#[structopt(long = "mode", short = "m", default_value = "normal")]
/// Sets steps to be run. [possible values: no-install, normal]
pub mode: String,
pub mode: BuildMode,

#[structopt(long = "no-typescript")]
/// By default a *.d.ts file is generated for the generated JS file, but
Expand All @@ -70,20 +91,16 @@ type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
impl Build {
/// Construct a build command from the given options.
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
let crate_path = set_crate_path(build_opts.path);
let crate_path = set_crate_path(build_opts.path)?;
let crate_name = manifest::get_crate_name(&crate_path)?;
let mode = match build_opts.mode.as_str() {
"no-install" => BuildMode::Noinstall,
_ => BuildMode::Normal,
};
// let build_config = manifest::xxx(&crate_path).xxx();
Ok(Build {
crate_path,
scope: build_opts.scope,
disable_dts: build_opts.disable_dts,
target: build_opts.target,
debug: build_opts.debug,
mode,
mode: build_opts.mode,
// build_config,
crate_name,
})
Expand Down Expand Up @@ -231,6 +248,7 @@ impl Build {
&bindgen_version,
install_permitted,
step,
log,
)?;
info!(&log, "Installing wasm-bindgen-cli was successful.");

Expand Down
12 changes: 11 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! CLI command structures, parsing, and execution.

mod build;
pub mod build;
mod login;
mod pack;
mod publish;
pub mod test;
pub mod utils;

use self::build::{Build, BuildOptions};
use self::login::login;
use self::pack::pack;
use self::publish::publish;
use self::test::{Test, TestOptions};
use error::Error;
use slog::Logger;
use std::path::PathBuf;
Expand Down Expand Up @@ -70,6 +72,10 @@ pub enum Command {
/// strategies besides classic username/password entry in legacy npm.
auth_type: Option<String>,
},

#[structopt(name = "test")]
/// 👩‍🔬 test your wasm!
Test(TestOptions),
}

/// Run a command with the given logger!
Expand Down Expand Up @@ -108,6 +114,10 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
);
login(registry, scope, always_auth, auth_type, &log)
}
Command::Test(test_opts) => {
info!(&log, "Running test command...");
Test::try_from_opts(test_opts).and_then(|t| t.run(&log))
}
};

match status {
Expand Down
2 changes: 1 addition & 1 deletion src/command/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use PBAR;
/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path);
let crate_path = set_crate_path(path)?;

info!(&log, "Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
Expand Down
2 changes: 1 addition & 1 deletion src/command/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use PBAR;
/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path);
let crate_path = set_crate_path(path)?;

info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
Expand Down
Loading

0 comments on commit a6ad544

Please sign in to comment.