diff --git a/CHANGELOG.md b/CHANGELOG.md index 524561857fe..d518f611665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs index 7d33d7b4208..a0ac01b39c8 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -13,6 +13,7 @@ use anyhow::{anyhow, bail, Context}; use clap::Parser; +use clap::ValueEnum; use log::error; use std::env; use std::fs; @@ -43,6 +44,17 @@ struct Cli { help = "Skip tests whose names contain FILTER (this flag can be used multiple times)" )] skip: Vec, + /// 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, #[arg( index = 2, value_name = "FILTER", @@ -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/...` @@ -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. @@ -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, +} diff --git a/crates/test-macro/src/lib.rs b/crates/test-macro/src/lib.rs index 510944ccd9a..16609ad8f12 100644 --- a/crates/test-macro/src/lib.rs +++ b/crates/test-macro/src/lib.rs @@ -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( @@ -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 }