Skip to content

Commit d89b606

Browse files
committedDec 19, 2024
Transition to wasm-bindgen-test/unstable-test-coverage
1 parent e464ba3 commit d89b606

File tree

19 files changed

+130
-113
lines changed

19 files changed

+130
-113
lines changed
 

‎CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
* `console.*()` calls in tests are now always intercepted by default. To show them use `--nocapture`. When shown they are always printed in-place instead of after test results, analogous to `cargo test`.
4141
[#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356)
4242

43+
* Instrumentation is now disabled on internal functions with the `msrv` crate feature instead of `cfg(wasm_bindgen_unstable_test_coverage)`.
44+
[#4369](https://github.com/rustwasm/wasm-bindgen/pull/4369)
45+
46+
* Replaced `cfg(wasm_bindgen_unstable_test_coverage)` with crate feature `unstable-test-coverage` on `wasm-bindgen-test`.
47+
[#4369](https://github.com/rustwasm/wasm-bindgen/pull/4369)
48+
4349
### Fixed
4450

4551
- Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly.
@@ -67,6 +73,9 @@
6773
* Internal functions are now removed instead of invalidly imported if they are unused.
6874
[#4366](https://github.com/rustwasm/wasm-bindgen/pull/4366)
6975

76+
* No coverage data is emitted when the module is not instrumented even if `unstable-test-coverage` is enabled.
77+
[#4369](https://github.com/rustwasm/wasm-bindgen/pull/4369)
78+
7079
--------------------------------------------------------------------------------
7180

7281
## [0.2.99](https://github.com/rustwasm/wasm-bindgen/compare/0.2.98...0.2.99)

‎Cargo.toml

+4-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std = []
3333
# The current rustc version is detected at compile-time, so enabling this
3434
# feature for older compilers will NOT result in a compilation error. Instead,
3535
# any unsupported language feature will not be used.
36-
msrv = ["rustversion"]
36+
msrv = ["rustversion", "wasm-bindgen-macro/msrv"]
3737

3838
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
3939
# all unused attributes
@@ -67,15 +67,8 @@ wasm-bindgen-futures = { path = 'crates/futures' }
6767
wasm-bindgen-test-crate-a = { path = 'tests/crates/a' }
6868
wasm-bindgen-test-crate-b = { path = 'tests/crates/b' }
6969

70-
[lints.rust]
71-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }
72-
73-
[lints.clippy]
74-
large_enum_variant = "allow"
75-
new_without_default = "allow"
76-
overly_complex_bool_expr = "allow"
77-
too_many_arguments = "allow"
78-
type_complexity = "allow"
70+
[lints]
71+
workspace = true
7972

8073
[workspace.lints.clippy]
8174
large_enum_variant = "allow"
@@ -135,6 +128,7 @@ resolver = "2"
135128

136129
[patch.crates-io]
137130
js-sys = { path = 'crates/js-sys' }
131+
minicov = { git = 'https://github.com/daxpedda/minicov', branch = 'web-time' }
138132
wasm-bindgen = { path = '.' }
139133
wasm-bindgen-futures = { path = 'crates/futures' }
140134
web-sys = { path = 'crates/web-sys' }

‎crates/backend/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version = "0.2.99"
1515

1616
[features]
1717
extra-traits = ["syn/extra-traits"]
18+
msrv = []
1819

1920
[dependencies]
2021
bumpalo = "3.0.0"

‎crates/backend/src/codegen.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ impl ToTokens for ast::Struct {
223223
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
224224
let unwrap_fn = Ident::new(&shared::unwrap_function(&name_str), Span::call_site());
225225
let wasm_bindgen = &self.wasm_bindgen;
226+
let coverage = coverage(wasm_bindgen);
226227
(quote! {
227228
#[automatically_derived]
228229
impl #wasm_bindgen::__rt::marker::SupportsConstructor for #name {}
@@ -301,9 +302,9 @@ impl ToTokens for ast::Struct {
301302
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
302303
#[automatically_derived]
303304
const _: () = {
304-
#wasm_bindgen::__wbindgen_coverage! {
305305
#[no_mangle]
306306
#[doc(hidden)]
307+
#coverage
307308
// `allow_delayed` is whether it's ok to not actually free the `ptr` immediately
308309
// if it's still borrowed.
309310
pub unsafe extern "C" fn #free_fn(ptr: u32, allow_delayed: u32) {
@@ -320,7 +321,6 @@ impl ToTokens for ast::Struct {
320321
let _ = <#name as #wasm_bindgen::convert::FromWasmAbi>::from_abi(ptr);
321322
}
322323
}
323-
}
324324
};
325325

326326
#[automatically_derived]
@@ -496,13 +496,14 @@ impl ToTokens for ast::StructField {
496496
}
497497

498498
let wasm_bindgen = &self.wasm_bindgen;
499+
let coverage = coverage(wasm_bindgen);
499500

500501
(quote! {
501502
#[automatically_derived]
502503
const _: () = {
503-
#wasm_bindgen::__wbindgen_coverage! {
504504
#[cfg_attr(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")), no_mangle)]
505505
#[doc(hidden)]
506+
#coverage
506507
pub unsafe extern "C" fn #getter(js: u32)
507508
-> #wasm_bindgen::convert::WasmRet<<#ty as #wasm_bindgen::convert::IntoWasmAbi>::Abi>
508509
{
@@ -517,7 +518,6 @@ impl ToTokens for ast::StructField {
517518
let val = #val;
518519
<#ty as IntoWasmAbi>::into_abi(val).into()
519520
}
520-
}
521521
};
522522
})
523523
.to_tokens(tokens);
@@ -543,9 +543,9 @@ impl ToTokens for ast::StructField {
543543
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
544544
#[automatically_derived]
545545
const _: () = {
546-
#wasm_bindgen::__wbindgen_coverage! {
547546
#[no_mangle]
548547
#[doc(hidden)]
548+
#coverage
549549
pub unsafe extern "C" fn #setter(
550550
js: u32,
551551
#(#args,)*
@@ -559,7 +559,6 @@ impl ToTokens for ast::StructField {
559559
let val = <#ty as FromWasmAbi>::from_abi(val);
560560
(*js).borrow_mut().#rust_name = val;
561561
}
562-
}
563562
};
564563
})
565564
.to_tokens(tokens);
@@ -584,6 +583,7 @@ impl TryToTokens for ast::Export {
584583

585584
let name = &self.rust_name;
586585
let wasm_bindgen = &self.wasm_bindgen;
586+
let coverage = coverage(wasm_bindgen);
587587
let wasm_bindgen_futures = &self.wasm_bindgen_futures;
588588
let receiver = match self.method_self {
589589
Some(ast::MethodSelf::ByValue) => {
@@ -827,12 +827,12 @@ impl TryToTokens for ast::Export {
827827
(quote! {
828828
#[automatically_derived]
829829
const _: () = {
830-
#wasm_bindgen::__wbindgen_coverage! {
831830
#(#attrs)*
832831
#[cfg_attr(
833832
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
834833
export_name = #export_name,
835834
)]
835+
#coverage
836836
pub unsafe extern "C" fn #generated_name(#(#args),*) -> #wasm_bindgen::convert::WasmRet<#projection::Abi> {
837837
const _: () = {
838838
#(#checks)*
@@ -841,7 +841,6 @@ impl TryToTokens for ast::Export {
841841
let #ret = #call;
842842
#convert_ret
843843
}
844-
}
845844
};
846845
})
847846
.to_tokens(into);
@@ -1870,21 +1869,21 @@ impl<T: ToTokens> ToTokens for Descriptor<'_, T> {
18701869
let inner = &self.inner;
18711870
let attrs = &self.attrs;
18721871
let wasm_bindgen = &self.wasm_bindgen;
1872+
let coverage = coverage(wasm_bindgen);
18731873
(quote! {
18741874
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
18751875
#[automatically_derived]
18761876
const _: () = {
1877-
#wasm_bindgen::__wbindgen_coverage! {
18781877
#(#attrs)*
18791878
#[no_mangle]
18801879
#[doc(hidden)]
1880+
#coverage
18811881
pub extern "C" fn #name() {
18821882
use #wasm_bindgen::describe::*;
18831883
// See definition of `link_mem_intrinsics` for what this is doing
18841884
#wasm_bindgen::__rt::link_mem_intrinsics();
18851885
#inner
18861886
}
1887-
}
18881887
};
18891888
})
18901889
.to_tokens(tokens);
@@ -1969,3 +1968,15 @@ fn respan(input: TokenStream, span: &dyn ToTokens) -> TokenStream {
19691968
}
19701969
new_tokens.into_iter().collect()
19711970
}
1971+
1972+
#[cfg(feature = "msrv")]
1973+
fn coverage(wasm_bindgen: &syn::Path) -> TokenStream {
1974+
quote! {
1975+
#[#wasm_bindgen::__rt::rustversion::attr(since(2024-12-18), coverage(off))]
1976+
}
1977+
}
1978+
1979+
#[cfg(not(feature = "msrv"))]
1980+
fn coverage(_: &syn::Path) -> TokenStream {
1981+
TokenStream::new()
1982+
}

‎crates/macro-support/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version = "0.2.99"
1515

1616
[features]
1717
extra-traits = ["syn/extra-traits"]
18+
msrv = ["wasm-bindgen-backend/msrv"]
1819
strict-macro = []
1920

2021
[dependencies]

‎crates/macro/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ version = "0.2.99"
1717
proc-macro = true
1818

1919
[features]
20+
msrv = ["wasm-bindgen-macro-support/msrv"]
2021
strict-macro = ["wasm-bindgen-macro-support/strict-macro"]
2122
xxx_debug_only_print_generated_code = []
2223

‎crates/macro/ui-tests/wasm-bindgen.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
error[E0433]: failed to resolve: could not find `__wbindgen_coverage` in `test`
1+
error[E0433]: failed to resolve: could not find `__rt` in `test`
22
--> ui-tests/wasm-bindgen.rs:37:1
33
|
44
37 | #[wasm_bindgen(wasm_bindgen = test)]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `__wbindgen_coverage` in `test`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `__rt` in `test`
66
|
77
= note: this error originates in the attribute macro `wasm_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
error[E0433]: failed to resolve: could not find `convert` in `test`
10+
--> ui-tests/wasm-bindgen.rs:37:1
11+
|
12+
37 | #[wasm_bindgen(wasm_bindgen = test)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `convert` in `test`
14+
|
15+
= note: this error originates in the attribute macro `wasm_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider importing one of these items
17+
|
18+
3 + use crate::extern_test::convert;
19+
|
20+
3 + use wasm_bindgen::convert;
21+
|
22+
923
error[E0425]: cannot find function `future_to_promise` in module `test`
1024
--> ui-tests/wasm-bindgen.rs:40:1
1125
|

‎crates/test-macro/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ version = "0.3.49"
1212
[lib]
1313
proc-macro = true
1414

15+
[features]
16+
msrv = []
17+
1518
[dependencies]
1619
proc-macro2 = "1.0"
1720
quote = "1.0"

‎crates/test-macro/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,22 @@ pub fn wasm_bindgen_test(
9393
let ignore_name = if ignore.is_some() { "$" } else { "" };
9494

9595
let wasm_bindgen_path = attributes.wasm_bindgen_path;
96+
#[cfg(feature = "msrv")]
97+
let coverage = quote! {
98+
#[#wasm_bindgen_path::__rt::wasm_bindgen::__rt::rustversion::attr(since(2024-12-18), coverage(off))]
99+
};
100+
#[cfg(not(feature = "msrv"))]
101+
let coverage = TokenStream::new();
96102
tokens.extend(
97103
quote! {
98104
const _: () = {
99-
#wasm_bindgen_path::__rt::wasm_bindgen::__wbindgen_coverage! {
100105
#[export_name = ::core::concat!("__wbgt_", #ignore_name, ::core::module_path!(), "::", ::core::stringify!(#ident))]
101106
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
107+
#coverage
102108
extern "C" fn __wbgt_test(cx: &#wasm_bindgen_path::__rt::Context) {
103109
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
104110
#test_body
105111
}
106-
}
107112
};
108113
},
109114
);

‎crates/test/Cargo.toml

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ version = "0.3.49"
1111

1212
[features]
1313
default = ["std"]
14+
msrv = ["wasm-bindgen/msrv", "wasm-bindgen-test-macro/msrv"]
1415
std = ["wasm-bindgen/std", "js-sys/std", "wasm-bindgen-futures/std"]
1516

1617
[dependencies]
@@ -20,18 +21,11 @@ wasm-bindgen = { path = '../..', version = '=0.2.99', default-features = false }
2021
wasm-bindgen-futures = { path = '../futures', version = '=0.4.49', default-features = false }
2122
wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.49' }
2223

23-
[target.'cfg(all(target_arch = "wasm32", wasm_bindgen_unstable_test_coverage))'.dependencies]
24-
minicov = "0.3"
24+
[target.'cfg(target_arch = "wasm32")'.dependencies]
25+
unstable-test-coverage = { package = "minicov", version = "0.3", optional = true }
2526

26-
[lints.rust]
27-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }
28-
29-
[lints.clippy]
30-
large_enum_variant = "allow"
31-
new_without_default = "allow"
32-
overly_complex_bool_expr = "allow"
33-
too_many_arguments = "allow"
34-
type_complexity = "allow"
27+
[lints]
28+
workspace = true
3529

3630
[lib]
3731
test = false

‎crates/test/src/coverage.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
use alloc::vec::Vec;
22
use wasm_bindgen::prelude::wasm_bindgen;
33

4-
#[cfg(wasm_bindgen_unstable_test_coverage)]
4+
#[cfg(feature = "unstable-test-coverage")]
55
#[wasm_bindgen]
66
pub fn __wbgtest_cov_dump() -> Option<Vec<u8>> {
77
let mut coverage = Vec::new();
8-
// SAFETY: this function is not thread-safe, but our whole test runner is running single-threaded.
9-
unsafe {
10-
minicov::capture_coverage(&mut coverage).unwrap();
11-
}
12-
if coverage.is_empty() {
13-
console_error!(
14-
"Empty coverage data received. Make sure you compile the tests with
15-
RUSTFLAGS=\"-Cinstrument-coverage -Zno-profile-runtime --emit=llvm-ir\"",
16-
);
8+
9+
if unstable_test_coverage::counters() > 0 {
10+
// SAFETY: this function is not thread-safe, but our whole test runner is running single-threaded.
11+
unsafe {
12+
unstable_test_coverage::capture_coverage(&mut coverage).unwrap();
13+
}
14+
15+
Some(coverage)
16+
} else {
17+
None
1718
}
18-
Some(coverage)
1919
}
2020

21-
#[cfg(not(wasm_bindgen_unstable_test_coverage))]
21+
#[cfg(not(feature = "unstable-test-coverage"))]
2222
#[wasm_bindgen]
2323
pub fn __wbgtest_cov_dump() -> Option<Vec<u8>> {
2424
None

‎guide/src/wasm-bindgen-test/coverage.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You can ask the runner to generate coverage data from functions marked as `#[was
99

1010
## Enabling the feature
1111

12-
To enable this feature, you need to enable `cfg(wasm_bindgen_unstable_test_coverage)`.
12+
To enable this feature, you need to enable the crate feature `unstable-test-coverage` on `wasm-bindgen-test`. Additionally its recommended to use the `msrv` crate feature on `wasm-bindgen-test` as well to disable profiling on internal functions, significantly improving the accuracy of the test coverage results.
1313

1414
## Generating the data
1515

@@ -43,7 +43,7 @@ This adapts code taken from the [Rustc book], see that for more examples and gen
4343
```sh
4444
# Run the tests:
4545
# `--tests` to not run documentation tests, which is currently not supported.
46-
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
46+
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir" \
4747
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner \
4848
cargo +nightly test --tests
4949
# Compile to object files:
@@ -54,7 +54,7 @@ crate_name=name_of_the_tested_crate_in_snake_case
5454
objects=()
5555
IFS=$'\n'
5656
for file in $(
57-
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
57+
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir" \
5858
cargo +nightly test --tests --no-run --message-format=json | \
5959
jq -r "select(.reason == \"compiler-artifact\") | (select(.target.kind == [\"test\"]) // select(.target.name == \"$crate_name\")) | .filenames[0]"
6060
)

0 commit comments

Comments
 (0)
Failed to load comments.