Compile/Internal.pm

filter: Statementstmt 99.3% 157 / 158 Branchbran 94.4% 51 / 54 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 33 / 33 Podpod 100% 15 / 15 totaltotal 98.5% 276 / 280 CC 40 SCAR 13.0
CC
40
Cov
98%
_perl_file_compiles
9.0
_run_command
6.0
_default_locations
4.0
CRAP 3.7
↑ Summary Compile.pm »
1 package Test::Compile::Internal;
2  
3 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 32 ▶use version; our $VERSION = version->declare("v3.3.3");
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use IPC::Open3 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Test::Compile::Internal - Assert that your Perl files compile OK.
15  
16 =head1 SYNOPSIS
17  
18 use Test::Compile::Internal;
19 my $test = Test::Compile::Internal->new();
20 $test->all_files_ok();
21 $test->done_testing();
22  
23 =head1 DESCRIPTION
24  
25 C<Test::Compile::Internal> is an object oriented tool for testing whether your
26 perl files compile.
27  
28 It is primarily to provide the inner workings of C<Test::Compile>, but it can
29 also be used directly to test a CPAN distribution.
30  
31 =head1 METHODS
32  
33 =over 4
34  
35 =item C<new()>
36  
37 A basic constructor, nothing special.
38 =cut
39  
40 33 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
41 33 my ($class, %self) = @_;
42 33 my $self = \%self;
43  
44 33 $self->{test} = Test::Builder->new();
45  
46 33 bless ($self, $class);
47 33 return $self;
48 }
49  
50 =item C<all_files_ok(@search)>
51  
52 Looks for perl files and tests them all for compilation errors.
53  
54 If C<@search> is defined then it is taken as an array of files or
55 directories to be searched for perl files, otherwise it searches the default
56 locations you'd expect to find perl files in a perl module - see
57 L</all_pm_files> and L</all_pl_files> for details.
58  
59 =cut
60 5 ▶sub all_files_ok {
Subroutine all_files_ok: called CC=2 SCAR=6.9
Pod documented
61 5 my ($self, @search) = @_;
62  
63 5 my $pm_ok = $self->all_pm_files_ok(@search);
64 5 my $pl_ok = $self->all_pl_files_ok(@search);
65  
66 5 ▶ return ( $pm_ok && $pl_ok );
Condition100%3/3
$pm_ok && $pl_ok
!ll
131
MC/DC100%2/2
$pm_ok && $pl_ok
$pm_ok $pl_ok
Truth table100%3/3
A && B
A= $pm_okB= $pl_ok
ABresult
0X0
100
111
67 }
68  
69  
70 =item C<all_pm_files_ok(@search)>
71  
72 Checks all the perl module files it can find for compilation errors.
73  
74 If C<@search> is defined then it is taken as an array of files or
75 directories to be searched for perl files, otherwise it searches the default
76 locations you'd expect to find perl files in a perl module - see
77 L</all_pm_files> for details.
78  
79 =cut
80 7 ▶sub all_pm_files_ok {
Subroutine all_pm_files_ok: called CC=3 SCAR=11.0
Pod documented
81 7 my ($self, @search) = @_;
82  
83 7 my $test = $self->{test};
84  
85 7 my $ok = 1;
86 7 for my $file ( $self->all_pm_files(@search) ) {
87 11 my $testok = $self->pm_file_compiles($file);
88 11 ▶ $ok = $testok ? $ok : 0;
Branch: 1 branch
BranchTrueFalseTotal
$testok ? : 10 1 11
89 11 $test->ok($testok, "$file compiles");
90 }
91 7 return $ok;
92 }
93  
94  
95 =item C<all_pl_files_ok(@search)>
96  
97 Checks all the perl program files it can find for compilation errors.
98  
99 If C<@search> is defined then it is taken as an array of directories to
100 be searched for perl files, otherwise it searches some default locations
101 - see L</all_pl_files>.
102  
103 =cut
104 7 ▶sub all_pl_files_ok {
Subroutine all_pl_files_ok: called CC=3 SCAR=11.0
Pod documented
105 7 my ($self, @search) = @_;
106  
107 7 my $test = $self->{test};
108  
109 7 my $ok = 1;
110 7 for my $file ( $self->all_pl_files(@search) ) {
111 3 my $testok = $self->pl_file_compiles($file);
112 3 ▶ $ok = $testok ? $ok : 0;
Branch: 1 branch
BranchTrueFalseTotal
$testok ? : 1 2 3
113 3 $test->ok($testok, "$file compiles");
114 }
115 7 return $ok;
116 }
117  
118  
119 =item C<verbose($verbose)>
120  
121 An accessor to get/set the verbosity. The default value (undef) will suppress output
122 unless the compilation fails. This is probably what you want.
123  
124 If C<verbose> is set to true, you'll get the output from 'perl -c'. If it's set to
125 false, all diagnostic output is suppressed.
126  
127 =cut
128  
129 115 ▶sub verbose {
Subroutine verbose: called CC=2 SCAR=6.9
Pod documented
130 115 my ($self, $verbose) = @_;
131  
132 115 ▶ if ( @_ eq 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ eq 2) 11 104 115
133 11 $self->{_verbose} = $verbose;
134 }
135  
136 115 return $self->{_verbose};
137 }
138  
139 =item C<all_pm_files(@search)>
140  
141 Searches for and returns a list of perl module files - that is, files with a
142 F<.pm> extension.
143  
144 If you provide C<@search>, it'll use that as a list of files to
145 process, or directories to search for perl modules.
146  
147 If you don't provide C<search>, it'll search for perl modules in the F<blib/lib>
148 directory (if that directory exists). Otherwise it'll search the F<lib> directory.
149  
150 Skips any files in F<CVS>, F<.svn>, or F<.git> directories.
151  
152 =cut
153  
154 13 ▶sub all_pm_files {
Subroutine all_pm_files: called CC=4 SCAR=13.9
Pod documented
155 13 my ($self, @search) = @_;
156  
157 13 ▶ if ( ! @search ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@search) 5 8 13
158 5 @search = $self->_default_locations('lib');
159 }
160  
161 13 my @pm;
162 13 for my $file ( $self->_find_files(@search) ) {
163 33 ▶ if ( $self->_perl_module($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_perl_module($file)) 21 12 33
164 21 push @pm, $file;
165 }
166 }
167 13 return @pm;
168 }
169  
170 =item C<all_pl_files(@search)>
171  
172 Searches for and returns a list of perl script files - that is, any files that
173 either have a case insensitive F<.pl>, F<.psgi> extension, or have no extension
174 but have a perl shebang line.
175  
176 If you provide C<@search>, it'll use that as a list of files to
177 process, or directories to search for perl scripts.
178  
179 If you don't provide C<search>, it'll search for perl scripts in the
180 F<blib/script/> and F<blib/bin/> directories if F<blib> exists, otherwise
181 it'll search the F<script/> and F<bin/> directories
182  
183 Skips any files in F<CVS>, F<.svn>, or F<.git> directories.
184  
185 =cut
186  
187 13 ▶sub all_pl_files {
Subroutine all_pl_files: called CC=4 SCAR=13.9
Pod documented
188 13 my ($self, @search) = @_;
189  
190 13 ▶ if ( ! @search ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@search) 4 9 13
191 4 @search = $self->_default_locations('script', 'bin');
192 }
193  
194 13 my @pl;
195 13 for my $file ( $self->_find_files(@search) ) {
196 22 ▶ if ( $self->_perl_script($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_perl_script($file)) 12 10 22
197 12 push @pl, $file;
198 }
199 }
200 13 return @pl;
201 }
202  
203 =item C<pl_file_compiles($file)>
204  
205 Returns true if C<$file> compiles as a perl script.
206  
207 =cut
208  
209 19 ▶sub pl_file_compiles {
Subroutine pl_file_compiles: called CC=1 SCAR=0.0
Pod documented
210 19 my ($self, $file) = @_;
211  
212 19 return $self->_perl_file_compiles($file);
213 }
214  
215 =item C<pm_file_compiles($file)>
216  
217 Returns true if C<$file> compiles as a perl module.
218  
219 =back
220  
221 =cut
222  
223 17 ▶sub pm_file_compiles {
Subroutine pm_file_compiles: called CC=1 SCAR=0.0
Pod documented
224 17 my ($self, $file) = @_;
225  
226 17 return $self->_perl_file_compiles($file);
227 }
228  
229 =head1 TEST METHODS
230  
231 C<Test::Compile::Internal> encapsulates a C<Test::Builder> object, and provides
232 access to some of its methods.
233  
234 =over 4
235  
236 =item C<ok($test, $name)>
237  
238 Your basic test. Pass if C<$test> is true, fail if C<$test> is false. Just
239 like C<Test::Simple>'s C<ok()>.
240  
241 =cut
242 12 ▶sub ok {
Subroutine ok: called CC=1 SCAR=0.0
Pod documented
243 12 my ($self, @args) = @_;
244 12 $self->{test}->ok(@args);
245 }
246  
247 =item C<done_testing()>
248  
249 Declares that you got to the end of your test plan, no more tests will be run after
250 this point.
251  
252 =cut
253 11 ▶sub done_testing {
Subroutine done_testing: called CC=1 SCAR=0.0
Pod documented
254 11 my ($self, @args) = @_;
255 11 $self->{test}->done_testing(@args);
256 }
257  
258 =item C<plan(tests =E<gt> $count)>
259  
260 Defines how many tests you plan to run.
261  
262 =cut
263 3 ▶sub plan {
Subroutine plan: called CC=1 SCAR=0.0
Pod documented
264 3 my ($self, @args) = @_;
265 3 $self->{test}->plan(@args);
266 }
267  
268 =item C<diag(@msgs)>
269  
270 Prints out the given C<@msgs>. Like print, arguments are simply appended
271 together.
272  
273 Output will be indented and marked with a # so as not to interfere with
274 test output. A newline will be put on the end if there isn't one already.
275  
276 We encourage using this rather than calling print directly.
277  
278 =cut
279  
280 1 ▶sub diag {
Subroutine diag: called CC=1 SCAR=0.0
Pod documented
281 1 my ($self, @args) = @_;
282 1 $self->{test}->diag(@args);
283 }
284  
285 =item C<skip($reason)>
286  
287 Skips the current test, reporting the C<$reason>.
288  
289 =cut
290  
291 1 ▶sub skip {
Subroutine skip: called CC=1 SCAR=0.0
Pod documented
292 1 my ($self, @args) = @_;
293 1 $self->{test}->skip(@args);
294 }
295  
296 =item C<skip_all($reason)>
297  
298 Skips all the tests, using the given C<$reason>. Exits immediately with 0.
299  
300 =back
301 =cut
302  
303 1 ▶sub skip_all {
Subroutine skip_all: called CC=1 SCAR=0.0
Pod documented
304 1 my ($self, @args) = @_;
305 1 $self->{test}->skip_all(@args);
306 }
307  
308 # Run a subcommand, catching STDOUT, STDERR and return code
309 34 ▶sub _run_command {
Subroutine _run_command: called CC=6 SCAR=17.9
310 34 my ($self, $cmd) = @_;
311  
312 34 my ($stdout, $stderr);
313 34 ▶ my $pid = IPC::Open3::open3(0, $stdout, $stderr, $cmd)
Branch: 1 branch
BranchTrueFalseTotal
my $pid = IPC::Open3::open3(0, $stdout, $stderr, $cmd) or die "open3() failed $!" 34 0 34
314 or die "open3() failed $!";
315  
316 34 my $output = [];
317 34 for my $handle ( $stdout, $stderr ) {
318 68 ▶ if ( $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle) 34 34 68
319 34 ▶ while ( my $line = <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 47 34 81
320 47 push @$output, $line;
321 }
322 }
323 }
324  
325 34 waitpid($pid, 0);
326 34 ▶ my $success = ($? == 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 25 9 34
327  
328 34 return ($success, $output);
329 }
330  
331 # Works it's way through the input array (files and/or directories), recursively
332 # finding files
333 33 ▶sub _find_files {
Subroutine _find_files: called CC=3 SCAR=11.0
334 33 my ($self, @search) = @_;
335  
336 33 my @filelist;
337 161 ▶ my $addFile = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
338 161 my ($fname) = @_;
339  
340 161 ▶ if ( -f $fname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fname) 110 51 161
341 110 ▶ if ( !($fname =~ m/CVS|\.svn|\.git/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fname =~ /CVS|\.svn|\.git/) 95 15 110
342 95 push @filelist, $fname;
343 }
344 }
345 33 };
346  
347 33 for my $item ( @search ) {
348 31 $addFile->($item);
349 31 ▶ if ( -d $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $item) 11 20 31
350 32 ▶ no warnings 'File::Find';
Subroutine BEGIN: called CC=1 SCAR=0.0
351 11 ▶ find({wanted => sub{$addFile->($File::Find::name)}, no_chdir => 1}, $item);
Subroutine __ANON__: called CC=1 SCAR=0.0
352 }
353 }
354 33 return (sort @filelist);
355 }
356  
357 # Check the syntax of a perl file
358 36 ▶sub _perl_file_compiles {
Subroutine _perl_file_compiles: called CC=9 SCAR=22.0
359 36 my ($self, $file) = @_;
360  
361 36 ▶ if ( ! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 2 34 36
362 2 ▶ if ( $self->verbose() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 2 2
363 0 $self->{test}->diag("$file could not be found");
364 }
365 2 return 0;
366 }
367  
368 34 my @inc = (File::Spec->catdir("blib", "lib"), @INC);
369 34 my $taint = $self->_taint_mode($file);
370 34 my $command = join(" ", (qq{"$^X"}, (map { qq{"-I$_"} } @inc), "-c$taint", $file));
371 34 ▶ if ( $self->verbose() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 2 32 34
372 2 $self->{test}->diag("Executing: " . $command);
373 }
374 34 my ($compiles, $output) = $self->_run_command($command);
375 34 ▶ if ( !defined($self->verbose()) || $self->verbose() != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->verbose or $self->verbose != 0) 25 9 34
Condition100%3/3
not defined $self->verbose or $self->verbose != 0
l!l&&r!l&&!r
2329
MC/DC100%2/2
not defined $self->verbose or $self->verbose != 0
not defined $self->verbose $self->verbose != 0
Truth table100%3/3
not(A) or B
A= not defined $self->verboseB= $self->verbose != 0
ABresult
1X1
011
000
376 25 ▶ if ( !$compiles || $self->verbose() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $compiles or $self->verbose) 7 18 25
Condition100%3/3
not $compiles or $self->verbose
l!l&&r!l&&!r
6118
MC/DC100%2/2
not $compiles or $self->verbose
not $compiles $self->verbose
Truth table100%3/3
not(A) or B
A= not $compilesB= $self->verbose
ABresult
1X1
011
000
377 7 for my $line ( @$output ) {
378 16 $self->{test}->diag($line);
379 }
380 }
381 }
382  
383 34 return $compiles;
384 }
385  
386 # Where do we expect to find perl files?
387 13 ▶sub _default_locations {
Subroutine _default_locations: called CC=4 SCAR=13.9
388 13 my ($self, @dirs) = @_;
389  
390 13 my $blib = -e 'blib';
391 13 my @locations = ();
392  
393 13 for my $dir ( @dirs ) {
394 16 my $location = File::Spec->catfile($dir);
395 16 ▶ if ( $blib ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blib) 16 0 16
396 16 $location = File::Spec->catfile('blib', $dir);
397 }
398 16 ▶ if ( -e $location ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $location) 7 9 16
399 7 push @locations, $location;
400 }
401 }
402 13 return @locations;
403 }
404  
405 # Extract the shebang line from a perl program
406 46 ▶sub _read_shebang {
Subroutine _read_shebang: called CC=4 SCAR=13.9
407 46 my ($self, $file) = @_;
408  
409 46 ▶ if ( open(my $f, "<", $file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $f, "<", $file) 45 1 46
410 45 my $line = <$f>;
411 45 ▶ if (defined $line && $line =~ m/^#!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ /^#!/) 14 31 45
Condition100%3/3
defined $line and $line =~ /^#!/
!ll&&!rl&&r
13014
MC/DC100%2/2
defined $line and $line =~ /^#!/
defined $line $line =~ /^#!/
Truth table100%3/3
A and B
A= defined $lineB= $line =~ /^#!/
ABresult
0X0
100
111
412 14 return $line;
413 }
414 }
415 }
416  
417 # Should the given file be checked with taint mode on?
418 37 ▶sub _taint_mode {
Subroutine _taint_mode: called CC=2 SCAR=6.9
419 37 my ($self, $file) = @_;
420  
421 37 my $shebang = $self->_read_shebang($file);
422 37 my $taint = "";
423 37 ▶ if ($shebang =~ /^#!\s*[\/\w]+\s+-\w*([tT])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shebang =~ m[^#!\s*[/\w]+\s+-\w*([tT])]) 5 32 37
424 5 $taint = $1;
425 }
426 37 return $taint;
427 }
428  
429 # Does this file look like a perl script?
430 26 ▶sub _perl_script {
Subroutine _perl_script: called CC=4 SCAR=13.9
431 26 my ($self, $file) = @_;
432  
433 # Files with .pl or .psgi extensions are perl scripts
434 26 ▶ if ( $file =~ /\.p(?:l|sgi)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.p(?:l|sgi)$/i) 12 14 26
435 12 return 1;
436 }
437  
438 # Files with no extension, but a perl shebang are perl scripts
439 14 ▶ if ( $file =~ /(?:^[^.]+$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /(?:^[^.]+$)/) 6 8 14
440 6 my $shebang = $self->_read_shebang($file);
441 6 ▶ if ( $shebang =~ m/perl/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shebang =~ /perl/) 2 4 6
442 2 return 1;
443 }
444 }
445 }
446  
447 # Does this file look like a perl module?
448 37 ▶sub _perl_module {
Subroutine _perl_module: called CC=1 SCAR=0.0
449 37 my ($self, $file) = @_;
450  
451 37 return ( $file =~ /\.pm$/ );
452 }
453  
454 32 1;
455  
456 =head1 AUTHORS
457  
458 Sagar R. Shah C<< <srshah@cpan.org> >>,
459 Marcel GrE<uuml>nauer, C<< <marcel@cpan.org> >>,
460 Evan Giles, C<< <egiles@cpan.org> >>
461  
462 =head1 COPYRIGHT AND LICENSE
463  
464 Copyright 2007-2023 by the authors.
465  
466 This library is free software; you can redistribute it and/or modify
467 it under the same terms as Perl itself.
468  
469 =head1 SEE ALSO
470  
471 L<Test::Strict> provides functions to ensure your perl files compile, with
472 the added bonus that it will check you have used strict in all your files.
473  
474 L<Test::LoadAllModules> just handles modules, not script files, but has more
475 fine-grained control.
476  
477 =cut
↑ Summary Compile.pm »