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 # The canonical version of this file is maintained in the rra-c-util package,
10 # which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
12 # Written by Russ Allbery <eagle@eyrie.org>
14 # The Board of Trustees of the Leland Stanford Junior University
16 # Permission is hereby granted, free of charge, to any person obtaining a
17 # copy of this software and associated documentation files (the "Software"),
18 # to deal in the Software without restriction, including without limitation
19 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 # and/or sell copies of the Software, and to permit persons to whom the
21 # Software is furnished to do so, subject to the following conditions:
23 # The above copyright notice and this permission notice shall be included in
24 # all copies or substantial portions of the Software.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 # DEALINGS IN THE SOFTWARE.
43 # For Perl 5.006 compatibility.
44 ## no critic (ClassHierarchies::ProhibitExplicitISA)
46 # Declare variables that should be set in BEGIN for robustness.
47 our (@EXPORT_OK, @ISA, $VERSION);
49 # Set $VERSION and everything export-related in a BEGIN block for robustness
50 # against circular module loading (not that we load any modules, but
51 # consistency is good).
54 @EXPORT_OK = qw(skip_unless_maintainer use_prereq);
56 # This version should match the corresponding rra-c-util release, but with
57 # two digits for the minor version, including a leading zero if necessary,
58 # so that it will sort properly.
62 # Skip this test unless maintainer tests are requested. Takes a short
63 # description of what tests this script would perform, which is used in the
64 # skip message. Calls plan skip_all, which will terminate the program.
66 # $description - Short description of the tests
69 sub skip_unless_maintainer {
70 my ($description) = @_;
71 if (!$ENV{RRA_MAINTAINER_TESTS}) {
72 plan skip_all => "$description only run for maintainer";
77 # Attempt to load a module and skip the test if the module could not be
78 # loaded. If the module could be loaded, call its import function manually.
79 # If the module could not be loaded, calls plan skip_all, which will terminate
82 # The special logic here is based on Test::More and is required to get the
83 # imports to happen in the caller's namespace.
85 # $module - Name of the module to load
86 # @imports - Any arguments to import, possibly including a version
90 my ($module, @imports) = @_;
92 # If the first import looks like a version, pass it as a bare string.
94 if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
95 $version = shift(@imports);
98 # Get caller information to put imports in the correct package.
99 my ($package) = caller;
101 # Do the import with eval, and try to isolate it from the surrounding
102 # context as much as possible. Based heavily on Test::More::_eval.
103 ## no critic (BuiltinFunctions::ProhibitStringyEval)
104 ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
105 my ($result, $error, $sigdie);
109 local $SIG{__DIE__} = undef;
112 use $module $version \@imports;
116 $sigdie = $SIG{__DIE__} || undef;
119 # If the use failed for any reason, skip the test.
120 if (!$result || $error) {
121 my $name = length($version) > 0 ? "$module $version" : $module;
122 plan skip_all => "$name required for test";
125 # If the module set $SIG{__DIE__}, we cleared that via local. Restore it.
126 ## no critic (Variables::RequireLocalizedPunctuationVars)
127 if (defined($sigdie)) {
128 $SIG{__DIE__} = $sigdie;
137 Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
142 Test::RRA - Support functions for Perl tests
146 use Test::RRA qw(skip_unless_maintainer use_prereq);
148 # Skip this test unless maintainer tests are requested.
149 skip_unless_maintainer('Coding style tests');
151 # Load modules, skipping the test if they're not available.
152 use_prereq('Perl6::Slurp', 'slurp');
153 use_prereq('Test::Script::Run', '0.04');
157 This module collects utility functions that are useful for Perl test
158 scripts. It assumes Russ Allbery's Perl module layout and test
159 conventions and will only be useful for other people if they use the
164 None of these functions are imported by default. The ones used by a
165 script should be explicitly imported.
169 =item skip_unless_maintainer(DESC)
171 Checks whether RRA_MAINTAINER_TESTS is set in the environment and skips
172 the whole test (by calling C<plan skip_all> from Test::More) if it is not.
173 DESC is a description of the tests being skipped. A space and C<only run
174 for maintainer> will be appended to it and used as the skip reason.
176 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
178 Attempts to load MODULE with the given VERSION and import arguments. If
179 this fails for any reason, the test will be skipped (by calling C<plan
180 skip_all> from Test::More) with a skip reason saying that MODULE is
181 required for the test.
183 VERSION will be passed to C<use> as a version bareword if it looks like a
184 version number. The remaining IMPORT arguments will be passed as the
191 Russ Allbery <eagle@eyrie.org>
193 =head1 COPYRIGHT AND LICENSE
195 Copyright 2013 The Board of Trustees of the Leland Stanford Junior
198 Permission is hereby granted, free of charge, to any person obtaining a
199 copy of this software and associated documentation files (the "Software"),
200 to deal in the Software without restriction, including without limitation
201 the rights to use, copy, modify, merge, publish, distribute, sublicense,
202 and/or sell copies of the Software, and to permit persons to whom the
203 Software is furnished to do so, subject to the following conditions:
205 The above copyright notice and this permission notice shall be included in
206 all copies or substantial portions of the Software.
208 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
209 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
210 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
211 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
212 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
213 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
214 DEALINGS IN THE SOFTWARE.
218 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
220 This module is maintained in the rra-c-util package. The current version
221 is available from L<http://www.eyrie.org/~eagle/software/rra-c-util/>.