Massive Raft full of chnages. Hopefully, this is the exact right fix.
Kent Fredric [Wed, 9 Jun 2010 06:58:37 +0000 (18:58 +1200)]
Win32 Test hackery attempt

Changes
lib/Test/EOL.pm
t/12-fail.t

diff --git a/Changes b/Changes
index e8304e1..369d875 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,12 @@
 Revision history for Test-EOL
 
+    ( Kent Fredric )
+    - Use binmode :raw for input/output. Solves win32 translating
+      the \r\n character into \n's silently during input. 
+    - Add an ( currently undocumeted ) all_reasons option to show every line
+      that is broken.
+    - Add visualising of invisible characters that match the regex.
+
 0.7  2010-03-03
     - Deal correctly with -I includes paths that include spaces in the
       tests to stop them unexpectedly failing.
index 2a04267..3976a44 100644 (file)
@@ -62,6 +62,29 @@ sub _all_files {
     return @found;
 }
 
+# Formats various human invisible symbols
+# to similar visible ones.
+# Perhaps ^M or something like that 
+# would be more appropriate?
+
+sub _show_whitespace { 
+    my $string = shift;
+    $string =~ s/\r/[\\r]/g;
+    $string =~ s/\t/[\\t]/g;
+    $string =~ s/ /[\\s]/g;
+    return $string;
+}
+
+# Format a line record for diagnostics.
+
+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{} 
+    );
+}
+
 sub eol_unix_ok {
     my $file = shift;
     my $test_txt;
@@ -70,19 +93,38 @@ sub eol_unix_ok {
     my $options = shift if ref $_[0] eq 'HASH';
     $options ||= {
         trailing_whitespace => 0,
+        all_reasons => 0,
     };
     $file = _module_to_path($file);
+    
     open my $fh, $file or do { $Test->ok(0, $test_txt); $Test->diag("Could not open $file: $!"); return; };
+    # Windows-- , default is :crlf, which hides \r\n  -_-
+    binmode( $fh, ':raw:utf8' );
     my $line = 0;
+    my @fails;
     while (<$fh>) {
         $line++;
-        if (
-           (!$options->{trailing_whitespace} && /\r$/) ||
-           ( $options->{trailing_whitespace} && /(\r|[ \t]+)$/)
-        ) {
-          $Test->ok(0, $test_txt . " on line $line");
-          return 0;
+        if ( !$options->{trailing_whitespace} && /(\r+)$/ ) {
+          my $match = $1;
+          push @fails, [ _show_whitespace( $match ) , $line , $_ ];
+        }
+        if (  $options->{trailing_whitespace} && /([ \t]*\r+|[ \t]+)$/ ) {
+          my $match = $1;
+          push @fails, [ _show_whitespace($match), $line , $_ ];
         }
+        # Minor short-circuit for people who don't need the whole file scanned
+        # once there's an err.
+        last if( @fails > 0 && !$options->{all_reasons} );
+    }
+    if( @fails ){ 
+       $Test->ok( 0, $test_txt . " on "  . _debug_line({ show_lines => 0 } , $fails[0]  )  );
+       if ( $options->{all_reasons} || 1 ){
+          $Test->diag( "  Problem Lines: ");
+          for ( @fails ){ 
+            $Test->diag(_debug_line({ show_lines => 1 } , $_ ) );
+          }
+       }
+       return 0;
     }
     $Test->ok(1, $test_txt);
     return 1;
index 9d7a38b..eef773b 100644 (file)
@@ -3,12 +3,20 @@ use strict;
 use Test::More qw(no_plan);
 
 use File::Temp qw( tempdir tempfile );
-
 my $perl  = $^X || 'perl';
 my $inc = join(' -I ', map { qq{"$_"} } @INC) || '';
 $inc = "-I $inc" if $inc;
 
 {
+    my ( $dir, $filename ) = make_raw_badfile();
+    local $/ = undef;
+    open my $fh, '<', $filename or die $!;
+    binmode( $fh, ':raw' );
+    my $content = <$fh>;
+    is( $content, ascii_string(), 'Data written to file is there when we look for it later' ); 
+
+}
+{
     my $dir = make_bad_file_1();
     my (undef, $outfile) = tempfile();
     ok( `$perl $inc -MTest::EOL -e "all_perl_files_ok( '$dir' )" 2>&1 > $outfile` );
@@ -50,22 +58,41 @@ $inc = "-I $inc" if $inc;
     unlink $outfile;
 }
 
+sub ascii_string { 
+  my $o = "<before \r\n between \r\n after \n normal >";
+  return $o x 3;
+}
+
+sub make_raw_badfile { 
+  my $tmpdir = tempdir( CLEANUP => 1 ); 
+  my ( $fh, $filename ) = tempfile( DIR => $tmpdir, SUFFIX =>  '.tXt' ); 
+  binmode $fh, ':raw:utf8';
+  print $fh ascii_string();
+  close $fh;
+  return ( $tmpdir, $filename );
+}
+
+
 sub make_bad_file_1 {
   my $tmpdir = tempdir( CLEANUP => 1 );
   my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
-  print $fh <<"DUMMY";
+  binmode $fh, ':raw:utf8';
+  my $str = <<"DUMMY";
 #!perl
 
 sub main {
     print "Hello!\r\n";
 }
 DUMMY
+  print $fh $str;
+
   return $tmpdir;
 }
 
 sub make_bad_file_2 {
   my $tmpdir = tempdir( CLEANUP => 1 );
   my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
+  binmode $fh, ':raw:utf8';
   print $fh <<"DUMMY";
 #!perl
 
@@ -87,6 +114,7 @@ DUMMY
 sub make_bad_file_3 {
   my $tmpdir = tempdir( CLEANUP => 1 );
   my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pm' );
+  binmode $fh, ':raw:utf8';
   print $fh <<"DUMMY";
 use strict;\r
 \r
@@ -108,6 +136,7 @@ DUMMY
 sub make_bad_file_4 {
   my $tmpdir = tempdir( CLEANUP => 1 );
   my ($fh, $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.pL' );
+  binmode $fh, ':raw:utf8';
   print $fh <<"DUMMY";
 #!perl