Ignoring chunks of code for source-based coverage

I've been giving the new source-based code coverage a spin (https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/source-based-code-coverage.html) and I'm loving this idea and hope it gets better and better.

One thing I can't find any information about, though, is whether I can skip some parts of code, that is, not consider them as parts of code to cover. In particular, this would be useful to say that I don't care about covering every little bit in my test modules, but obviously it could have a wider use.

Anyone knows whether it's possible? If not, is it perhaps in the works? Any clever ways to hack it? Any information about this would be useful. I tried going through the referenced issues on github, but there's so much there, it's difficult to go through, so if anyone knows this, I would appreciate any info, pointers, or links.

All the best
Michal

I haven't tried this myself, but if you're interested in enabling coverage only in crates you are modifying, I believe you can set rustflags to include -Zinstrument-coverage in a cargo.config file (or files) in/for specific crates:

Linking crates with and without coverage shouldn't be a problem. (You may need to enable coverage in the final binary though, including tests. Thay may be necessary to make sure the LLVM profiling library is linked in and enabled, to write the .profraw data.)

What I'm looking for is something different. What I would like is to ignore some of my own code. Essentially, this is so that, say, my unit tests aren't counted into the stats. The reasoning is that there might be some code that I don't necessarily use right now but it could be used in near future, but since it's only in the test modules, I don't particularly want to generate any warnings or decrease coverage % because of that. Also, it can seriously skew the report data, some tests can be bulky and then the final coverage doesn't reflect what portion of the actual production code is actually covered.

I hope this clears things up. I was wondering if there exists (or if there are any plans for), say, an attribute that would ignore a block of code (similar to #[cfg_attr(tarpaulin, ignore)]).

The cfg example helps, but that doesn't exist. You could file a feature request or, if so inclined, submit a PR. It doesn't seem that difficult to implement as a check at the top of InstrumentCoverage. (Check out the Rust Dev Guide section on LLVM-based coverage if you're interested.)

Thanks, I'll look into that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.