From cf933c7348e3836d4869bdabd5bfe8552a688210 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 20 Jun 2020 17:44:28 -0700 Subject: [PATCH] show first file difference when --assert-tidy causes an error --- lib/Perl/Tidy.pm | 79 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 73ab3d43..363156cd 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1066,8 +1066,10 @@ EOM } # MD5 sum of input file is evaluated before any prefilter + my $saved_input_buf; if ( $rOpts->{'assert-tidy'} || $rOpts->{'assert-untidy'} ) { - $digest_input = $md5_hex->($buf); + $digest_input = $md5_hex->($buf); + $saved_input_buf = $buf; } # Prefilters and postfilters: The prefilter is a code reference @@ -1462,9 +1464,13 @@ EOM if ( $rOpts->{'assert-tidy'} ) { my $digest_output = $md5_hex->($buf); if ( $digest_output ne $digest_input ) { - $logger_object->warning( -"assertion failure: '--assert-tidy' is set but output differs from input\n" - ); + my $diff_msg = + compare_string_buffers( $saved_input_buf, $buf, + $is_encoded_data ); + $logger_object->warning(<{'assert-untidy'} ) { @@ -1696,6 +1702,71 @@ EOM return 1; } # end of main program perltidy +sub compare_string_buffers { + + # Compare input and output string buffers and return a brief text + # description of the first difference. + my ( $bufi, $bufo, $is_encoded_data ) = @_; + + my $leni = length($bufi); + my $leno = length($bufo); + my $msg = + "Input file length is $leni chars, output file length is $leno chars\n"; + return $msg unless $leni && $leno; + + my ( $fhi, $fnamei ) = streamhandle( \$bufi, 'r', $is_encoded_data ); + my ( $fho, $fnameo ) = streamhandle( \$bufo, 'r', $is_encoded_data ); + return $msg unless ( $fho && $fhi ); # for safety, shouldn't happen + my ( $linei, $lineo ); + my ( $counti, $counto ) = ( 0, 0 ); + while (1) { + my $last_common_line = $linei; + $linei = $fhi->getline(); + $lineo = $fho->getline(); + + # compare chomp'ed lines + if ( defined($linei) ) { $counti++; chomp $linei } + if ( defined($lineo) ) { $counto++; chomp $lineo } + + # see if one or both ended before a difference + last unless ( $counti == $counto ); + + if ( $linei ne $lineo ) { + $msg .= <$counto:$lineo +EOM + return $msg; + } + } + + if ( $counti > $counto ) { + $msg .= <