Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Devel::Cover with prove?

Tags:

perl

I see there are some similar questions here and on http://www.perlmonks.org but I still do not get it. Imagine I have a project with a 'lib/' and a 't' directories. I run my tests with 'prove':

$ cd $PROJECT_ROOT
$ prove ./*.t

I want to get a report in html for one or more files in the 'lib/' directory. I do not want reports for the files in the 't' directory. A simple example should be enough. Thanks

like image 289
Беров Avatar asked Jun 26 '12 17:06

Беров


2 Answers

  1. perl Makefile.PL or perl Build.PL
  2. cover -test
like image 81
daxim Avatar answered Sep 20 '22 10:09

daxim


The proper way is to always start out with Makefile.PL/Build.PL, just as selected answer suggests. However, sometimes you are not the one who started out, so...

I used to make a fake makefile:

 % cat Makefile
 test:
      prove -Ilib -r t

The following also seems to work (w/o touching ANY files on disk):

cover -t -make 'prove -Ilib -r t; exit $?'

This only works because of how perl's system/exec handle an argument with shell metacharacters in it (; in this case) and may break in the future if cover decides to quote it more rigirously. Also it shouldn't work under windows. I wish cover had a -prove option instead.

This one still generates coverage for *.t as well as CPAN modules at nonstandard locations. This behaviour can be fixed using +select/+ignore options (see the Devel::Cover's manpage):

cover -t +select ^lib +ignore ^

So the tl;dr "magic" command is

cover -t +select ^lib +ignore ^ -make 'prove -Ilib -r t; exit $?'

EDIT The following didn't work for me - it only prints short summary:

 PERL5OPT="$PERL5OPT -MDevel::Cover" prove -Ilib -r t
 cover -t +select ^lib +ignore ^

Note that prove -MSomething applies Something to prove itself and doesn't pass it on (unlike with -I).

like image 44
Dallaylaen Avatar answered Sep 21 '22 10:09

Dallaylaen