Skip to content

Commit

Permalink
Support #[coverage(off)] to exclude results (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
zecakeh authored Jul 29, 2024
1 parent ae9e07a commit e9a108c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
From 2019 onwards, all notable changes to tarpaulin will be documented in this
file.

## [Unreleased]
### Added
- Support for `#[coverage(off)]` to exclude expressions from coverage results

## [0.31.0] 2024-07-22
### Added
- Ability to remove coveralls from the build making openssl optional.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ fn main() {
println!("I won't be included in results");
}

// Also supports the nightly rustc `no_coverage` attribute.
#[no_coverage]
// Also supports the nightly rustc `coverage(off)` attribute.
#[coverage(off)]
fn not_included() {

}
Expand Down
17 changes: 15 additions & 2 deletions src/source_analysis/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ pub(crate) fn check_cfg_attr(attr: &Meta) -> bool {

if id.is_ident("no_coverage") {
ignore_span = true;
} else if id.is_ident("coverage") {
if let Meta::List(ml) = attr {
let _ = ml.parse_nested_meta(|nested| {
ignore_span |= nested.path.is_ident("off");
Ok(())
});
}
} else if id.is_ident("cfg") {
if let Meta::List(ml) = attr {
let _ = ml.parse_nested_meta(|nested| {
Expand All @@ -62,15 +69,21 @@ pub(crate) fn check_cfg_attr(attr: &Meta) -> bool {
}
} else if id.is_ident("cfg_attr") {
if let Meta::List(ml) = attr {
let tarp_cfged_ignores = &["no_coverage"];
let mut first = true;
let mut is_tarpaulin = false;
let _ = ml.parse_nested_meta(|nested| {
if first && nested.path.is_ident("tarpaulin") {
first = false;
is_tarpaulin = true;
} else if !first && is_tarpaulin {
ignore_span |= tarp_cfged_ignores.iter().any(|x| nested.path.is_ident(x));
if nested.path.is_ident("no_coverage") {
ignore_span = true;
} else if nested.path.is_ident("coverage") {
let _ = nested.parse_nested_meta(|nested| {
ignore_span |= nested.path.is_ident("off");
Ok(())
});
}
}
Ok(())
});
Expand Down
15 changes: 15 additions & 0 deletions src/source_analysis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ fn tarpaulin_skip_attr() {
fn uncovered4() {
println!(\"zombie lincoln\");
}
#[coverage(off)]
fn uncovered5() {
println!(\"zombie lincoln\");
}
#[cfg_attr(tarpaulin, coverage(off))]
fn uncovered6() {
println!(\"zombie lincoln\");
}
",
file: Path::new(""),
ignore_mods: RefCell::new(HashSet::new()),
Expand All @@ -1031,7 +1041,12 @@ fn tarpaulin_skip_attr() {
assert!(lines.ignore.contains(&Lines::Line(18)));
assert!(lines.ignore.contains(&Lines::Line(22)));
assert!(lines.ignore.contains(&Lines::Line(23)));
assert!(lines.ignore.contains(&Lines::Line(27)));
assert!(lines.ignore.contains(&Lines::Line(28)));
assert!(lines.ignore.contains(&Lines::Line(32)));
assert!(lines.ignore.contains(&Lines::Line(33)));
assert!(lines.ignore.contains(&Lines::Line(37)));
assert!(lines.ignore.contains(&Lines::Line(38)));

let ctx = Context {
config: &config,
Expand Down

0 comments on commit e9a108c

Please sign in to comment.