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 9package Test::RRA; 10 11use 5.006; 12use strict; 13use warnings; 14 15use Exporter; 16use Test::More; 17 18# For Perl 5.006 compatibility. 19## no critic (ClassHierarchies::ProhibitExplicitISA) 20 21# Declare variables that should be set in BEGIN for robustness. 22our (@EXPORT_OK, @ISA, $VERSION); 23 24# Set $VERSION and everything export-related in a BEGIN block for robustness 25# against circular module loading (not that we load any modules, but 26# consistency is good). 27BEGIN { 28 @ISA = qw(Exporter); 29 @EXPORT_OK = qw(skip_unless_author skip_unless_automated use_prereq); 30 31 # This version should match the corresponding rra-c-util release, but with 32 # two digits for the minor version, including a leading zero if necessary, 33 # so that it will sort properly. 34 $VERSION = '5.09'; 35} 36 37# Skip this test unless author tests are requested. Takes a short description 38# of what tests this script would perform, which is used in the skip message. 39# Calls plan skip_all, which will terminate the program. 40# 41# $description - Short description of the tests 42# 43# Returns: undef 44sub skip_unless_author { 45 my ($description) = @_; 46 if (!$ENV{AUTHOR_TESTING}) { 47 plan skip_all => "$description only run for author"; 48 } 49 return; 50} 51 52# Skip this test unless doing automated testing or release testing. This is 53# used for tests that should be run by CPAN smoke testing or during releases, 54# but not for manual installs by end users. Takes a short description of what 55# tests this script would perform, which is used in the skip message. Calls 56# plan skip_all, which will terminate the program. 57# 58# $description - Short description of the tests 59# 60# Returns: undef 61sub skip_unless_automated { 62 my ($description) = @_; 63 for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) { 64 return if $ENV{$env}; 65 } 66 plan skip_all => "$description normally skipped"; 67 return; 68} 69 70# Attempt to load a module and skip the test if the module could not be 71# loaded. If the module could be loaded, call its import function manually. 72# If the module could not be loaded, calls plan skip_all, which will terminate 73# the program. 74# 75# The special logic here is based on Test::More and is required to get the 76# imports to happen in the caller's namespace. 77# 78# $module - Name of the module to load 79# @imports - Any arguments to import, possibly including a version 80# 81# Returns: undef 82sub use_prereq { 83 my ($module, @imports) = @_; 84 85 # If the first import looks like a version, pass it as a bare string. 86 my $version = q{}; 87 if (@imports >= 1 && $imports[0] =~ m{ \A \d+ (?: [.][\d_]+ )* \z }xms) { 88 $version = shift(@imports); 89 } 90 91 # Get caller information to put imports in the correct package. 92 my ($package) = caller; 93 94 # Do the import with eval, and try to isolate it from the surrounding 95 # context as much as possible. Based heavily on Test::More::_eval. 96 ## no critic (BuiltinFunctions::ProhibitStringyEval) 97 ## no critic (ValuesAndExpressions::ProhibitImplicitNewlines) 98 my ($result, $error, $sigdie); 99 { 100 local $@ = undef; 101 local $! = undef; 102 local $SIG{__DIE__} = undef; 103 $result = eval qq{ 104 package $package; 105 use $module $version \@imports; 106 1; 107 }; 108 $error = $@; 109 $sigdie = $SIG{__DIE__} || undef; 110 } 111 112 # If the use failed for any reason, skip the test. 113 if (!$result || $error) { 114 my $name = length($version) > 0 ? "$module $version" : $module; 115 plan skip_all => "$name required for test"; 116 } 117 118 # If the module set $SIG{__DIE__}, we cleared that via local. Restore it. 119 ## no critic (Variables::RequireLocalizedPunctuationVars) 120 if (defined($sigdie)) { 121 $SIG{__DIE__} = $sigdie; 122 } 123 return; 124} 125 1261; 127__END__ 128 129=for stopwords 130Allbery Allbery's DESC bareword sublicense MERCHANTABILITY NONINFRINGEMENT 131rra-c-util CPAN 132 133=head1 NAME 134 135Test::RRA - Support functions for Perl tests 136 137=head1 SYNOPSIS 138 139 use Test::RRA 140 qw(skip_unless_author skip_unless_automated use_prereq); 141 142 # Skip this test unless author tests are requested. 143 skip_unless_author('Coding style tests'); 144 145 # Skip this test unless doing automated or release testing. 146 skip_unless_automated('POD syntax tests'); 147 148 # Load modules, skipping the test if they're not available. 149 use_prereq('Perl6::Slurp', 'slurp'); 150 use_prereq('Test::Script::Run', '0.04'); 151 152=head1 DESCRIPTION 153 154This module collects utility functions that are useful for Perl test scripts. 155It assumes Russ Allbery's Perl module layout and test conventions and will 156only be useful for other people if they use the same conventions. 157 158=head1 FUNCTIONS 159 160None of these functions are imported by default. The ones used by a script 161should be explicitly imported. 162 163=over 4 164 165=item skip_unless_author(DESC) 166 167Checks whether AUTHOR_TESTING is set in the environment and skips the whole 168test (by calling C<plan skip_all> from Test::More) if it is not. DESC is a 169description of the tests being skipped. A space and C<only run for author> 170will be appended to it and used as the skip reason. 171 172=item skip_unless_automated(DESC) 173 174Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are set 175in the environment and skips the whole test (by calling C<plan skip_all> from 176Test::More) if they are not. This should be used by tests that should not run 177during end-user installs of the module, but which should run as part of CPAN 178smoke testing and release testing. 179 180DESC is a description of the tests being skipped. A space and C<normally 181skipped> will be appended to it and used as the skip reason. 182 183=item use_prereq(MODULE[, VERSION][, IMPORT ...]) 184 185Attempts to load MODULE with the given VERSION and import arguments. If this 186fails for any reason, the test will be skipped (by calling C<plan skip_all> 187from Test::More) with a skip reason saying that MODULE is required for the 188test. 189 190VERSION will be passed to C<use> as a version bareword if it looks like a 191version number. The remaining IMPORT arguments will be passed as the value of 192an array. 193 194=back 195 196=head1 AUTHOR 197 198Russ Allbery <eagle@eyrie.org> 199 200=head1 COPYRIGHT AND LICENSE 201 202Copyright 2013, 2014 The Board of Trustees of the Leland Stanford Junior 203University 204 205Permission is hereby granted, free of charge, to any person obtaining a copy 206of this software and associated documentation files (the "Software"), to deal 207in the Software without restriction, including without limitation the rights 208to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 209copies of the Software, and to permit persons to whom the Software is 210furnished to do so, subject to the following conditions: 211 212The above copyright notice and this permission notice shall be included in all 213copies or substantial portions of the Software. 214 215THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 216IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 217FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 218AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 219LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 220OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 221SOFTWARE. 222 223=head1 SEE ALSO 224 225Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3) 226 227This module is maintained in the rra-c-util package. The current version is 228available from L<http://www.eyrie.org/~eagle/software/rra-c-util/>. 229 230The functions to control when tests are run use environment variables defined 231by the L<Lancaster 232Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>. 233 234=cut 235