blib/lib/Test/Pod.pm

filter: Statementstmt 81.8% 63 / 77 Branchbran 53.1% 17 / 32 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 85.7% 12 / 14 Podpod 100% 3 / 3 totaltotal 71.5% 103 / 144 CC 25 SCAR 23.8
CC
25
Cov
72%
all_pod_files_ok
42.0
pod_file_ok
8.0
_is_perl
8.0
CRAP 10.8
↑ Summary
1 package Test::Pod;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Test::Pod - check for POD errors in files
9  
10 =head1 VERSION
11  
12 Version 1.52
13  
14 =cut
15  
16 9 our $VERSION = '1.52';
17  
18 =head1 SYNOPSIS
19  
20 C<Test::Pod> lets you check the validity of a POD file, and report
21 its results in standard C<Test::Simple> fashion.
22  
23 use Test::Pod tests => $num_tests;
24 pod_file_ok( $file, "Valid POD file" );
25  
26 Module authors can include the following in a F<t/pod.t> file and
27 have C<Test::Pod> automatically find and check all POD files in a
28 module distribution:
29  
30 use Test::More;
31 eval "use Test::Pod 1.00";
32 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
33 all_pod_files_ok();
34  
35 You can also specify a list of files to check, using the
36 C<all_pod_files()> function supplied:
37  
38 use strict;
39 use Test::More;
40 eval "use Test::Pod 1.00";
41 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
42 my @poddirs = qw( blib script );
43 all_pod_files_ok( all_pod_files( @poddirs ) );
44  
45 Or even (if you're running under L<Apache::Test>):
46  
47 use strict;
48 use Test::More;
49 eval "use Test::Pod 1.00";
50 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
51  
52 my @poddirs = qw( blib script );
53 use File::Spec::Functions qw( catdir updir );
54 all_pod_files_ok(
55 all_pod_files( map { catdir updir, $_ } @poddirs )
56 );
57  
58 =head1 DESCRIPTION
59  
60 Check POD files for errors or warnings in a test file, using
61 C<Pod::Simple> to do the heavy lifting.
62  
63 =cut
64  
65 9 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 9 ▶use Pod::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 9 our %ignore_dirs = (
69 '.bzr' => 'Bazaar',
70 '.git' => 'Git',
71 '.hg' => 'Mercurial',
72 '.pc' => 'quilt',
73 '.svn' => 'Subversion',
74 CVS => 'CVS',
75 RCS => 'RCS',
76 SCCS => 'SCCS',
77 _darcs => 'darcs',
78 _sgbak => 'Vault/Fortress',
79 );
80  
81 9 my $Test = Test::Builder->new;
82  
83 9 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
84 9 my $self = shift;
85 9 my $caller = caller;
86  
87 9 for my $func ( qw( pod_file_ok all_pod_files all_pod_files_ok ) ) {
88 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 27 *{$caller."::".$func} = \&$func;
90 }
91  
92 9 $Test->exported_to($caller);
93 9 $Test->plan(@_);
94 }
95  
96 6 ▶sub _additional_test_pod_specific_checks {
Subroutine _additional_test_pod_specific_checks: called CC=1 SCAR=0.0
97 6 my ($ok, $errata, $file) = @_;
98  
99 6 return $ok;
100 }
101  
102 =head1 FUNCTIONS
103  
104 =head2 pod_file_ok( FILENAME[, TESTNAME ] )
105  
106 C<pod_file_ok()> will okay the test if the POD parses correctly. Certain
107 conditions are not reported yet, such as a file with no pod in it at all.
108  
109 When it fails, C<pod_file_ok()> will show any pod checking errors as
110 diagnostics.
111  
112 The optional second argument TESTNAME is the name of the test. If it
113 is omitted, C<pod_file_ok()> chooses a default test name "POD test
114 for FILENAME".
115  
116 =cut
117  
118 8 ▶sub pod_file_ok {
Subroutine pod_file_ok: called CC=8 SCAR=20.8
Pod documented
119 8 my $file = shift;
120 8 ▶ my $name = @_ ? shift : "POD test for $file";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 4 8
121  
122 8 ▶ if ( !-f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 2 6 8
123 2 $Test->ok( 0, $name );
124 2 $Test->diag( "$file does not exist" );
125 2 return;
126 }
127  
128 6 my $checker = Pod::Simple->new;
129  
130 6 $checker->output_string( \my $trash ); # Ignore any output
131 6 $checker->parse_file( $file );
132  
133 6 my $ok = !$checker->any_errata_seen;
134 6 ▶ $ok = _additional_test_pod_specific_checks( $ok, ($checker->{errata}||={}), $file );
Condition100%2/2
$checker->{"errata"} ||= {}
l!l
33
MC/DC100%1/1
$checker->{"errata"} ||= {}
$checker->{"errata"}
Truth table100%2/2
A
A= $checker->{"errata"}
Aresult
11
00
135  
136 6 ▶ $name .= ' (no pod)' if !$checker->content_seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $checker->content_seen 0 6 6
137 6 $Test->ok( $ok, $name );
138 6 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 3 3 6
139 3 my $lines = $checker->{errata};
140 3 for my $line ( sort { $a<=>$b } keys %$lines ) {
141 6 my $errors = $lines->{$line};
142 6 $Test->diag( "$file ($line): $_" ) for @$errors;
143 }
144 }
145  
146 6 return $ok;
147 } # pod_file_ok
148  
149 =head2 all_pod_files_ok( [@entries] )
150  
151 Checks all the files under C<@entries> for valid POD. It runs
152 L<all_pod_files()> on directories and assumes everything else to be a file to
153 be tested. It calls the C<plan()> function for you (one test for each file),
154 so you can't have already called C<plan>.
155  
156 If C<@entries> is empty or not passed, the function finds all POD files in
157 files in the F<blib> directory if it exists, or the F<lib> directory if not.
158 A POD file matches the conditions specified below in L</all_pod_files>.
159  
160 If you're testing a module, just make a F<t/pod.t>:
161  
162 use Test::More;
163 eval "use Test::Pod 1.00";
164 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
165 all_pod_files_ok();
166  
167 Returns true if all pod files are ok, or false if any fail.
168  
169 =cut
170  
171 0 ▶sub all_pod_files_ok {
Subroutine all_pod_files_ok: not called CC=6 SCAR=37.4
Pod documented
172 0 ▶ my @args = @_ ? @_ : _starting_points();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
173 0 ▶ my @files = map { -d $_ ? all_pod_files($_) : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
174  
175 0 ▶ unless (@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 0 0
176 0 $Test->skip_all( "No files found in (@args)\n" );
177 0 return 1;
178 }
179  
180 0 $Test->plan( tests => scalar @files );
181  
182 0 my $ok = 1;
183 0 foreach my $file ( @files ) {
184 0 ▶ pod_file_ok( $file ) or undef $ok;
Branch: 1 branch
BranchTrueFalseTotal
pod_file_ok($file) or undef $ok 0 0 0
185 }
186 0 return $ok;
187 }
188  
189 =head2 all_pod_files( [@dirs] )
190 X<all_pod_files>
191  
192 Returns a list of all the POD files in I<@dirs> and in directories below. If
193 no directories are passed, it defaults to F<blib> if F<blib> exists, or else
194 F<lib> if not. Skips any files in F<CVS>, F<.svn>, F<.git> and similar
195 directories. See C<%Test::Pod::ignore_dirs> for a list of them.
196  
197 A POD file is:
198  
199 =over 4
200  
201 =item * Any file that ends in F<.pl>, F<.PL>, F<.pm>, F<.pod>, F<.psgi> or F<.t>.
202  
203 =item * Any file that has a first line with a shebang and "perl" on it.
204  
205 =item * Any file that ends in F<.bat> and has a first line with "--*-Perl-*--" on it.
206  
207 =back
208  
209 The order of the files returned is machine-dependent. If you want them
210 sorted, you'll have to sort them yourself.
211  
212 =cut
213  
214 1 ▶sub all_pod_files {
Subroutine all_pod_files: called CC=2 SCAR=7.2
Pod documented
215 1 my @pod;
216 1 require File::Find;
217 File::Find::find({
218 3 ▶ preprocess => sub { grep {
Subroutine __ANON__: called CC=2 SCAR=8.1
219 3 ▶ !exists $ignore_dirs{$_}
Condition33%1/3
exists $Test::Pod::ignore_dirs{$_} && -d 'File::Spec'->catfile($File::Find::dir, $_)
!ll&&!rl&&r
1200
MC/DC0%0/2
exists $Test::Pod::ignore_dirs{$_} && -d 'File::Spec'->catfile($File::Find::dir, $_)
exists $Test::Pod::ignore_dirs{$_} -d 'File::Spec'->catfile($File::Find::dir, $_)
Truth table33%1/3
A && B
A= exists $Test::Pod::ignore_dirs{$_}B= -d 'File::Spec'->catfile($File::Find::dir, $_)
ABresult
0X0
100
111
220 || !-d File::Spec->catfile($File::Find::dir, $_)
221 } @_ },
222 8 ▶ wanted => sub { -f $_ && _is_perl($_) && push @pod, $File::Find::name },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and _is_perl($_) and push @pod, $File::Find::name 5 3 8
Condition66%2/3
-f $_ and _is_perl($_)
!ll&&!rl&&r
305
MC/DC50%1/2
-f $_ and _is_perl($_)
-f $_ _is_perl($_)
Truth table66%2/3
A and B
A= -f $_B= _is_perl($_)
ABresult
0X0
100
111
223 1 ▶ no_chdir => 1,
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
224 }, @_ ? @_ : _starting_points());
225 1 return @pod;
226 }
227  
228 0 ▶sub _starting_points {
Subroutine _starting_points: not called CC=2 SCAR=17.9
229 0 ▶ return 'blib' if -e 'blib';
Branch: 1 branch
BranchTrueFalseTotal
if -e "blib" 0 0 0
230 0 return 'lib';
231 }
232  
233 5 ▶sub _is_perl {
Subroutine _is_perl: called CC=7 SCAR=20.8
234 5 my $file = shift;
235  
236 # accept as a Perl file everything that ends with a well known Perl suffix ...
237 5 ▶ return 1 if $file =~ /[.](?:PL|p(?:[lm]|od|sgi)|t)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /[.](?:PL|p(?:[lm]|od|sgi)|t)$/ 3 2 5
238  
239 2 ▶ open my $fh, '<', $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 2 0 2
240 2 my $first = <$fh>;
241 2 close $fh;
242 2 ▶ return unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 2 2
243  
244 # ... or that has a she-bang as first line ...
245 2 ▶ return 1 if $first =~ /^#!.*perl/;
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /^#!.*perl/ 1 1 2
246  
247 # ... or that is a .bat ad has a Perl comment line first
248 1 ▶ return 1 if $file =~ /[.]bat$/i && $first =~ /--[*]-Perl-[*]--/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /[.]bat$/i and $first =~ /--[*]-Perl-[*]--/ 1 0 1
Condition33%1/3
$file =~ /[.]bat$/i and $first =~ /--[*]-Perl-[*]--/
!ll&&!rl&&r
001
MC/DC0%0/2
$file =~ /[.]bat$/i and $first =~ /--[*]-Perl-[*]--/
$file =~ /[.]bat$/i $first =~ /--[*]-Perl-[*]--/
Truth table33%1/3
A and B
A= $file =~ /[.]bat$/iB= $first =~ /--[*]-Perl-[*]--/
ABresult
0X0
100
111
249  
250 0 return;
251 }
252  
253 =head1 SUPPORT
254  
255 This module is managed in an open L<GitHub
256 repository|http://github.com/perl-pod/test-pod/>. Feel free to fork and
257 contribute, or to clone L<git://github.com/perl-pod/test-pod.git> and send
258 patches!
259  
260 Found a bug? Please L<post|http://github.com/perl-pod/test-pod/issues> or
261 L<email|mailto:bug-test-pod@rt.cpan.org> a report!
262  
263 =head1 AUTHORS
264  
265 =over
266  
267 =item David E. Wheeler <david@justatheory.com>
268  
269 Current maintainer.
270  
271 =item Andy Lester C<< <andy at petdance.com> >>
272  
273 Maintainer emeritus.
274  
275 =item brian d foy
276  
277 Original author.
278  
279 =back
280  
281 =head1 ACKNOWLEDGEMENTS
282  
283 Thanks brian d foy for the original code, and to these folks for contributions:
284  
285 =over
286  
287 =item * Andy Lester
288  
289 =item * David E. Wheeler
290  
291 =item * Paul Miller
292  
293 =item * Peter Edwards
294  
295 =item * Luca Ferrari
296  
297 =back
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 Copyright 2006-2010, Andy Lester; 2010-2015 David E. Wheeler. Some Rights
302 Reserved.
303  
304 This module is free software; you can redistribute it and/or modify it under
305 the same terms as Perl itself.
306  
307 =cut
308  
309 9 1;
↑ Summary