Skip to content

Commit

Permalink
Add --list argument to `wasm-bindgen-test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 15, 2024
1 parent 7e5f2f1 commit f6cfc41
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* 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`.
[#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356)

### Changed

* Optional parameters are now typed as `T | undefined | null` to reflect the actual JS behavior.
Expand Down
56 changes: 41 additions & 15 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use anyhow::{anyhow, bail, Context};
use clap::Parser;
use clap::ValueEnum;
use log::error;
use std::env;
use std::fs;
Expand Down Expand Up @@ -43,6 +44,17 @@ struct Cli {
help = "Skip tests whose names contain FILTER (this flag can be used multiple times)"
)]
skip: Vec<String>,
/// Only list all tests and benchmarks.
#[arg(long = "list", help = "List all tests and benchmarks")]
list: bool,
/// Specifies the format of the output.
#[arg(
long = "format",
value_enum,
value_name = "terse",
help = "Configure formatting of output"
)]
format: Option<FormatSetting>,
#[arg(
index = 2,
value_name = "FILTER",
Expand Down Expand Up @@ -86,6 +98,28 @@ fn main() -> anyhow::Result<()> {
.and_then(|s| s.to_str())
.context("file to test is not a valid file, can't extract file name")?;

// Collect all tests that the test harness is supposed to run. We assume
// that any exported function with the prefix `__wbg_test` is a test we need
// to execute.
let wasm = fs::read(&cli.file).context("failed to read Wasm file")?;
let mut wasm =
walrus::Module::from_buffer(&wasm).context("failed to deserialize Wasm module")?;
let mut tests = Vec::new();

for export in wasm.exports.iter() {
if export.name.starts_with("__wbgt_") {
tests.push(export.name.to_string());
}
}

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

return Ok(());
}

// wasm_file_to_test may be
// - a cargo-like directory layout and generate output at
// `target/wasm32-unknown-unknown/...`
Expand All @@ -112,21 +146,6 @@ fn main() -> anyhow::Result<()> {

let module = "wasm-bindgen-test";

// Collect all tests that the test harness is supposed to run. We assume
// that any exported function with the prefix `__wbg_test` is a test we need
// to execute.
let wasm = fs::read(&cli.file).context("failed to read Wasm file")?;
let mut wasm =
walrus::Module::from_buffer(&wasm).context("failed to deserialize Wasm module")?;
let mut tests = Vec::new();

for export in wasm.exports.iter() {
if !export.name.starts_with("__wbgt_") {
continue;
}
tests.push(export.name.to_string());
}

// Right now there's a bug where if no tests are present then the
// `wasm-bindgen-test` runtime support isn't linked in, so just bail out
// early saying everything is ok.
Expand Down Expand Up @@ -386,3 +405,10 @@ fn coverage_args(tmpdir: &Path) -> PathBuf {
None => PathBuf::from(generated(tmpdir, &prefix)),
}
}

/// Possible values for the `--format` option.
#[derive(Debug, Clone, Copy, ValueEnum)]
enum FormatSetting {
/// Display one character per test
Terse,
}
12 changes: 2 additions & 10 deletions crates/test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
extern crate proc_macro;

use proc_macro2::*;
use quote::format_ident;
use quote::quote;
use quote::quote_spanned;
use std::sync::atomic::*;

static CNT: AtomicUsize = AtomicUsize::new(0);

#[proc_macro_attribute]
pub fn wasm_bindgen_test(
Expand Down Expand Up @@ -94,18 +90,14 @@ pub fn wasm_bindgen_test(
quote! { cx.execute_sync(test_name, #ident, #should_panic_par, #ignore); }
};

// We generate a `#[no_mangle]` with a known prefix so the test harness can
// later slurp up all of these functions and pass them as arguments to the
// main test harness. This is the entry point for all tests.
let name = format_ident!("__wbgt_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst));
let wasm_bindgen_path = attributes.wasm_bindgen_path;
tokens.extend(
quote! {
const _: () = {
#wasm_bindgen_path::__rt::wasm_bindgen::__wbindgen_coverage! {
#[no_mangle]
#[export_name = concat!("__wbgt_", ::core::module_path!(), "::", ::core::stringify!(#ident))]
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
pub extern "C" fn #name(cx: &#wasm_bindgen_path::__rt::Context) {
extern "C" fn __wbgt_test(cx: &#wasm_bindgen_path::__rt::Context) {
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
#test_body
}
Expand Down

0 comments on commit f6cfc41

Please sign in to comment.