- Update documentation for starting point change in version 1.5
- Check *.pod files as well as *.pm, *.pl and *.t (RT#82032)
+ - Add check for wrong number of newlines at the end of the file.
1.6 2015-07-30 21:51:00-07:00 America/Los_Angeles
- Add 'no_test' import option to allow more composability
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{}
);
}
my $options = shift if ref $_[0] eq 'HASH';
$options ||= {
trailing_whitespace => 0,
+ trailing_newline => 0,
all_reasons => 0,
};
$file = _module_to_path($file);
# 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.
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,
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
use Test::EOL;
use Cwd;
use File::Spec;
-all_perl_files_ok(File::Spec->catdir(cwd(), 'lib'), { trailing_whitespace => 1 });
+all_perl_files_ok(File::Spec->catdir(cwd(), 'lib'), {
+ trailing_whitespace => 1,
+ trailing_newline => 1,
+});
eol_unix_ok( $file3 );
my $file4 = make_file3();
-eol_unix_ok( $file3, { trailing_whitespace => 1 });
+eol_unix_ok( $file3, { trailing_whitespace => 1, trailing_newline => 1 });
unlink foreach ( $file1, $file2, $file3, $file4 );
'Trailing ws EOL found in tmp file 4' );
}
+{
+ my $dir = make_bad_file_5();
+ run_ok( "all_perl_files_ok({trailing_newline => 1}, '$dir' )",
+ qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 2: Missing "\n" at end of file/m,
+ 'Missing final newline found in tmp file 5' );
+}
+
+{
+ my $dir = make_bad_file_6();
+ run_ok( "all_perl_files_ok({trailing_newline => 1}, '$dir' )",
+ qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 2: 1 blank line at end of file/m,
+ 'Trailing blank line found in tmp file 6' );
+}
+
+{
+ my $dir = make_bad_file_7();
+ run_ok( "all_perl_files_ok({
+ trailing_whitespace => 1,
+ trailing_newline => 1,
+ all_reasons => 1,
+ }, '$dir' )",
+ qr/^not ok 1 - No incorrect line endings in '[^']*'.*^# line 2: \Q[\s]: [\s]\E.*^# line 2: 1 blank line at end of file.*^# line 2: Missing "\\n" at end of file/ms,
+ 'Trailing blank line found in tmp file 7' );
+}
+
sub run_ok {
my ($code, $match, $test_name) = @_;
my (undef, $file, $line) = caller;
return $tmpdir;
}
+sub make_bad_file_5 {
+ my $tmpdir = tempdir( CLEANUP => 1 );
+ my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
+ binmode $fh, ':raw';
+ print $fh "one line here\nno EOL here";
+ close $fh;
+ return $filename;
+}
+
+sub make_bad_file_6 {
+ my $tmpdir = tempdir( CLEANUP => 1 );
+ my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
+ binmode $fh, ':raw';
+ print $fh "blank line following here\n \n";
+ close $fh;
+ return $filename;
+}
+
+sub make_bad_file_7 {
+ my $tmpdir = tempdir( CLEANUP => 1 );
+ my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
+ binmode $fh, ':raw';
+ print $fh "blank trailing line without newline\n ";
+ close $fh;
+ return $filename;
+}
+