diff options
Diffstat (limited to 'tests/tap/perl/Test/RRA.pm')
| -rw-r--r-- | tests/tap/perl/Test/RRA.pm | 57 | 
1 files changed, 54 insertions, 3 deletions
diff --git a/tests/tap/perl/Test/RRA.pm b/tests/tap/perl/Test/RRA.pm index 8608e31..807508c 100644 --- a/tests/tap/perl/Test/RRA.pm +++ b/tests/tap/perl/Test/RRA.pm @@ -5,6 +5,8 @@  # by both C packages with Automake and by stand-alone Perl modules.  See  # Test::RRA::Automake for additional functions specifically for C Automake  # distributions. +# +# SPDX-License-Identifier: MIT  package Test::RRA; @@ -13,6 +15,7 @@ use strict;  use warnings;  use Exporter; +use File::Temp;  use Test::More;  # For Perl 5.006 compatibility. @@ -26,12 +29,56 @@ our (@EXPORT_OK, @ISA, $VERSION);  # consistency is good).  BEGIN {      @ISA       = qw(Exporter); -    @EXPORT_OK = qw(skip_unless_author skip_unless_automated use_prereq); +    @EXPORT_OK = qw( +      is_file_contents skip_unless_author skip_unless_automated use_prereq +    );      # This version should match the corresponding rra-c-util release, but with      # two digits for the minor version, including a leading zero if necessary,      # so that it will sort properly. -    $VERSION = '5.10'; +    $VERSION = '7.02'; +} + +# Compare a string to the contents of a file, similar to the standard is() +# function, but to show the line-based unified diff between them if they +# differ. +# +# $got      - The output that we received +# $expected - The path to the file containing the expected output +# $message  - The message to use when reporting the test results +# +# Returns: undef +#  Throws: Exception on failure to read or write files or run diff +sub is_file_contents { +    my ($got, $expected, $message) = @_; + +    # If they're equal, this is simple. +    open(my $fh, '<', $expected) or BAIL_OUT("Cannot open $expected: $!\n"); +    my $data = do { local $/ = undef; <$fh> }; +    close($fh) or BAIL_OUT("Cannot close $expected: $!\n"); +    if ($got eq $data) { +        is($got, $data, $message); +        return; +    } + +    # Otherwise, we show a diff, but only if we have IPC::System::Simple. +    eval { require IPC::System::Simple }; +    if ($@) { +        ok(0, $message); +        return; +    } + +    # They're not equal.  Write out what we got so that we can run diff. +    my $tmp     = File::Temp->new(); +    my $tmpname = $tmp->filename; +    print {$tmp} $got or BAIL_OUT("Cannot write to $tmpname: $!\n"); +    my @command = ('diff', '-u', $expected, $tmpname); +    my $diff = IPC::System::Simple::capturex([0 .. 1], @command); +    diag($diff); + +    # Remove the temporary file and report failure. +    ok(0, $message); +    return;  }  # Skip this test unless author tests are requested.  Takes a short description @@ -225,10 +272,14 @@ SOFTWARE.  Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)  This module is maintained in the rra-c-util package.  The current version is -available from L<http://www.eyrie.org/~eagle/software/rra-c-util/>. +available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.  The functions to control when tests are run use environment variables defined  by the L<Lancaster  Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.  =cut + +# Local Variables: +# copyright-at-end-flag: t +# End:  | 
