1 # Helper functions for test programs written in Perl.
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
9 # SPDX-License-Identifier: MIT
14 use base qw(Exporter);
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.
23 if ($INC{'Test/More.pm'}) {
24 croak('Test::More loaded before Test::RRA');
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
36 print "1..0 # SKIP Test::More required for test\n"
37 or croak('Cannot write to stdout');
41 # Declare variables that should be set in BEGIN for robustness.
42 our (@EXPORT_OK, $VERSION);
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).
49 is_file_contents skip_unless_author skip_unless_automated use_prereq
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.
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
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
67 # Throws: Exception on failure to read or write files or run diff
68 sub is_file_contents {
69 my ($got, $expected, $message) = @_;
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");
76 is($got, $data, $message);
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
84 require IPC::System::Simple;
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);
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.
109 # $description - Short description of the tests
112 sub skip_unless_author {
113 my ($description) = @_;
114 if (!$ENV{AUTHOR_TESTING}) {
115 plan(skip_all => "$description only run for author");
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.
126 # $description - Short description of the tests
129 sub skip_unless_automated {
130 my ($description) = @_;
131 for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) {
132 return if $ENV{$env};
134 plan(skip_all => "$description normally skipped");
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
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.
146 # $module - Name of the module to load
147 # @imports - Any arguments to import, possibly including a version
151 my ($module, @imports) = @_;
153 # If the first import looks like a version, pass it as a bare string.
155 if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
156 $version = shift(@imports);
159 # Get caller information to put imports in the correct package.
160 my ($package) = caller;
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);
170 local $SIG{__DIE__} = undef;
173 use $module $version \@imports;
177 $sigdie = $SIG{__DIE__} || undef;
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");
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;
198 Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
203 Test::RRA - Support functions for Perl tests
208 qw(skip_unless_author skip_unless_automated use_prereq);
210 # Skip this test unless author tests are requested.
211 skip_unless_author('Coding style tests');
213 # Skip this test unless doing automated or release testing.
214 skip_unless_automated('POD syntax tests');
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');
222 This module collects utility functions that are useful for Perl test scripts.
223 It assumes Russ Allbery's Perl module layout and test conventions and will
224 only be useful for other people if they use the same conventions.
226 This module B<must> be loaded before Test::More or it will abort during
227 import. It will skip the test (by printing a skip message to standard output
228 and exiting with status 0, equivalent to C<plan skip_all>) during import if
229 Test::More is not available. This allows tests written in Perl using this
230 module to be skipped if run on a system with Perl but not Test::More, such as
231 Red Hat systems with the C<perl> package but not the C<perl-core> package
236 None of these functions are imported by default. The ones used by a script
237 should be explicitly imported.
241 =item skip_unless_author(DESC)
243 Checks whether AUTHOR_TESTING is set in the environment and skips the whole
244 test (by calling C<plan skip_all> from Test::More) if it is not. DESC is a
245 description of the tests being skipped. A space and C<only run for author>
246 will be appended to it and used as the skip reason.
248 =item skip_unless_automated(DESC)
250 Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are set
251 in the environment and skips the whole test (by calling C<plan skip_all> from
252 Test::More) if they are not. This should be used by tests that should not run
253 during end-user installs of the module, but which should run as part of CPAN
254 smoke testing and release testing.
256 DESC is a description of the tests being skipped. A space and C<normally
257 skipped> will be appended to it and used as the skip reason.
259 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
261 Attempts to load MODULE with the given VERSION and import arguments. If this
262 fails for any reason, the test will be skipped (by calling C<plan skip_all>
263 from Test::More) with a skip reason saying that MODULE is required for the
266 VERSION will be passed to C<use> as a version bareword if it looks like a
267 version number. The remaining IMPORT arguments will be passed as the value of
274 Russ Allbery <eagle@eyrie.org>
276 =head1 COPYRIGHT AND LICENSE
278 Copyright 2016, 2018-2019 Russ Allbery <eagle@eyrie.org>
280 Copyright 2013-2014 The Board of Trustees of the Leland Stanford Junior
283 Permission is hereby granted, free of charge, to any person obtaining a copy
284 of this software and associated documentation files (the "Software"), to deal
285 in the Software without restriction, including without limitation the rights
286 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
287 copies of the Software, and to permit persons to whom the Software is
288 furnished to do so, subject to the following conditions:
290 The above copyright notice and this permission notice shall be included in all
291 copies or substantial portions of the Software.
293 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
294 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
295 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
296 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
297 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
298 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
303 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
305 This module is maintained in the rra-c-util package. The current version is
306 available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.
308 The functions to control when tests are run use environment variables defined
310 Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.
315 # copyright-at-end-flag: t