Skip to content

Commit

Permalink
Add --ignored to wasm-bindgen-test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 16, 2024
1 parent 6900550 commit 5d9af65
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Support importing memory and using `wasm_bindgen::module()` in Node.js.
[#4349](https://github.com/rustwasm/wasm-bindgen/pull/4349)

* Add `--list` argument to `wasm-bindgen-test-runner`.
* Add `--list` and `ignored` to `wasm-bindgen-test-runner`, analogous to `cargo test`.
[#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356)

### Changed
Expand Down
10 changes: 8 additions & 2 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ struct Cli {
help = "The file to test. `cargo test` passes this argument for you."
)]
file: PathBuf,
#[arg(long, help = "Run ignored tests")]
#[arg(long, conflicts_with = "ignored", help = "Run ignored tests")]
include_ignored: bool,
#[arg(long, conflicts_with = "include_ignored", help = "Run ignored tests")]
ignored: bool,
#[arg(
long,
value_name = "FILTER",
Expand Down Expand Up @@ -67,6 +69,7 @@ struct Cli {
impl Cli {
fn into_args(self) -> String {
let include_ignored = self.include_ignored;
let ignored = self.ignored;
let skip = self.skip;
let filter = if let Some(filter) = self.filter {
&format!("\"{filter}\"")
Expand All @@ -78,6 +81,7 @@ impl Cli {
r#"
// Forward runtime arguments.
cx.include_ignored({include_ignored:?});
cx.ignored({ignored:?});
cx.skip({skip:?});
cx.filter({filter});
"#
Expand Down Expand Up @@ -114,7 +118,9 @@ fn main() -> anyhow::Result<()> {

if cli.list {
for test in tests {
println!("{}: test", test.split_once("::").unwrap().1);
if !cli.ignored || test.starts_with("__wbgt_$") {
println!("{}: test", test.split_once("::").unwrap().1);
}
}

return Ok(());
Expand Down
4 changes: 3 additions & 1 deletion crates/test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ pub fn wasm_bindgen_test(
quote! { cx.execute_sync(test_name, #ident, #should_panic_par, #ignore_par); }
};

let ignore_name = if ignore.is_some() { "$" } else { "" };

let wasm_bindgen_path = attributes.wasm_bindgen_path;
tokens.extend(
quote! {
const _: () = {
#wasm_bindgen_path::__rt::wasm_bindgen::__wbindgen_coverage! {
#[export_name = ::core::concat!("__wbgt_", ::core::module_path!(), "::", ::core::stringify!(#ident))]
#[export_name = ::core::concat!("__wbgt_", #ignore_name, ::core::module_path!(), "::", ::core::stringify!(#ident))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
extern "C" fn __wbgt_test(cx: &#wasm_bindgen_path::__rt::Context) {
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
Expand Down
51 changes: 33 additions & 18 deletions crates/test/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,20 @@ struct State {
/// Include ignored tests.
include_ignored: Cell<bool>,

/// Include ignored tests.
ignored: Cell<bool>,

/// Tests to skip.
skip: RefCell<Vec<String>>,

/// Counter of the number of tests that have succeeded.
succeeded: Cell<usize>,
succeeded_count: Cell<usize>,

/// Counter of the number of tests that have been filtered
filtered: Cell<usize>,
filtered_count: Cell<usize>,

/// Counter of the number of tests that have been ignored
ignored: Cell<usize>,
ignored_count: Cell<usize>,

/// A list of all tests which have failed.
///
Expand Down Expand Up @@ -349,13 +352,14 @@ impl Context {
state: Rc::new(State {
filter: Default::default(),
include_ignored: Default::default(),
ignored: Default::default(),
skip: Default::default(),
failures: Default::default(),
filtered: Default::default(),
ignored: Default::default(),
filtered_count: Default::default(),
ignored_count: Default::default(),
remaining: Default::default(),
running: Default::default(),
succeeded: Default::default(),
succeeded_count: Default::default(),
formatter,
timer,
}),
Expand All @@ -367,6 +371,11 @@ impl Context {
self.state.include_ignored.set(include_ignored);
}

/// Handle `--ignored` flag.
pub fn ignored(&mut self, ignored: bool) {
self.state.ignored.set(ignored);
}

/// Handle `--skip` arguments.
pub fn skip(&mut self, skip: Vec<String>) {
*self.state.skip.borrow_mut() = skip;
Expand Down Expand Up @@ -547,27 +556,33 @@ impl Context {
let filter = self.state.filter.borrow();
if let Some(filter) = &*filter {
if !name.contains(filter) {
let filtered = self.state.filtered.get();
self.state.filtered.set(filtered + 1);
let filtered = self.state.filtered_count.get();
self.state.filtered_count.set(filtered + 1);
return;
}
}

for skip in &*self.state.skip.borrow() {
if name.contains(skip) {
let filtered = self.state.filtered.get();
self.state.filtered.set(filtered + 1);
let filtered = self.state.filtered_count.get();
self.state.filtered_count.set(filtered + 1);
return;
}
}

if !self.state.include_ignored.get() {
if self.state.ignored.get() && ignore.is_none() {
let filtered = self.state.filtered_count.get();
self.state.filtered_count.set(filtered + 1);
return;
}

if !self.state.include_ignored.get() && !self.state.ignored.get() {
if let Some(ignore) = ignore {
self.state
.formatter
.log_test(name, &TestResult::Ignored(ignore.map(str::to_owned)));
let ignored = self.state.ignored.get();
self.state.ignored.set(ignored + 1);
let ignored = self.state.ignored_count.get();
self.state.ignored_count.set(ignored + 1);
return;
}
}
Expand Down Expand Up @@ -665,7 +680,7 @@ impl State {
}

self.formatter.log_test(&test.name, &TestResult::Ok);
self.succeeded.set(self.succeeded.get() + 1);
self.succeeded_count.set(self.succeeded_count.get() + 1);
} else {
self.formatter
.log_test(&test.name, &TestResult::Err(JsValue::NULL));
Expand All @@ -677,7 +692,7 @@ impl State {
self.formatter.log_test(&test.name, &result);

match result {
TestResult::Ok => self.succeeded.set(self.succeeded.get() + 1),
TestResult::Ok => self.succeeded_count.set(self.succeeded_count.get() + 1),
TestResult::Err(e) => self.failures.borrow_mut().push((test, Failure::Error(e))),
_ => (),
}
Expand Down Expand Up @@ -710,10 +725,10 @@ impl State {
{} filtered out\
{}\n",
if failures.len() == 0 { "ok" } else { "FAILED" },
self.succeeded.get(),
self.succeeded_count.get(),
failures.len(),
self.ignored.get(),
self.filtered.get(),
self.ignored_count.get(),
self.filtered_count.get(),
finished_in,
));
}
Expand Down

0 comments on commit 5d9af65

Please sign in to comment.