1# Helper functions for test programs written in Perl.
2#
3# This module provides a collection of helper functions used by test programs
4# written in Perl.  This is a general collection of functions that can be used
5# by both C packages with Automake and by stand-alone Perl modules.  See
6# Test::RRA::Automake for additional functions specifically for C Automake
7# distributions.
8#
9# SPDX-License-Identifier: MIT
10
11package Test::RRA;
12
13use 5.008;
14use base qw(Exporter);
15use strict;
16use warnings;
17
18use Carp qw(croak);
19use File::Temp;
20
21# Abort if Test::More was loaded before Test::RRA to be sure that we get the
22# benefits of the Test::More probing below.
23if ($INC{'Test/More.pm'}) {
24    croak('Test::More loaded before Test::RRA');
25}
26
27# Red Hat's base perl package doesn't include Test::More (one has to install
28# the perl-core package in addition).  Try to detect this and skip any Perl
29# tests if Test::More is not present.  This relies on Test::RRA being included
30# before Test::More.
31eval {
32    require Test::More;
33    Test::More->import();
34};
35if ($@) {
36    print "1..0 # SKIP Test::More required for test\n"
37      or croak('Cannot write to stdout');
38    exit 0;
39}
40
41# Declare variables that should be set in BEGIN for robustness.
42our (@EXPORT_OK, $VERSION);
43
44# Set $VERSION and everything export-related in a BEGIN block for robustness
45# against circular module loading (not that we load any modules, but
46# consistency is good).
47BEGIN {
48    @EXPORT_OK = qw(
49      is_file_contents skip_unless_author skip_unless_automated use_prereq
50    );
51
52    # This version should match the corresponding rra-c-util release, but with
53    # two digits for the minor version, including a leading zero if necessary,
54    # so that it will sort properly.
55    $VERSION = '8.01';
56}
57
58# Compare a string to the contents of a file, similar to the standard is()
59# function, but to show the line-based unified diff between them if they
60# differ.
61#
62# $got      - The output that we received
63# $expected - The path to the file containing the expected output
64# $message  - The message to use when reporting the test results
65#
66# Returns: undef
67#  Throws: Exception on failure to read or write files or run diff
68sub is_file_contents {
69    my ($got, $expected, $message) = @_;
70
71    # If they're equal, this is simple.
72    open(my $fh, '<', $expected) or BAIL_OUT("Cannot open $expected: $!\n");
73    my $data = do { local $/ = undef; <$fh> };
74    close($fh) or BAIL_OUT("Cannot close $expected: $!\n");
75    if ($got eq $data) {
76        is($got, $data, $message);
77        return;
78    }
79
80    # Otherwise, we show a diff, but only if we have IPC::System::Simple and
81    # diff succeeds.  Otherwise, we fall back on showing the full expected and
82    # seen output.
83    eval {
84        require IPC::System::Simple;
85
86        my $tmp     = File::Temp->new();
87        my $tmpname = $tmp->filename;
88        print {$tmp} $got or BAIL_OUT("Cannot write to $tmpname: $!\n");
89        my @command = ('diff', '-u', $expected, $tmpname);
90        my $diff    = IPC::System::Simple::capturex([0 .. 1], @command);
91        diag($diff);
92    };
93    if ($@) {
94        diag('Expected:');
95        diag($expected);
96        diag('Seen:');
97        diag($data);
98    }
99
100    # Report failure.
101    ok(0, $message);
102    return;
103}
104
105# Skip this test unless author tests are requested.  Takes a short description
106# of what tests this script would perform, which is used in the skip message.
107# Calls plan skip_all, which will terminate the program.
108#
109# $description - Short description of the tests
110#
111# Returns: undef
112sub skip_unless_author {
113    my ($description) = @_;
114    if (!$ENV{AUTHOR_TESTING}) {
115        plan(skip_all => "$description only run for author");
116    }
117    return;
118}
119
120# Skip this test unless doing automated testing or release testing.  This is
121# used for tests that should be run by CPAN smoke testing or during releases,
122# but not for manual installs by end users.  Takes a short description of what
123# tests this script would perform, which is used in the skip message.  Calls
124# plan skip_all, which will terminate the program.
125#
126# $description - Short description of the tests
127#
128# Returns: undef
129sub skip_unless_automated {
130    my ($description) = @_;
131    for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) {
132        return if $ENV{$env};
133    }
134    plan(skip_all => "$description normally skipped");
135    return;
136}
137
138# Attempt to load a module and skip the test if the module could not be
139# loaded.  If the module could be loaded, call its import function manually.
140# If the module could not be loaded, calls plan skip_all, which will terminate
141# the program.
142#
143# The special logic here is based on Test::More and is required to get the
144# imports to happen in the caller's namespace.
145#
146# $module  - Name of the module to load
147# @imports - Any arguments to import, possibly including a version
148#
149# Returns: undef
150sub use_prereq {
151    my ($module, @imports) = @_;
152
153    # If the first import looks like a version, pass it as a bare string.
154    my $version = q{};
155    if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
156        $version = shift(@imports);
157    }
158
159    # Get caller information to put imports in the correct package.
160    my ($package) = caller;
161
162    # Do the import with eval, and try to isolate it from the surrounding
163    # context as much as possible.  Based heavily on Test::More::_eval.
164    ## no critic (BuiltinFunctions::ProhibitStringyEval)
165    ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
166    my ($result, $error, $sigdie);
167    {
168        local $@            = undef;
169        local $!            = undef;
170        local $SIG{__DIE__} = undef;
171        $result = eval qq{
172            package $package;
173            use $module $version \@imports;
174            1;
175        };
176        $error  = $@;
177        $sigdie = $SIG{__DIE__} || undef;
178    }
179
180    # If the use failed for any reason, skip the test.
181    if (!$result || $error) {
182        my $name = length($version) > 0 ? "$module $version" : $module;
183        plan(skip_all => "$name required for test");
184    }
185
186    # If the module set $SIG{__DIE__}, we cleared that via local.  Restore it.
187    ## no critic (Variables::RequireLocalizedPunctuationVars)
188    if (defined($sigdie)) {
189        $SIG{__DIE__} = $sigdie;
190    }
191    return;
192}
193
1941;
195__END__
196
197=for stopwords
198Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
199rra-c-util CPAN
200
201=head1 NAME
202
203Test::RRA - Support functions for Perl tests
204
205=head1 SYNOPSIS
206
207    use Test::RRA
208      qw(skip_unless_author skip_unless_automated use_prereq);
209
210    # Skip this test unless author tests are requested.
211    skip_unless_author('Coding style tests');
212
213    # Skip this test unless doing automated or release testing.
214    skip_unless_automated('POD syntax tests');
215
216    # Load modules, skipping the test if they're not available.
217    use_prereq('Perl6::Slurp', 'slurp');
218    use_prereq('Test::Script::Run', '0.04');
219
220=head1 DESCRIPTION
221
222This module collects utility functions that are useful for Perl test scripts.
223It assumes Russ Allbery's Perl module layout and test conventions and will
224only be useful for other people if they use the same conventions.
225
226This module B<must> be loaded before Test::More or it will abort during
227import.  It will skip the test (by printing a skip message to standard output
228and exiting with status 0, equivalent to C<plan skip_all>) during import if
229Test::More is not available.  This allows tests written in Perl using this
230module to be skipped if run on a system with Perl but not Test::More, such as
231Red Hat systems with the C<perl> package but not the C<perl-core> package
232installed.
233
234=head1 FUNCTIONS
235
236None of these functions are imported by default.  The ones used by a script
237should be explicitly imported.
238
239=over 4
240
241=item skip_unless_author(DESC)
242
243Checks whether AUTHOR_TESTING is set in the environment and skips the whole
244test (by calling C<plan skip_all> from Test::More) if it is not.  DESC is a
245description of the tests being skipped.  A space and C<only run for author>
246will be appended to it and used as the skip reason.
247
248=item skip_unless_automated(DESC)
249
250Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are set
251in the environment and skips the whole test (by calling C<plan skip_all> from
252Test::More) if they are not.  This should be used by tests that should not run
253during end-user installs of the module, but which should run as part of CPAN
254smoke testing and release testing.
255
256DESC is a description of the tests being skipped.  A space and C<normally
257skipped> will be appended to it and used as the skip reason.
258
259=item use_prereq(MODULE[, VERSION][, IMPORT ...])
260
261Attempts to load MODULE with the given VERSION and import arguments.  If this
262fails for any reason, the test will be skipped (by calling C<plan skip_all>
263from Test::More) with a skip reason saying that MODULE is required for the
264test.
265
266VERSION will be passed to C<use> as a version bareword if it looks like a
267version number.  The remaining IMPORT arguments will be passed as the value of
268an array.
269
270=back
271
272=head1 AUTHOR
273
274Russ Allbery <eagle@eyrie.org>
275
276=head1 COPYRIGHT AND LICENSE
277
278Copyright 2016, 2018-2019 Russ Allbery <eagle@eyrie.org>
279
280Copyright 2013-2014 The Board of Trustees of the Leland Stanford Junior
281University
282
283Permission is hereby granted, free of charge, to any person obtaining a copy
284of this software and associated documentation files (the "Software"), to deal
285in the Software without restriction, including without limitation the rights
286to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
287copies of the Software, and to permit persons to whom the Software is
288furnished to do so, subject to the following conditions:
289
290The above copyright notice and this permission notice shall be included in all
291copies or substantial portions of the Software.
292
293THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
294IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
295FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
296AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
297LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
298OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
299SOFTWARE.
300
301=head1 SEE ALSO
302
303Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
304
305This module is maintained in the rra-c-util package.  The current version is
306available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.
307
308The functions to control when tests are run use environment variables defined
309by the L<Lancaster
310Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.
311
312=cut
313
314# Local Variables:
315# copyright-at-end-flag: t
316# End:
317