Add check for wrong number of newlines at the end of the file
[catagits/Test-EOL.git] / lib / Test / EOL.pm
index 860e427..accd162 100644 (file)
@@ -22,7 +22,6 @@ my %file_find_arg = ($] <= 5.006) ? () : (
 );
 
 my $Test  = Test::Builder->new;
-my $updir = File::Spec->updir();
 
 my $no_plan;
 
@@ -45,7 +44,7 @@ sub import {
 
 sub _all_perl_files {
     my @all_files = _all_files(@_);
-    return grep { _is_perl_module($_) || _is_perl_script($_) } @all_files;
+    return grep { _is_perl_module($_) || _is_perl_script($_) || _is_pod_file($_) } @all_files;
 }
 
 sub _all_files {
@@ -87,9 +86,11 @@ sub _show_whitespace {
 
 sub _debug_line {
     my ( $options, $line ) = @_;
-    $line->[2] =~ s/\n\z//g;
-    return "line $line->[1]: $line->[0] " . (
-      $options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] )  : q{}
+    $line->[2] =~ s/\n\z//g if defined $line->[2];
+    return "line $line->[1]: $line->[0]" . (
+        $options->{show_lines} && defined $line->[2]
+            ? qq{: } . _show_whitespace( $line->[2] )
+            : q{}
     );
 }
 
@@ -101,6 +102,7 @@ sub eol_unix_ok {
     my $options = shift if ref $_[0] eq 'HASH';
     $options ||= {
         trailing_whitespace => 0,
+        trailing_newline => 0,
         all_reasons => 0,
     };
     $file = _module_to_path($file);
@@ -109,16 +111,33 @@ sub eol_unix_ok {
     # Windows-- , default is :crlf, which hides \r\n  -_-
     binmode( $fh, ':raw' );
     my $line = 0;
-    my @fails;
+    my $blank_lines = 0;
+    my (@fails, %fails);
     while (<$fh>) {
         $line++;
-        if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
+        if ( /(\r+)$/ ) {
           my $match = $1;
-          push @fails, [ _show_whitespace( $match ) , $line , $_ ];
+          push @fails, [ _show_whitespace($match) , $line , $_ ]
+              if !$fails{eol}++ or $options->{all_reasons};
         }
-        if (  $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
+        if ( $options->{trailing_whitespace} && /([ \t]+)\r*$/ ) {
           my $match = $1;
-          push @fails, [ _show_whitespace($match), $line , $_ ];
+          push @fails, [ _show_whitespace($match), $line , $_ ]
+              if !$fails{tws}++ or $options->{all_reasons};
+        }
+        if ( /\A\s*\z/ ) {
+          $blank_lines++;
+        }
+        else {
+          $blank_lines = 0;
+        }
+        if ( $options->{trailing_newline} && eof ) {
+          push @fails, [ sprintf('%d blank line%s at end of file',
+                                 $blank_lines, $blank_lines > 1 ? 's' : ''),
+                         $line, undef ]
+            if $blank_lines;
+          push @fails, [ 'Missing "\n" at end of file', $line, undef ]
+            unless /\n\z/;
         }
         # Minor short-circuit for people who don't need the whole file scanned
         # once there's an err.
@@ -150,6 +169,10 @@ sub _is_perl_module {
     $_[0] =~ /\.pm$/i || $_[0] =~ /::/;
 }
 
+sub _is_pod_file {
+    $_[0] =~ /\.pod$/i;
+}
+
 sub _is_perl_script {
     my $file = shift;
     return 1 if $file =~ /\.pl$/i;
@@ -230,6 +253,17 @@ or
   all_perl_files_ok();
   done_testing;
 
+and if authors would like to check that there's a single newline and no
+blank lines at the end of each file:
+
+  use Test::EOL;
+  all_perl_files_ok({ trailing_newline => 1 });
+
+or
+
+  use Test::EOL;
+  all_perl_files_ok({ trailing_newline => 1 }, @mydirs );
+
 =head1 DESCRIPTION
 
 This module scans your project/distribution for any perl files (scripts,
@@ -245,9 +279,9 @@ if you don't export anything, such as for a purely object-oriented module.
   all_perl_files_ok( [ \%options ], [ @directories ] )
 
 Applies C<eol_unix_ok()> to all perl files found in C<@directories> (and sub
-directories). If no <@directories> is given, the starting point is one level
-above the current running script, that should cover all the files of a typical
-CPAN distribution. A perl file is *.pl or *.pm or *.t or a file starting
+directories). If no <@directories> is given, the starting point is the current
+working directory, as tests are usually run from the top directory in a typical
+CPAN distribution. A perl file is *.pl or *.pm or *.pod or *.t or a file starting
 with C<#!...perl>
 
 Valid C<\%options> currently are:
@@ -260,6 +294,11 @@ By default Test::EOL only looks for Windows (CR/LF) line-endings. Set this
 to true to raise errors if any kind of trailing whitespace is present in
 the file.
 
+=item * trailing_newline
+
+Set this to true to raise an error if the file doesn't end with exactly
+one newline and no blank lines.
+
 =item * all_reasons
 
 Normally Test::EOL reports only the first error in every file (given that