@ -11,16 +11,21 @@ use lockfile::Lockfile;
use log ::info ;
use manifest ;
use std ::path ::PathBuf ;
use std ::str ::FromStr ;
use std ::time ::Instant ;
use structopt ::clap ::AppSettings ;
use test ::{ self , webdriver } ;
#[ derive(Debug, Default, StructOpt) ]
#[ structopt(
// Allows unknown `--option`s to be parsed as positional arguments, so we can forward it to `cargo`.
setting = AppSettings ::AllowLeadingHyphen ,
// Allows `--` to be parsed as an argument, so we can forward it to `cargo`.
setting = AppSettings ::TrailingVarArg ,
) ]
/// Everything required to configure the `wasm-pack test` command.
pub struct TestOptions {
#[ structopt(parse(from_os_str)) ]
/// The path to the Rust crate. If not set, searches up the path from the current dirctory.
pub path : Option < PathBuf > ,
#[ structopt(long = " node " ) ]
/// Run the tests in Node.js.
pub node : bool ,
@ -74,9 +79,14 @@ pub struct TestOptions {
/// Build with the release profile.
pub release : bool ,
#[ structopt(last = true) ]
/// List of extra options to pass to `cargo test`
pub extra_options : Vec < String > ,
/// Path to the Rust crate, and extra options to pass to `cargo test`.
///
/// If the path is not provided, this command searches up the path from the current dirctory
///
/// This is a workaround to allow wasm pack to provide the same command line interface as `cargo`.
/// See <https://github.com/rustwasm/wasm-pack/pull/851> for more information.
#[ structopt(allow_hyphen_values = true) ]
pub path_and_extra_options : Vec < String > ,
}
/// A configured `wasm-pack test` command.
@ -104,7 +114,6 @@ impl Test {
/// Construct a test command from the given options.
pub fn try_from_opts ( test_opts : TestOptions ) -> Result < Self , Error > {
let TestOptions {
path ,
node ,
mode ,
headless ,
@ -115,9 +124,23 @@ impl Test {
geckodriver ,
safari ,
safaridriver ,
extra_options ,
mut path_and_ extra_options,
} = test_opts ;
let first_arg_is_path = path_and_extra_options
. get ( 0 )
. map ( | first_arg | ! first_arg . starts_with ( "-" ) )
. unwrap_or ( false ) ;
let ( path , extra_options ) = if first_arg_is_path {
let path = PathBuf ::from_str ( & path_and_extra_options . remove ( 0 ) ) ? ;
let extra_options = path_and_extra_options ;
( Some ( path ) , extra_options )
} else {
( None , path_and_extra_options )
} ;
let crate_path = get_crate_path ( path ) ? ;
let crate_data = manifest ::CrateData ::new ( & crate_path , None ) ? ;
let any_browser = chrome | | firefox | | safari ;
@ -243,7 +266,15 @@ impl Test {
fn step_build_tests ( & mut self ) -> Result < ( ) , Error > {
info ! ( "Compiling tests to wasm..." ) ;
build ::cargo_build_wasm_tests ( & self . crate_path , ! self . release ) ? ;
// If the user has run `wasm-pack test -- --features "f1" -- test_name`, then we want to only pass through
// `--features "f1"` to `cargo build`
let extra_options =
if let Some ( index ) = self . extra_options . iter ( ) . position ( | arg | arg = = "--" ) {
& self . extra_options [ .. index ]
} else {
& self . extra_options
} ;
build ::cargo_build_wasm_tests ( & self . crate_path , ! self . release , extra_options ) ? ;
info ! ( "Finished compiling tests to wasm." ) ;
Ok ( ( ) )