]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/tap/perl/Test/RRA.pm
Use Perl6::Slurp instead of File::Slurp
[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
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_maintainer 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 = '4.09';
60 }
61
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.
65 #
66 # $description - Short description of the tests
67 #
68 # Returns: undef
69 sub skip_unless_maintainer {
70     my ($description) = @_;
71     if (!$ENV{RRA_MAINTAINER_TESTS}) {
72         plan skip_all => "$description only run for maintainer";
73     }
74     return;
75 }
76
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
80 # the program.
81 #
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.
84 #
85 # $module  - Name of the module to load
86 # @imports - Any arguments to import, possibly including a version
87 #
88 # Returns: undef
89 sub use_prereq {
90     my ($module, @imports) = @_;
91
92     # If the first import looks like a version, pass it as a bare string.
93     my $version = q{};
94     if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) {
95         $version = shift(@imports);
96     }
97
98     # Get caller information to put imports in the correct package.
99     my ($package) = caller;
100
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);
106     {
107         local $@            = undef;
108         local $!            = undef;
109         local $SIG{__DIE__} = undef;
110         $result = eval qq{
111             package $package;
112             use $module $version \@imports;
113             1;
114         };
115         $error = $@;
116         $sigdie = $SIG{__DIE__} || undef;
117     }
118
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";
123     }
124
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;
129     }
130     return;
131 }
132
133 1;
134 __END__
135
136 =for stopwords
137 Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT
138 rra-c-util
139
140 =head1 NAME
141
142 Test::RRA - Support functions for Perl tests
143
144 =head1 SYNOPSIS
145
146     use Test::RRA qw(skip_unless_maintainer use_prereq);
147
148     # Skip this test unless maintainer tests are requested.
149     skip_unless_maintainer('Coding style tests');
150
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');
154
155 =head1 DESCRIPTION
156
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
160 same conventions.
161
162 =head1 FUNCTIONS
163
164 None of these functions are imported by default.  The ones used by a
165 script should be explicitly imported.
166
167 =over 4
168
169 =item skip_unless_maintainer(DESC)
170
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.
175
176 =item use_prereq(MODULE[, VERSION][, IMPORT ...])
177
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.
182
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
185 value of an array.
186
187 =back
188
189 =head1 AUTHOR
190
191 Russ Allbery <eagle@eyrie.org>
192
193 =head1 COPYRIGHT AND LICENSE
194
195 Copyright 2013 The Board of Trustees of the Leland Stanford Junior
196 University
197
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:
204
205 The above copyright notice and this permission notice shall be included in
206 all copies or substantial portions of the Software.
207
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.
215
216 =head1 SEE ALSO
217
218 Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
219
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/>.
222
223 =cut