Skip to content

Commit

Permalink
Add --exact 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 962bc9a commit 14ddf73
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 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` and `ignored` to `wasm-bindgen-test-runner`, analogous to `cargo test`.
* Add `--list`, `--ignored` and `--exact` to `wasm-bindgen-test-runner`, analogous to `cargo test`.
[#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356)

### Changed
Expand Down
12 changes: 11 additions & 1 deletion crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct Cli {
include_ignored: bool,
#[arg(long, conflicts_with = "include_ignored", help = "Run ignored tests")]
ignored: bool,
#[arg(long, help = "Exactly match filters rather than by substring")]
exact: bool,
#[arg(
long,
value_name = "FILTER",
Expand Down Expand Up @@ -70,6 +72,7 @@ impl Cli {
fn into_args(self) -> String {
let include_ignored = self.include_ignored;
let ignored = self.ignored;
let exact = self.exact;
let skip = self.skip;
let filter = if let Some(filter) = self.filter {
&format!("\"{filter}\"")
Expand All @@ -82,6 +85,7 @@ impl Cli {
// Forward runtime arguments.
cx.include_ignored({include_ignored:?});
cx.ignored({ignored:?});
cx.exact({exact:?});
cx.skip({skip:?});
cx.filter({filter});
"#
Expand Down Expand Up @@ -120,7 +124,13 @@ fn main() -> anyhow::Result<()> {
'outer: for test in tests {
if !cli.ignored || test.starts_with("__wbgt_$") {
if let Some(filter) = &cli.filter {
if !test.contains(filter) {
let matches = if cli.exact {
test == *filter
} else {
test.contains(filter)
};

if !matches {
continue;
}
}
Expand Down
19 changes: 18 additions & 1 deletion crates/test/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ struct State {
/// Include ignored tests.
ignored: Cell<bool>,

/// Only execute with exactly matching name.
exact: Cell<bool>,

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

Expand Down Expand Up @@ -353,6 +356,7 @@ impl Context {
filter: Default::default(),
include_ignored: Default::default(),
ignored: Default::default(),
exact: Default::default(),
skip: Default::default(),
failures: Default::default(),
filtered_count: Default::default(),
Expand All @@ -376,6 +380,11 @@ impl Context {
self.state.ignored.set(ignored);
}

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

/// Handle `--skip` arguments.
pub fn skip(&mut self, skip: Vec<String>) {
*self.state.skip.borrow_mut() = skip;
Expand Down Expand Up @@ -555,7 +564,15 @@ impl Context {
// on, nothing to do here.
let filter = self.state.filter.borrow();
if let Some(filter) = &*filter {
if !name.contains(filter) {
let exact = self.state.exact.get();

let matches = if exact {
name == filter
} else {
name.contains(filter)
};

if !matches {
let filtered = self.state.filtered_count.get();
self.state.filtered_count.set(filtered + 1);
return;
Expand Down

0 comments on commit 14ddf73

Please sign in to comment.