blib/lib/Test/Prereq.pm

filter: Statementstmt 87.9% 146 / 166 Branchbran 67.5% 27 / 40 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 96.7% 30 / 31 Podpod 100% 1 / 1 totaltotal 83.1% 207 / 249 CC 30 SCAR 21.9
CC
30
Cov
83%
_prereq_check
31.7
_get_loaded_modules
5.0
_get_dist_modules
3.0
CRAP 9.0
↑ Summary blib/lib/Test/Prereq/Build.pm »
1 package Test::Prereq;
2 8 ▶use parent qw(Test::Builder::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use v5.22;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use feature qw(postderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶no warnings qw(experimental::postderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Test::Prereq - check if Makefile.PL has the right pre-requisites
19  
20 =head1 SYNOPSIS
21  
22 # if you use Makefile.PL
23 use Test::More;
24 eval "use Test::Prereq";
25 plan skip_all => "Test::Prereq required to test dependencies" if $@;
26 prereq_ok();
27  
28 # if you use Module::Build
29 use Test::More;
30 eval "use Test::Prereq::Build";
31 plan skip_all => "Test::Prereq::Build required to test dependencies" if $@;
32 prereq_ok();
33  
34 # or from the command line for a one-off check
35 perl -MTest::Prereq -eprereq_ok
36  
37 #The prerequisites test take quite some time so the following construct is
38 #recommended for non-author testers
39 use Test::More;
40 eval "use Test::Prereq::Build";
41  
42 my $msg;
43 if ($@) {
44 $msg = 'Test::Prereq::Build required to test dependencies';
45 } elsif (not $ENV{TEST_AUTHOR}) {
46 $msg = 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.';
47 }
48 plan skip_all => $msg if $msg;
49 prereq_ok();
50  
51 =head1 DESCRIPTION
52  
53 The C<prereq_ok()> function examines the modules it finds in
54 F<blib/lib/>, F<blib/script>, and the test files it finds in F<t/>
55 (and F<test.pl>). It figures out which modules they use and compares
56 that list of modules to those in the C<PREREQ_PM> section of
57 F<Makefile.PL>.
58  
59 If you use C<Module::Build> instead, see L<Test::Prereq::Build>
60 instead.
61  
62 =head2 Warning about redefining ExtUtils::MakeMaker::WriteMakefile
63  
64 C<Test::Prereq> has its own version of
65 C<ExtUtils::MakeMaker::WriteMakefile> so it can run the F<Makefile.PL>
66 and get the argument list of that function. You may see warnings
67 about this.
68  
69 =cut
70  
71 8 ▶use vars qw($VERSION $EXCLUDE_CPANPLUS @EXPORT @prereqs);
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73  
74 8 $VERSION = '2.005';
75  
76 8 @EXPORT = qw( prereq_ok );
77  
78 8 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 8 ▶use ExtUtils::MakeMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 8 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 8 ▶use Module::Extract::Use;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 8 my $Test = __PACKAGE__->builder;
84  
85 {
86 8 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 4 ▶* ExtUtils::MakeMaker::WriteMakefile = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
89 4 my %hash = @_;
90  
91 4 my $name = $hash{NAME};
92 my %prereqs =
93 16 ▶ map { defined $_ ? %$_ : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 12 4 16
94 4 @hash{qw(PREREQ_PM BUILD_REQUIRES CONFIGURE_REQUIRES TEST_REQUIRES)};
95  
96 4 @Test::Prereq::prereqs = sort keys %prereqs;
97  
98 4 1;
99 }
100 8 }
101  
102 #unless( caller ) { prereq_ok() }
103  
104 =head1 FUNCTIONS
105  
106 =over 4
107  
108 =item prereq_ok( [ NAME [, SKIP_ARRAY] ] )
109  
110 Tests F<Makefile.PL> to ensure all non-core module dependencies are in
111 C<PREREQ_PM>. If you haven't set a testing plan already,
112 C<prereq_ok()> creates a plan of one test.
113  
114 Valid versions come from C<Module::CoreList> (which uses C<$]>).
115  
116 #!/usr/bin/perl
117 use Module::CoreList;
118 print map "$_\n", sort keys %Module::CoreList::version;
119  
120 C<prereq_ok> attempts to remove modules found in F<lib/> and libraries
121 found in F<t/> from the reported prerequisites.
122  
123 The optional third argument is an array reference to a list of names
124 that C<prereq_ok> should ignore. You might want to use this if your
125 tests do funny things with C<require>.
126  
127 Versions prior to 1.038 would use CPAN.pm to virtually include
128 prerequisites in distributions that you declared explicitly. This
129 isn't really a good idea. Some modules have moved to different
130 distributions, so you should just specify all the modules that you use
131 instead of relying on a particular distribution to provide them. Not
132 only that, expanding distributions with CPAN.pm takes forever.
133  
134 If you want the old behavior, set the C<TEST_PREREQ_EXPAND_WITH_CPAN>
135 environment variable to a true value.
136  
137 =cut
138  
139 8 my $default_version = $];
140 8 my $version = $];
141  
142 2 ▶sub prereq_ok {
Subroutine prereq_ok: called CC=2 SCAR=6.9
Pod documented
143 2 ▶ $Test->plan( tests => 1 ) unless $Test->has_plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test->has_plan 1 1 2
144 2 __PACKAGE__->_prereq_check( @_ );
145 }
146  
147 8 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
148 8 my $self = shift;
149 8 my $caller = caller;
150 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 8 *{$caller.'::prereq_ok'} = \&prereq_ok;
152  
153 8 $Test->exported_to($caller);
154 8 $Test->plan(@_);
155 }
156  
157 2 ▶sub _prereq_check {
Subroutine _prereq_check: called CC=14 SCAR=34.6
158 2 my $class = shift;
159  
160 2 ▶ my $name = shift // 'Prereq test';
Condition50%1/2
shift() // "Prereq test"
l!l
02
MC/DC0%0/1
shift() // "Prereq test"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
161 2 ▶ my $skip = shift // [];
Condition50%1/2
shift() // []
l!l
02
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
162  
163 2 ▶ unless( ref $skip eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $skip eq ref []) 0 2 2
164 0 carp( 'The second parameter to prereq_ok must be an array reference!' );
165 0 return;
166 }
167  
168 # get the declared prereqs from the Makefile.PL
169 2 my $prereqs = $class->_get_prereqs();
170 2 ▶ unless( $prereqs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prereqs) 0 2 2
171 0 $class->_not_ok( "\t" .
172 $class->_master_file . " did not return a true value.\n" );
173 0 return 0;
174 }
175  
176 2 my $loaded = $class->_get_loaded_modules();
177  
178 2 ▶ unless( $loaded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 0 2 2
179 0 $class->_not_ok( "\tCouldn't look up the modules for some reasons.\n" ,
180 "\tDo the blib/lib and t directories exist?\n",
181 );
182 0 return 0;
183 }
184  
185 # remove modules found in PREREQ_PM
186 2 foreach my $module ( @$prereqs ) {
187 34 delete $loaded->{$module};
188 }
189  
190 # remove modules found in distribution
191 2 my $distro = $class->_get_dist_modules( 'blib/lib' );
192 2 foreach my $module ( $distro->@* ) {
193 4 delete $loaded->{$module};
194 }
195  
196 # remove modules found in test directory
197 2 $distro = $class->_get_test_libraries();
198 2 foreach my $module ( $distro->@* ) {
199 0 delete $loaded->{$module};
200 }
201  
202 # remove modules in the skip array
203 2 foreach my $module ( $skip->@* ) {
204 0 delete $loaded->{$module};
205 }
206  
207 2 ▶ if( $EXCLUDE_CPANPLUS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::Prereq::EXCLUDE_CPANPLUS) 0 2 2
208 0 foreach my $module ( keys %$loaded ) {
209 0 ▶ next unless $module =~ m/^CPANPLUS::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /^CPANPLUS::/u 0 0 0
210 0 delete $loaded->{$module};
211 }
212 }
213  
214 2 ▶ if( keys %$loaded ) { # stuff left in %loaded, oops!
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$loaded) { } 0 2 2
215 $class->_not_ok( "Found some modules that didn't show up in PREREQ_PM or *_REQUIRES\n",
216 0 map { "\t$_\n" } sort keys %$loaded );
217 }
218 else {
219 2 $Test->ok( 1, $name );
220 }
221  
222 2 return 1;
223 }
224  
225 0 ▶sub _not_ok {
Subroutine _not_ok: not called CC=1 SCAR=6.9
226 0 my( $self, $name, @message ) = @_;
227  
228 0 $Test->ok( 0, $name );
229 0 $Test->diag( join "", @message );
230 }
231  
232 4 ▶sub _master_file { 'Makefile.PL' }
Subroutine _master_file: called CC=1 SCAR=0.0
233  
234 5 ▶sub _get_prereqs {
Subroutine _get_prereqs: called CC=2 SCAR=6.9
235 5 my $class = shift;
236 5 my $file = $class->_master_file;
237  
238 5 delete $INC{$file}; # make sure we load it again
239  
240 {
241 5 local $^W = 0;
242  
243 5 ▶ unless( do "./$file" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do "./$file") 1 4 5
244 1 print STDERR "_get_prereqs: Error loading $file: $@\n";
245 1 return;
246 }
247 4 delete $INC{$file}; # pretend we were never here
248 }
249  
250 4 my @modules = sort @Test::Prereq::prereqs;
251 4 @Test::Prereq::prereqs = ();
252 4 return \@modules;
253 }
254  
255 # get all the loaded modules. we'll filter this later
256 3 ▶sub _get_loaded_modules {
Subroutine _get_loaded_modules: called CC=5 SCAR=16.2
257 3 my $class = shift;
258  
259 # return unless( defined $_[0] and defined $_[1] );
260 # return unless( -d $_[0] and -d $_[1] );
261  
262 3 my( @libs, @t, @scripts );
263  
264 3 ▶ File::Find::find( sub { push @libs, $File::Find::name if m/\.pm$/ }, 'blib/lib' )
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if -e "blib/lib" 3 0 3
if /\.pm$/u 6 24 30
265 if -e 'blib/lib';
266 3 ▶ File::Find::find( sub { push @t, $File::Find::name if m/\.t$/ }, 't' )
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if -e "t" 3 0 3
if /\.t$/u 27 6 33
267 if -e 't';
268 3 ▶ File::Find::find( sub { push @scripts, $File::Find::name if -f $_ }, 'blib/script' )
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if -e "blib/script" 3 0 3
if -f $_ 3 3 6
269 if -e 'blib/script';
270  
271 3 my @found = ();
272 3 foreach my $file ( @libs, @t, @scripts ) {
273 36 push @found, @{ $class->_get_from_file( $file ) };
274 }
275  
276 3 return { map { $_, 1 } @found };
277 }
278  
279 2 ▶sub _get_test_libraries {
Subroutine _get_test_libraries: called CC=2 SCAR=7.9
280 2 my $class = shift;
281  
282 2 my $dirsep = "/";
283  
284 2 my @found = ();
285  
286 2 ▶ File::Find::find( sub { push @found, $File::Find::name if m/\.p(l|m)$/ }, 't' );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if /\.p(l|m)$/u 0 22 22
287  
288 my @files =
289 map {
290 0 my $x = $_;
291 0 $x =~ s/^.*$dirsep//;
292 0 $x =~ s|$dirsep|::|g;
293 0 $x;
294 }
295 @found;
296  
297 2 ▶ push @files, 'test.pl' if -e 'test.pl';
Branch: 1 branch
BranchTrueFalseTotal
if -e "test.pl" 0 2 2
298  
299 2 return \@files;
300 }
301  
302 2 ▶sub _get_dist_modules {
Subroutine _get_dist_modules: called CC=3 SCAR=11.1
303 2 my $class = shift;
304  
305 2 ▶ return unless( defined $_[0] and -d $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and -d $_[0] 0 2 2
Condition33%1/3
defined $_[0] and -d $_[0]
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_[0] and -d $_[0]
defined $_[0] -d $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= -d $_[0]
ABresult
0X0
100
111
306  
307 2 my $dirsep = "/";
308  
309 2 my @found = ();
310  
311 2 ▶ File::Find::find( sub { push @found, $File::Find::name if m/\.pm$/ }, $_[0] );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if /\.pm$/u 4 16 20
312  
313 my @files =
314 map {
315 2 my $x = $_;
316 4 $x =~ s/^$_[0]($dirsep)?//;
317 4 $x =~ s/\.pm$//;
318 4 $x =~ s|$dirsep|::|g;
319 4 $x;
320 }
321 @found;
322  
323 2 return \@files;
324 }
325  
326 38 ▶sub _get_from_file {
Subroutine _get_from_file: called CC=2 SCAR=6.9
327 38 state $extor = Module::Extract::Use->new;
328 38 my( $class, $file ) = @_;
329  
330 38 my $modules = $extor->get_modules_with_details( $file );
331  
332 # We also depend on the super classes, which might not be
333 # part of the distro
334 my @imports =
335 map {
336 38 state $can_import = { map { $_, 1 } qw(base parent) };
337 126 ▶ exists $can_import->{$_->module}
Branch: 1 branch
BranchTrueFalseTotal
exists $$can_import{$_->module} ? : 7 119 126
338 ?
339 $_->imports->@*
340 :
341 ();
342 } $modules->@*;
343  
344 my @modules =
345 133 grep { state %Seen; ! $Seen{$_}++ } (
346 @imports,
347 38 map { $_->module } $modules->@*
348 );
349  
350 38 return \@modules;
351 }
352  
353 =back
354  
355 =head1 TO DO
356  
357 =over 4
358  
359 =item * set up a couple fake module distributions to test
360  
361 =item * warn about things that show up in C<PREREQ_PM> unnecessarily
362  
363 =back
364  
365 =head1 SOURCE AVAILABILITY
366  
367 This source is in Github:
368  
369 http://github.com/briandfoy/test-prereq
370  
371 =head1 CONTRIBUTORS
372  
373 Many thanks to:
374  
375 Andy Lester, Slavin Rezić, Randal Schwartz, Iain Truskett, Dylan Martin
376  
377 =head1 AUTHOR
378  
379 brian d foy, C<< <briandfoy@pobox.com> >>
380  
381 =head1 COPYRIGHT and LICENSE
382  
383 Copyright © 2002-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
384 This software is available under the Artistic License 2.
385  
386 =cut
387  
388 8 1;
↑ Summary blib/lib/Test/Prereq/Build.pm »