blib/lib/Test2/Plugin/Cover.pm

filter: Statementstmt 95.8% 254 / 265 Branchbran 73.0% 111 / 152 Conditioncond 43.5% 27 / 62 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 100% 39 / 39 Podpod 100% 21 / 21 totaltotal 79.9% 457 / 572 CC 199 SCAR 21.6
CC
199
Cov
82%
_validate_from
16.4
_validate_from
16.4
_exclude_roots
15.1
CRAP 8.7
↑ Summary blib/lib/Test2/EventFacet/Coverage.pm »
1 package Test2::Plugin::Cover;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 0 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 0 ▶use Test2::API qw/test2_add_callback_exit context/;
Subroutine BEGIN: called CC=1 SCAR=1.2
6 0 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.2
7 16 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use File::Spec();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 my $SEP = File::Spec->catfile('', '');
12  
13 14 our $VERSION = '0.000030';
14  
15 # Directly modifying this is a bad idea, but for the XS to work it needs to be
16 # a package var, not a lexical.
17 14 our $FROM = '*';
18 14 my $FROM_MODIFIED = 0;
19 14 my $FROM_MANAGER;
20  
21 14 our ($ENABLED, $ROOT, $LOAD_ROOT, %REPORT, @OPENS, $TRACE_OPENS);
22 16 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=16.2
23 16 $TRACE_OPENS = 0;
24 16 $ENABLED = 0;
25 # realpath can die (deleted cwd), never let that kill the module load.
26 0 ▶ $LOAD_ROOT = "" . (eval { path('.')->realpath } // eval { path('.')->absolute } // path('.'));
Condition16%2/12
(eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }) // path(".")
l!l&&r!l&&!r
1600
eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }
l!l&&r!l&&!r
1600
(eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }) // path(".")
l!l&&r!l&&!r
000
eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }
l!l&&r!l&&!r
000
MC/DC0%0/3
(eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }) // path(".")
eval { do { path(".")->realpath } } eval { do { path(".")->absolute } } path(".")
MC/DC0%0/3
(eval { do { path(".")->realpath } } // eval { do { path(".")->absolute } }) // path(".")
eval { do { path(".")->realpath } } eval { do { path(".")->absolute } } path(".")
Truth table25%1/4
A // B // C
A= eval { do { path(".")->realpath } }B= eval { do { path(".")->absolute } }C= path(".")
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A // B // C
A= eval { do { path(".")->realpath } }B= eval { do { path(".")->absolute } }C= path(".")
ABCresult
1XX1
01X1
0011
0000
27 16 $ROOT = $LOAD_ROOT;
28 }
29  
30 14 my %FILTER;
31 my %EXCLUDE_ROOT_CACHE;
32  
33 16 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 XSLoader::load(__PACKAGE__, $VERSION);
35  
36 15 1;
37  
38 15 my $IMPORTED = 0;
39 8 ▶sub import {
Subroutine import: called CC=7 SCAR=24.5
40 8 my $class = shift;
41 8 my %params = $class->_parse_params(@_);
42  
43 8 ▶ if ($params{disabled}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params{"disabled"}) 1 7 8
if ($params{"disabled"}) 0 0 0
44 1 $class->disable;
45 1 return;
46 }
47  
48 7 $class->enable;
49  
50 7 ▶ return if $params{no_event};
Branch: 2 branches
BranchTrueFalseTotal
if $params{"no_event"} 0 7 7
if $params{"no_event"} 0 0 0
51  
52 7 ▶ if ($IMPORTED++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($IMPORTED++) 0 7 7
if ($IMPORTED++) 0 0 0
53 0 ▶ croak "$class has already been imported, too late to add params" if keys %params;
Branch: 2 branches
BranchTrueFalseTotal
if keys %params 0 0 0
if keys %params 0 0 0
54 0 return;
55 }
56  
57 7 $class->reload;
58  
59 7 my $ran = 0;
60 7 ▶ $ROOT = "" . $params{root} if $params{root};
Branch: 2 branches
BranchTrueFalseTotal
if $params{"root"} 0 7 7
if $params{"root"} 0 0 0
61  
62 # A failed report should not be able to break test teardown, losing the
63 # coverage data (with a warning) is better than breaking the test.
64 14 ▶ my $callback = sub {
Subroutine __ANON__: called CC=3 SCAR=12.6
65 14 ▶ return if $ran++;
Branch: 2 branches
BranchTrueFalseTotal
if $ran++ 7 7 14
if $ran++ 0 0 0
66 7 local ($@, $!);
67 7 ▶ eval { $class->report(%params, ctx => $_[0], root => $ROOT); 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $class->report(%params, "ctx", $_[0], "root", $ROOT); 1 } } or warn "$class could not send the coverage report: $@" 7 0 7
eval { do { $class->report(%params, "ctx", $_[0], "root", $ROOT); 1 } } or warn "$class could not send the coverage report: $@" 0 0 0
68 or warn "$class could not send the coverage report: $@";
69 7 };
70  
71 7 test2_add_callback_exit($callback);
72  
73 # Fallback if we fork.
74 7 ▶ eval 'END { local $?; $callback->() }; 1'
Subroutine END: called CC=1 SCAR=0.8
Branch: 2 branches
BranchTrueFalseTotal
eval "END { local \$?; \$callback->() }; 1" or carp("$class could not install the END block fallback, coverage may be lost in forked processes: $@") 7 0 7
eval "END { local \$?; \$callback->() }; 1" or carp("$class could not install the END block fallback, coverage may be lost in forked processes: $@") 0 0 0
75 or carp "$class could not install the END block fallback, coverage may be lost in forked processes: $@";
76 }
77  
78 7 ▶sub reload {
Subroutine reload: called CC=3 SCAR=11.4
Pod documented
Pod documented
79 7 ▶ $ROOT = $LOAD_ROOT // "" . path('.')->realpath;
Condition50%1/2
$LOAD_ROOT // '' . path(".")->realpath
l!l
70
MC/DC0%0/1
$LOAD_ROOT // '' . path(".")->realpath
$LOAD_ROOT
Truth table50%1/2
A
A= $LOAD_ROOT
Aresult
11
00
80 7 ▶ %FILTER = map {-f $_ ? ($_ => 1) : ()} $0, __FILE__, File::Spec->rel2abs($0), File::Spec->rel2abs(__FILE__);
Branch: 1 branch
BranchTrueFalseTotal
-f $_ ? : 28 0 28
81 7 %EXCLUDE_ROOT_CACHE = ();
82 }
83  
84 6 ▶sub enabled { $ENABLED }
Subroutine enabled: called CC=1 SCAR=0.0
Pod documented
85 18 ▶sub enable { $ENABLED = 1 }
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
86 4 ▶sub disable { $ENABLED = 0 }
Subroutine disable: called CC=1 SCAR=0.0
Pod documented
87  
88 7 ▶sub full_reset {
Subroutine full_reset: called CC=1 SCAR=0.0
Pod documented
89 7 reset_from();
90 7 reset_coverage();
91 }
92  
93 10 ▶sub reset_from {
Subroutine reset_from: called CC=1 SCAR=0.0
Pod documented
94 10 $FROM = '*';
95 10 $FROM_MODIFIED = 0;
96 10 $FROM_MANAGER = undef;
97 }
98  
99 30 ▶sub reset_coverage {
Subroutine reset_coverage: called CC=1 SCAR=0.0
Pod documented
100 30 %REPORT = ();
101 }
102  
103 2 ▶sub set_root { $ROOT = "" . pop };
Subroutine set_root: called CC=1 SCAR=0.0
Pod documented
104  
105 5 ▶sub get_from { $FROM //= '*' }
Subroutine get_from: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$FROM //= "*"
l!l
50
MC/DC0%0/1
$FROM //= "*"
$FROM
Truth table50%1/2
A
A= $FROM
Aresult
11
00
106 18 ▶sub set_from { my $from = pop; return unless _validate_from($from); $FROM_MODIFIED++; $FROM = $from }
Subroutine set_from: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_from($from) 3 15 18
107 2 ▶sub clear_from { $FROM = '*' }
Subroutine clear_from: called CC=1 SCAR=0.0
Pod documented
108 6 ▶sub was_from_modified { $FROM_MODIFIED ? 1 : 0 }
Subroutine was_from_modified: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$FROM_MODIFIED ? : 3 3 6
109 2 ▶sub set_from_manager { $FROM_MODIFIED++; $FROM_MANAGER = pop }
Subroutine set_from_manager: called CC=1 SCAR=0.0
Pod documented
110  
111 # 'from' values end up serialized into the coverage event, catch things here
112 # that cannot survive that. This warns and rejects instead of dying, coverage
113 # collection should never introduce new exceptions into the code it is
114 # observing, we just collect what data we can.
115 32 ▶sub _validate_from {
Subroutine _validate_from: called CC=16 SCAR=28.0
116 32 my ($val, $seen) = @_;
117 32 ▶ return 1 unless ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 16 16 32
118  
119 16 ▶ my $type = reftype($val) // '';
Condition50%1/2
builtin::reftype($val) // ""
l!l
160
MC/DC0%0/1
builtin::reftype($val) // ""
builtin::reftype($val)
Truth table50%1/2
A
A= builtin::reftype($val)
Aresult
11
00
120 16 ▶ if ($type eq 'CODE' || $type eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "CODE" or $type eq "GLOB") 3 13 16
Condition100%3/3
$type eq "CODE" or $type eq "GLOB"
l!l&&r!l&&!r
2113
MC/DC100%2/2
$type eq "CODE" or $type eq "GLOB"
$type eq "CODE" $type eq "GLOB"
Truth table100%3/3
A or B
A= $type eq "CODE"B= $type eq "GLOB"
ABresult
1X1
011
000
121 3 carp "'from' values must be serializable, they may not be (or contain) $type references, ignoring this 'from' value";
122 3 return 0;
123 }
124  
125 13 ▶ $seen //= {};
Condition100%2/2
$seen //= {}
l!l
67
MC/DC100%1/1
$seen //= {}
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
126 13 ▶ return 1 if $seen->{"$val"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$seen{"$val"}++ 1 12 13
127  
128 12 ▶ if ($type eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 5 7 12
elsif ($type eq 'ARRAY') { } 6 1 7
elsif ($type eq 'SCALAR' or $type eq 'REF') { } 1 0 1
129 5 ▶ for my $v (values %$val) { return 0 unless _validate_from($v, $seen) }
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_from($v, $seen) 2 3 5
130 }
131 elsif ($type eq 'ARRAY') {
132 6 ▶ for my $v (@$val) { return 0 unless _validate_from($v, $seen) }
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_from($v, $seen) 1 7 8
133 }
134 ▶ elsif ($type eq 'SCALAR' || $type eq 'REF') {
Condition33%1/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
100
MC/DC0%0/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table33%1/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
135 1 ▶ return 0 unless _validate_from($$val, $seen);
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_from($$val, $seen) 0 1 1
136 }
137  
138 9 return 1;
139 }
140  
141 7 ▶sub touch_data_file {
Subroutine touch_data_file: called CC=4 SCAR=14.2
Pod documented
142 7 my $class = shift;
143 7 my ($file, $from) = @_;
144 7 ▶ croak "A file is required" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 7 7
145 7 ▶ return unless $ENABLED;
Branch: 1 branch
BranchTrueFalseTotal
unless $ENABLED 1 6 7
146 6 ▶ $from //= $FROM;
Condition33%1/3
$from //= $FROM
l!l&&r!l&&!r
060
MC/DC0%0/2
$from //= $FROM
$from $FROM
Truth table33%1/3
A //= B
A= $fromB= $FROM
ABresult
1X1
011
000
147  
148 6 $REPORT{$file}{'<>'}{$from} = $from;
149 6 return;
150 }
151  
152 21 ▶sub touch_source_file {
Subroutine touch_source_file: called CC=7 SCAR=19.7
Pod documented
153 21 my $class = shift;
154 21 my ($file, $subs, $from) = @_;
155 21 ▶ croak "A file is required" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 20 21
156 20 ▶ return unless $ENABLED;
Branch: 1 branch
BranchTrueFalseTotal
unless $ENABLED 1 19 20
157  
158 19 ▶ $subs //= ['*'];
Condition66%2/3
$subs //= ["*"]
l!l
7120
MC/DC100%1/1
$subs //= ["*"]
$subs
Truth table100%2/2
A
A= $subs
Aresult
11
00
159 19 ▶ $subs = [$subs] unless 'ARRAY' eq ref($subs);
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $subs 6 13 19
160  
161 19 ▶ $from //= $FROM;
Condition33%1/3
$from //= $FROM
l!l&&r!l&&!r
0190
MC/DC0%0/2
$from //= $FROM
$from $FROM
Truth table33%1/3
A //= B
A= $fromB= $FROM
ABresult
1X1
011
000
162  
163 19 $REPORT{$file}{$_}{$from} = $from for @$subs;
164  
165 19 return;
166 }
167  
168 135 ▶sub filter {
Subroutine filter: called CC=6 SCAR=17.9
Pod documented
169 135 my $class = shift;
170 135 my ($file, %params) = @_;
171  
172 135 my $root = $class->_resolve_root($params{root});
173  
174 135 ▶ my $path = $INC{$file} ? path($INC{$file}) : path($file);
Branch: 1 branch
BranchTrueFalseTotal
$INC{$file} ? : 0 135 135
175 135 ▶ $path = $path->realpath if $path->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $path->exists 84 51 135
176  
177 # Compare the resolved path, not the raw input, so symlinked roots and
178 # files resolve consistently with the relative() call below.
179 135 ▶ return () unless $root->subsumes($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $root->subsumes($path) 34 101 135
180  
181 101 for my $exclude (@{$class->_exclude_roots($params{exclude}, $root)}) {
182 19 ▶ return () if $exclude->subsumes($path);
Branch: 1 branch
BranchTrueFalseTotal
if $exclude->subsumes($path) 12 7 19
183 }
184  
185 89 return $path->relative($root)->stringify();
186 }
187  
188 156 ▶sub extract {
Subroutine extract: called CC=13 SCAR=25.8
Pod documented
189 156 my $class = shift;
190 156 my ($file) = @_;
191  
192 # If we opened a file with 2-arg open
193 156 $file =~ s/^[\+\-]?(?:>{1,2}|<|\|)[\+\-]?//;
194  
195 # Sometimes things get nested and we need to extract and then extract again...
196 156 while (1) {
197 # No hope :-(
198 204 ▶ return if $file =~ m/^\(eval( \d+\)?)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^\(eval( \d+\)?)$/ 4 200 204
199  
200 # Easy
201 200 ▶ return $file if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 77 123 200
202  
203 123 my $start = $file;
204  
205 # Moose like to write "blah blah (defined at filename line 123)"
206 123 ▶ $file = $1 if $file =~ m/(?:defined|declared) (?:at|in) (.+) at line \d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /(?:defined|declared) (?:at|in) (.+) at line \d+/ 4 119 123
207 123 ▶ $file = $1 if $file =~ m/(?:defined|declared) (?:at|in) (.+) line \d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /(?:defined|declared) (?:at|in) (.+) line \d+/ 5 118 123
208 123 ▶ $file = $1 if $file =~ m/\(eval \d+\)\[(.+):\d+\]/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\(eval \d+\)\[(.+):\d+\]/ 37 86 123
209 123 ▶ $file = $1 if $file =~ m/\((.+)\) line \d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\((.+)\) line \d+/ 1 122 123
210 123 ▶ $file = $1 if $file =~ m/\((.+)\) at line \d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\((.+)\) at line \d+/ 1 122 123
211  
212 # Extracted everything away
213 123 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 123 123
214  
215 # Not going to change anymore
216 123 ▶ last if $file eq $start;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $start 75 48 123
217 }
218  
219 # These characters are rare in file names, but common in calls where files
220 # could not be determined, so we probably failed to extract. If this
221 # assumption is wrong for someone they can write a custom extract, this is
222 # not a bug.
223 75 ▶ return if $file =~ m/([\[\]\(\)]|->|\beval\b)/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /([\[\]\(\)]|->|\beval\b)/ 7 68 75
224  
225 # If we have a foo.bar pattern, or a string that contains this platforms
226 # file separator we will consider it a valid file.
227 68 ▶ return $file if $file =~ m/\S+\.\S+$/i || $file =~ m/\Q$SEP\E/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\S+\.\S+$/i or $file =~ /\Q$SEP\E/ 68 0 68
Condition66%2/3
$file =~ /\S+\.\S+$/i or $file =~ /\Q$SEP\E/
l!l&&r!l&&!r
6620
MC/DC0%0/2
$file =~ /\S+\.\S+$/i or $file =~ /\Q$SEP\E/
$file =~ /\S+\.\S+$/i $file =~ /\Q$SEP\E/
Truth table66%2/3
A or B
A= $file =~ /\S+\.\S+$/iB= $file =~ /\Q$SEP\E/
ABresult
1X1
011
000
228  
229 0 return;
230 }
231  
232 15 my %HIDDEN_SUBS = (
233 '__ANON__' => 1,
234 'eval' => 1,
235 );
236  
237 15 my %SPECIAL_SUBS = (
238 'BEGIN' => 1,
239 'CHECK' => 1,
240 'END' => 1,
241 'INIT' => 1,
242 'UNITCHECK' => 1,
243 );
244  
245 15 ▶sub files {
Subroutine files: called CC=1 SCAR=0.0
Pod documented
246 15 my $class = shift;
247 15 my %params = @_;
248  
249 15 my $report = $class->_process(%params);
250  
251 15 return [sort keys %$report];
252 }
253  
254 20 ▶sub data {
Subroutine data: called CC=7 SCAR=19.5
Pod documented
255 20 my $class = shift;
256 20 my %params = @_;
257  
258 20 my $report = $class->_process(%params);
259  
260 20 my $out = {};
261  
262 20 for my $file (keys %$report) {
263 33 ▶ my $rval = $report->{$file} // next;
Condition33%1/3
$report->{$file} // (next)
l!l
3300
MC/DC0%0/1
$report->{$file} // (next)
$report->{$file}
Truth table50%1/2
A
A= $report->{$file}
Aresult
11
00
264 33 my $oval = $out->{$file} = {};
265 33 my %seen;
266  
267 33 for my $sub (keys %$rval) {
268 43 ▶ next if $HIDDEN_SUBS{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if $HIDDEN_SUBS{$sub} 1 42 43
269  
270 42 ▶ my $key = $SPECIAL_SUBS{$sub} ? '*' : $sub;
Branch: 1 branch
BranchTrueFalseTotal
$SPECIAL_SUBS{$sub} ? : 1 41 42
271 42 push @{$oval->{$key}} => grep { !$seen{$key}{_from_key($_)}++ } values %{$rval->{$sub}};
272 }
273  
274 33 @$_ = sort { _from_key($a) cmp _from_key($b) } @$_ for values %$oval;
275 }
276  
277 20 return $out;
278 }
279  
280 # Sort/dedup key for 'from' values. References are keyed by serialized
281 # content, not address, so output order is stable between runs and
282 # identical structures from separate set_from() calls collapse into one.
283 87 ▶sub _from_key {
Subroutine _from_key: called CC=2 SCAR=6.9
284 87 my ($val) = @_;
285 87 ▶ return "s:$val" unless ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 70 17 87
286  
287 17 require Data::Dumper;
288 17 local $Data::Dumper::Indent = 0;
289 17 local $Data::Dumper::Sortkeys = 1;
290 17 local $Data::Dumper::Terse = 1;
291  
292 17 return "r:" . Data::Dumper::Dumper($val);
293 }
294  
295 10 ▶sub report {
Subroutine report: called CC=4 SCAR=14.1
Pod documented
296 10 my $class = shift;
297 10 my %params = @_;
298  
299 10 my $data = $class->data(%params);
300 10 my $details = "This test covered " . scalar(keys %$data) . " source files.";
301 10 ▶ my $type = $FROM_MODIFIED ? 'split' : 'flat';
Branch: 1 branch
BranchTrueFalseTotal
$FROM_MODIFIED ? : 4 6 10
302  
303 # Relative paths in the file list are resolved against this root. A consumer
304 # in another process cannot know it otherwise, and would have to guess from
305 # its own directory.
306 10 my $root = $class->_resolve_root($params{root})->stringify;
307  
308 10 ▶ my $ctx = $params{ctx} // context();
Condition33%1/3
$params{"ctx"} // context()
l!l&&r!l&&!r
0100
MC/DC0%0/2
$params{"ctx"} // context()
$params{"ctx"} context()
Truth table33%1/3
A // B
A= $params{"ctx"}B= context()
ABresult
1X1
011
000
309 my $event = $ctx->send_ev2(
310 about => {package => __PACKAGE__, details => $details},
311  
312 coverage => {
313 root => $root,
314 files => $data,
315 details => $details,
316 test_type => $type,
317 from_manager => $FROM_MANAGER,
318 },
319  
320 10 info => [{tag => 'COVERAGE', details => $details, debug => $params{verbose}}],
321 );
322 10 ▶ $ctx->release unless $params{ctx};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"ctx"} 10 0 10
323  
324 10 return $event;
325 }
326  
327 # 'exclude' can be given more than once, which a hash cannot hold, so the
328 # argument list is walked in pairs instead of being assigned to one.
329 12 ▶sub _parse_params {
Subroutine _parse_params: called CC=6 SCAR=17.9
330 12 my $class = shift;
331  
332 12 my (%params, @exclude);
333 12 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 11 20
334 9 my $key = shift;
335  
336 9 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 8 9
337 1 carp "Odd number of parameters for $class, '$key' has no value";
338 1 last;
339 }
340  
341 8 my $val = shift;
342  
343 8 ▶ if ($key eq 'exclude') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'exclude') { } 5 3 8
344 5 ▶ push @exclude => ref($val) eq 'ARRAY' ? @$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 4 5
345 }
346 else {
347 3 $params{$key} = $val;
348 }
349 }
350  
351 12 ▶ $params{exclude} = \@exclude if @exclude;
Branch: 1 branch
BranchTrueFalseTotal
if @exclude 3 9 12
352  
353 12 return %params;
354 }
355  
356 145 ▶sub _resolve_root {
Subroutine _resolve_root: called CC=3 SCAR=12.0
357 145 my $class = shift;
358 145 my ($root) = @_;
359  
360 145 ▶ $root = path($root // '.');
Condition33%1/3
$root // "."
l!l
14500
MC/DC0%0/1
$root // "."
$root
Truth table50%1/2
A
A= $root
Aresult
11
00
361 145 ▶ return $root->exists ? $root->realpath : $root;
Branch: 1 branch
BranchTrueFalseTotal
$root->exists ? : 145 0 145
362 }
363  
364 101 ▶sub _exclude_roots {
Subroutine _exclude_roots: called CC=12 SCAR=27.1
365 101 my $class = shift;
366 101 my ($exclude, $root) = @_;
367  
368 101 ▶ return [] unless defined $exclude;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exclude 83 18 101
369  
370 18 my @out;
371 18 ▶ for my $item (ref($exclude) eq 'ARRAY' ? @$exclude : ($exclude)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $exclude eq 'ARRAY' ? : 2 16 18
372 19 ▶ next unless defined $item && length "$item";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item and length "$item" 0 19 19
Condition33%1/3
defined $item and length "$item"
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $item and length "$item"
defined $item length "$item"
Truth table33%1/3
A and B
A= defined $itemB= length "$item"
ABresult
0X0
100
111
373  
374 19 my $path = path($item);
375  
376 # A relative exclusion belongs to the tree being measured, not to
377 # whatever directory the process happens to be in when it reports.
378 19 ▶ $path = $root->child($path) if $root && !$path->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if $root and not $path->is_absolute 1 18 19
Condition66%2/3
$root and not $path->is_absolute
!ll&&!rl&&r
0181
MC/DC50%1/2
$root and not $path->is_absolute
$root not $path->is_absolute
Truth table66%2/3
A and not(B)
A= $rootB= not $path->is_absolute
ABresult
0X0
100
111
379  
380 # filter() runs once per recorded file and would otherwise resolve the
381 # same roots again for every one of them. Anything still relative here
382 # had no root to anchor to, and means something different after a
383 # chdir, so it is not cached.
384 19 ▶ unless ($path->is_absolute) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path->is_absolute) 0 19 19
385 0 ▶ push @out => $path->exists ? $path->realpath : $path->absolute;
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 0 0 0
386 0 next;
387 }
388  
389 19 ▶ push @out => $EXCLUDE_ROOT_CACHE{"$path"} //= $path->exists ? $path->realpath : $path->absolute;
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 3 1 4
Condition66%2/3
$EXCLUDE_ROOT_CACHE{"$path"} //= $path->exists ? $path->realpath : $path->absolute
l!l&&r!l&&!r
1540
MC/DC0%0/2
$EXCLUDE_ROOT_CACHE{"$path"} //= $path->exists ? $path->realpath : $path->absolute
$EXCLUDE_ROOT_CACHE{"$path"} $path->exists ? $path->realpath : $path->absolute
Truth table66%2/3
A //= B
A= $EXCLUDE_ROOT_CACHE{"$path"}B= $path->exists ? $path->realpath : $path->absolute
ABresult
1X1
011
000
390 }
391  
392 18 return \@out;
393 }
394  
395 35 ▶sub _process {
Subroutine _process: called CC=14 SCAR=26.8
396 35 my $class = shift;
397 35 my %params = @_;
398  
399 35 my $filter = $class->can('filter');
400 35 my $extract = $class->can('extract');
401  
402 35 my $clone = _clone_report();
403 35 my %report;
404  
405 35 for my $raw (keys %$clone) {
406 152 ▶ next unless $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw 0 152 152
407 152 ▶ next if $FILTER{$raw};
Branch: 1 branch
BranchTrueFalseTotal
if $FILTER{$raw} 26 126 152
408  
409 # A single bad entry (custom extract/filter that dies, a file deleted
410 # mid-processing, etc) must not cost us the rest of the report.
411 126 my $file;
412 126 ▶ unless (eval { $file = $class->$extract($raw, %params); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $file = $class->$extract($raw, %params); 1 } }) 1 125 126
413 1 warn "$class could not extract a filename from '$raw', skipping it: $@";
414 1 next;
415 }
416 125 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 125 125
417 125 ▶ next if $FILTER{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $FILTER{$file} 7 118 125
418  
419 118 my $path;
420 118 ▶ unless (eval { $path = $class->$filter($file, %params); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $path = $class->$filter($file, %params); 1 } }) 1 117 118
421 1 warn "$class could not filter '$file', skipping it: $@";
422 1 next;
423 }
424 117 ▶ next unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 37 80 117
425 80 ▶ next if $FILTER{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $FILTER{$path} 0 80 80
426  
427 80 my $from = $clone->{$raw};
428  
429 # Merge
430 80 ▶ my $into = $report{$path} //= {};
Condition33%1/3
$report{$path} //= {}
l!l
0800
MC/DC0%0/1
$report{$path} //= {}
$report{$path}
Truth table50%1/2
A
A= $report{$path}
Aresult
11
00
431  
432 80 for my $sub (keys %$from) {
433 102 ▶ my $src = $from->{$sub} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $src = $from->{$sub} or next 102 0 102
434 102 ▶ my $dst = $into->{$sub} //= {};
Condition33%1/3
$into->{$sub} //= {}
l!l
01020
MC/DC0%0/1
$into->{$sub} //= {}
$into->{$sub}
Truth table50%1/2
A
A= $into->{$sub}
Aresult
11
00
435 102 %$dst = (%$dst, %$src);
436 }
437 }
438  
439 35 return \%report;
440 }
441  
442 # Copy the fixed 3-level {file}{sub}{from_key} structure of %REPORT so later
443 # processing cannot modify the live data. The innermost 'from' values are
444 # intentionally shared, not cloned - they can be arbitrary user structures
445 # and cloning them (Storable) dies on things like code refs.
446 35 ▶sub _clone_report {
Subroutine _clone_report: called CC=3 SCAR=11.0
447 35 my %clone;
448  
449 35 for my $file (keys %REPORT) {
450 152 ▶ my $subs = $REPORT{$file} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $subs = $REPORT{$file} or next 152 0 152
451 152 $clone{$file} = {map { ($_ => {%{$subs->{$_}}}) } keys %$subs};
452 }
453  
454 35 return \%clone;
455 }
456  
457 15 1;
458  
459 __END__
↑ Summary blib/lib/Test2/EventFacet/Coverage.pm »