t/02-name-guard.t

filter: Statementstmt 92.5% 124 / 134 Branchbran 65.1% 43 / 66 Conditioncond 42.8% 9 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 76.7% 191 / 249 CC 27 SCAR 20.7
CC
27
Cov
80%
find_io_k8s
12.1
packlist_files
10.7
scan_file
10.3
CRAP 7.9
↑ Summary blib/lib/IO/K8s/Api/Admissionregistration/V1alpha1/Initializer.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Module::Metadata ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # THE CLAIM THIS TEST MAKES (stated up front, so that a future change has to
11 # say whether it keeps this claim or replaces it):
12 #
13 # "No package this distribution ships is shipped by the current IO-K8s."
14 #
15 # A tombstone is only right for a name IO-K8s has stopped shipping. Both
16 # distributions install into the same tree, so a name present in both is the
17 # same file path twice and whichever is installed LAST wins. Installing this
18 # dist over a current IO-K8s then replaces a working class with a stub that
19 # dies on load -- and that names a "replacement" for a class that is not gone.
20 #
21 # It can happen without anyone touching this repository: IO-K8s brings a name
22 # back. The 76-name "*List" sweep once included the two meta.v1 discovery
23 # Kinds APIGroupList and APIResourceList; IO-K8s 1.106 shipped both again as
24 # real classes, and from that release on the two tombstones were a trap.
25 # Nothing in a tombstone can notice that by itself, and t/01-tombstones.t
26 # stays green throughout, because the stub still dies exactly as asked. This
27 # guard is what makes it loud.
28 #
29 # Where "the current IO-K8s" is read from -- first hit wins, never the
30 # network:
31 #
32 # 1. $ENV{IO_K8S_LIB}, an explicit lib directory.
33 # 2. A checkout named "io-k8s-p5" next to this one. Looked for up to three
34 # levels up, so it is also found from inside a dzil build directory
35 # (<repo>/IO-K8s-Deprecated-X.Y, <repo>/.build/<id>) -- "dzil test" and
36 # "dzil release" are when this guard matters most.
37 # 3. An installed IO::K8s in @INC.
38 #
39 # With none of them there is nothing to compare against, and the test skips.
40 #
41 # An installed tree needs three precautions a checkout does not:
42 #
43 # * What IO-K8s ships is what its .packlist lists, never what happens to
44 # lie in the tree. An installer does not delete files a newer release no
45 # longer brings, so an upgraded site_perl keeps the classes of every older
46 # IO-K8s (InitializerConfigurationList and friends) although the current
47 # one does not ship them. Only the files listed in the .packlist of the
48 # installed IO-K8s (auto/IO/K8s/.packlist in @INC) count; an installed
49 # IO::K8s without a .packlist that lists its IO/K8s.pm has no reliable
50 # list of files, and the test skips.
51 #
52 # * It is shared with an installed copy of THIS distribution, whose stubs
53 # sit in the very same directories. They are not IO-K8s shipping the name
54 # -- and they are not in IO-K8s's .packlist, so they never enter the
55 # comparison. A stub that did overwrite a file IO-K8s ships still sits in
56 # that .packlist and is reported as the collision it is. Every tombstone
57 # must still die the way $TOMBSTONE_RE expects -- unconditionally, at the
58 # top level, "die __PACKAGE__ ..." at the start of a line -- which is
59 # asserted below.
60 #
61 # * It may be OLD. An IO-K8s from before the removals still ships the
62 # tombstoned names, by design -- that is the very release the takeover is
63 # aimed at, and it says nothing about the current IO-K8s. So an installed
64 # IO::K8s only counts when it is at least as new as this distribution,
65 # whose version is by policy above the last IO-K8s release that shipped
66 # any tombstoned name (VERSION POLICY in IO::K8s::Deprecated). An older
67 # one skips.
68 #
69 # A checkout gets neither: it IS the current IO-K8s, so a stale one fails
70 # loudly instead of being argued away.
71  
72 3 my $TOMBSTONE_RE = qr/^die __PACKAGE__\b/;
73 3 my $MAIN_MODULE = 'IO/K8s/Deprecated.pm';
74 3 my $CHECKOUT = 'io-k8s-p5';
75  
76 3 my $ROOT = abs_path(dirname(__FILE__).'/..');
77 3 my $LIB_DIR = $ROOT.'/lib';
78  
79 # What one module file declares in its CODE. POD and everything after
80 # __END__/__DATA__ are skipped: a "package My::App;" in a SYNOPSIS is an
81 # example, not a shipped name.
82 876 ▶sub scan_file {
Subroutine scan_file: called CC=9 SCAR=23.3
83 876 my ( $file ) = @_;
84 876 ▶ open my $fh, '<', $file or BAIL_OUT('cannot read '.$file.': '.$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or BAIL_OUT('cannot read ' . $file . ': ' . $!) 876 0 876
85 876 my ( @packages, $in_pod, $tombstone );
86 876 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 17079 3 17082
87 17079 ▶ last if $line =~ /^__(?:END|DATA)__\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^__(?:END|DATA)__\s*$/ 873 16206 17079
88 0 ▶ if ($line =~ /^=cut\b/) { $in_pod = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=cut\b/) 0 16206 16206
89 0 ▶ if ($line =~ /^=[a-zA-Z]/) { $in_pod = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=[a-zA-Z]/) 0 16206 16206
90 16206 ▶ next if $in_pod;
Branch: 1 branch
BranchTrueFalseTotal
if $in_pod 0 16206 16206
91 16206 ▶ $tombstone = 1 if $line =~ $TOMBSTONE_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$TOMBSTONE_RE/ 870 15336 16206
92 16206 ▶ push @packages, $1
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /(?:^|[;{}])\s*package\s+([A-Za-z_][\w:]*)\s*(?=[;{]|v?[0-9])/g and do { push @packages, $1 } 876 16206 17082
93 while $line =~ /(?:^|[;{}])\s*package\s+([A-Za-z_][\w:]*)\s*(?=[;{]|v?[0-9])/g;
94 }
95 876 close $fh;
96 876 return { packages => \@packages, tombstone => $tombstone };
97 }
98  
99 876 ▶sub scan_module {
Subroutine scan_module: called CC=1 SCAR=0.0
100 876 my ( $lib, $file ) = @_;
101 876 my $scan = scan_file($file);
102 876 $scan->{file} = $file;
103 876 $scan->{rel} = substr($file, length($lib) + 1);
104 876 return $scan;
105 }
106  
107 # Every *.pm below $lib.
108 3 ▶sub scan_tree {
Subroutine scan_tree: called CC=1 SCAR=0.0
109 3 my ( $lib ) = @_;
110 3 my @modules;
111 File::Find::find(
112 {
113 no_chdir => 1,
114 1086 ▶ wanted => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
115 1086 ▶ push @modules, scan_module($lib, $File::Find::name)
Branch: 1 branch
BranchTrueFalseTotal
if /\.pm\z/ and -f $_ 873 213 1086
Condition66%2/3
/\.pm\z/ and -f $_
!ll&&!rl&&r
2130873
MC/DC50%1/2
/\.pm\z/ and -f $_
/\.pm\z/ -f $_
Truth table66%2/3
A and B
A= /\.pm\z/B= -f $_
ABresult
0X0
100
111
116 if /\.pm\z/ && -f $_;
117 },
118 },
119 3 $lib,
120 );
121 3 return sort { $a->{rel} cmp $b->{rel} } @modules;
122 }
123  
124 # The files the installed IO-K8s lists in its .packlist, below $lib. The
125 # .packlist of a pure-Perl dist lives in the arch directory, so every @INC
126 # entry is searched; one counts only when it lists $lib/IO/K8s.pm itself.
127 # Returns an empty list when there is no such .packlist.
128 3 ▶sub packlist_files {
Subroutine packlist_files: called CC=10 SCAR=23.7
129 3 my ( $lib ) = @_;
130 3 my $root = abs_path($lib);
131 3 for my $inc ( grep { !ref } @INC ) {
132 14 my $packlist = $inc.'/auto/IO/K8s/.packlist';
133 14 ▶ next unless -f $packlist;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $packlist 12 2 14
134 2 ▶ open my $fh, '<', $packlist or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $packlist or next 2 0 2
135 2 my %listed;
136 2 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 2 5
137 3 chomp $line;
138 3 ▶ 1 while $line =~ s/ \w+=\S*\z//; # "path type=link from=..." entries
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/ \w+=\S*\z// and do { '???' } 0 3 3
139 3 ▶ next unless $line =~ /\.pm\z/ && -f $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\.pm\z/ and -f $line 0 3 3
Condition33%1/3
$line =~ /\.pm\z/ and -f $line
!ll&&!rl&&r
003
MC/DC0%0/2
$line =~ /\.pm\z/ and -f $line
$line =~ /\.pm\z/ -f $line
Truth table33%1/3
A and B
A= $line =~ /\.pm\z/B= -f $line
ABresult
0X0
100
111
140 3 my $path = abs_path($line);
141 3 ▶ $listed{$path} = 1 if defined $path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $path 3 0 3
142 }
143 2 close $fh;
144 2 ▶ next unless $listed{ $root.'/IO/K8s.pm' };
Branch: 1 branch
BranchTrueFalseTotal
unless $listed{$root . '/IO/K8s.pm'} 0 2 2
145 2 return grep { index($_, $root.'/') == 0 } sort keys %listed;
146 }
147 1 return;
148 }
149  
150 3 ▶sub io_k8s_version {
Subroutine io_k8s_version: called CC=1 SCAR=0.0
151 3 my ( $lib ) = @_;
152 3 return eval { Module::Metadata->new_from_file($lib.'/IO/K8s.pm')->version('IO::K8s') };
153 }
154  
155 3 ▶sub find_io_k8s {
Subroutine find_io_k8s: called CC=8 SCAR=24.9
156 3 ▶ if (defined $ENV{IO_K8S_LIB} && length $ENV{IO_K8S_LIB}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"IO_K8S_LIB"} and length $ENV{"IO_K8S_LIB"}) 0 3 3
Condition33%1/3
defined $ENV{"IO_K8S_LIB"} and length $ENV{"IO_K8S_LIB"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $ENV{"IO_K8S_LIB"} and length $ENV{"IO_K8S_LIB"}
defined $ENV{"IO_K8S_LIB"} length $ENV{"IO_K8S_LIB"}
Truth table33%1/3
A and B
A= defined $ENV{"IO_K8S_LIB"}B= length $ENV{"IO_K8S_LIB"}
ABresult
0X0
100
111
157 0 ( my $lib = $ENV{IO_K8S_LIB} ) =~ s{/+\z}{};
158 # Asked for explicitly: pointing nowhere must not end in a green skip.
159 0 ▶ BAIL_OUT('IO_K8S_LIB='.$ENV{IO_K8S_LIB}.' holds no IO/K8s.pm')
Branch: 1 branch
BranchTrueFalseTotal
unless -e $lib . '/IO/K8s.pm' 0 0 0
160 unless -e $lib.'/IO/K8s.pm';
161 0 return ( 'IO_K8S_LIB', $lib );
162 }
163 3 my $dir = $ROOT;
164 3 for ( 1 .. 3 ) {
165 9 $dir = dirname($dir);
166 9 my $lib = $dir.'/'.$CHECKOUT.'/lib';
167 9 ▶ return ( 'checkout', $lib ) if -e $lib.'/IO/K8s.pm';
Branch: 1 branch
BranchTrueFalseTotal
if -e $lib . '/IO/K8s.pm' 0 9 9
168 }
169 3 for my $inc ( grep { !ref } @INC ) {
170 6 ( my $lib = $inc ) =~ s{/+\z}{};
171 6 ▶ return ( 'installed', $lib ) if -e $lib.'/IO/K8s.pm';
Branch: 1 branch
BranchTrueFalseTotal
if -e $lib . '/IO/K8s.pm' 3 3 6
172 }
173 0 return;
174 }
175  
176 # A guard that quietly checks nothing is worse than no guard: neither a
177 # missing lib/ nor an empty scan may look like success.
178 3 ▶BAIL_OUT('no lib directory at '.$LIB_DIR) unless -d $LIB_DIR;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $LIB_DIR 0 3 3
179  
180 3 my @own = scan_tree($LIB_DIR);
181 BAIL_OUT('found no packages under '.$LIB_DIR.' -- guard is inoperative')
182 3 ▶ unless grep { @{ $_->{packages} } } @own;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {@{$_->{"packages"};};} @own 0 3 3
183  
184 3 my ( $source, $main_lib ) = find_io_k8s();
185  
186 3 ▶plan skip_all => 'IO-K8s not available to compare against: no '.$CHECKOUT
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main_lib 0 3 3
187 .' checkout next to this one, no IO::K8s installed, IO_K8S_LIB not set'
188 unless defined $main_lib;
189  
190 3 my $main_version = io_k8s_version($main_lib);
191 3 my $installed = $source eq 'installed';
192  
193 3 ▶if ($installed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed) 3 0 3
194 3 my $own_version = eval {
195 3 Module::Metadata->new_from_file($LIB_DIR.'/'.$MAIN_MODULE)
196 ->version('IO::K8s::Deprecated');
197 };
198 3 ▶ plan skip_all => 'cannot read a version from the installed IO::K8s in '.$main_lib
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main_version and defined $own_version 0 3 3
Condition33%1/3
defined $main_version and defined $own_version
!ll&&!rl&&r
003
MC/DC0%0/2
defined $main_version and defined $own_version
defined $main_version defined $own_version
Truth table33%1/3
A and B
A= defined $main_versionB= defined $own_version
ABresult
0X0
100
111
199 unless defined $main_version && defined $own_version;
200 3 ▶ plan skip_all => 'installed IO::K8s '.$main_version.' is older than this'
Branch: 1 branch
BranchTrueFalseTotal
if $main_version < $own_version 0 3 3
201 .' distribution ('.$own_version.'): it predates the removals and says'
202 .' nothing about the current IO-K8s'
203 if $main_version < $own_version;
204 }
205  
206 3 my @main;
207 3 ▶if ($installed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed) { } 3 0 3
208 3 my @listed = packlist_files($main_lib);
209 3 ▶ plan skip_all => 'the installed IO::K8s in '.$main_lib.' has no .packlist'
Branch: 1 branch
BranchTrueFalseTotal
unless @listed 1 2 3
210 .' listing its files (auto/IO/K8s/.packlist): files lying in the tree are'
211 .' not evidence that IO-K8s ships them'
212 unless @listed;
213 1 @main = sort { $a->{rel} cmp $b->{rel} }
214 2 map { scan_module(abs_path($main_lib), $_) } @listed;
215 }
216 else {
217 0 @main = scan_tree($main_lib);
218 }
219  
220 2 my ( %main_package, %main_path );
221 2 for my $module (@main) {
222 3 $main_path{ $module->{rel} } = $module->{file};
223 3 ▶ $main_package{$_} //= $module->{file} for @{ $module->{packages} };
Condition33%1/3
$main_package{$_} //= $module->{"file"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$main_package{$_} //= $module->{"file"}
$main_package{$_} $module->{"file"}
Truth table33%1/3
A //= B
A= $main_package{$_}B= $module->{"file"}
ABresult
1X1
011
000
224 }
225  
226 BAIL_OUT('scan of '.$main_lib.' did not find package IO::K8s -- guard is inoperative')
227 2 ▶ unless $main_package{'IO::K8s'};
Branch: 1 branch
BranchTrueFalseTotal
unless $main_package{"IO::K8s"} 0 2 2
228  
229 2 ▶my $main_label = 'IO-K8s '.( defined $main_version ? $main_version : '(no version)' )
Branch: 1 branch
BranchTrueFalseTotal
defined $main_version ? : 2 0 2
230 .' ('.$source.': '.$main_lib.')';
231 2 note('comparing '.scalar(@own).' modules against '.$main_label.', '
232 .scalar(keys %main_package).' packages');
233  
234 0 my @unmarked = map { $_->{rel} }
235 2 ▶ grep { !$_->{tombstone} && $_->{rel} ne $MAIN_MODULE } @own;
Condition66%2/3
!$_->{'tombstone'} && $_->{'rel'} ne $MAIN_MODULE
!ll&&!rl&&r
58020
MC/DC0%0/2
!$_->{'tombstone'} && $_->{'rel'} ne $MAIN_MODULE
!$_->{'tombstone'} $_->{'rel'} ne $MAIN_MODULE
Truth table66%2/3
not(A) && B
A= !$_->{'tombstone'}B= $_->{'rel'} ne $MAIN_MODULE
ABresult
0X0
100
111
236 2 ▶is_deeply(\@unmarked, [],
Branch: 1 branch
BranchTrueFalseTotal
is_deeply(\@unmarked, scalar [], "every tombstone has the die line an installed copy is recognised by") or diag("expected a line starting with: die __PACKAGE__") 2 0 2
237 'every tombstone has the die line an installed copy is recognised by')
238 or diag('expected a line starting with: die __PACKAGE__');
239  
240 2 for my $module (@own) {
241 582 my %hit;
242 582 ▶ $hit{ $main_path{ $module->{rel} } } = 1 if $main_path{ $module->{rel} };
Branch: 1 branch
BranchTrueFalseTotal
if $main_path{$module->{"rel"}} 1 581 582
243 $hit{ $main_package{$_} } = 1
244 582 for grep { $main_package{$_} } @{ $module->{packages} };
245  
246 582 ▶ my $name = join(', ', @{ $module->{packages} }) || $module->{rel};
Condition33%1/3
join(', ', @{$module->{'packages'};}) || $module->{'rel'}
l!l&&r!l&&!r
58200
MC/DC0%0/2
join(', ', @{$module->{'packages'};}) || $module->{'rel'}
join(', ', @{$module->{'packages'};}) $module->{'rel'}
Truth table33%1/3
A || B
A= join(', ', @{$module->{'packages'};})B= $module->{'rel'}
ABresult
1X1
011
000
247 ok(!%hit, $name.' is not shipped by IO-K8s')
248 or diag($name." is shipped by BOTH distributions.\n"
249 .' here: lib/'.$module->{rel}."\n"
250 582 ▶ .join('', map { ' IO-K8s: '.$_."\n" } sort keys %hit)
Branch: 1 branch
BranchTrueFalseTotal
&ok(!%hit, $name . ' is not shipped by IO-K8s') or diag($name . " is shipped by BOTH distributions.\n" . " here: lib/" . $module->{"rel"} . "\n" . join("", map({' IO-K8s: ' . $_ . "\n";} sort(keys %hit))) . ' against: ' . $main_label . "\n" . "Whichever is installed last overwrites the other. A name IO-K8s ships\n" . "is not an orphan: remove the tombstone here (the file, its line in\n" . "IO::K8s::Deprecated's POD and in t/01-tombstones.t).") 581 1 582
251 .' against: '.$main_label."\n"
252 ."Whichever is installed last overwrites the other. A name IO-K8s ships\n"
253 ."is not an orphan: remove the tombstone here (the file, its line in\n"
254 ."IO::K8s::Deprecated's POD and in t/01-tombstones.t).");
255 }
256  
257 2 done_testing;
↑ Summary blib/lib/IO/K8s/Api/Admissionregistration/V1alpha1/Initializer.pm »