]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/tap/perl/Test/RRA.pm
Update to rra-c-util 5.4
[kerberos/krb5-strength.git] / tests / tap / perl / Test / RRA.pm
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 # 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/>.
11 #
12 # Written by Russ Allbery <eagle@eyrie.org>
13 # Copyright 2013, 2014
14 #     The Board of Trustees of the Leland Stanford Junior University
15 #
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:
22 #
23 # The above copyright notice and this permission notice shall be included in
24 # all copies or substantial portions of the Software.
25 #
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.
33
34 package Test::RRA;
35
36 use 5.006;
37 use strict;
38 use warnings;
39
40 use Exporter;
41 use Test::More;
42
43 # For Perl 5.006 compatibility.
44 ## no critic (ClassHierarchies::ProhibitExplicitISA)
45
46 # Declare variables that should be set in BEGIN for robustness.
47 our (@EXPORT_OK, @ISA, $VERSION);
48
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).
52 BEGIN {
53     @ISA       = qw(Exporter);
54     @EXPORT_OK = qw(skip_unless_author skip_unless_automated use_prereq);
55
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.
59     $VERSION = '5.04';
60 }
61
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.
65 #
66 # $description - Short description of the tests
67 #
68 # Returns: undef
69 sub skip_unless_author {
70     my ($description) = @_;
71     if (!$ENV{AUTHOR_TESTING}) {
72         plan skip_all => "$description only run for author";
73     }
74     return;
75 }
76
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.
82 #
83 # $description - Short description of the tests
84 #
85 # Returns: undef
86 sub skip_unless_automated {
87     my ($description) = @_;
88     for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) {
89         return if $ENV{$env};
90     }
91     plan skip_all => "$description normally skipped";
92     return;
93 }
94
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
98 # the program.
99 #
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.
102 #
103 # $module  - Name of the module to load
104 # @imports - Any arguments to import, possibly including a version
105 #
106 # Returns: undef
107 sub use_prereq {
108     my ($module, @imports) = @_;
109
110     # If the first import looks like a version, pass it as a bare string.
111     my $version = q{};
112     if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
113         $version = shift(@imports);
114     }
115
116     # Get caller information to put imports in the correct package.
117     my ($package) = caller;
118
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);
124     {
125         local $@            = undef;
126         local $!            = undef;
127         local $SIG{__DIE__} = undef;
128         $result = eval qq{
129             package $package;
130             use $module $version \@imports;
131             1;
132         };
133         $error = $@;
134         $sigdie = $SIG{__DIE__} || undef;
135     }
136
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";
141     }
142
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;
147     }
148     return;
149 }
150
151 1;
152 __END__
153
154 =for stopwords
155 Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
156 rra-c-util
157
158 =head1 NAME
159
160 Test::RRA - Support functions for Perl tests
161
162 =head1 SYNOPSIS
163
164     use Test::RRA
165       qw(skip_unless_author skip_unless_automated use_prereq);
166
167     # Skip this test unless author tests are requested.
168     skip_unless_author('Coding style tests');
169
170     # Skip this test unless doing automated or release testing.
171     skip_unless_automated('POD syntax tests');
172
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');
176
177 =head1 DESCRIPTION
178
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
182 same conventions.
183
184 =head1 FUNCTIONS
185
186 None of these functions are imported by default.  The ones used by a
187 script should be explicitly imported.
188
189 =over 4
190
191 =item skip_unless_author(DESC)
192
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.
197
198 =item skip_unless_automated(DESC)
199
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.
205
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.
208
209 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
210
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.
215
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
218 value of an array.
219
220 =back
221
222 =head1 AUTHOR
223
224 Russ Allbery <eagle@eyrie.org>
225
226 =head1 COPYRIGHT AND LICENSE
227
228 Copyright 2013, 2014 The Board of Trustees of the Leland Stanford Junior
229 University
230
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:
237
238 The above copyright notice and this permission notice shall be included in
239 all copies or substantial portions of the Software.
240
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.
248
249 =head1 SEE ALSO
250
251 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
252
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/>.
255
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>.
259
260 =cut