Add check for wrong number of newlines at the end of the file
[catagits/Test-EOL.git] / lib / Test / EOL.pm
index a1d7a70..accd162 100644 (file)
@@ -86,9 +86,11 @@ sub _show_whitespace {
 
 sub _debug_line {
     my ( $options, $line ) = @_;
-    $line->[2] =~ s/\n\z//g;
+    $line->[2] =~ s/\n\z//g if defined $line->[2];
     return "line $line->[1]: $line->[0]" . (
-      $options->{show_lines} ? qq{: } . _show_whitespace( $line->[2] )  : q{}
+        $options->{show_lines} && defined $line->[2]
+            ? qq{: } . _show_whitespace( $line->[2] )
+            : q{}
     );
 }
 
@@ -100,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);
@@ -108,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.
@@ -233,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,
@@ -263,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