blib/lib/Test/DiagINC.pm

filter: Statementstmt 92.6% 63 / 68 Branchbran 68.7% 22 / 32 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 83.3% 5 / 6 Podpod n/a totaltotal 81.4% 101 / 124 CC 24 SCAR 24.7
CC
24
Cov
83%
_assert_no_fail
16.7
BEGIN
11.5
END
4.2
CRAP 11.8
↑ Summary examples/lib/Foo.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Test::DiagINC;
4 # ABSTRACT: List modules and versions loaded if tests fail
5  
6 3 our $VERSION = '0.010';
7  
8 # If the tested module did not load strict/warnings we do not want
9 # to load them either. On the other hand we would like to know our
10 # code is at least somewhat ok. Therefore this madness ;)
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=24.4
12 3 ▶ if ( $ENV{RELEASE_TESTING} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"RELEASE_TESTING"}) 0 3 3
13 0 ▶ require warnings && warnings->import;
Branch: 1 branch
BranchTrueFalseTotal
require warnings and "warnings"->import 0 0 0
14 0 ▶ require strict && strict->import;
Branch: 1 branch
BranchTrueFalseTotal
require strict and "strict"->import 0 0 0
15 }
16 }
17  
18 4 ▶sub _max_length {
Subroutine _max_length: called CC=3 SCAR=11.0
19 4 my $max = 0;
20 516 ▶ do { $max = length if length > $max }
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $max 18 498 516
21 4 for @_;
22 4 return $max;
23 }
24  
25 # Get our CWD *without* loading anything. Original idea by xdg++
26 # ribasushi thinks this is fragile and will break sooner rather than
27 # later, but adding it as is because haarg and xdg both claim it's fine.
28 # Requires %ENV cleanup to work under taint mode
29 3 my $REALPATH_CWD = do {
30 3 local $ENV{PATH};
31 3 delete $ENV{PATH};
32 3 local $ENV{IFS};
33 3 delete $ENV{IFS};
34 3 local $ENV{CDPATH};
35 3 delete $ENV{CDPATH};
36 3 local $ENV{ENV};
37 3 delete $ENV{ENV};
38 3 local $ENV{BASH_ENV};
39 3 delete $ENV{BASH_ENV};
40 3 my ($perl) = $^X =~ /(.+)/; # $^X is internal how could it be tainted?!
41 3 `"$perl" -MCwd -le "print getcwd"`;
42 };
43 3 chomp $REALPATH_CWD;
44  
45 3 my $ORIGINAL_PID = $$;
46  
47 3 ▶END {
Subroutine END: called CC=4 SCAR=14.2
48 3 ▶ if ( $$ == $ORIGINAL_PID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ == $ORIGINAL_PID) 3 0 3
49 # make sure we report only on stuff that was loaded by the test,
50 # nothing more
51 # get a snapshot early in order to not misreport B.pm and friends
52 # below - this *will* skip any extra modules loaded in END, it was
53 # deemed an acceptable compromise by ribasushi and xdg
54 3 my @INC_list = keys %INC;
55  
56 # If we meet the "fail" criteria - no need to load B and fire
57 # an extra check in an extra END (also doesn't work on 5.6)
58 3 ▶ if ( _assert_no_fail(@INC_list) and $] >= 5.008 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_assert_no_fail(@INC_list) and $] >= "5.008") 1 2 3
Condition66%2/3
_assert_no_fail(@INC_list) and $] >= "5.008"
!ll&&!rl&&r
201
MC/DC50%1/2
_assert_no_fail(@INC_list) and $] >= "5.008"
_assert_no_fail(@INC_list) $] >= "5.008"
Truth table66%2/3
A and B
A= _assert_no_fail(@INC_list)B= $] >= "5.008"
ABresult
0X0
100
111
59  
60 # we did not report anything yet - add an extra END to catch
61 # possible future-fails
62 1 require B;
63 1 ▶ push @{ B::end_av()->object_2svref }, sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
64 0 _assert_no_fail(@INC_list);
65 1 };
66 }
67 }
68 }
69  
70 # Dump %INC IFF in the main process and test is failing or exit is non-zero
71 # return true if no failure or if PID mismatches, return false otherwise
72 3 ▶sub _assert_no_fail {
Subroutine _assert_no_fail: called CC=16 SCAR=28.2
73  
74 3 ▶ return 1 if $$ != $ORIGINAL_PID;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $ORIGINAL_PID 0 3 3
75  
76 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($? or $INC{"Test/Builder.pm"} and "Test::Builder"->can("is_passing") and not "Test::Builder"->new->is_passing) 2 1 3
Condition66%6/9
$? or $INC{"Test/Builder.pm"} and "Test::Builder"->can("is_passing") and not "Test::Builder"->new->is_passing
l!l&&r!l&&!r
111
$INC{"Test/Builder.pm"} and "Test::Builder"->can("is_passing") and not "Test::Builder"->new->is_passing
!ll&&!rl&&r
011
$INC{"Test/Builder.pm"} and "Test::Builder"->can("is_passing")
!ll&&!rl&&r
002
MC/DC50%2/4
$? or $INC{"Test/Builder.pm"} and "Test::Builder"->can("is_passing") and not "Test::Builder"->new->is_passing
$? $INC{"Test/Builder.pm"} "Test::Builder"->can("is_passing") not "Test::Builder"->new->is_passing
Truth table60%3/5
A or B and C and not(D)
A= $?B= $INC{"Test/Builder.pm"}C= "Test::Builder"->can("is_passing")D= not "Test::Builder"->new->is_passing
ABCDresult
1XXX1
01111
00XX0
010X0
01100
77 $?
78 or ( $INC{'Test/Builder.pm'}
79 and Test::Builder->can('is_passing')
80 and !Test::Builder->new->is_passing )
81 )
82 {
83  
84 2 require Cwd;
85 2 require File::Spec;
86 2 require Cwd;
87  
88 2 my %results;
89  
90 2 for my $pkg_as_path (@_) {
91 266 ▶ next unless ( my $p = $pkg_as_path ) =~ s/\.pm\z//;
Branch: 1 branch
BranchTrueFalseTotal
unless (my $p = $pkg_as_path) =~ s/\.pm\z// 4 262 266
92 262 $p =~ s{/}{::}g;
93 262 ▶ next unless $p =~ /\A[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*\Z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /\A[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*\Z/ 0 262 262
94  
95 # a module we recorded as INCed disappeared...
96 262 ▶ if ( not exists $INC{$pkg_as_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$pkg_as_path}) 0 262 262
97 0 $results{$p} = 'Module unloaded in END...?';
98 0 next;
99 }
100  
101 262 ▶ if ( not defined $INC{$pkg_as_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INC{$pkg_as_path}) 2 260 262
102 2 $results{$p} = 'Found and failed to load';
103 2 next;
104 }
105  
106 next
107 if (
108 # rel2abs on an absolute path is a noop
109 # https://metacpan.org/source/SMUELLER/PathTools-3.40/lib/File/Spec/Unix.pm#L474
110 # https://metacpan.org/source/SMUELLER/PathTools-3.40/lib/File/Spec/Win32.pm#L324
111 260 ▶ Cwd::realpath( File::Spec->rel2abs( $INC{$pkg_as_path}, $REALPATH_CWD ) )
Branch: 1 branch
BranchTrueFalseTotal
if Cwd::realpath("File::Spec"->rel2abs($INC{$pkg_as_path}, $REALPATH_CWD)) =~ m[ \A \Q$REALPATH_CWD\E [\\\/] ]x 4 256 260
112 =~ m| \A \Q$REALPATH_CWD\E [\\\/] |x
113 );
114  
115 256 my $ver = do {
116 256 local $@;
117 256 my $v = eval { $p->VERSION };
118 256 ▶ $@ ? '->VERSION call failed' : $v;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 256 256
119 };
120 256 ▶ $ver = 'undef' unless defined $ver;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ver 44 212 256
121 256 $results{$p} = $ver;
122 }
123  
124 2 my $diag = "Listing modules from %INC\n";
125  
126 2 my $ml = _max_length( keys %results );
127 2 my $vl = _max_length( values %results );
128  
129 2 for ( sort keys %results ) {
130 $diag .= sprintf(
131 " %*s %*s\n",
132 # pairs of [ padding-length => content ]
133 258 $vl => $results{$_},
134 -$ml => $_
135 );
136 }
137  
138 2 ▶ if ( $INC{"Test/Builder.pm"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Test/Builder.pm'}) { } 1 1 2
139 1 Test::Builder->new->diag($diag);
140 }
141 else {
142 1 $diag =~ s/^/# /mg;
143 1 print STDERR $diag;
144 }
145  
146 2 return 0;
147 }
148  
149 1 return 1;
150 }
151  
152 3 1;
153  
154  
155 # vim: ts=4 sts=4 sw=4 et:
156  
157 __END__
↑ Summary examples/lib/Foo.pm »