Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Perl's Devel::Cover ignore certain lines in coverage total?

In some code coverage tools you can "hide" certain lines of code from the coverage tool, so that those lines do not count towards the coverage totals. For example, some code might be run only in circumstances that are hard or impossible to test (such as certain hardware failures). Thus, you might get 100% coverage reported even though some code was not exercised.

Setting aside for the moment whether this is wise, is this sort of thing possible with Perl's Devel::Cover?

(Devel::Cover can ignore entire files, but I am interested in ignoring just a few lines in a single file.)

like image 252
rlandster Avatar asked Jan 30 '26 19:01

rlandster


2 Answers

A lot of uncoverable code features have been implemented but they are not documented because I wasn't sure of the interface. However, it's been a few years since anything changed in that area.

Probably the easiest way to see how to use the features is to look at tests/uncoverable in the distribution (see https://github.com/pjcj/Devel--Cover/blob/master/test/uncoverable). If you can't or don't want to change your code you can use the .uncoverable file (see https://github.com/pjcj/Devel--Cover/blob/master/tests/.uncoverable) and the cover options as mentioned by toolic.

If you do this, be sure to use the basic_html report which will mark a construct as in error if you tag it as uncoverable but it gets executed anyway.

I really should get around to tidying everything up and documenting it.

like image 137
pjcj Avatar answered Feb 01 '26 13:02

pjcj


According to the TODO file on CPAN, this capability is not currently supported, but the developers see it as a valuable addition:

  • Enhancements:
    • Marking of unreachable code - commandline tool and gui.

The cover script mentions promising options: -add_uncoverable_point and -delete_uncoverable_point.

like image 30
toolic Avatar answered Feb 01 '26 13:02

toolic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!