blib/lib/Dist/Zilla/Plugin/DBIO/CoverageGate.pm

filter: Statementstmt 26.6% 20 / 75 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 1 totaltotal 17.8% 25 / 140 CC 26 SCAR 60.8
CC
26
Cov
16%
test
600.0
__ANON__
6.0
__ANON__
6.0
CRAP 437.2
↑ Summary blib/lib/Dist/Zilla/Plugin/DBIO/GatherSkills.pm »
1 package Dist::Zilla::Plugin::DBIO::CoverageGate;
2 # ABSTRACT: Judge the Devel::Cover database after the whole test suite has run
3 8 our $VERSION = '0.900007';
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use File::pushd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 with 'Dist::Zilla::Role::TestRunner';
9  
10  
11 has coverage_threshold => (
12 is => 'ro',
13 isa => 'Int',
14 lazy => 1,
15 0 ▶ default => sub { $_[0]->payload->{coverage_threshold} // 80 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->payload->{"coverage_threshold"} // 80
l!l
00
MC/DC0%0/1
$_[0]->payload->{"coverage_threshold"} // 80
$_[0]->payload->{"coverage_threshold"}
Truth table0%0/2
A
A= $_[0]->payload->{"coverage_threshold"}
Aresult
11
00
16 8 );
17  
18 0 ▶sub test {
Subroutine test: not called CC=24 SCAR=64.0
Pod undocumented
19 0 my ($self, $target, $arg) = @_;
20  
21 0 my $threshold = $self->coverage_threshold;
22 0 ▶ unless ($threshold > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($threshold > 0) 0 0 0
23 0 $self->log('coverage gate disabled (coverage_threshold = 0)');
24 0 return;
25 }
26  
27 # Dist::Zilla changes into the build directory and passes it relative to the
28 # directory it came from, so a relative target means the current directory.
29 0 my $root = Path::Tiny::path($target);
30 0 ▶ $root = Path::Tiny->cwd if !$root->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
unless $root->is_absolute 0 0 0
31 0 my $db_dir = $root->child('cover_db');
32 0 my $release = $ENV{RELEASE};
33  
34 # A release without a coverage run, or with one that measured nothing, is a
35 # failed release: the gate must not pass by having nothing to look at.
36 0 ▶ my $unmeasured = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 my ($why) = @_;
38 0 ▶ $self->log_fatal($why) if $release;
Branch: 1 branch
BranchTrueFalseTotal
if $release 0 0 0
39 0 $self->log("coverage not checked: $why");
40 0 return;
41 0 };
42  
43 0 ▶ return $unmeasured->('no cover_db/ -- run with HARNESS_PERL_SWITCHES=-MDevel::Cover')
Branch: 1 branch
BranchTrueFalseTotal
unless $db_dir->is_dir 0 0 0
44 unless $db_dir->is_dir;
45 return $unmeasured->("Devel::Cover::DB is required for the coverage gate: $@")
46 0 ▶ unless eval { require Devel::Cover::DB; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Devel::Cover::DB; 1 } } 0 0 0
47  
48 # Devel::Cover finds the source files of a run (structure, digests) by their
49 # recorded relative path, so merge and read from the build directory.
50 0 my $dir = File::pushd::pushd("$root");
51 0 my $db = Devel::Cover::DB->new(db => "$db_dir");
52 0 ▶ return $unmeasured->('cover_db/ is not a valid Devel::Cover database')
Branch: 1 branch
BranchTrueFalseTotal
unless $db->is_valid 0 0 0
53 unless $db->is_valid;
54  
55 # A coverage run leaves runs/ only; the merged database is what `cover`
56 # makes. Merge here, so a fresh cover_db/ is scored.
57 0 $db->merge_runs;
58 0 my $cover = $db->cover;
59  
60 0 my (@per_module, $covered, $total);
61 0 for my $file (sort $cover->items) {
62 # Only the lib/ of this dist: a path relative to the build directory (or
63 # absolute below it) that starts with lib/ or blib/lib/.
64 0 (my $rel = $file) =~ s{^\Q$root\E/}{};
65 0 ▶ next unless $rel =~ m{^(?:blib/)?lib/};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel =~ m[^(?:blib/)?lib/] 0 0 0
66  
67 0 my $f = $cover->file($file);
68 0 ▶ next unless $f->items;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->items 0 0 0
69 0 ▶ my $crit = $f->criterion('statement') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $crit = $f->criterion("statement") or next 0 0 0
70  
71 0 my ($c_covered, $c_total) = (0, 0);
72 0 for my $loc ($crit->items) {
73 # An entry that is not a statement object counts as uncovered.
74 0 for my $stmt (@{ $crit->location($loc) }) {
75 0 $c_total++;
76 0 ▶ $c_covered++ if blessed($stmt) && $stmt->can('val') && $stmt->val > 0;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($stmt) and $stmt->can("val") and $stmt->val > 0 0 0 0
Condition0%0/6
builtin::blessed($stmt) and $stmt->can("val") and $stmt->val > 0
!ll&&!rl&&r
000
builtin::blessed($stmt) and $stmt->can("val")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($stmt) and $stmt->can("val") and $stmt->val > 0
builtin::blessed($stmt) $stmt->can("val") $stmt->val > 0
Truth table0%0/4
A and B and C
A= builtin::blessed($stmt)B= $stmt->can("val")C= $stmt->val > 0
ABCresult
0XX0
10X0
1100
1111
77 }
78 }
79 0 ▶ next unless $c_total;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_total 0 0 0
80  
81 0 $covered += $c_covered;
82 0 $total += $c_total;
83 0 push @per_module, { file => $rel, pct => 100 * $c_covered / $c_total };
84 }
85  
86 0 ▶ return $unmeasured->('no lib/ file was measured in cover_db/') unless $total;
Branch: 1 branch
BranchTrueFalseTotal
unless $total 0 0 0
87  
88 0 my $overall = sprintf '%.1f', 100 * $covered / $total;
89 0 ▶ if ($overall + 0 >= $threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overall + 0 >= $threshold) 0 0 0
90 0 $self->log("statement coverage $overall% >= $threshold%");
91 0 return;
92 }
93  
94 # The 20 lowest modules, so a failure stays readable for a large dist.
95 0 ▶ my @lowest = sort { $a->{pct} <=> $b->{pct} or $a->{file} cmp $b->{file} } @per_module;
Condition0%0/3
$Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'pct'} <=> $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'pct'} || $Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'file'} cmp $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'file'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'pct'} <=> $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'pct'} || $Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'file'} cmp $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'file'}
$Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'pct'} <=> $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'pct'} $Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'file'} cmp $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'file'}
Truth table0%0/3
A || B
A= $Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'pct'} <=> $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'pct'}B= $Dist::Zilla::Plugin::DBIO::CoverageGate::a->{'file'} cmp $Dist::Zilla::Plugin::DBIO::CoverageGate::b->{'file'}
ABresult
1X1
011
000
96 0 ▶ my $more = @lowest > 20 ? @lowest - 20 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@lowest > 20 ? : 0 0 0
97 0 ▶ splice @lowest, 20 if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 0 0 0
98 my $msg = "statement coverage $overall% below threshold $threshold%; lowest first: "
99 0 ▶ . join(', ', map { sprintf '%s %.1f%%', $_->{file}, $_->{pct} } @lowest)
Branch: 1 branch
BranchTrueFalseTotal
$more ? : 0 0 0
100 . ($more ? " and $more more" : '');
101 0 ▶ $self->log_fatal($msg) if $release || $ENV{COVERAGE_STRICT};
Branch: 1 branch
BranchTrueFalseTotal
if $release or $ENV{"COVERAGE_STRICT"} 0 0 0
Condition0%0/3
$release or $ENV{"COVERAGE_STRICT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$release or $ENV{"COVERAGE_STRICT"}
$release $ENV{"COVERAGE_STRICT"}
Truth table0%0/3
A or B
A= $releaseB= $ENV{"COVERAGE_STRICT"}
ABresult
1X1
011
000
102 0 $self->log("$msg -- set COVERAGE_STRICT=1 or RELEASE=1 to enforce");
103 0 return;
104 }
105  
106 8 __PACKAGE__->meta->make_immutable;
107 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 8 1;
109  
110 __END__
↑ Summary blib/lib/Dist/Zilla/Plugin/DBIO/GatherSkills.pm »