Skip to content

Commit b650488

Browse files
committed
Refactor test coverage enabling in proc-macros to crate feature
1 parent 09bda50 commit b650488

File tree

10 files changed

+43
-81
lines changed

10 files changed

+43
-81
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-featu
5252
"atomics",
5353
] }
5454

55+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"), wasm_bindgen_unstable_test_coverage))'.dependencies]
56+
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.95", default-features = false, features = [
57+
"coverage",
58+
] }
59+
5560
[dev-dependencies]
5661
wasm-bindgen-test = { path = 'crates/test' }
5762

crates/backend/Cargo.toml

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

1616
[features]
1717
atomics = []
18+
coverage = []
1819
default = ["std"]
1920
extra-traits = ["syn/extra-traits"]
2021
spans = []
@@ -28,6 +29,3 @@ proc-macro2 = "1.0"
2829
quote = '1.0'
2930
syn = { version = '2.0', features = ['full'] }
3031
wasm-bindgen-shared = { path = "../shared", version = "=0.2.95" }
31-
32-
[lints.rust]
33-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

crates/backend/src/codegen.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,7 @@ impl ToTokens for ast::Struct {
224224
let free_fn = Ident::new(&shared::free_function(&name_str), Span::call_site());
225225
let unwrap_fn = Ident::new(&shared::unwrap_function(&name_str), Span::call_site());
226226
let wasm_bindgen = &self.wasm_bindgen;
227-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
228-
Some(quote! { #[coverage(off)] })
229-
} else {
230-
None
231-
};
227+
let maybe_no_coverage = coverage();
232228
(quote! {
233229
#[automatically_derived]
234230
impl #wasm_bindgen::describe::WasmDescribe for #name {
@@ -481,11 +477,7 @@ impl ToTokens for ast::StructField {
481477
quote! { assert_copy::<#ty>() }
482478
};
483479
let maybe_assert_copy = respan(maybe_assert_copy, ty);
484-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
485-
Some(quote! { #[coverage(off)] })
486-
} else {
487-
None
488-
};
480+
let maybe_no_coverage = coverage();
489481

490482
// Split this out so that it isn't affected by `quote_spanned!`.
491483
//
@@ -797,11 +789,7 @@ impl TryToTokens for ast::Export {
797789
quote! {}
798790
};
799791

800-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
801-
Some(quote! { #[coverage(off)] })
802-
} else {
803-
None
804-
};
792+
let maybe_no_coverage = coverage();
805793

806794
(quote! {
807795
#[automatically_derived]
@@ -1172,11 +1160,7 @@ impl ToTokens for ast::StringEnum {
11721160
let hole = variant_count + 1;
11731161
let attrs = &self.rust_attrs;
11741162

1175-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
1176-
Some(quote! { #[coverage(off)] })
1177-
} else {
1178-
None
1179-
};
1163+
let maybe_no_coverage = coverage();
11801164

11811165
let invalid_to_str_msg = format!(
11821166
"Converting an invalid string enum ({}) back to a string is currently not supported",
@@ -1558,16 +1542,12 @@ impl ToTokens for ast::Enum {
15581542
let name_len = name_str.len() as u32;
15591543
let name_chars = name_str.chars().map(|c| c as u32);
15601544
let hole = &self.hole;
1561-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
1562-
Some(quote! { #[coverage(off)] })
1563-
} else {
1564-
None
1565-
};
15661545
let underlying = if self.signed {
15671546
quote! { i32 }
15681547
} else {
15691548
quote! { u32 }
15701549
};
1550+
let maybe_no_coverage = coverage();
15711551
let cast_clauses = self.variants.iter().map(|variant| {
15721552
let variant_name = &variant.name;
15731553
quote! {
@@ -1880,11 +1860,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
18801860
return;
18811861
}
18821862

1883-
let maybe_no_coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
1884-
Some(quote! { #[coverage(off)] })
1885-
} else {
1886-
None
1887-
};
1863+
let maybe_no_coverage = coverage();
18881864

18891865
let name = Ident::new(&format!("__wbindgen_describe_{}", ident), ident.span());
18901866
let inner = &self.inner;
@@ -1988,3 +1964,10 @@ fn respan(input: TokenStream, span: &dyn ToTokens) -> TokenStream {
19881964
}
19891965
new_tokens.into_iter().collect()
19901966
}
1967+
1968+
fn coverage() -> Option<TokenStream> {
1969+
#[cfg(feature = "coverage")]
1970+
return Some(quote! { #[coverage(off)] });
1971+
#[cfg(not(feature = "coverage"))]
1972+
None
1973+
}

crates/macro-support/Cargo.toml

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

1616
[features]
1717
atomics = ["wasm-bindgen-backend/atomics"]
18+
coverage = ["wasm-bindgen-backend/coverage"]
1819
default = ["std"]
1920
extra-traits = ["syn/extra-traits"]
2021
spans = ["wasm-bindgen-backend/spans"]

crates/macro/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ proc-macro = true
1818

1919
[features]
2020
atomics = ["wasm-bindgen-macro-support/atomics"]
21+
coverage = ["wasm-bindgen-macro-support/coverage"]
2122
default = ["std"]
2223
spans = ["wasm-bindgen-macro-support/spans"]
2324
std = ["wasm-bindgen-macro-support/std"]
@@ -34,6 +35,3 @@ trybuild = "1.0"
3435
wasm-bindgen = { path = "../.." }
3536
wasm-bindgen-futures = { path = "../futures" }
3637
web-sys = { path = "../web-sys", features = ["Worker"] }
37-
38-
[lints.rust]
39-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

crates/macro/src/lib.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2")]
22
#![cfg_attr(
3-
any(
4-
wasm_bindgen_unstable_test_coverage,
5-
all(not(feature = "std"), feature = "atomics")
6-
),
3+
any(feature = "coverage", all(not(feature = "std"), feature = "atomics")),
74
feature(allow_internal_unstable),
85
allow(internal_features)
96
)]
@@ -14,10 +11,7 @@ use proc_macro::TokenStream;
1411
use quote::quote;
1512

1613
#[proc_macro_attribute]
17-
#[cfg_attr(
18-
wasm_bindgen_unstable_test_coverage,
19-
allow_internal_unstable(coverage_attribute)
20-
)]
14+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
2115
#[cfg_attr(
2216
all(not(feature = "std"), feature = "atomics"),
2317
allow_internal_unstable(thread_local)
@@ -48,10 +42,7 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
4842
/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
4943
/// ```
5044
#[proc_macro]
51-
#[cfg_attr(
52-
wasm_bindgen_unstable_test_coverage,
53-
allow_internal_unstable(coverage_attribute)
54-
)]
45+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
5546
pub fn link_to(input: TokenStream) -> TokenStream {
5647
match wasm_bindgen_macro_support::expand_link_to(input.into()) {
5748
Ok(tokens) => {
@@ -68,10 +59,7 @@ pub fn link_to(input: TokenStream) -> TokenStream {
6859
}
6960

7061
#[proc_macro_attribute]
71-
#[cfg_attr(
72-
wasm_bindgen_unstable_test_coverage,
73-
allow_internal_unstable(coverage_attribute)
74-
)]
62+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
7563
pub fn __wasm_bindgen_class_marker(attr: TokenStream, input: TokenStream) -> TokenStream {
7664
match wasm_bindgen_macro_support::expand_class_marker(attr.into(), input.into()) {
7765
Ok(tokens) => {

crates/test-macro/Cargo.toml

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

15+
[features]
16+
coverage = []
17+
1518
[dependencies]
1619
proc-macro2 = "1.0"
1720
quote = "1.0"
@@ -26,6 +29,3 @@ syn = { version = "2.0", default-features = false, features = [
2629
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2730
trybuild = "1.0"
2831
wasm-bindgen-test = { path = "../test" }
29-
30-
[lints.rust]
31-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

crates/test-macro/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! going on here.
33
44
#![cfg_attr(
5-
wasm_bindgen_unstable_test_coverage,
5+
feature = "coverage",
66
feature(allow_internal_unstable),
77
allow(internal_features)
88
)]
@@ -18,10 +18,7 @@ use std::sync::atomic::*;
1818
static CNT: AtomicUsize = AtomicUsize::new(0);
1919

2020
#[proc_macro_attribute]
21-
#[cfg_attr(
22-
wasm_bindgen_unstable_test_coverage,
23-
allow_internal_unstable(coverage_attribute)
24-
)]
21+
#[cfg_attr(feature = "coverage", allow_internal_unstable(coverage_attribute))]
2522
pub fn wasm_bindgen_test(
2623
attr: proc_macro::TokenStream,
2724
body: proc_macro::TokenStream,
@@ -109,7 +106,7 @@ pub fn wasm_bindgen_test(
109106
// main test harness. This is the entry point for all tests.
110107
let name = format_ident!("__wbgt_{}_{}", ident, CNT.fetch_add(1, Ordering::SeqCst));
111108
let wasm_bindgen_path = attributes.wasm_bindgen_path;
112-
let coverage = if cfg!(wasm_bindgen_unstable_test_coverage) {
109+
let coverage = if cfg!(feature = "coverage") {
113110
Some(quote! { #[coverage(off)] })
114111
} else {
115112
None

crates/test/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.45' }
2424

2525
[target.'cfg(all(target_arch = "wasm32", wasm_bindgen_unstable_test_coverage))'.dependencies]
2626
minicov = "0.3"
27+
wasm-bindgen-test-macro = { path = '../test-macro', version = '=0.3.45', features = ["coverage"] }
2728

2829
[lints.rust]
2930
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wasm_bindgen_unstable_test_coverage)'] }

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

+11-20
Original file line numberDiff line numberDiff line change
@@ -9,12 +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 set `cfg(wasm_bindgen_unstable_test_coverage)` for `wasm-bindgen-test` and its dependencies.
13-
14-
Currently it is particularly difficult to [deliver compile-line arguments to proc-macros when cross-compiling with Cargo][1]. To circumvent this [host-config] can be used.
15-
16-
[1]: https://github.com/rust-lang/cargo/issues/4423
17-
[host-config]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#host-config
12+
To enable this feature, you need to enable `cfg(wasm_bindgen_unstable_test_coverage)`.
1813

1914
## Generating the data
2015

@@ -26,18 +21,18 @@ Due to the current limitation of `llvm-cov`, we can't collect profiling symbols
2621

2722
### Arguments to the test runner
2823

29-
The following environment variables can be used to control the coverage output when [executing the test runner][2]:
24+
The following environment variables can be used to control the coverage output when [executing the test runner][1]:
3025

31-
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT` to control the file name of the profraw or the directory in which it is placed
26+
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_OUT` to control the file name of the profraw or the directory in which it is placed. It might be necessary to provide the full path if e.g. running tests in a workspace.
3227
- `WASM_BINDGEN_UNSTABLE_TEST_PROFRAW_PREFIX` to add a custom prefix to the profraw files. This can be useful if you're running the tests automatically in succession.
3328

34-
[2]: usage.html#appendix-using-wasm-bindgen-test-without-wasm-pack
29+
[1]: usage.html#appendix-using-wasm-bindgen-test-without-wasm-pack
3530

3631
### Target features
3732

38-
This feature relies on the [minicov] crate, which provides a profiling runtime for WebAssembly. It in turn uses [cc] to compile the runtime to Wasm, which [currently doesn't support accounting for target feature][3]. Use e.g. `CFLAGS_wasm32_unknown_unknown="-matomics -mbulk-memory"` to account for that.
33+
This feature relies on the [minicov] crate, which provides a profiling runtime for WebAssembly. It in turn uses [cc] to compile the runtime to Wasm, which [currently doesn't support accounting for target feature][2]. Use e.g. `CFLAGS_wasm32_unknown_unknown="-matomics -mbulk-memory"` to account for that.
3934

40-
[3]: https://github.com/rust-lang/cc-rs/issues/268
35+
[2]: https://github.com/rust-lang/cc-rs/issues/268
4136
[cc]: https://crates.io/crates/cc
4237
[minicov]: https://crates.io/crates/minicov
4338

@@ -47,13 +42,10 @@ This adapts code taken from the [Rustc book], see that for more examples and gen
4742

4843
```sh
4944
# Run the tests:
50-
# - `CARGO_HOST_RUSTFLAGS` to pass the configuration to `wasm-bindgen-macro`.
51-
# - `-Ztarget-applies-to-host -Zhost-config` to enable `CARGO_HOST_RUSTFLAGS`.
52-
# - `--tests` to not run documentation tests, which is currently not supported.
53-
CARGO_HOST_RUSTFLAGS=--cfg=wasm_bindgen_unstable_test_coverage \
45+
# `--tests` to not run documentation tests, which is currently not supported.
5446
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
5547
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner \
56-
cargo +nightly test -Ztarget-applies-to-host -Zhost-config --tests
48+
cargo +nightly test --tests
5749
# Compile to object files:
5850
# - Extract a list of compiled artifacts from Cargo and filter them with `jq`.
5951
# - Figure out the path to the LLVM IR file corresponding to an artifact.
@@ -62,9 +54,8 @@ crate_name=name_of_the_tested_crate_in_snake_case
6254
objects=()
6355
IFS=$'\n'
6456
for file in $(
65-
CARGO_HOST_RUSTFLAGS=--cfg=wasm_bindgen_unstable_test_coverage \
6657
RUSTFLAGS="-Cinstrument-coverage -Zno-profiler-runtime --emit=llvm-ir --cfg=wasm_bindgen_unstable_test_coverage" \
67-
cargo +nightly test -Ztarget-applies-to-host -Zhost-config --tests --no-run --message-format=json | \
58+
cargo +nightly test --tests --no-run --message-format=json | \
6859
jq -r "select(.reason == \"compiler-artifact\") | (select(.target.kind == [\"test\"]) // select(.target.name == \"$crate_name\")) | .filenames[0]"
6960
)
7061
do
@@ -89,7 +80,7 @@ llvm-cov-19 show -show-instantiations=false -Xdemangler=rustfilt -output-dir cov
8980

9081
## Attribution
9182

92-
These methods have originally been pioneered by [Hacken OÜ], see [their guide][4] as well.
83+
These methods have originally been pioneered by [Hacken OÜ], see [their guide][3] as well.
9384

94-
[4]: https://hknio.github.io/wasmcov
85+
[3]: https://hknio.github.io/wasmcov
9586
[Hacken OÜ]: https://hacken.io

0 commit comments

Comments
 (0)