Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set cfg(coverage) to easily use #[no_coverage] #72

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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