perlcode-coveragemakemaker

How do I get a MakeMaker target for code coverage?


In my ExtUtils::MakeMaker based project, I want to generate a code coverage report from my tests. I use Devel::Cover for that, but I have to run it manually:

HARNESS_PERL_SWITCHES=-MDevel::Cover make test && cover

That's too much to type (and remember), so I'd like to run something like make cover instead. ExtUtils::MakeMaker::Coverage seems to do that, but I cannot install it.

~/pl$ sudo cpan install ExtUtils::MakeMaker::Coverage
Loading internal logger. Log::Log4perl recommended for better logging
Reading '/Users/rwenner/.cpan/Metadata'
Database was generated on Thu, 08 Oct 2020 22:56:18 GMT
>(error): Could not expand [ExtUtils::MakeMaker::Coverage]. Check the module name.
>(info): I can suggest names if you install one of Text::Levenshtein::XS, Text::Levenshtein::Damerau::XS, Text::Levenshtein, and Text::Levenshtein::Damerau::PP
>(info): and you provide the -x option on invocation.
>(error): Skipping ExtUtils::MakeMaker::Coverage because I couldn't find a matching namespace.

Why is that and how can I install that module? I can install other modules with cpan just fine, so I don't think it's a network issue.

Or is ExtUtils::MakeMaker::Coverage outdated (looks like the last update is from 2005)? What should I use instead?


Solution

  • Add a postamble to your Makefile.PL:

    # Makefile.PL
    use ExtUtils::MakeMaker;
    WriteMakefile(...);
    
    # specify additional testing targets
    package MY;
    sub MY::postamble {
        return q~
    cover :: pure_all
        HARNESS_PERL_SWITCHES=-MDevel::Cover make test && cover
    
        ~;
    }
    

    This will add a target that will execute your desired command when you run make cover.