« blib/lib/Pod/Weaver/PluginBundle/DBIO.pm
↑ Summary
blib/lib/Dist/Zilla/Plugin/DBIO/LicenseFile.pm »
| 1 | package Dist::Zilla::Plugin::DBIO::CoverageTest; | ||||||||||||
| 2 | # ABSTRACT: Generate xt/release/coverage.t from a Devel::Cover DB | ||||||||||||
| 3 | 5 | our $VERSION = '0.900004'; | |||||||||||
| 4 | 5 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 5 | ▶ | use Dist::Zilla::File::InMemory; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 5 | with 'Dist::Zilla::Role::FileGatherer'; | |||||||||||
| 7 | |||||||||||||
| 8 | |||||||||||||
| 9 | has coverage_threshold => ( | ||||||||||||
| 10 | is => 'ro', | ||||||||||||
| 11 | isa => 'Int', | ||||||||||||
| 12 | lazy => 1, | ||||||||||||
| 13 | 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
MC/DC0%0/1
$_[0]->payload->{"coverage_threshold"} // 80
$_[0]->payload->{"coverage_threshold"}
Truth table0%0/2
A
A= $_[0]->payload->{"coverage_threshold"}
| |||||||||||||
| 14 | 5 | ); | |||||||||||
| 15 | |||||||||||||
| 16 | 5 | ▶ | sub gather_files { | ||||||||||
|
Subroutine
gather_files: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 17 | 5 | my ($self) = @_; | |||||||||||
| 18 | 5 | $self->add_file(Dist::Zilla::File::InMemory->new( | |||||||||||
| 19 | name => 'xt/release/coverage.t', | ||||||||||||
| 20 | content => $self->_template, | ||||||||||||
| 21 | )); | ||||||||||||
| 22 | 5 | $self->log_debug('gathered xt/release/coverage.t'); | |||||||||||
| 23 | } | ||||||||||||
| 24 | |||||||||||||
| 25 | # Build the test script. The body is a heredoc so the perl inside is easy | ||||||||||||
| 26 | # to read and the placeholders stay obvious -- no string-formatting surprises. | ||||||||||||
| 27 | 5 | ▶ | sub _template { | ||||||||||
|
Subroutine
_template: called CC=2 SCAR=6.9
| |||||||||||||
| 28 | 5 | my ($self) = @_; | |||||||||||
| 29 | 5 | my $threshold = $self->coverage_threshold; | |||||||||||
| 30 | |||||||||||||
| 31 | # If the threshold is 0 (or negative) the test degenerates to a no-op | ||||||||||||
| 32 | # skip -- the bundle still gathers the file, but it never fails and never | ||||||||||||
| 33 | # reports a number. Useful for distributions that opt out of coverage. | ||||||||||||
| 34 | 5 | ▶ | my $body = $threshold > 0 ? $self->_strict_body : $self->_off_body; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 5 | $body =~ s/__THRESHOLD__/$threshold/g; | |||||||||||
| 36 | 5 | return $body; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 4 | ▶ | sub _strict_body { | ||||||||||
|
Subroutine
_strict_body: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 4 | return <<'PERL'; | |||||||||||
| 41 | use strict; | ||||||||||||
| 42 | use warnings; | ||||||||||||
| 43 | use FindBin; | ||||||||||||
| 44 | use Cwd; | ||||||||||||
| 45 | use Scalar::Util 'blessed'; | ||||||||||||
| 46 | use Test::More; | ||||||||||||
| 47 | |||||||||||||
| 48 | # Resolve the dist root: the parent of t/ when the test was rewritten from | ||||||||||||
| 49 | # xt/release/coverage.t (dzil test/release layout), or $FindBin::Bin if run | ||||||||||||
| 50 | # straight from xt/release/. Then chdir there so the relative 'cover_db/' | ||||||||||||
| 51 | # path resolves no matter where the harness invoked us from. | ||||||||||||
| 52 | my $dist_root = -d 'cover_db' ? Cwd::getcwd() : $FindBin::Bin; | ||||||||||||
| 53 | $dist_root =~ s{/t$}{}; | ||||||||||||
| 54 | chdir $dist_root or die "chdir $dist_root: $!" | ||||||||||||
| 55 | unless Cwd::getcwd() eq $dist_root; | ||||||||||||
| 56 | |||||||||||||
| 57 | my $THRESHOLD = __THRESHOLD__; | ||||||||||||
| 58 | |||||||||||||
| 59 | if (! -d 'cover_db') { | ||||||||||||
| 60 | # A release without a coverage run is a failed release: the gate must not | ||||||||||||
| 61 | # pass by having nothing to look at. | ||||||||||||
| 62 | if ($ENV{RELEASE}) { | ||||||||||||
| 63 | fail('No cover_db/ under RELEASE=1 -- run with HARNESS_PERL_SWITCHES=-MDevel::Cover'); | ||||||||||||
| 64 | done_testing; | ||||||||||||
| 65 | exit; | ||||||||||||
| 66 | } | ||||||||||||
| 67 | plan skip_all => 'No cover_db/ -- run with HARNESS_PERL_SWITCHES=-MDevel::Cover'; | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | # Hard dependency on Devel::Cover::DB at test-time (recorded in cpanfile | ||||||||||||
| 71 | # under on test). If a downstream install strips it, fail loud rather than | ||||||||||||
| 72 | # silently mark the test as passed. | ||||||||||||
| 73 | eval { require Devel::Cover::DB; 1 } | ||||||||||||
| 74 | or plan skip_all => "Devel::Cover::DB required for coverage test: $@"; | ||||||||||||
| 75 | |||||||||||||
| 76 | my $db = Devel::Cover::DB->new(db => 'cover_db'); | ||||||||||||
| 77 | unless ($db->is_valid) { | ||||||||||||
| 78 | plan skip_all => 'cover_db/ is not a valid Devel::Cover database'; | ||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | # A coverage run leaves runs/ only; the merged database is what `cover` makes. | ||||||||||||
| 82 | # Do the merge here, so a fresh cover_db is scored rather than skipped (k12). | ||||||||||||
| 83 | $db->merge_runs; | ||||||||||||
| 84 | |||||||||||||
| 85 | # Materialise the merged run data. Devel::Cover::DB->cover is lazy: it | ||||||||||||
| 86 | # reads structure + runs on first call and returns a Cover handle. | ||||||||||||
| 87 | my $cover = $db->cover; | ||||||||||||
| 88 | |||||||||||||
| 89 | my (@per_module, $covered, $total); | ||||||||||||
| 90 | for my $file (sort $cover->items) { | ||||||||||||
| 91 | # Only score the distribution's own lib/ -- vendor and Devel::Cover itself | ||||||||||||
| 92 | # are out of scope. | ||||||||||||
| 93 | next unless $file =~ m{(?:^|/)lib/}; | ||||||||||||
| 94 | next if $file =~ m{(?:^|/)Devel/Cover}; | ||||||||||||
| 95 | |||||||||||||
| 96 | my $f = $cover->file($file); | ||||||||||||
| 97 | next unless $f->items; # no data for this file | ||||||||||||
| 98 | |||||||||||||
| 99 | # Statement criterion may be absent if Devel::Cover was started without it. | ||||||||||||
| 100 | my $crit = $f->criterion('statement'); | ||||||||||||
| 101 | my @locs = $crit ? $crit->items : (); | ||||||||||||
| 102 | unless (@locs) { | ||||||||||||
| 103 | push @per_module, { file => $file, pct => 'n/a', note => 'no statement data' }; | ||||||||||||
| 104 | next; | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | my ($c_covered, $c_total) = (0, 0); | ||||||||||||
| 108 | for my $loc (@locs) { | ||||||||||||
| 109 | my $loc_data = $crit->location($loc); | ||||||||||||
| 110 | # Each location is an arrayref of Devel::Cover::Statement objects; | ||||||||||||
| 111 | # a statement with val > 0 was executed. | ||||||||||||
| 112 | for my $stmt (@$loc_data) { | ||||||||||||
| 113 | $c_total++; | ||||||||||||
| 114 | $c_covered++ if blessed($stmt) && $stmt->can('val') && $stmt->val > 0; | ||||||||||||
| 115 | } | ||||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | next unless $c_total; | ||||||||||||
| 119 | my $pct = 100 * $c_covered / $c_total; | ||||||||||||
| 120 | $covered += $c_covered; | ||||||||||||
| 121 | $total += $c_total; | ||||||||||||
| 122 | push @per_module, { file => $file, pct => sprintf('%.1f', $pct), covered => $c_covered, total => $c_total }; | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | if (!$total) { | ||||||||||||
| 126 | plan skip_all => 'No lib/ files were exercised under coverage'; | ||||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | my $overall = sprintf '%.1f', 100 * $covered / $total; | ||||||||||||
| 130 | |||||||||||||
| 131 | my $strict = $ENV{COVERAGE_STRICT} || $ENV{RELEASE}; | ||||||||||||
| 132 | my @diag = map { " $_->{file}: $_->{pct}%" . (defined $_->{note} ? " ($_->{note})" : '') } @per_module; | ||||||||||||
| 133 | |||||||||||||
| 134 | if ($overall + 0 >= $THRESHOLD) { | ||||||||||||
| 135 | pass("statement coverage $overall% >= $THRESHOLD%"); | ||||||||||||
| 136 | diag("coverage per module:\n", join("\n", @diag)) if @diag; | ||||||||||||
| 137 | done_testing; | ||||||||||||
| 138 | } | ||||||||||||
| 139 | else { | ||||||||||||
| 140 | my $msg = "statement coverage $overall% below threshold $THRESHOLD%"; | ||||||||||||
| 141 | if ($strict) { | ||||||||||||
| 142 | fail($msg); | ||||||||||||
| 143 | diag("coverage per module:\n", join("\n", @diag)); | ||||||||||||
| 144 | done_testing; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | else { | ||||||||||||
| 147 | $msg .= " -- set COVERAGE_STRICT=1 or RELEASE=1 to enforce"; | ||||||||||||
| 148 | pass($msg); | ||||||||||||
| 149 | diag("coverage per module:\n", join("\n", @diag)); | ||||||||||||
| 150 | diag("run with COVERAGE_STRICT=1 (or RELEASE=1) to turn this into a failure"); | ||||||||||||
| 151 | done_testing; | ||||||||||||
| 152 | } | ||||||||||||
| 153 | } | ||||||||||||
| 154 | PERL | ||||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | 1 | ▶ | sub _off_body { | ||||||||||
|
Subroutine
_off_body: called CC=1 SCAR=0.0
| |||||||||||||
| 158 | 1 | return <<'PERL'; | |||||||||||
| 159 | use strict; | ||||||||||||
| 160 | use warnings; | ||||||||||||
| 161 | use Test::More; | ||||||||||||
| 162 | plan skip_all => 'coverage enforcement disabled (coverage_threshold = 0)'; | ||||||||||||
| 163 | PERL | ||||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 167 | 5 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 168 | 5 | 1; | |||||||||||
| 169 | |||||||||||||
| 170 | __END__ | ||||||||||||