3 use Test::More qw(no_plan);
5 use File::Temp qw( tempdir tempfile );
6 my $perl = $^X || 'perl';
7 my $inc = join(' -I ', map { qq{"$_"} } @INC) || '';
8 $inc = "-I $inc" if $inc;
11 my ( $dir, $filename ) = make_raw_badfile();
13 open my $fh, '<', $filename or die $!;
14 binmode( $fh, ':raw' );
16 is( $content, ascii_string(), 'Data written to file is there when we look for it later' );
20 my $dir = make_bad_file_1();
21 run_ok( "all_perl_files_ok( '$dir' )",
22 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 5: [\r]/m,
23 'windows EOL found in tmp file 1' );
26 my $dir = make_bad_file_2();
27 run_ok( "all_perl_files_ok( '$dir' )",
28 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 8: [\r][\r][\r][\r][\r][\r][\r]/m,
29 'windows EOL found in tmp file2 ' );
32 my ($dir, $file) = make_bad_file_3();
33 run_ok( "all_perl_files_ok( '$file' )",
34 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 1: [\r]/m,
35 'windows EOL found in tmp file 3' );
39 my $dir = make_bad_file_4();
40 run_ok( "all_perl_files_ok({trailing_whitespace => 1}, '$dir' )",
41 # Note that line number will be 13
42 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 12: [\s][\t][\s][\s]/m,
43 'Trailing ws EOL found in tmp file 4' );
47 my $dir = make_bad_file_5();
48 run_ok( "all_perl_files_ok({trailing_newline => 1}, '$dir' )",
49 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 2: Missing "\n" at end of file/m,
50 'Missing final newline found in tmp file 5' );
54 my $dir = make_bad_file_6();
55 run_ok( "all_perl_files_ok({trailing_newline => 1}, '$dir' )",
56 qr/^not ok 1 - No incorrect line endings in '[^']*' \Qon line 2: 1 blank line at end of file/m,
57 'Trailing blank line found in tmp file 6' );
61 my $dir = make_bad_file_7();
62 run_ok( "all_perl_files_ok({
63 trailing_whitespace => 1,
64 trailing_newline => 1,
67 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,
68 'Trailing blank line found in tmp file 7' );
72 my ($code, $match, $test_name) = @_;
73 my (undef, $file, $line) = caller;
74 die "code containing double quotes is not portable on Win32 in one-liners at $file $line.\n" if $code =~ /"/;
75 local $Test::Builder::Level = $Test::Builder::Level + 1;
76 my (undef, $outfile) = tempfile();
77 is( `$perl $inc -MTest::EOL -e "$code" > $outfile 2>&1`, '', "test sub program: output redirected" );
78 is( $? >> 8, 1, "test sub program: exit code is 1" );
80 open my $fh, '<', $outfile or die "Can't open $outfile: $!";
82 close $fh or die "Can't close $outfile: $!";
83 like( $content, $match, $test_name );
88 my $o = "<before \r\n between \r\n after \n normal >";
92 sub make_raw_badfile {
93 my $tmpdir = tempdir( CLEANUP => 1 );
94 my ( $fh, $filename ) = tempfile( DIR => $tmpdir, SUFFIX => '.tXt' );
96 print $fh ascii_string();
98 return ( $tmpdir, $filename );
102 sub make_bad_file_1 {
103 my $tmpdir = tempdir( CLEANUP => 1 );
104 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
110 # Note that the generated file will have the CRLF expanded in the source
119 sub make_bad_file_2 {
120 my $tmpdir = tempdir( CLEANUP => 1 );
121 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
130 test.pL - A test script
141 sub make_bad_file_3 {
142 my $tmpdir = tempdir( CLEANUP => 1 );
143 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pm' );
151 my (\$class) = \@_;\r
152 my \$self = bless { }, \$class;\r
160 return ($tmpdir, $filename);
163 sub make_bad_file_4 {
164 my $tmpdir = tempdir( CLEANUP => 1 );
165 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
174 test.pL - A test script
181 print $fh qq{ print "Hello!\\n"; \t \n}; # <-- whitespace
187 sub make_bad_file_5 {
188 my $tmpdir = tempdir( CLEANUP => 1 );
189 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
191 print $fh "one line here\nno EOL here";
196 sub make_bad_file_6 {
197 my $tmpdir = tempdir( CLEANUP => 1 );
198 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
200 print $fh "blank line following here\n \n";
205 sub make_bad_file_7 {
206 my $tmpdir = tempdir( CLEANUP => 1 );
207 my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
209 print $fh "blank trailing line without newline\n ";