blib/lib/Module/Mask/Deps.pm

filter: Statementstmt 97.3% 108 / 111 Branchbran 91.1% 31 / 34 Conditioncond 95.6% 22 / 23 MC/DCmcdc 84.6% 11 / 13 Subroutinesub 100% 22 / 22 Podpod 100% 4 / 4 totaltotal 95.6% 198 / 207 CC 28 SCAR 13.5
CC
28
Cov
96%
_clean_version
8.0
INC
6.0
get_deps
5.3
CRAP 3.8
↑ Summary
1 package Module::Mask::Deps;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.07';
7  
8 =head1 NAME
9  
10 Module::Mask::Deps - Mask modules not listed as dependencies
11  
12 =head1 SYNOPSIS
13  
14 Cause your test suite to blow up if you require a module not listed as a
15 requirement in C<META.yml> or C<META.json>.
16  
17 perl Build.PL
18 HARNESS_PERL_SWITCHES=-MModule::Mask::Deps ./Build test
19  
20 Or use directly in your testing code:
21  
22 use Module::Mask::Deps;
23  
24 BEGIN {
25 # die if an unlisted module is used
26 use_ok('My::Module');
27 }
28  
29 # turn off masking (at compile time)
30 no Module::Mask::Deps;
31  
32 # .. or at run-time
33 unimport Module::Mask::Deps;
34  
35 Or use lexically:
36  
37 require Module::Mask::Deps;
38  
39 {
40 my $mask = new Module::Mask::Deps;
41  
42 # Non-dependencies masked until end-of-scope.
43  
44 # ...
45 }
46  
47 require Arbitrary::Module;
48  
49 =head1 DESCRIPTION
50  
51 This module aims to help module developers keep track of their dependencies by
52 only allowing modules to be loaded if they are in core or are listed as
53 dependencies.
54  
55 It uses L<Module::CoreList> and L<Parse::CPAN::Meta> to build its list of
56 declared dependant modules.
57  
58 =cut
59  
60 3 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 3 ▶use Module::Mask;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶use Parse::CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 # Need to load these early in case we mask them later
65 3 ▶use CPAN::Meta::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 3 ▶use File::Spec::Functions qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
69 file_name_is_absolute
70 updir
71 splitdir
72 abs2rel
73 3 );
74  
75 3 our @ISA = qw( Module::Mask::Inverted );
76  
77 =head1 METHODS
78  
79 =head2 import
80  
81 use Module::Mask::Deps;
82  
83 import Module::Mask::Deps;
84  
85 Causes a L<Module::Mask::Deps> object to be created as
86 C<$Module::Mask::Deps::Mask>. This means that when called with C<use> the mask
87 object is is in scope at compile time and therefore should affect all
88 subsequent C<use> and C<require> statements in the program.
89  
90 =cut
91  
92 3 our $Mask;
93  
94 5 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
95 5 my $class = shift;
96  
97 5 ▶ if ($Mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Module::Mask::Deps::Mask) { } 1 4 5
98 1 $Mask->set_mask;
99 }
100 else {
101 4 $Mask = $class->new;
102 }
103 }
104  
105 =head2 unimport
106  
107 unimport Module::Mask::Deps
108  
109 no Module::Mask::Deps;
110  
111 Stops the mask from working until import is called again. See clear_mask in
112 L<Module::Mask>
113  
114 Note that C<no Module::Mask::Deps> occurs at compile time and is not lexical
115 in the same way as C<no strict> and <no warnings> are.
116  
117 =cut
118  
119 2 ▶sub unimport {
Subroutine unimport: called CC=2 SCAR=6.9
120 2 ▶ $Mask->clear_mask if $Mask;
Branch: 1 branch
BranchTrueFalseTotal
if $Module::Mask::Deps::Mask 1 1 2
121 }
122  
123 =head2 new
124  
125 $obj = $class->new()
126  
127 Returns a new C<Module::Mask::Deps> object. See L<Module::Mask> for details
128 about how modules are masked.
129  
130 =cut
131  
132 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
133 4 my $class = shift;
134  
135 4 my @deps = $class->get_deps;
136  
137 4 return $class->SUPER::new(@deps);
138 }
139  
140 =head2 set_mask
141  
142 $obj = $obj->set_mask()
143  
144 Overloaded from L<Module::Mask> to place the mask object after any relative
145 paths at the beginning of @INC.
146  
147 Typically, in a testing environment, local paths are unshifted into @INC by
148 C<blib.pm>, C<lib.pm> or command-line switches. We don't want the mask to
149 affect those paths.
150  
151 Also, relative paths passed to require will not be masked.
152  
153 # Will check @INC but won't be masked
154 require 't/my_script.pl';
155  
156 # Won't even check @INC
157 require './t/my_script.pl';
158  
159 =cut
160  
161 5 ▶sub set_mask {
Subroutine set_mask: called CC=3 SCAR=11.0
Pod documented
162 5 my $self = shift;
163  
164 5 $self->SUPER::set_mask();
165 # now the mask should be at the start of @INC
166  
167 # This is less code, but it's not as clear.
168 # Might even be less efficient.
169 # for (my $i = 1; $self->_rel_path($INC[$i]); $i++) {
170 # unshift @INC, splice @INC, $i, 1;
171 # }
172  
173 # count how many relative paths follow the mask object
174 5 my $count = 0;
175 5 for my $entry (@INC[1 .. $#INC]) {
176 19 ▶ if ($self->_rel_path($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_rel_path($entry)) { } 14 5 19
177 14 $count++;
178 }
179 else {
180 5 last;
181 }
182 }
183  
184 # move relative entries in front of the mask object
185 5 unshift @INC, splice @INC, 1, $count;
186  
187 5 return $self;
188 }
189  
190 19 ▶sub _rel_path {
Subroutine _rel_path: called CC=2 SCAR=6.9
191 19 my ($self, $entry) = @_;
192
193 19 ▶ return !file_name_is_absolute($entry)
Condition100%3/3
!file_name_is_absolute($entry) || (splitdir(abs2rel($entry)))[0] ne '..'
l!l&&r!l&&!r
955
MC/DC100%2/2
!file_name_is_absolute($entry) || (splitdir(abs2rel($entry)))[0] ne '..'
!file_name_is_absolute($entry) (splitdir(abs2rel($entry)))[0] ne '..'
Truth table100%3/3
not(A) || B
A= !file_name_is_absolute($entry)B= (splitdir(abs2rel($entry)))[0] ne '..'
ABresult
1X1
011
000
194 || (splitdir(abs2rel($entry)))[0] ne updir;
195 }
196  
197 # prevent sub-dependencies from being masked
198 11 ▶sub Module::Mask::Deps::INC {
Subroutine INC: called CC=6 SCAR=18.0
Pod documented
199 11 my ($self, $module) = @_;
200  
201 11 ▶ if ($self->is_masked($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_masked($module)) 8 3 11
202 8 my ($call_pack, $call_file) = caller;
203  
204 8 ▶ if ($self->_is_listed($call_pack)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_listed($call_pack)) { } 4 4 8
elsif (-f $module) { } 0 4 4
205 # we've explicitly whitelisted the calling package,
206 # don't mask its dependencies
207  
208 # also add this module to the whitelist
209 4 $self->mask_modules($module);
210  
211 4 return;
212 }
213 elsif (-f $module) {
214 # $module must be a local, relative path.
215 # Absolute paths don't check @INC
216 # It will be loaded as long as . is in @INC
217 0 return;
218 }
219 else {
220 # Maybe we're being called from a package defined inside a module
221 # file
222 4 my %inc_lookup = reverse %INC;
223  
224 # This won't work unless the module is loaded from the filesystem
225 4 my $call_mod = $inc_lookup{$call_file};
226  
227 4 ▶ if ($call_mod and $self->_is_listed($call_mod)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_mod and $self->_is_listed($call_mod)) 2 2 4
Condition100%3/3
$call_mod and $self->_is_listed($call_mod)
!ll&&!rl&&r
112
MC/DC100%2/2
$call_mod and $self->_is_listed($call_mod)
$call_mod $self->_is_listed($call_mod)
Truth table100%3/3
A and B
A= $call_modB= $self->_is_listed($call_mod)
ABresult
0X0
100
111
228 # Add the sub-package to the whitelist so we don't need to
229 # re-check next time
230 2 $self->mask_modules($call_pack, $module);
231  
232 2 return;
233 }
234 }
235 }
236  
237 5 return $self->SUPER::INC($module);
238 }
239  
240 =head2 get_deps
241  
242 @deps = $class->get_deps()
243  
244 Returns current dependencies as defined in either C<META.json> or C<META.yml>,
245 checked in that order. This is used internally by import and new, so there's
246 no need to call it directly.
247  
248 It returns all explicitly defined dependencies, plus all core dependencies for
249 the appropriate version of Perl, i.e. either the minimum version specified in
250 the meta file or the currently running version.
251  
252 The sections of the metadata checked for dependencies are:
253  
254 =over
255  
256 =item requires
257  
258 =item test_requires
259  
260 =item build_requires
261  
262 =back
263  
264 =cut
265  
266 6 ▶sub get_deps {
Subroutine get_deps: called CC=5 SCAR=16.6
Pod documented
267 6 my $class = shift;
268  
269 6 my @errors;
270  
271 6 for my $file ($class->_meta_files) {
272 # Ignore errors reading the file
273 9 ▶ if (my $meta = eval { Parse::CPAN::Meta->load_file($file) }) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $meta = eval { do { 'Parse::CPAN::Meta'->load_file($file) } }) { } 6 3 9
elsif ($@) { } 3 0 3
274 6 my %deps = $class->_meta_reqs($meta);
275  
276 6 ▶ my $perl_version = delete $deps{perl} || $];
Condition66%2/3
delete $deps{'perl'} || $]
l!l&&r!l&&!r
420
MC/DC0%0/2
delete $deps{'perl'} || $]
delete $deps{'perl'} $]
Truth table66%2/3
A || B
A= delete $deps{'perl'}B= $]
ABresult
1X1
011
000
277  
278 6 return $class->_merge_core($perl_version, keys %deps);
279 }
280 elsif ($@) {
281 3 push @errors, $@;
282 }
283 }
284  
285 0 local $" = "\n";
286 0 die "$class: Couldn't find dependencies\n@errors\n";
287 }
288  
289 6 ▶sub _meta_files {
Subroutine _meta_files: called CC=1 SCAR=0.0
290 6 return qw( META.json META.yml );
291 }
292  
293 # Extract requirements from metadata.
294 # There are two formats for this, try all combinations and return as a hash
295 6 ▶sub _meta_reqs {
Subroutine _meta_reqs: called CC=2 SCAR=6.9
296 6 my ($class, $meta) = @_;
297  
298 6 my @top_keys = grep /(?:_|^)requires$/, keys %$meta;
299  
300 return (
301 ( map %$_, @$meta{ @top_keys } ),
302 6 ▶ ( map %{ $_->{requires} }, values %{ $meta->{prereqs} || {} } ),
Condition100%2/2
$meta->{'prereqs'} || {}
l!l
33
MC/DC100%1/1
$meta->{'prereqs'} || {}
$meta->{'prereqs'}
Truth table100%2/2
A
A= $meta->{'prereqs'}
Aresult
11
00
303 );
304 }
305  
306 # convenience function to return unique deps for a given perl version and
307 # dependency list
308 6 ▶sub _merge_core {
Subroutine _merge_core: called CC=1 SCAR=0.0
309 6 my ($class, $version, @deps) = @_;
310 6 my @core = $class->_get_core($version);
311 6 my %seen;
312  
313 6 return grep { !$seen{$_}++ } (@core, @deps);
314 }
315  
316 # Find core modules for the given perl version
317 390 ▶sub _get_core {
Subroutine _get_core: called CC=4 SCAR=13.9
318 390 my $class = shift;
319 390 my $perl_version = shift;
320 390 my @core;
321  
322 390 @core = $class->_core_for_version($perl_version);
323  
324 390 ▶ return @core if @core;
Branch: 1 branch
BranchTrueFalseTotal
if @core 347 43 390
325  
326 # Nothing found,
327 # Maybe $perl_version needs reformatting..
328  
329 43 ▶ if (my $clean = $class->_clean_version($perl_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $clean = $class->_clean_version($perl_version)) 42 1 43
330 42 @core = $class->_core_for_version($clean);
331 }
332  
333 43 ▶ return @core if @core;
Branch: 1 branch
BranchTrueFalseTotal
if @core 38 5 43
334  
335 # still nothing..
336  
337 5 die "$class: Couldn't find core modules for perl $perl_version\n";
338 }
339  
340 # wrap %Module::CoreList::version
341 432 ▶sub _core_for_version {
Subroutine _core_for_version: called CC=3 SCAR=11.0
342 432 my $class = shift;
343 432 my $perl_version = shift;
344  
345 3 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
346 432 ▶ confess "undef perl version" unless defined $perl_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl_version 0 432 432
347  
348 432 ▶ if (exists $Module::CoreList::version{$perl_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Module::CoreList::version{$perl_version}) { } 385 47 432
349 385 return keys %{$Module::CoreList::version{$perl_version}};
350 }
351 else {
352 47 return;
353 }
354 }
355  
356 # try to transform perl version numbers into the type used in Module::CoreList
357 43 ▶sub _clean_version {
Subroutine _clean_version: called CC=8 SCAR=20.8
358 43 my ($class, $version) = @_;
359 43 $version =~ tr/0-9._//dc;
360 43 my ($major, @minors) = split(/[._]/, $version);
361  
362 # we don't want trailing zeros
363 43 ▶ pop @minors while @minors && $minors[-1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
@minors and $minors[-1] == 0 and do { pop @minors } 13 43 56
Condition100%3/3
@minors and $minors[-1] == 0
!ll&&!rl&&r
53813
MC/DC100%2/2
@minors and $minors[-1] == 0
@minors $minors[-1] == 0
Truth table100%3/3
A and B
A= @minorsB= $minors[-1] == 0
ABresult
0X0
100
111
364  
365 43 @minors = map { sprintf('%03d', $_) } @minors;
366  
367 43 ▶ if (@minors > 1 && $major == 5 && $minors[0] >= 3 && $minors[0] < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@minors > 1 and $major == 5 and $minors[0] >= 3 and $minors[0] < 6) 9 34 43
Condition100%9/9
@minors > 1 and $major == 5 and $minors[0] >= 3 and $minors[0] < 6
!ll&&!rl&&r
22129
@minors > 1 and $major == 5 and $minors[0] >= 3
!ll&&!rl&&r
21121
@minors > 1 and $major == 5
!ll&&!rl&&r
20122
MC/DC100%4/4
@minors > 1 and $major == 5 and $minors[0] >= 3 and $minors[0] < 6
@minors > 1 $major == 5 $minors[0] >= 3 $minors[0] < 6
Truth table100%5/5
A and B and C and D
A= @minors > 1B= $major == 5C= $minors[0] >= 3D= $minors[0] < 6
ABCDresult
0XXX0
10XX0
110X0
11100
11111
368 # Between 5.3 and 5.6, the second minor version is 2 digits
369 # 5.3.7 => 5.00307
370 # 5.5.3 => 5.00503
371  
372 9 $minors[1] =~ s/^0//;
373 }
374  
375 43 local $" = '';
376  
377 43 ▶ return @minors ? "$major.@minors" : $major;
Branch: 1 branch
BranchTrueFalseTotal
@minors ? : 38 5 43
378 }
379  
380 3 1;
381  
382 __END__
↑ Summary