blib/lib/App/Prove/Plugin/TraceUse.pm

filter: Statementstmt 73.6% 159 / 216 Branchbran 59.3% 38 / 64 Conditioncond 27.2% 6 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 85.7% 30 / 35 Podpod 100% 1 / 1 totaltotal 67.0% 236 / 352 CC 46 SCAR 28.1
CC
46
Cov
67%
present_dependencies
90.0
__ANON__
42.0
load
9.9
CRAP 16.6
↑ Summary
1 package App::Prove::Plugin::TraceUse;
2  
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use version qw/is_lax qv/; our $VERSION = qv('1.0.3');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Tree::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Set::Object qw/set/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Pod::Perldoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 my $pd;
16  
17 209 ▶sub _parse_module_and_version {
Subroutine _parse_module_and_version: called CC=2 SCAR=6.9
18  
19 209 my @parsed = ();
20  
21 209 ▶ if ( $_[0] =~ /^\s+\d+\.\s+(\S+)(?: ([\d\._]+))?,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s+\d+\.\s+(\S+)(?: ([\d\._]+))?,/) 203 6 209
22 203 @parsed = grep defined , ($1,$2);
23 }
24 209 return @parsed;
25  
26 }
27  
28 1 ▶sub _parse_traceuse {
Subroutine _parse_traceuse: called CC=4 SCAR=13.9
29  
30 # General idea:
31 # * create a tree from the indentation levels
32 # * root nodes should be modules with [main]
33 # * output that has less indent modules than any [main]
34 # should give a parse error
35  
36 # When tree done:
37 # * check @INC of local system
38 # * root nodes are candidates for being a dependency
39 # * root nodes not in @INC shoule be skipped and next level considered instead
40  
41 1 my @lines = split /\n/, $_[0];
42  
43 1 my %indent_parents =
44 ( 0 => Tree::Simple->new( 1, Tree::Simple->ROOT ) );
45  
46 1 for ( @lines ) {
47  
48 108 my($mod,$ver) = _parse_module_and_version($_);
49  
50 108 ▶ next unless $mod;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod 6 102 108
51  
52 102 my $node = Tree::Simple->new([$mod,$ver]);
53  
54 102 my ($indent) = /^\s+\d+\.(\s+)\S+/;
55  
56 102 my $indent_level = length $indent;
57  
58 102 $indent_parents{$indent_level} = $node;
59 102 my $parent = $indent_parents{$indent_level - 2};
60  
61 102 ▶ unless ( $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 0 102 102
62 0 confess "Odd, should have had a parent for:\n$_\nIndent: $indent_level, indent: [$indent]\n";
63 }
64  
65 102 $parent->addChild( $node );
66  
67 }
68  
69 1 return $indent_parents{0};
70  
71 }
72  
73 36 ▶sub _system_inc {
Subroutine _system_inc: called CC=3 SCAR=11.0
74  
75 36 my $negate = shift;
76  
77 36 my $s1 = set( @INC );
78  
79 my $s2 = set( ".",
80 36 ▶ (exists($ENV{PERL5LIB}) ? split( ":", $ENV{PERL5LIB}) : ())
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'PERL5LIB'} ? : 36 0 36
81 );
82  
83 36 ▶ my @i = $negate ? (@$s2) : @{$s1 - $s2};
Branch: 1 branch
BranchTrueFalseTotal
$negate ? : 18 18 36
84  
85 36 return @i;
86  
87 }
88  
89 16 ▶sub _module_dir {
Subroutine _module_dir: called CC=5 SCAR=16.1
90  
91 16 my ($mod,$check_noninc) = @_;
92  
93 16 my @inc = _system_inc;
94 16 my @noninc = _system_inc(1);
95  
96 16 ▶ $pd ||= Pod::Perldoc->new;
Condition66%2/3
$pd ||= "Pod::Perldoc"->new
l!l&&r!l&&!r
1420
MC/DC0%0/2
$pd ||= "Pod::Perldoc"->new
$pd "Pod::Perldoc"->new
Truth table66%2/3
A ||= B
A= $pdB= "Pod::Perldoc"->new
ABresult
1X1
011
000
97  
98 16 ▶ my $d = $check_noninc ? $pd->searchfor( 1, $mod, @noninc ) : $pd->searchfor( 1, $mod, @inc );
Branch: 1 branch
BranchTrueFalseTotal
$check_noninc ? : 7 9 16
99  
100 16 ▶ return unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 7 9 16
101  
102 9 (my $m2 = $mod) =~ s|::|/|g;
103 9 $m2 .= ".pm";
104  
105 9 $d =~ s|/$m2||;
106  
107 9 ▶ return unless -d $d;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $d 0 9 9
108  
109 9 return $d;
110  
111 }
112  
113 3 ▶sub __recursively_check_loaded_modules {
Subroutine __recursively_check_loaded_modules: called CC=4 SCAR=13.9
114 3 my($node,$dep_ref) = @_;
115  
116 3 for ( @{ $node->getAllChildren } ) {
117  
118 6 my $mdir = _module_dir( $_->getNodeValue->[0] );
119 6 my $mdir_n = _module_dir( $_->getNodeValue->[0], 1 );
120  
121 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mdir and not defined $mdir_n) { } 5 1 6
Condition66%2/3
defined $mdir and not defined $mdir_n
!ll&&!rl&&r
105
MC/DC50%1/2
defined $mdir and not defined $mdir_n
defined $mdir not defined $mdir_n
Truth table66%2/3
A and not(B)
A= defined $mdirB= not defined $mdir_n
ABresult
0X0
100
111
122 defined($mdir) and not defined($mdir_n)
123 ) {
124 5 push @$dep_ref, $_->getNodeValue;
125 } else {
126 1 __recursively_check_loaded_modules($_, $dep_ref);
127 }
128  
129 }
130  
131 }
132  
133 2 ▶sub _find_dependent_modules {
Subroutine _find_dependent_modules: called CC=1 SCAR=0.0
134  
135 2 my ($t) = @_;
136  
137 2 my @dependencies;
138  
139 2 __recursively_check_loaded_modules($t,\@dependencies);
140  
141 2 return \@dependencies;
142  
143 }
144  
145 34 ▶sub _find_module_in_code {
Subroutine _find_module_in_code: called CC=1 SCAR=0.0
146  
147 34 my($mod,$code) = @_;
148  
149 ## super simple test if the module is present in the code without being comented
150 34 return $code =~ /^[^#]*\b$mod\b.*\d.*$/m;
151  
152 }
153  
154 11 ▶sub _check_makefile_pl_for_module {
Subroutine _check_makefile_pl_for_module: called CC=1 SCAR=0.0
155  
156 11 my($mod) = @_;
157  
158 11 my $mf = read_file( "./Makefile.PL" );
159  
160 11 return _find_module_in_code($mod,$mf);
161  
162 }
163  
164 11 ▶sub _check_build_pl_for_module {
Subroutine _check_build_pl_for_module: called CC=1 SCAR=0.0
165  
166 11 my($mod) = @_;
167  
168 11 my $bf = read_file( "./Build.PL" );
169  
170 11 return _find_module_in_code($mod,$bf);
171  
172 }
173  
174 3 ▶sub _parse_makefile_pl {
Subroutine _parse_makefile_pl: called CC=7 SCAR=19.7
175  
176 3 my ($makefile_input) = @_;
177  
178 3 my $makefile_content;
179  
180 3 ▶ if ( $makefile_input ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($makefile_input) { } 2 1 3
elsif (-e 'Makefile.PL') { } 1 0 1
181  
182 2 ▶ if ( -e $makefile_input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $makefile_input) { } 2 0 2
183 2 $makefile_content = read_file($makefile_input);
184 }
185 else {
186 0 croak "Dont know what to do with input";
187 }
188  
189 }
190 elsif ( -e "Makefile.PL" ) {
191 1 $makefile_content = read_file("Makefile.PL");
192 }
193  
194 3 ▶ return unless $makefile_content;
Branch: 1 branch
BranchTrueFalseTotal
unless $makefile_content 0 3 3
195  
196 3 my($prereq_content) = $makefile_content =~ /^\s*PREREQ_PM\s*=>\s*({\s*[^}]+})/m;
197  
198 3 my $compartment = Safe->new;
199  
200 3 my $prereq_hash_ref = $compartment->reval($prereq_content);
201  
202 ## make sure it is sane
203 3 ▶ while ( my($mod,$ver) = each %$prereq_hash_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$prereq_hash_ref) 26 3 29
204  
205 26 ▶ if ( not is_lax($ver) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_lax($ver)) 2 24 26
206 2 delete $prereq_hash_ref->{$mod};
207 }
208  
209 }
210  
211 3 return $prereq_hash_ref;
212  
213 }
214  
215 3 ▶sub _parse_build_pl {
Subroutine _parse_build_pl: called CC=8 SCAR=21.2
216  
217 3 my ($build_input) = @_;
218  
219 3 my $build_content;
220  
221 3 ▶ if ( $build_input ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($build_input) { } 2 1 3
elsif (-e 'Build.PL') { } 1 0 1
222  
223 2 ▶ if ( -e $build_input ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $build_input) { } 2 0 2
224 2 $build_content = read_file($build_input);
225 }
226 else {
227 0 croak "Dont know what to do with input";
228 }
229  
230 }
231 elsif ( -e "Build.PL" ) {
232 1 $build_content = read_file("Build.PL");
233 }
234  
235 3 ▶ return unless $build_content;
Branch: 1 branch
BranchTrueFalseTotal
unless $build_content 0 3 3
236  
237 3 my($prereq_content) = $build_content =~ /^\s*requires\s*=>\s*({\s*[^}]+})/m;
238  
239 3 ▶ return unless $prereq_content;
Branch: 1 branch
BranchTrueFalseTotal
unless $prereq_content 0 3 3
240  
241 3 my $compartment = Safe->new;
242 3 my $prereq_hash_ref = $compartment->reval($prereq_content);
243  
244 ## make sure it is sane
245 3 ▶ while ( my($mod,$ver) = each %$prereq_hash_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$prereq_hash_ref) 32 3 35
246  
247 32 ▶ if ( not is_lax($ver) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_lax($ver)) 2 30 32
248 2 delete $prereq_hash_ref->{$mod};
249 }
250  
251 }
252  
253 3 return $prereq_hash_ref;
254  
255 }
256  
257 {
258 package TAP::Harness::FOO;
259  
260 13 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
261 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 13 ▶ use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 13 ▶ use base 'TAP::Harness';
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 13 ▶ use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
267 13 ▶ use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
268  
269 13 ▶ use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 13 ▶ use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
272  
273 4 ▶ sub _uniquify_dependencies {
Subroutine _uniquify_dependencies: called CC=5 SCAR=16.1
274  
275 4 my $self = shift;
276  
277 4 my %d;
278  
279 4 for ( @{ $self->{collected_dependencies} } ) {
280  
281 9 ▶ if ( version->new($_->[1]) > version->new($d{ $_->[0] } || 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->new($_->[1]) > "version"->new($d{$_->[0]} || 0)) 6 3 9
Condition100%2/2
$d{$_->[0]} || 0
l!l
45
MC/DC100%1/1
$d{$_->[0]} || 0
$d{$_->[0]}
Truth table100%2/2
A
A= $d{$_->[0]}
Aresult
11
00
282 6 $d{ $_->[0] } = $_->[1];
283 }
284  
285 }
286  
287 4 my @d;
288 4 ▶ while ( my ($k,$v) = each %d ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %d) 5 4 9
289 5 push @d, [$k,$v];
290 }
291  
292 4 $self->{collected_dependencies} = \@d;
293  
294 }
295  
296 0 ▶ sub present_dependencies {
Subroutine present_dependencies: not called CC=9 SCAR=45.0
297  
298 0 my $self = shift;
299  
300 my @d = sort {
301 0 $a->[0] cmp $b->[0]
302 0 } @{ $self->{collected_dependencies} };
303  
304 0 my $n = max( map {length $_->[0]} @d ) + 2;
305  
306 0 print "# TraceUse report:\n";
307  
308 0 ▶ if ( not @d ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@d) 0 0 0
309 0 print "# no noncore dependencies found\n";
310 0 return;
311 }
312  
313 0 my $makefile_requirements = App::Prove::Plugin::TraceUse::_parse_makefile_pl();
314 0 my $build_requirements = App::Prove::Plugin::TraceUse::_parse_build_pl();
315  
316 0 ▶ my $present_file_dep = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
317 0 my ($dep_hash) = @_;
318  
319 0 my $hash_fails = 0;
320  
321 0 for (@d) {
322  
323 0 my($mod,$ver) = @$_;
324  
325 0 my $v = $dep_hash->{$mod};
326  
327 0 ▶ if ( not $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $v) { } 0 0 0
elsif ($v and qv($v) < qv($ver)) { } 0 0 0
328 0 print "# ";
329 0 print colored ['bold red'], sprintf "%-${n}s => '%s',\n", "'".$_->[0]."'", $_->[1];
330 0 $hash_fails = 1;
331 }
332 ▶ elsif ( $v and qv($v) < qv($ver) ) {
Condition0%0/3
$v and qv($v) < qv($ver)
!ll&&!rl&&r
000
MC/DC0%0/2
$v and qv($v) < qv($ver)
$v qv($v) < qv($ver)
Truth table0%0/3
A and B
A= $vB= qv($v) < qv($ver)
ABresult
0X0
100
111
333 0 print "# ";
334 0 print colored ['bold yellow'], sprintf "%-${n}s => '%s',\n", "'".$_->[0]."'", $_->[1];
335 0 $hash_fails = 1;
336 }
337  
338 }
339  
340 0 ▶ if ( not $hash_fails ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash_fails) 0 0 0
341 0 print "# - dependencies are ok\n";
342 }
343  
344 0 return not $hash_fails;
345  
346 0 };
347  
348 0 my $dependencies_are_good = 1;
349  
350 0 ▶ if ( $makefile_requirements ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($makefile_requirements) 0 0 0
351  
352 0 print "# Makefile.PL:\n";
353 0 my $ok = $present_file_dep->($makefile_requirements);
354 0 ▶ $dependencies_are_good &&= $ok;
Condition0%0/3
$dependencies_are_good &&= $ok
!ll&&!rl&&r
000
MC/DC0%0/2
$dependencies_are_good &&= $ok
$dependencies_are_good $ok
Truth table0%0/3
A &&= B
A= $dependencies_are_goodB= $ok
ABresult
0X0
100
111
355  
356 }
357  
358 0 ▶ if ( $build_requirements ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($build_requirements) 0 0 0
359  
360 0 print "# Build.PL:\n";
361 0 my $ok = $present_file_dep->($build_requirements);
362 0 ▶ $dependencies_are_good &&= $ok;
Condition0%0/3
$dependencies_are_good &&= $ok
!ll&&!rl&&r
000
MC/DC0%0/2
$dependencies_are_good &&= $ok
$dependencies_are_good $ok
Truth table0%0/3
A &&= B
A= $dependencies_are_goodB= $ok
ABresult
0X0
100
111
363  
364 }
365  
366 0 ▶ if ( not $dependencies_are_good and 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dependencies_are_good and 0) 0 0 0
Condition0%0/2
not $dependencies_are_good and 0
!ll
00
MC/DC0%0/1
not $dependencies_are_good and 0
not $dependencies_are_good
Truth table0%0/2
not(A)
A= not $dependencies_are_good
Aresult
00
11
367 0 print "# List of dependencies found during testing:\n";
368 0 for ( @d ) {
369 0 printf "# %-${n}s => '%s',\n", "'".$_->[0]."'", $_->[1];
370 }
371 }
372  
373 }
374  
375 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.6
376  
377 1 my $self = shift;
378  
379 1 my $tf = File::Temp->new;
380 1 my $fn = "$tf";
381  
382 ## add the traceuse option
383 1 $_[0]->{switches} = ["-d:TraceUse=hidecore,output:$fn"];
384  
385 1 my $obj = $self->SUPER::new(@_);
386  
387 1 $obj->{collected_dependencies} = [];
388  
389 0 ▶ my $trace_use_sub = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
390  
391 0 my $dt = read_file( $fn );
392 0 my $p = App::Prove::Plugin::TraceUse::_parse_traceuse($dt);
393 0 my $deps = App::Prove::Plugin::TraceUse::_find_dependent_modules($p);
394 0 push @{ $obj->{collected_dependencies} }, @$deps;
395  
396 1 };
397  
398 1 $obj->callback( "after_test", $trace_use_sub );
399  
400 0 ▶ my $collected_dependencies = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
401 0 $obj->_uniquify_dependencies;
402 0 $obj->present_dependencies;
403 1 };
404  
405 1 $obj->callback( "after_runtests", $collected_dependencies );
406  
407 1 return $obj;
408  
409 }
410  
411 }
412  
413  
414 13 ▶sub load {
Subroutine load: not called CC=3 SCAR=23.0
Pod documented
415  
416 0 my( $class, $p ) = @_;
417  
418 0 my $app = $p->{app_prove};
419  
420 0 ▶ if ( defined($app->harness) and $app->harness ne "TAP::Harness" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $app->harness and $app->harness ne "TAP::Harness") 0 0 0
Condition0%0/3
defined $app->harness and $app->harness ne "TAP::Harness"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $app->harness and $app->harness ne "TAP::Harness"
defined $app->harness $app->harness ne "TAP::Harness"
Truth table0%0/3
A and B
A= defined $app->harnessB= $app->harness ne "TAP::Harness"
ABresult
0X0
100
111
421 0 croak "TraceUse plugin is only compatible wtih TAP::Harness";
422 }
423  
424 0 $app->{harness_class} = "TAP::Harness::FOO";
425  
426 0 1;
427  
428 }
429  
430 13 1; # Magic true value required at end of module
431 __END__
↑ Summary