Skip to content

Commit

Permalink
Merge #72
Browse files Browse the repository at this point in the history
72: Set cfg(coverage) to easily use #[no_coverage] r=taiki-e a=taiki-e

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

Since `#[no_coverage]` is unstable, it is recommended to use it together with `cfg(coverage)` set by cargo-llvm-cov.

```rust
#![cfg_attr(coverage, feature(no_coverage))]

#[cfg_attr(coverage, no_coverage)]
fn exclude_from_coverage() {
    // ...
}
```

Closes #71

Co-authored-by: Taiki Endo <[email protected]>
  • Loading branch information
bors[bot] and taiki-e authored Aug 26, 2021
2 parents 819727e + 9a2ade6 commit 5ef562d
Show file tree
Hide file tree
Showing 13 changed files with 879 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- [Set `cfg(coverage)` to easily use `#[no_coverage]`.](https://github.com/taiki-e/cargo-llvm-cov/pull/72)

- [Add `--quiet`, `--doc`, and `--jobs` options.](https://github.com/taiki-e/cargo-llvm-cov/pull/70)

## [0.1.1] - 2021-08-25
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This is a wrapper around rustc [`-Z instrument-coverage`][instrument-coverage] a

- [Usage](#usage)
- [Continuous Integration](#continuous-integration)
- [Exclude function from coverage](#exclude-function-from-coverage)
- [Installation](#installation)
- [Known limitations](#known-limitations)
- [License](#license)
Expand Down Expand Up @@ -269,6 +270,7 @@ cargo llvm-cov --no-report --features b
cargo llvm-cov --no-run --lcov
```


### Continuous Integration

Here is an example of GitHub Actions workflow that uploads coverage to [Codecov].
Expand Down Expand Up @@ -299,6 +301,21 @@ jobs:
**NOTE:** Currently, only line coverage is available on Codecov. This is because `-Z instrument-coverage` does not support branch coverage and Codecov does not support region coverage. See also [#8], [#12], and [#20].

### Exclude function from coverage

To exclude the specific function from coverage, use the [`#[no_coverage]` attribute][rust-lang/rust#84605].

Since `#[no_coverage]` is unstable, it is recommended to use it together with `cfg(coverage)` set by cargo-llvm-cov.

```rust
#![cfg_attr(coverage, feature(no_coverage))]
#[cfg_attr(coverage, no_coverage)]
fn exclude_from_coverage() {
// ...
}
```

## Installation

<!-- omit in toc -->
Expand Down Expand Up @@ -357,6 +374,7 @@ See also [the code-coverage-related issues reported in rust-lang/rust](https://g
[instrument-coverage]: https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/instrument-coverage.html
[rust-lang/rust#79417]: https://github.com/rust-lang/rust/issues/79417
[rust-lang/rust#79649]: https://github.com/rust-lang/rust/issues/79649
[rust-lang/rust#84605]: https://github.com/rust-lang/rust/issues/84605
[rust-lang/rust#86177]: https://github.com/rust-lang/rust/issues/86177

## License
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ fn run_test(cx: &Context) -> Result<()> {
let llvm_profile_file = cx.target_dir.join(format!("{}-%m.profraw", cx.package_name));

let rustflags = &mut cx.env.rustflags.clone().unwrap_or_default();
rustflags.push(" -Z instrument-coverage --cfg coverage");
// --remap-path-prefix is needed because sometimes macros are displayed with absolute path
rustflags.push(format!(
" -Z instrument-coverage --remap-path-prefix {}/=",
cx.metadata.workspace_root
));
rustflags.push(format!(" --remap-path-prefix {}/=", cx.metadata.workspace_root));
if cx.target.is_none() {
rustflags.push(" --cfg trybuild_no_target");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/auxiliary/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
.unwrap()
.filter_map(Result::ok)
.collect();
assert_eq!(files.len(), 38);
assert_eq!(files.len(), 40);

for file in files {
let s = fs::read_to_string(file).unwrap();
Expand Down
Loading

0 comments on commit 5ef562d

Please sign in to comment.