7 Test::Pod - check for POD errors in files
15 use vars qw( $VERSION );
20 C<Test::Pod> lets you check the validity of a POD file, and report
21 its results in standard C<Test::Simple> fashion.
23 use Test::Pod tests => $num_tests;
24 pod_file_ok( $file, "Valid POD file" );
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
31 eval "use Test::Pod 1.00";
32 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
35 You can also specify a list of files to check, using the
36 C<all_pod_files()> function supplied:
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 ) );
45 Or even (if you're running under L<Apache::Test>):
49 eval "use Test::Pod 1.00";
50 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
52 my @poddirs = qw( blib script );
53 use File::Spec::Functions qw( catdir updir );
55 all_pod_files( map { catdir updir, $_ } @poddirs )
60 Check POD files for errors or warnings in a test file, using
61 C<Pod::Simple> to do the heavy lifting.
71 my $Test = Test::Builder->new;
77 for my $func ( qw( pod_file_ok all_pod_files all_pod_files_ok ) ) {
79 *{$caller."::".$func} = \&$func;
82 $Test->exported_to($caller);
88 =head2 pod_file_ok( FILENAME[, TESTNAME ] )
90 C<pod_file_ok()> will okay the test if the POD parses correctly. Certain
91 conditions are not reported yet, such as a file with no pod in it at all.
93 When it fails, C<pod_file_ok()> will show any pod checking errors as
96 The optional second argument TESTNAME is the name of the test. If it
97 is omitted, C<pod_file_ok()> chooses a default test name "POD test
104 my $name = @_ ? shift : "POD test for $file";
107 $Test->ok( 0, $name );
108 $Test->diag( "$file does not exist" );
112 my $checker = Pod::Simple->new;
114 $checker->output_string( \my $trash ); # Ignore any output
115 $checker->parse_file( $file );
117 my $ok = !$checker->any_errata_seen;
118 $Test->ok( $ok, $name );
120 my $lines = $checker->{errata};
121 for my $line ( sort { $a<=>$b } keys %$lines ) {
122 my $errors = $lines->{$line};
123 $Test->diag( "$file ($line): $_" ) for @$errors;
130 =head2 all_pod_files_ok( [@files/@directories] )
132 Checks all the files in C<@files> for valid POD. It runs
133 L<all_pod_files()> on each file/directory, and calls the C<plan()> function for you
134 (one test for each function), so you can't have already called C<plan>.
136 If C<@files> is empty or not passed, the function finds all POD files in
137 the F<blib> directory if it exists, or the F<lib> directory if not.
138 A POD file is one that ends with F<.pod>, F<.pl> and F<.pm>, or any file
139 where the first line looks like a shebang line.
141 If you're testing a module, just make a F<t/pod.t>:
144 eval "use Test::Pod 1.00";
145 plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
148 Returns true if all pod files are ok, or false if any fail.
152 sub all_pod_files_ok {
153 my @files = @_ ? @_ : all_pod_files();
155 $Test->plan( tests => scalar @files );
158 foreach my $file ( @files ) {
159 pod_file_ok( $file, $file ) or undef $ok;
164 =head2 all_pod_files( [@dirs] )
166 Returns a list of all the Perl files in I<$dir> and in directories below.
167 If no directories are passed, it defaults to F<blib> if F<blib> exists,
168 or else F<lib> if not. Skips any files in CVS or .svn directories.
174 =item * Any file that ends in F<.PL>, F<.pl>, F<.pm>, F<.pod> or F<.t>.
176 =item * Any file that has a first line with a shebang and "perl" on it.
180 The order of the files returned is machine-dependent. If you want them
181 sorted, you'll have to sort them yourself.
186 my @queue = @_ ? @_ : _starting_points();
190 my $file = shift @queue;
193 opendir DH, $file or next;
194 my @newfiles = readdir DH;
197 @newfiles = File::Spec->no_upwards( @newfiles );
198 @newfiles = grep { $_ ne "CVS" && $_ ne ".svn" } @newfiles;
200 foreach my $newfile (@newfiles) {
201 my $filename = File::Spec->catfile( $file, $newfile );
202 if ( -f $filename ) {
203 push @queue, $filename;
206 push @queue, File::Spec->catdir( $file, $newfile );
211 push @pod, $file if _is_perl( $file );
217 sub _starting_points {
218 return 'blib' if -e 'blib';
225 return 1 if $file =~ /\.PL$/;
226 return 1 if $file =~ /\.p(l|m|od)$/;
227 return 1 if $file =~ /\.t$/;
230 open FH, $file or return;
234 return 1 if defined $first && ($first =~ /^#!.*perl/);
243 Note the changes that are being made.
245 Note that you no longer can test for "no pod".
249 Currently maintained by Andy Lester, C<< <andy at petdance.com> >>.
251 Originally by brian d foy.
253 =head1 ACKNOWLEDGEMENTS
259 for contributions and to C<brian d foy> for the original code.
263 Copyright 2006, Andy Lester, All Rights Reserved.
265 You may use, modify, and distribute this package under the
266 same terms as Perl itself.