2 # ABSTRACT: Check the correct line endings in your project
12 use vars qw( $PERL $UNTAINT_PATTERN $PERL_PATTERN);
14 $PERL = $^X || 'perl';
15 $UNTAINT_PATTERN = qr|^([-+@\w./:\\]+)$|;
16 $PERL_PATTERN = qr/^#!.*perl/;
18 my %file_find_arg = ($] <= 5.006) ? () : (
20 untaint_pattern => $UNTAINT_PATTERN,
24 my $Test = Test::Builder->new;
25 my $updir = File::Spec->updir();
32 *{$caller.'::eol_unix_ok'} = \&eol_unix_ok;
33 *{$caller.'::all_perl_files_ok'} = \&all_perl_files_ok;
35 $Test->exported_to($caller);
40 my @all_files = _all_files(@_);
41 return grep { _is_perl_module($_) || _is_perl_script($_) } @all_files;
45 my @base_dirs = @_ ? @_ : File::Spec->catdir($Bin, $updir);
48 return if ($File::Find::dir =~ m![\\/]?CVS[\\/]|[\\/]?\.svn[\\/]!); # Filter out cvs or subversion dirs/
49 return if ($File::Find::dir =~ m![\\/]?blib[\\/]libdoc$!); # Filter out pod doc in dist
50 return if ($File::Find::dir =~ m![\\/]?blib[\\/]man\d$!); # Filter out pod doc in dist
51 return if ($File::Find::dir =~ m![\\/]?inc!); # Filter out Module::Install stuff
52 return if ($File::Find::name =~ m!Build$!i); # Filter out autogenerated Build script
53 return unless (-f $File::Find::name && -r _);
54 push @found, File::Spec->no_upwards( $File::Find::name );
61 find( $find_arg, @base_dirs);
65 # Formats various human invisible symbols
66 # to similar visible ones.
67 # Perhaps ^M or something like that
68 # would be more appropriate?
70 sub _show_whitespace {
72 $string =~ s/\r/[\\r]/g;
73 $string =~ s/\t/[\\t]/g;
74 $string =~ s/ /[\\s]/g;
78 # Format a line record for diagnostics.
81 my ( $options, $line ) = @_;
82 $line->[2] =~ s/\n\z//g;
83 return "line $line->[1]: $line->[0] " . (
84 $options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] ) : q{}
91 $test_txt = shift if !ref $_[0];
92 $test_txt ||= "No incorrect line endings in '$file'";
93 my $options = shift if ref $_[0] eq 'HASH';
95 trailing_whitespace => 0,
98 $file = _module_to_path($file);
100 open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
101 # Windows-- , default is :crlf, which hides \r\n -_-
102 binmode( $fh, ':raw' );
107 if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
109 push @fails, [ _show_whitespace( $match ) , $line , $_ ];
111 if ( $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
113 push @fails, [ _show_whitespace($match), $line , $_ ];
115 # Minor short-circuit for people who don't need the whole file scanned
116 # once there's an err.
117 last if( @fails > 0 && !$options->{all_reasons} );
120 $Test->ok( 0, $test_txt . " on " . _debug_line({ show_lines => 0 } , $fails[0] ) );
121 if ( $options->{all_reasons} || 1 ){
122 $Test->diag( " Problem Lines: ");
124 $Test->diag(_debug_line({ show_lines => 1 } , $_ ) );
129 $Test->ok(1, $test_txt);
132 sub all_perl_files_ok {
133 my $options = shift if ref $_[0] eq 'HASH';
134 my @files = _all_perl_files( @_ );
136 foreach my $file ( @files ) {
137 eol_unix_ok($file, $options);
141 sub _is_perl_module {
142 $_[0] =~ /\.pm$/i || $_[0] =~ /::/;
145 sub _is_perl_script {
147 return 1 if $file =~ /\.pl$/i;
148 return 1 if $file =~ /\.t$/;
149 open (my $fh, $file) or return;
151 return 1 if defined $first && ($first =~ $PERL_PATTERN);
155 sub _module_to_path {
157 return $file unless ($file =~ /::/);
158 my @parts = split /::/, $file;
159 my $module = File::Spec->catfile(@parts) . '.pm';
160 foreach my $dir (@INC) {
161 my $candidate = File::Spec->catfile($dir, $module);
162 next unless (-e $candidate && -f _ && -r _);
169 unless ($Test->has_plan) {
170 $Test->plan( 'no_plan' );
172 $Test->expected_tests;
176 my @untainted = map { ($_ =~ $UNTAINT_PATTERN) } @_;
177 return wantarray ? @untainted : $untainted[0];
184 C<Test::EOL> lets you check for the presence of trailing whitespace and/or
185 windows line endings in your perl code. It reports its results in standard
186 C<Test::Simple> fashion:
188 use Test::EOL tests => 1;
189 eol_unix_ok( 'lib/Module.pm', 'Module is ^M free');
191 and to add checks for trailing whitespace:
193 use Test::EOL tests => 1;
194 eol_unix_ok( 'lib/Module.pm', 'Module is ^M and trailing whitespace free', { trailing_whitespace => 1 });
196 Module authors can include the following in a t/eol.t and have C<Test::EOL>
197 automatically find and check all perl files in a module distribution:
205 all_perl_files_ok( @mydirs );
207 and if authors would like to check for trailing whitespace:
210 all_perl_files_ok({ trailing_whitespace => 1 });
215 all_perl_files_ok({ trailing_whitespace => 1 }, @mydirs );
219 This module scans your project/distribution for any perl files (scripts,
220 modules, etc) for the presence of windows line endings.
224 A list of functions that can be exported. You can delete this section
225 if you don't export anything, such as for a purely object-oriented module.
227 =func all_perl_files_ok
229 all_perl_files_ok( [ \%options ], [ @directories ] )
231 Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
232 directories). If no <@directories> is given, the starting point is one level
233 above the current running script, that should cover all the files of a typical
234 CPAN distribution. A perl file is *.pl or *.pm or *.t or a file starting
237 Valid C<\%options> currently are:
241 =item * trailing_whitespace
243 By default Test::EOL only looks for Windows (CR/LF) line-endings. Set this
244 to true to raise errors if any kind of trailing whitespace is present in
249 Normally Test::EOL reports only the first error in every file (given that
250 a text file originated on Windows will fail every single line). Set this
251 a true value to register a test failure for every line with an error.
255 If the test plan is defined:
257 use Test::EOL tests => 3;
260 the total number of files tested must be specified.
264 eol_unix_ok ( $file [, $text] [, \%options ] )
266 Run a unix EOL check on C<$file>. For a module, the path (lib/My/Module.pm) or the
267 name (My::Module) can be both used. C<$text> is the diagnostic label emited after
268 the C<ok>/C<not ok> TAP output. C<\%options> takes the same values as described in
269 L</all_perl_files_ok>.
271 =head1 ACKNOWLEDGEMENTS
273 Shamelessly ripped off from L<Test::NoTabs>.
277 L<Test::More>, L<Test::Pod>. L<Test::Distribution>, L<Test:NoWarnings>,
278 L<Test::NoTabs>, L<Module::Install::AuthorTests>.