tests: Port all of our tests to use `assert_cmd`

master
Nick Fitzgerald 6 years ago committed by Ashley Williams
parent 741bb0107e
commit 5637b07577
  1. 2
      src/manifest/mod.rs
  2. 123
      tests/all/build.rs
  3. 51
      tests/all/manifest.rs
  4. 238
      tests/all/test.rs
  5. 17
      tests/all/utils/fixture.rs

@ -294,7 +294,7 @@ impl CrateData {
pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) { pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) {
manifest_and_keys.unused_keys.iter().for_each(|path| { manifest_and_keys.unused_keys.iter().for_each(|path| {
PBAR.warn(&format!( PBAR.warn(&format!(
"\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.", "\"{}\" is an unknown key and will be ignored. Please check your Cargo.toml.",
path path
)); ));
}); });

@ -1,40 +1,24 @@
use assert_cmd::prelude::*;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use structopt::StructOpt;
use utils; use utils;
use wasm_pack::Cli;
#[test] #[test]
fn build_in_non_crate_directory_doesnt_panic() { fn build_in_non_crate_directory_doesnt_panic() {
let fixture = utils::fixture::not_a_crate(); let fixture = utils::fixture::not_a_crate();
let cli = Cli::from_iter_safe(vec![ fixture
"wasm-pack", .wasm_pack()
"build", .arg("build")
&fixture.path.display().to_string(), .assert()
]) .failure()
.unwrap(); .stderr(predicates::str::contains("missing a `Cargo.toml`"));
let result = fixture.run(cli.cmd);
assert!(
result.is_err(),
"running wasm-pack in a non-crate directory should fail, but it should not panic"
);
let err = result.unwrap_err();
assert!(err
.iter_chain()
.any(|e| e.to_string().contains("missing a `Cargo.toml`")));
} }
#[test] #[test]
fn it_should_build_js_hello_world_example() { fn it_should_build_js_hello_world_example() {
let fixture = utils::fixture::js_hello_world(); let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![ fixture.wasm_pack().arg("build").assert().success();
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
} }
#[test] #[test]
@ -75,15 +59,14 @@ fn it_should_build_crates_in_a_workspace() {
#[wasm_bindgen] #[wasm_bindgen]
pub fn hello() -> u32 { 42 } pub fn hello() -> u32 { 42 }
"#, "#,
); )
fixture.install_local_wasm_bindgen(); .install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![ fixture
"wasm-pack", .wasm_pack()
"build", .current_dir(&fixture.path.join("blah"))
&fixture.path.join("blah").display().to_string(), .arg("build")
]) .assert()
.unwrap(); .success();
fixture.run(cli.cmd).unwrap();
} }
#[test] #[test]
@ -116,28 +99,21 @@ fn renamed_crate_name_works() {
#[wasm_bindgen] #[wasm_bindgen]
pub fn one() -> u32 { 1 } pub fn one() -> u32 { 1 }
"#, "#,
); )
fixture.install_local_wasm_bindgen(); .install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![ fixture.wasm_pack().arg("build").assert().success();
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap();
} }
#[test] #[test]
fn it_should_build_nested_project_with_transitive_dependencies() { fn it_should_build_nested_project_with_transitive_dependencies() {
let fixture = utils::fixture::transitive_dependencies(); let fixture = utils::fixture::transitive_dependencies();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![ fixture
"wasm-pack", .wasm_pack()
"build", .current_dir(fixture.path.join("main"))
&fixture.path.join("main").display().to_string(), .arg("build")
]) .assert()
.unwrap(); .success();
fixture.run(cli.cmd).unwrap();
} }
#[test] #[test]
@ -149,14 +125,12 @@ fn build_different_profiles() {
.iter() .iter()
.cloned() .cloned()
{ {
let cli = Cli::from_iter_safe(vec![ fixture
"wasm-pack", .wasm_pack()
"build", .arg("build")
profile, .arg(profile)
&fixture.path.display().to_string(), .assert()
]) .success();
.unwrap();
fixture.run(cli.cmd).unwrap();
} }
} }
@ -208,17 +182,15 @@ fn build_with_and_without_wasm_bindgen_debug() {
pub fn take(self) {} pub fn take(self) {}
} }
"#, "#,
); )
.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
"--dev",
&fixture.path.display().to_string(),
])
.unwrap();
fixture.run(cli.cmd).unwrap(); fixture
.wasm_pack()
.arg("build")
.arg("--dev")
.assert()
.success();
let contents = fs::read_to_string(fixture.path.join("pkg/whatever.js")).unwrap(); let contents = fs::read_to_string(fixture.path.join("pkg/whatever.js")).unwrap();
assert_eq!( assert_eq!(
@ -233,14 +205,11 @@ fn build_with_and_without_wasm_bindgen_debug() {
fn build_with_arbitrary_cargo_options() { fn build_with_arbitrary_cargo_options() {
let fixture = utils::fixture::js_hello_world(); let fixture = utils::fixture::js_hello_world();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
fixture
let cli = Cli::from_iter_safe(vec![ .wasm_pack()
"wasm-pack", .arg("build")
"build", .arg("--")
&fixture.path.display().to_string(), .arg("--no-default-features")
"--", .assert()
"--no-default-features", .success();
])
.unwrap();
fixture.run(cli.cmd).unwrap();
} }

@ -1,11 +1,9 @@
use assert_cmd::prelude::*;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt;
use utils::{self, fixture}; use utils::{self, fixture};
use wasm_pack::{self, license, manifest, readme, Cli}; use wasm_pack::{self, license, manifest, readme};
#[test] #[test]
fn it_gets_the_crate_name_default_path() { fn it_gets_the_crate_name_default_path() {
@ -370,22 +368,15 @@ fn configure_wasm_bindgen_debug_incorrectly_is_error() {
debug-js-glue = "not a boolean" debug-js-glue = "not a boolean"
"#, "#,
); );
fixture
let cli = Cli::from_iter_safe(vec![ .wasm_pack()
"wasm-pack", .arg("build")
"build", .arg("--dev")
"--dev", .assert()
&fixture.path.display().to_string(), .failure()
]) .stderr(predicates::str::contains(
.unwrap(); "package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug",
));
let result = fixture.run(cli.cmd);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.iter_chain().any(|c| c
.to_string()
.contains("package.metadata.wasm-pack.profile.dev.wasm-bindgen.debug")));
} }
#[test] #[test]
@ -415,15 +406,17 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() {
debug-js-glue = true debug-js-glue = true
"#, "#,
) )
.hello_world_src_lib(); .hello_world_src_lib()
.install_local_wasm_bindgen();
let result = manifest::CrateData::parse_crate_data(&fixture.path.join("Cargo.toml")); fixture
.wasm_pack()
assert!(result.is_ok()); .arg("build")
.assert()
let manifest::ManifestAndUnsedKeys { unused_keys, .. } = result.unwrap(); .success()
.stdout(predicates::str::contains(
assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); "[WARN]: \"package.metadata.wasm-pack.profile.production\" is an unknown key and will \
be ignored. Please check your Cargo.toml."
));
} }
#[test] #[test]

