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>
13 # Copyright 2013, 2014
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_author skip_unless_automated 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 author tests are requested. Takes a short description
63 # of what tests this script would perform, which is used in the skip message.
64 # Calls plan skip_all, which will terminate the program.
66 # $description - Short description of the tests
69 sub skip_unless_author {
70 my ($description) = @_;
71 if (!$ENV{AUTHOR_TESTING}) {
72 plan skip_all => "$description only run for author";
77 # Skip this test unless doing automated testing or release testing. This is
78 # used for tests that should be run by CPAN smoke testing or during releases,
79 # but not for manual installs by end users. Takes a short description of what
80 # tests this script would perform, which is used in the skip message. Calls
81 # plan skip_all, which will terminate the program.
83 # $description - Short description of the tests
86 sub skip_unless_automated {
87 my ($description) = @_;
88 for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) {
91 plan skip_all => "$description normally skipped";
95 # Attempt to load a module and skip the test if the module could not be
96 # loaded. If the module could be loaded, call its import function manually.
97 # If the module could not be loaded, calls plan skip_all, which will terminate
100 # The special logic here is based on Test::More and is required to get the
101 # imports to happen in the caller's namespace.
103 # $module - Name of the module to load
104 # @imports - Any arguments to import, possibly including a version
108 my ($module, @imports) = @_;
110 # If the first import looks like a version, pass it as a bare string.
112 if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
113 $version = shift(@imports);
116 # Get caller information to put imports in the correct package.
117 my ($package) = caller;
119 # Do the import with eval, and try to isolate it from the surrounding
120 # context as much as possible. Based heavily on Test::More::_eval.
121 ## no critic (BuiltinFunctions::ProhibitStringyEval)
122 ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines)
123 my ($result, $error, $sigdie);
127 local $SIG{__DIE__} = undef;
130 use $module $version \@imports;
134 $sigdie = $SIG{__DIE__} || undef;
137 # If the use failed for any reason, skip the test.
138 if (!$result || $error) {
139 my $name = length($version) > 0 ? "$module $version" : $module;
140 plan skip_all => "$name required for test";
143 # If the module set $SIG{__DIE__}, we cleared that via local. Restore it.
144 ## no critic (Variables::RequireLocalizedPunctuationVars)
145 if (defined($sigdie)) {
146 $SIG{__DIE__} = $sigdie;
155 Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
160 Test::RRA - Support functions for Perl tests
165 qw(skip_unless_author skip_unless_automated use_prereq);
167 # Skip this test unless author tests are requested.
168 skip_unless_author('Coding style tests');
170 # Skip this test unless doing automated or release testing.
171 skip_unless_automated('POD syntax tests');
173 # Load modules, skipping the test if they're not available.
174 use_prereq('Perl6::Slurp', 'slurp');
175 use_prereq('Test::Script::Run', '0.04');
179 This module collects utility functions that are useful for Perl test
180 scripts. It assumes Russ Allbery's Perl module layout and test
181 conventions and will only be useful for other people if they use the
186 None of these functions are imported by default. The ones used by a
187 script should be explicitly imported.
191 =item skip_unless_author(DESC)
193 Checks whether AUTHOR_TESTING is set in the environment and skips the
194 whole test (by calling C<plan skip_all> from Test::More) if it is not.
195 DESC is a description of the tests being skipped. A space and C<only run
196 for author> will be appended to it and used as the skip reason.
198 =item skip_unless_automated(DESC)
200 Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are
201 set in the environment and skips the whole test (by calling C<plan
202 skip_all> from Test::More) if they are not. This should be used by tests
203 that should not run during end-user installs of the module, but which
204 should run as part of CPAN smoke testing and release testing.
206 DESC is a description of the tests being skipped. A space and C<normally
207 skipped> will be appended to it and used as the skip reason.
209 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
211 Attempts to load MODULE with the given VERSION and import arguments. If
212 this fails for any reason, the test will be skipped (by calling C<plan
213 skip_all> from Test::More) with a skip reason saying that MODULE is
214 required for the test.
216 VERSION will be passed to C<use> as a version bareword if it looks like a
217 version number. The remaining IMPORT arguments will be passed as the
224 Russ Allbery <eagle@eyrie.org>
226 =head1 COPYRIGHT AND LICENSE
228 Copyright 2013, 2014 The Board of Trustees of the Leland Stanford Junior
231 Permission is hereby granted, free of charge, to any person obtaining a
232 copy of this software and associated documentation files (the "Software"),
233 to deal in the Software without restriction, including without limitation
234 the rights to use, copy, modify, merge, publish, distribute, sublicense,
235 and/or sell copies of the Software, and to permit persons to whom the
236 Software is furnished to do so, subject to the following conditions:
238 The above copyright notice and this permission notice shall be included in
239 all copies or substantial portions of the Software.
241 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
242 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
243 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
244 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
245 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
246 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
247 DEALINGS IN THE SOFTWARE.
251 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
253 This module is maintained in the rra-c-util package. The current version
254 is available from L<http://www.eyrie.org/~eagle/software/rra-c-util/>.
256 The functions to control when tests are run use environment variables
257 defined by the L<Lancaster
258 Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.