Check *.pod files as well as *.pm, *.pl and *.t (RT#82032)
[catagits/Test-EOL.git] / lib / Test / EOL.pm
CommitLineData
a83ae797 1package Test::EOL;
94bc2901 2# ABSTRACT: Check the correct line endings in your project
a83ae797 3
4use strict;
5use warnings;
6
7use Test::Builder;
8use File::Spec;
a83ae797 9use File::Find;
856d7baf 10use Cwd qw/ cwd /;
a83ae797 11
94bc2901 12use vars qw( $PERL $UNTAINT_PATTERN $PERL_PATTERN);
a83ae797 13
14$PERL = $^X || 'perl';
15$UNTAINT_PATTERN = qr|^([-+@\w./:\\]+)$|;
16$PERL_PATTERN = qr/^#!.*perl/;
17
18my %file_find_arg = ($] <= 5.006) ? () : (
19 untaint => 1,
20 untaint_pattern => $UNTAINT_PATTERN,
21 untaint_skip => 1,
22);
23
24my $Test = Test::Builder->new;
a83ae797 25
5a0bc53a 26my $no_plan;
27
a83ae797 28sub import {
29 my $self = shift;
30 my $caller = caller;
31 {
32 no strict 'refs';
33 *{$caller.'::eol_unix_ok'} = \&eol_unix_ok;
34 *{$caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
35 }
36 $Test->exported_to($caller);
5a0bc53a 37
38 if ($_[0] && $_[0] eq 'no_plan') {
39 shift;
40 $no_plan = 1;
41 }
a83ae797 42 $Test->plan(@_);
43}
44
45sub _all_perl_files {
46 my @all_files = _all_files(@_);
b303ce9f 47 return grep { _is_perl_module($_) || _is_perl_script($_) || _is_pod_file($_) } @all_files;
a83ae797 48}
49
50sub _all_files {
856d7baf 51 my @base_dirs = @_ ? @_ : cwd();
52 my $options = pop(@base_dirs) if ref $base_dirs[-1] eq 'HASH';
a83ae797 53 my @found;
54 my $want_sub = sub {
e52debaf 55 return if ($File::Find::dir =~ m![\\/]?CVS[\\/]|[\\/]?\.svn[\\/]!); # Filter out cvs or subversion dirs/
a83ae797 56 return if ($File::Find::dir =~ m![\\/]?blib[\\/]libdoc$!); # Filter out pod doc in dist
57 return if ($File::Find::dir =~ m![\\/]?blib[\\/]man\d$!); # Filter out pod doc in dist
27c70d46 58 return if ($File::Find::dir =~ m![\\/]?inc!); # Filter out Module::Install stuff
a83ae797 59 return if ($File::Find::name =~ m!Build$!i); # Filter out autogenerated Build script
60 return unless (-f $File::Find::name && -r _);
61 push @found, File::Spec->no_upwards( $File::Find::name );
62 };
63 my $find_arg = {
64 %file_find_arg,
65 wanted => $want_sub,
66 no_chdir => 1,
67 };
68 find( $find_arg, @base_dirs);
69 return @found;
70}
71
f17f4176 72# Formats various human invisible symbols
73# to similar visible ones.
fe02d07d 74# Perhaps ^M or something like that
f17f4176 75# would be more appropriate?
76
fe02d07d 77sub _show_whitespace {
f17f4176 78 my $string = shift;
79 $string =~ s/\r/[\\r]/g;
80 $string =~ s/\t/[\\t]/g;
81 $string =~ s/ /[\\s]/g;
82 return $string;
83}
84
85# Format a line record for diagnostics.
86
fe02d07d 87sub _debug_line {
f17f4176 88 my ( $options, $line ) = @_;
89 $line->[2] =~ s/\n\z//g;
d3afcf90 90 return "line $line->[1]: $line->[0]" . (
fe02d07d 91 $options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] ) : q{}
f17f4176 92 );
93}
94
a83ae797 95sub eol_unix_ok {
96 my $file = shift;
91613276 97 my $test_txt;
98 $test_txt = shift if !ref $_[0];
939ef613 99 $test_txt ||= "No incorrect line endings in '$file'";
91613276 100 my $options = shift if ref $_[0] eq 'HASH';
101 $options ||= {
102 trailing_whitespace => 0,
f17f4176 103 all_reasons => 0,
91613276 104 };
a83ae797 105 $file = _module_to_path($file);
fe02d07d 106
a83ae797 107 open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
f17f4176 108 # Windows-- , default is :crlf, which hides \r\n -_-
a2bfe7c3 109 binmode( $fh, ':raw' );
a83ae797 110 my $line = 0;
f17f4176 111 my @fails;
a83ae797 112 while (<$fh>) {
113 $line++;
f17f4176 114 if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
115 my $match = $1;
116 push @fails, [ _show_whitespace( $match ) , $line , $_ ];
a83ae797 117 }
f17f4176 118 if ( $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
119 my $match = $1;
120 push @fails, [ _show_whitespace($match), $line , $_ ];
121 }
122 # Minor short-circuit for people who don't need the whole file scanned
123 # once there's an err.
124 last if( @fails > 0 && !$options->{all_reasons} );
125 }
fe02d07d 126 if( @fails ){
f17f4176 127 $Test->ok( 0, $test_txt . " on " . _debug_line({ show_lines => 0 } , $fails[0] ) );
128 if ( $options->{all_reasons} || 1 ){
129 $Test->diag( " Problem Lines: ");
fe02d07d 130 for ( @fails ){
f17f4176 131 $Test->diag(_debug_line({ show_lines => 1 } , $_ ) );
132 }
133 }
134 return 0;
a83ae797 135 }
136 $Test->ok(1, $test_txt);
137 return 1;
138}
a83ae797 139sub all_perl_files_ok {
91613276 140 my $options = shift if ref $_[0] eq 'HASH';
a83ae797 141 my @files = _all_perl_files( @_ );
142 _make_plan();
143 foreach my $file ( @files ) {
91613276 144 eol_unix_ok($file, $options);
a83ae797 145 }
146}
147
148sub _is_perl_module {
149 $_[0] =~ /\.pm$/i || $_[0] =~ /::/;
150}
151
b303ce9f 152sub _is_pod_file {
153 $_[0] =~ /\.pod$/i;
154}
155
a83ae797 156sub _is_perl_script {
157 my $file = shift;
158 return 1 if $file =~ /\.pl$/i;
159 return 1 if $file =~ /\.t$/;
7ebe98c1 160 open (my $fh, $file) or return;
a83ae797 161 my $first = <$fh>;
162 return 1 if defined $first && ($first =~ $PERL_PATTERN);
163 return;
164}
165
166sub _module_to_path {
167 my $file = shift;
168 return $file unless ($file =~ /::/);
169 my @parts = split /::/, $file;
170 my $module = File::Spec->catfile(@parts) . '.pm';
171 foreach my $dir (@INC) {
172 my $candidate = File::Spec->catfile($dir, $module);
173 next unless (-e $candidate && -f _ && -r _);
174 return $candidate;
175 }
176 return $file;
177}
178
179sub _make_plan {
5a0bc53a 180 return if $no_plan;
a83ae797 181 unless ($Test->has_plan) {
182 $Test->plan( 'no_plan' );
183 }
184 $Test->expected_tests;
185}
186
187sub _untaint {
188 my @untainted = map { ($_ =~ $UNTAINT_PATTERN) } @_;
189 return wantarray ? @untainted : $untainted[0];
190}
191
1921;
a83ae797 193
194=head1 SYNOPSIS
195
936fb8fb 196C<Test::EOL> lets you check for the presence of trailing whitespace and/or
197windows line endings in your perl code. It reports its results in standard
198C<Test::Simple> fashion:
a83ae797 199
200 use Test::EOL tests => 1;
29330dd1 201 eol_unix_ok( 'lib/Module.pm', 'Module is ^M free');
a83ae797 202
91613276 203and to add checks for trailing whitespace:
204
205 use Test::EOL tests => 1;
206 eol_unix_ok( 'lib/Module.pm', 'Module is ^M and trailing whitespace free', { trailing_whitespace => 1 });
207
a83ae797 208Module authors can include the following in a t/eol.t and have C<Test::EOL>
209automatically find and check all perl files in a module distribution:
210
211 use Test::EOL;
212 all_perl_files_ok();
213
214or
215
216 use Test::EOL;
217 all_perl_files_ok( @mydirs );
218
91613276 219and if authors would like to check for trailing whitespace:
220
221 use Test::EOL;
222 all_perl_files_ok({ trailing_whitespace => 1 });
223
224or
225
226 use Test::EOL;
227 all_perl_files_ok({ trailing_whitespace => 1 }, @mydirs );
228
5a0bc53a 229or
230
231 use Test::More;
232 use Test::EOL 'no_test';
233 all_perl_files_ok();
234 done_testing;
235
a83ae797 236=head1 DESCRIPTION
237
238This module scans your project/distribution for any perl files (scripts,
29330dd1 239modules, etc) for the presence of windows line endings.
a83ae797 240
241=head1 EXPORT
242
243A list of functions that can be exported. You can delete this section
244if you don't export anything, such as for a purely object-oriented module.
245
936fb8fb 246=func all_perl_files_ok
247
248 all_perl_files_ok( [ \%options ], [ @directories ] )
a83ae797 249
250Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
7713e104 251directories). If no <@directories> is given, the starting point is the current
252working directory, as tests are usually run from the top directory in a typical
b303ce9f 253CPAN distribution. A perl file is *.pl or *.pm or *.pod or *.t or a file starting
a83ae797 254with C<#!...perl>
255
936fb8fb 256Valid C<\%options> currently are:
257
258=over
259
260=item * trailing_whitespace
261
262By default Test::EOL only looks for Windows (CR/LF) line-endings. Set this
263to true to raise errors if any kind of trailing whitespace is present in
264the file.
265
266=item * all_reasons
267
268Normally Test::EOL reports only the first error in every file (given that
269a text file originated on Windows will fail every single line). Set this
270a true value to register a test failure for every line with an error.
271
272=back
273
a83ae797 274If the test plan is defined:
275
276 use Test::EOL tests => 3;
277 all_perl_files_ok();
278
279the total number of files tested must be specified.
280
936fb8fb 281=func eol_unix_ok
282
283 eol_unix_ok ( $file [, $text] [, \%options ] )
a83ae797 284
285Run a unix EOL check on C<$file>. For a module, the path (lib/My/Module.pm) or the
936fb8fb 286name (My::Module) can be both used. C<$text> is the diagnostic label emited after
287the C<ok>/C<not ok> TAP output. C<\%options> takes the same values as described in
288L</all_perl_files_ok>.
a83ae797 289
a83ae797 290=head1 ACKNOWLEDGEMENTS
291
292Shamelessly ripped off from L<Test::NoTabs>.
293
294=head1 SEE ALSO
295
296L<Test::More>, L<Test::Pod>. L<Test::Distribution>, L<Test:NoWarnings>,
297L<Test::NoTabs>, L<Module::Install::AuthorTests>.
298
a83ae797 299=cut