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