Skip to content

Commit

Permalink
coverage_attribute has been stabilized
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Dec 18, 2024
1 parent cec754f commit 932a369
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 277 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This is a wrapper around rustc [`-C instrument-coverage`][instrument-coverage] a
- [Get coverage of C/C++ code linked to Rust library/binary](#get-coverage-of-cc-code-linked-to-rust-librarybinary)
- [Get coverage of external tests](#get-coverage-of-external-tests)
- [Exclude file from coverage](#exclude-file-from-coverage)
- [Exclude function from coverage](#exclude-function-from-coverage)
- [Exclude code from coverage](#exclude-code-from-coverage)
- [Continuous Integration](#continuous-integration)
- [Display coverage in VS Code](#display-coverage-in-vs-code)
- [Environment variables](#environment-variables)
Expand Down Expand Up @@ -485,17 +485,22 @@ To exclude specific file patterns from the report, use the `--ignore-filename-re
cargo llvm-cov --open --ignore-filename-regex build
```

### Exclude function from coverage
### Exclude code from coverage

To exclude the specific function from coverage, use the [`#[coverage(off)]` attribute][rust-lang/rust#84605].
To exclude the specific function or module from coverage, use the [`#[coverage(off)]` attribute][rust-lang/rust#84605].

Since `#[coverage(off)]` is unstable, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov.
Since `#[coverage(off)]` attribute stabilized in Rust 1.85, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov for compatibility with old Rust.

```rust
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
// function
#[cfg_attr(coverage, coverage(off))]
fn exclude_fn_from_coverage() {
// ...
}

#[cfg_attr(coverage_nightly, coverage(off))]
fn exclude_from_coverage() {
// module
#[cfg_attr(coverage, coverage(off))]
mod exclude_mod_from_coverage {
// ...
}
```
Expand All @@ -512,11 +517,17 @@ Rust 1.80+ warns the above cfgs as `unexpected_cfgs`. The recommended way to add
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }
```

If you want to ignore all `#[test]`-related code, consider using [coverage-helper] crate version 0.2+.
If you want to ignore all `#[test]`-related code, you can use module-level `#[coverage(off)]` attribute:

cargo-llvm-cov excludes code contained in the directory named `tests` from the report by default, so you can also use it instead of coverage-helper crate.
```rust
#[cfg(test)]
#[cfg_attr(coverage, coverage(off))]
mod tests {
// ...
}
```

**Note:** `#[coverage(off)]` was previously named `#[no_coverage]`. When using `#[no_coverage]` in the old nightly, replace `feature(coverage_attribute)` with `feature(no_coverage)`, `coverage(off)` with `no_coverage`, and `coverage-helper` 0.2+ with `coverage-helper` 0.1.
cargo-llvm-cov excludes code contained in the directory named `tests` from the report by default, so you can also use it instead of `#[coverage(off)]` attribute.

### Continuous Integration

Expand Down Expand Up @@ -716,7 +727,6 @@ See also [the code-coverage-related issues reported in rust-lang/rust](https://g

## Related Projects

- [coverage-helper]: Helper for [#123].
- [cargo-config2]: Library to load and resolve Cargo configuration. cargo-llvm-cov uses this library.
- [cargo-hack]: Cargo subcommand to provide various options useful for testing and continuous integration.
- [cargo-minimal-versions]: Cargo subcommand for proper use of `-Z minimal-versions`.
Expand All @@ -730,7 +740,6 @@ See also [the code-coverage-related issues reported in rust-lang/rust](https://g
[cargo-hack]: https://github.com/taiki-e/cargo-hack
[cargo-minimal-versions]: https://github.com/taiki-e/cargo-minimal-versions
[codecov]: https://codecov.io
[coverage-helper]: https://github.com/taiki-e/coverage-helper
[instrument-coverage]: https://doc.rust-lang.org/rustc/instrument-coverage.html
[nextest]: https://nexte.st/book/test-coverage.html
[rust-lang/rust#79417]: https://github.com/rust-lang/rust/issues/79417
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"coverage":{"src/lib.rs":{"5":"1/1","6":"1/1","7":"1/1","8":"0/1","9":"1/1","10":"0/1","11":"0/1","13":"1/1"}}}
{"coverage":{"src/lib.rs":{"3":"1/1","4":"1/1","5":"1/1","6":"0/1","7":"1/1","8":"0/1","9":"0/1","11":"1/1"}}}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
1| |#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
1| |use coverage_helper::test;
2| |
3| |use coverage_helper::test;
4| |
5| 2|fn func(x: i32) {
6| 2| match x {
7| 1| 0 => {}
8| 0| 1 => {}
9| 1| 2 => {}
10| 0| 3 => {}
11| 0| _ => {}
12| | }
13| 2|}
14| |
15| |#[test]
16| |fn test() {
17| | func(0);
18| |
19| | if false {
20| | func(1);
21| | } else {
22| | func(2);
23| | }
24| |}
3| 2|fn func(x: i32) {
4| 2| match x {
5| 1| 0 => {}
6| 0| 1 => {}
7| 1| 2 => {}
8| 0| 3 => {}
9| 0| _ => {}
10| | }
11| 2|}
12| |
13| |#[test]
14| |fn test() {
15| | func(0);
16| |
17| | if false {
18| | func(1);
19| | } else {
20| | func(2);
21| | }
22| |}
44 changes: 21 additions & 23 deletions tests/fixtures/coverage-reports/coverage_helper/coverage_helper.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
1| |#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
1| |use coverage_helper::test;
2| |
3| |use coverage_helper::test;
4| |
5| 2|fn func(x: i32) {
6| 2| match x {
7| 1| 0 => {}
8| 0| 1 => {}
9| 1| 2 => {}
10| 0| 3 => {}
11| 0| _ => {}
12| | }
13| 2|}
14| |
15| |#[test]
16| |fn test() {
17| | func(0);
18| |
19| | if false {
20| | func(1);
21| | } else {
22| | func(2);
23| | }
24| |}
3| 2|fn func(x: i32) {
4| 2| match x {
5| 1| 0 => {}
6| 0| 1 => {}
7| 1| 2 => {}
8| 0| 3 => {}
9| 0| _ => {}
10| | }
11| 2|}
12| |
13| |#[test]
14| |fn test() {
15| | func(0);
16| |
17| | if false {
18| | func(1);
19| | } else {
20| | func(2);
21| | }
22| |}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"coverage":{"src/lib.rs":{"3":"1/1","4":"1/1","5":"1/1","6":"0/1","7":"0/1","8":"0/1","9":"0/1","11":"1/1","15":"1/1","16":"1/1","17":"1/1","18":"1/2","19":"0/1","20":"1/2","21":"1/1","26":"1/1","27":"1/2","28":"0/1","29":"0/1","30":"1/2","31":"1/1","39":"1/1","40":"1/2","41":"0/1","42":"1/2","43":"1/1"}}}
{"coverage":{"src/lib.rs":{"1":"1/1","2":"1/1","3":"1/1","4":"0/1","5":"0/1","6":"0/1","7":"0/1","9":"1/1","24":"1/1","25":"1/2","26":"0/1","27":"0/1","28":"1/2","29":"1/1"}}}
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
1| |#![cfg_attr(coverage, feature(coverage_attribute))]
2| |
3| 1|fn func(x: i32) {
4| 1| match x {
5| 1| 0 => {}
6| 0| 1 => {}
7| 0| 2 => {}
8| 0| 3 => {}
9| 0| _ => {}
10| | }
11| 1|}
12| |
13| |#[cfg_attr(coverage, coverage(off))]
14| |#[test]
15| 1|fn fn_level() {
16| 1| func(0);
17| 1|
18| 1| if false {
19| 0| func(1);
20| 1| }
21| 1|}
22| |
23| |// #[coverage(off)] has no effect on expressions.
24| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
25| |#[test]
26| 1|fn expr_level() {
27| 1| if false {
28| 0| // #[cfg_attr(coverage, coverage(off))]
29| 0| func(2);
30| 1| }
31| 1|}
32| |
33| |// #[coverage(off)] has no effect on modules.
34| |#[cfg_attr(coverage, coverage(off))]
35| |mod mod_level {
36| | use super::func;
37| |
38| | #[test]
39| 1| fn mod_level() {
40| 1| if false {
41| 0| func(3);
42| 1| }
43| 1| }
44| |}
1| 1|fn func(x: i32) {
2| 1| match x {
3| 1| 0 => {}
4| 0| 1 => {}
5| 0| 2 => {}
6| 0| 3 => {}
7| 0| _ => {}
8| | }
9| 1|}
10| |
11| |#[coverage(off)]
12| |#[test]
13| |fn fn_level() {
14| | func(0);
15| |
16| | if false {
17| | func(1);
18| | }
19| |}
20| |
21| |// #[coverage(off)] has no effect on expressions.
22| |// now error by rustc: error[E0788]: attribute should be applied to a function definition or closure
23| |#[test]
24| 1|fn expr_level() {
25| 1| if false {
26| 0| // #[coverage(off)]
27| 0| func(2);
28| 1| }
29| 1|}
30| |
31| |#[coverage(off)]
32| |mod mod_level {
33| | use super::func;
34| |
35| | #[test]
36| | fn mod_level() {
37| | if false {
38| | func(3);
39| | }
40| | }
41| |}
44 changes: 22 additions & 22 deletions tests/fixtures/coverage-reports/no_coverage/no_cfg_coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
"percent": 0.0
},
"functions": {
"count": 4,
"covered": 4,
"count": 2,
"covered": 2,
"percent": 100.0
},
"instantiations": {
"count": 4,
"covered": 4,
"count": 2,
"covered": 2,
"percent": 100.0
},
"lines": {
"count": 26,
"covered": 18,
"percent": 69.23076923076923
"count": 14,
"covered": 8,
"percent": 57.14285714285714
},
"regions": {
"count": 19,
"covered": 12,
"notcovered": 7,
"percent": 63.1578947368421
"count": 11,
"covered": 6,
"notcovered": 5,
"percent": 54.54545454545454
}
}
}
Expand All @@ -49,19 +49,19 @@
"percent": 0
},
"functions": {
"count": 4,
"covered": 4,
"count": 2,
"covered": 2,
"percent": 100
},
"instantiations": {
"count": 4,
"covered": 4,
"count": 2,
"covered": 2,
"percent": 100
},
"lines": {
"count": 26,
"covered": 18,
"percent": 69.23076923076923
"count": 14,
"covered": 8,
"percent": 57.14285714285714
},
"mcdc": {
"count": 0,
Expand All @@ -70,10 +70,10 @@
"percent": 0
},
"regions": {
"count": 19,
"covered": 12,
"notcovered": 7,
"percent": 63.1578947368421
"count": 11,
"covered": 6,
"notcovered": 5,
"percent": 54.54545454545454
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SF:src/lib.rs
FNF:4
FNH:4
FNF:2
FNH:2
BRF:0
BRH:0
LF:26
LH:18
LF:14
LH:8
end_of_record
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src/lib.rs 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
src/lib.rs 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 19 7 63.16% 4 0 100.00% 26 8 69.23% 0 0 -
TOTAL 11 5 54.55% 2 0 100.00% 14 6 57.14% 0 0 -
Loading

0 comments on commit 932a369

Please sign in to comment.