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 is_file_contents(GOT, EXPECTED, MESSAGE)
243 Check a string against the contents of a file, showing the differences if any
244 using diff (if IPC::System::Simple and diff are available). GOT is the output
245 the test received. EXPECTED is the path to a file containing the expected
246 output (not the output itself). MESSAGE is a message to display alongside the
249 =item skip_unless_author(DESC)
251 Checks whether AUTHOR_TESTING is set in the environment and skips the whole
252 test (by calling C<plan skip_all> from Test::More) if it is not. DESC is a
253 description of the tests being skipped. A space and C<only run for author>
254 will be appended to it and used as the skip reason.
256 =item skip_unless_automated(DESC)
258 Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are set
259 in the environment and skips the whole test (by calling C<plan skip_all> from
260 Test::More) if they are not. This should be used by tests that should not run
261 during end-user installs of the module, but which should run as part of CPAN
262 smoke testing and release testing.
264 DESC is a description of the tests being skipped. A space and C<normally
265 skipped> will be appended to it and used as the skip reason.
267 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
269 Attempts to load MODULE with the given VERSION and import arguments. If this
270 fails for any reason, the test will be skipped (by calling C<plan skip_all>
271 from Test::More) with a skip reason saying that MODULE is required for the
274 VERSION will be passed to C<use> as a version bareword if it looks like a
275 version number. The remaining IMPORT arguments will be passed as the value of
282 Russ Allbery <eagle@eyrie.org>
284 =head1 COPYRIGHT AND LICENSE
286 Copyright 2016, 2018-2019, 2021 Russ Allbery <eagle@eyrie.org>
288 Copyright 2013-2014 The Board of Trustees of the Leland Stanford Junior
291 Permission is hereby granted, free of charge, to any person obtaining a copy
292 of this software and associated documentation files (the "Software"), to deal
293 in the Software without restriction, including without limitation the rights
294 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
295 copies of the Software, and to permit persons to whom the Software is
296 furnished to do so, subject to the following conditions:
298 The above copyright notice and this permission notice shall be included in all
299 copies or substantial portions of the Software.
301 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
302 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
303 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
304 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
305 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
306 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
311 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
313 This module is maintained in the rra-c-util package. The current version is
314 available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.
316 The functions to control when tests are run use environment variables defined
318 Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.
323 # copyright-at-end-flag: t