Devel::Cover force adding unseen files - feedback requested.

"Jason Pyeron" <[email protected]> Sun, 25 Dec 2016 21:38:40 -0500
Newsgroups gmane.comp.lang.perl.quality-assurance
Organization PD Inc
Message-ID <AE072ADA2B2F4FB79AC38DF18B573A6E@black7>
Coverage works great as part on continuous integration, until a new file =
is added and the unit tests are blissfully ignorant of the new file's =
existence.

In the past with other coverage tools we would scan the source tree and =
do some mock operation to get the coverage tool to become aware of the =
source file.

Sometimes it is a bit messy, sometimes not. I think this one falls into =
the a little bit messy, because we had to go the internals of =
Devel::Cover::DB and reproduce how Cover.pm uses it.

Below is the script we are using. It provides the minimum functionality =
required, showing the file as 0% covered.

Does anyone have better suggestions?

-Jason Pyeron


#!/usr/bin/perl -w

use Time::HiRes qw(time);
use Data::Dumper;
use File::Find;
use Cwd;

print "load\n";

use Devel::Cover::DB;

my $dbpath=3D"cover_db";
my $db =3D Devel::Cover::DB->new(db =3D> $dbpath);
my $timeStart=3Dtime;
my $runKey=3D"$timeStart.$$";
my %known;

print "ingest\n";

find({ wanted =3D> \&process_coverfile, no_chdir =3D> 1 }, =
"$dbpath/runs/");
find({ wanted =3D> \&process_coverfile, no_chdir =3D> 1 }, =
"$dbpath/digests");
find({ wanted =3D> \&process_coverfile, no_chdir =3D> 1 }, =
"$dbpath/structure/");

sub process_coverfile
{
    if (-f $_)
    {
        my $x=3D$db->read($_);
        foreach my $run ($x->runs)
        {
            my $h=3D$run->{digests};
            foreach my $file (keys %$h)
            {
                if ( ! exists $known{$file} )
                {
                    $known{$file}=3D$run->{digests}{$file};
                }
            }
        }
    }
}

print scalar keys %known, " known covered file(s) found\n";

print "preprocess\n";

my %toadd;

find({ wanted =3D> \&process_file, no_chdir =3D> 1 }, "scripts");

sub process_file
{
    if (-f $_)
    {
        if ( ! exists $known{$_} )
        {
            $toadd{$_}=3DDevel::Cover::DB::Structure->digest($_);
        }
    }
}

print scalar keys %toadd, " uncovered file(s) found and hashed\n";


print "process\n";

if (scalar keys %toadd =3D=3D 0)
{
    print "no files to process\n";
    exit;
}

print "run: $runKey\n";

$db->{runs}{$runKey}{"OS"}=3D$^O;
$db->{runs}{$runKey}{"collected"}=3D["branch","condition","pod","statemen=
t","subroutine","time"];
$db->{runs}{$runKey}{"dir"}=3DCwd::abs_path();
$db->{runs}{$runKey}{"vec"}=3D{};
$db->{runs}{$runKey}{"start"}=3D$timeStart;
$db->{runs}{$runKey}{"run"}=3D$0;
$_=3D$^V;
s/v//;
$db->{runs}{$runKey}{"perl"}=3D$_;

my $s=3D$db->{structure}=3DDevel::Cover::DB::Structure->new;

foreach my $file (keys %toadd)
{
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"branch"}=3Dundef=
;
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"condition"}=3Dun=
def;
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"pod"}=3Dundef;
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"subroutine"}=3Du=
ndef;
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"time"}=3Dundef;
    =
$db->{structure}->{f}{$file}{start}{-1}{"__COVER__"}[0]{"statement"}=3D0;=

    $db->{structure}->{f}{$file}{file}=3D$file;
    $db->{structure}->{f}{$file}{digest}=3D$toadd{$file};
    $db->{structure}->{f}{$file}{statement}=3D[1];
    $db->{runs}{$runKey}{"count"}{$file}{'statement'}=3D[0];
    $db->{runs}{$runKey}{"digests"}{$file}=3D$toadd{$file};
}

$db->{runs}{$runKey}{"finish"}=3Dtime;

print "saving\n";

$db->write("$dbpath/runs/$runKey");

--
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-
-                                                               -
- Jason Pyeron                      PD Inc. http://www.pdinc.us -
- Principal Consultant              10 West 24th Street #100    -
- +1 (443) 269-1555 x333            Baltimore, Maryland 21218   -
-                                                               -
-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-