@ -1,43 +1,32 @@
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use failure::Error;
use predicates::prelude::*; use predicates::prelude::*;
use std::env; use std::env;
use utils::fixture; use utils::fixture;
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] #[test]
fn it_can_run_node_tests() { fn it_can_run_node_tests() {
let fixture = fixture::wbg_test_node(); let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions { let _lock = fixture.lock();
path: Some(fixture.path.clone()), fixture
node: true, .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
..Default::default() .arg("--node")
}); .assert()
fixture.run(cmd).unwrap(); .success();
} }
#[test] #[test]
fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() { fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() {
let fixture = fixture::wbg_test_diff_versions(); let fixture = fixture::wbg_test_diff_versions();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions { let _lock = fixture.lock();
path: Some(fixture.path.clone()), fixture
node: true, .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
..Default::default() .arg("--node")
}); .assert()
fixture.run(cmd).unwrap(); .success();
} }
#[test] #[test]
@ -77,33 +66,37 @@ fn it_can_run_browser_tests() {
return; return;
} }
let cmd = Command::Test(test::TestOptions { let mut cmd = fixture.wasm_pack();
path: Some(fixture.path.clone()), cmd.arg("test").arg("--headless");
firefox,
chrome, if firefox {
safari, cmd.arg("--firefox");
headless: true, }
mode: build::BuildMode::Noinstall, if chrome {
..Default::default() cmd.arg("--chrome");
}); }
if safari {
fixture.run(cmd).unwrap(); cmd.arg("--safari");
}
let _lock = fixture.lock();
cmd.assert().success();
} }
#[test] #[test]
fn it_can_run_failing_tests() { fn it_can_run_failing_tests() {
let fixture = fixture::wbg_test_fail(); let fixture = fixture::wbg_test_fail();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions { let _lock = fixture.lock();
path: Some(fixture.path.clone()), fixture
node: true, .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
..Default::default() .arg("--node")
}); .assert()
assert_err( .failure()
fixture.run(cmd), .stderr(predicates::str::contains(
"Running Wasm tests with wasm-bindgen-test failed", "Running Wasm tests with wasm-bindgen-test failed",
); ));
} }
#[test] #[test]
@ -115,8 +108,6 @@ fn it_can_run_failing_tests() {
all(target_os = "windows", target_arch = "x86_64") all(target_os = "windows", target_arch = "x86_64")
))] ))]
fn it_can_find_a_webdriver_on_path() { fn it_can_find_a_webdriver_on_path() {
use std::process::Command;
let fixture = fixture::wbg_test_browser(); let fixture = fixture::wbg_test_browser();
let local_geckodriver = fixture.install_local_geckodriver(); let local_geckodriver = fixture.install_local_geckodriver();
let local_wasm_bindgen = fixture.install_local_wasm_bindgen(); let local_wasm_bindgen = fixture.install_local_wasm_bindgen();
@ -127,54 +118,44 @@ fn it_can_find_a_webdriver_on_path() {
let path = env::join_paths(paths).unwrap(); let path = env::join_paths(paths).unwrap();
let _lock = fixture.lock(); let _lock = fixture.lock();
fixture
let mut me = env::current_exe().unwrap(); .wasm_pack()
me.pop(); .env("PATH", &path)
me.pop();
me.push("wasm-pack");
let output = Command::new(&me)
.arg("test") .arg("test")
.arg("--firefox") .arg("--firefox")
.arg("--headless") .arg("--headless")
.arg("--mode") .arg("--mode")
.arg("no-install") .arg("no-install")
.env("PATH", &path) .assert()
.arg(&fixture.path) .success();
.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] #[test]
fn it_requires_node_or_a_browser() { fn it_requires_node_or_a_browser() {
let fixture = fixture::wbg_test_node(); let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let _lock = fixture.lock();
let cmd = Command::Test(test::TestOptions { fixture
path: Some(fixture.path.clone()), .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
// Note: not setting node or any browser to true here. .assert()
..Default::default() .failure()
}); .stderr(predicates::str::contains("Must specify at least one of"));
assert_err(fixture.run(cmd), "Must specify at least one of");
} }
#[test] #[test]
fn the_headless_flag_requires_a_browser() { fn the_headless_flag_requires_a_browser() {
let fixture = fixture::wbg_test_node(); let fixture = fixture::wbg_test_node();
fixture.install_local_wasm_bindgen(); fixture.install_local_wasm_bindgen();
let _lock = fixture.lock();
let cmd = Command::Test(test::TestOptions { fixture
path: Some(fixture.path.clone()), .wasm_pack()
node: true, .arg("test")
mode: build::BuildMode::Noinstall, .arg("--node")
headless: true, .arg("--headless")
..Default::default() .assert()
}); .failure()
assert_err(fixture.run(cmd), "only applies to browser tests"); .stderr(predicates::str::contains("only applies to browser tests"));
} }
#[test] #[test]
@ -205,44 +186,18 @@ fn complains_about_missing_wasm_bindgen_test_dependency() {
) )
.hello_world_src_lib() .hello_world_src_lib()
.install_local_wasm_bindgen(); .install_local_wasm_bindgen();
let _lock = fixture.lock();
let cmd = Command::Test(test::TestOptions { fixture
path: Some(fixture.path.clone()), .wasm_pack()
node: true, .arg("test")
mode: build::BuildMode::Noinstall, .arg("--node")
..Default::default() .assert()
}); .failure()
let error = assert_err(fixture.run(cmd), "Ensure that you have"); .stderr(predicates::str::contains(
"Ensure that you have \"wasm-bindgen-test\" as a dependency in your Cargo.toml file",
// 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 .stderr(predicates::str::contains("[dev-dependencies]"))
// occurrences, so we find the first and the last and assert that they .stderr(predicates::str::contains("wasm-bindgen-test = \"0.2\""));
// aren't the same occurrence.
//
// This should protect against regresstions where we said:
//
// Ensure that you have "wasm-bindgen" as a dependency in your Cargo.toml file:
// [dev-dependencies]
// wasm-bindgen-test = "0.2"
//
// instead of
//
// Ensure that you have "wasm-bindgen-test" as a dependency in your Cargo.toml file:
// [dev-dependencies]
// wasm-bindgen-test = "0.2"
//
// Note that the whole reason we are doing this string manipulation instead
// of just doing `assert_eq!` is because the first occurrence of the
// dependency name is bolded with terminal escape codes and if I try to pipe
// the output to a text file, then the escape codes go away, so I can't
// figure out which exact escape codes are even used here.
let err_msg = error.to_string();
let first = err_msg.find("wasm-bindgen-test");
assert!(first.is_some());
let second = err_msg.rfind("wasm-bindgen-test");
assert!(second.is_some());
assert_ne!(first, second, "should have found two occurrences");
} }
#[test] #[test]
@ -278,15 +233,15 @@ fn renamed_crate_name_works() {
#[wasm_bindgen] #[wasm_bindgen]
pub fn one() -> u32 { 1 } pub fn one() -> u32 { 1 }
"#, "#,
); )
fixture.install_local_wasm_bindgen(); .install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions { let _lock = fixture.lock();
path: Some(fixture.path.clone()), fixture
node: true, .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
..Default::default() .arg("--node")
}); .assert()
fixture.run(cmd).unwrap(); .success();
} }
#[test] #[test]
@ -326,15 +281,15 @@ fn cdylib_not_required() {
foo::foo(); foo::foo();
} }
"#, "#,
); )
fixture.install_local_wasm_bindgen(); .install_local_wasm_bindgen();
let cmd = Command::Test(test::TestOptions { let _lock = fixture.lock();
path: Some(fixture.path.clone()), fixture
node: true, .wasm_pack()
mode: build::BuildMode::Noinstall, .arg("test")
..Default::default() .arg("--node")
}); .assert()
fixture.run(cmd).unwrap(); .success();
} }
#[test] #[test]
@ -342,7 +297,7 @@ fn test_output_is_printed_once() {
let fixture = fixture::Fixture::new(); let fixture = fixture::Fixture::new();
fixture fixture
.readme() .readme()
.cargo_toml("wbg-test-node") .cargo_toml("test-output-printed-once")
.hello_world_src_lib() .hello_world_src_lib()
.file( .file(
"tests/node.rs", "tests/node.rs",
@ -359,20 +314,21 @@ fn test_output_is_printed_once() {
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
fn pass() { fn yabba() {
log("YABBA DABBA DOO"); log("YABBA DABBA DOO");
assert_eq!(1, 2); assert_eq!(1, 2);
} }
"#, "#,
); )
.install_local_wasm_bindgen();
let _lock = fixture.lock();
fixture fixture
.wasm_pack() .wasm_pack()
.arg("test") .arg("test")
.arg("--node") .arg("--node")
.assert() .assert()
.failure()
.stderr(predicate::function(|err: &str| { .stderr(predicate::function(|err: &str| {
err.matches("YABBA DABBA DOO").count() == 1 err.matches("YABBA DABBA DOO").count() == 1
})) }));
.failure();
} }

@ -278,23 +278,6 @@ impl Fixture {
self self
} }
pub fn run(&self, cmd: wasm_pack::command::Command) -> Result<(), failure::Error> {
match cmd {
wasm_pack::command::Command::Test(cmd) => {
let _lock = self.lock();
let mut test = wasm_pack::command::test::Test::try_from_opts(cmd)?;
test.set_cache(self.cache());
test.run()
}
wasm_pack::command::Command::Build(cmd) => {
let mut build = wasm_pack::command::build::Build::try_from_opts(cmd)?;
build.set_cache(self.cache());
build.run()
}
_ => unreachable!(),
}
}
/// Get a `wasm-pack` command configured to run in this fixure's temp /// Get a `wasm-pack` command configured to run in this fixure's temp
/// directory and using the test cache. /// directory and using the test cache.
pub fn wasm_pack(&self) -> Command { pub fn wasm_pack(&self) -> Command {

Loading…
Cancel
Save