Apache-Test
view release on metacpan or search on metacpan
lib/Apache/Test.pm view on Meta::CPAN
my @reasons = ();
for (@modules) {
if (/^[a-z0-9_.]+$/) {
my $mod = $_;
$mod .= '.c' unless $mod =~ /\.c$/;
next if $cfg->{modules}->{$mod};
$mod = 'mod_' . $mod unless $mod =~ /^mod_/;
next if $cfg->{modules}->{$mod};
if (exists $cfg->{cmodules_disabled}->{$mod}) {
push @reasons, $cfg->{cmodules_disabled}->{$mod};
next;
}
}
die "bogus module name $_" unless /^[\w:.]+$/;
# if the module was explicitly passed with a .c extension,
# do not try to eval it as a Perl module
my $not_found = 1;
unless (/\.c$/) {
eval "require $_";
$not_found = 0 unless $@;
#print $@ if $@;
}
push @reasons, "cannot find module '$_'" if $not_found;
}
if (@reasons) {
push @SkipReasons, @reasons;
return 0;
}
else {
return 1;
}
}
sub need_min_perl_version {
my $version = shift;
return 1 if $] >= $version;
push @SkipReasons, "perl >= $version is required";
return 0;
}
# currently supports only perl modules
sub need_min_module_version {
my($module, $version) = @_;
# need_module requires the perl module
return 0 unless need_module($module);
# support dev versions like 0.18_01
return 1
if eval { no warnings qw(numeric); $module->VERSION($version) };
push @SkipReasons, "$module version $version or higher is required";
return 0;
}
sub need_cgi {
return _need_multi(qw(cgi.c cgid.c));
}
sub need_cache_disk {
return _need_multi(qw(cache_disk.c disk_cache.c));
}
sub need_php {
return _need_multi(qw(php4 php5 sapi_apache2.c));
}
sub need_php4 {
return _need_multi(qw(php4 sapi_apache2.c));
}
sub need_access {
return _need_multi(qw(access authz_host));
}
sub need_auth {
return _need_multi(qw(auth auth_basic));
}
sub need_imagemap {
return need_module("imagemap") || need_module("imap");
}
sub _need_multi {
my @check = @_;
my $rc = 0;
{
local @SkipReasons;
foreach my $module (@check) {
$rc ||= need_module($module);
}
}
my $reason = join ' or ', @check;
push @SkipReasons, "cannot find one of $reason"
unless $rc;
return $rc;
}
sub need_apache {
my $version = shift;
my $cfg = Apache::Test::config();
my $rev = $cfg->{server}->{rev};
if ($rev == $version) {
return 1;
}
else {
push @SkipReasons,
"apache version $version required, this is version $rev";
lib/Apache/Test.pm view on Meta::CPAN
=item sok
Allows to skip a sub-test, controlled from the command line. The
argument to sok() is a CODE reference or a BLOCK whose return value
will be passed to ok(). By default behaves like ok(). If all sub-tests
of the same test are written using sok(), and a test is executed as:
% ./t/TEST -v skip_subtest 1 3
only sub-tests 1 and 3 will be run, the rest will be skipped.
=item skip
Same as I<Test::skip>, see I<Test.pm> documentation.
=item test_pm_refresh
Normally called by I<Apache::Test::plan>, this function will refresh
the global state maintained by I<Test.pm>, allowing C<plan> and
friends to be called more than once per-process. This function is not
exported.
=back
Functions that can be used as a last argument to the extended plan().
Note that for each C<need_*> function there is a C<have_*> equivalent
that performs the exact same function except that it is designed to
be used outside of C<plan()>. C<need_*> functions have the side effect
of generating skip messages, if the test is skipped. C<have_*> functions
don't have this side effect. In other words, use C<need_apache()>
with C<plan()> to decide whether a test will run, but C<have_apache()>
within test logic to adjust expectations based on older or newer
server versions.
=over
=item need_http11
plan tests => 5, need_http11;
Require HTTP/1.1 support.
=item need_ssl
plan tests => 5, need_ssl;
Require SSL support.
Not exported by default.
=item need_lwp
plan tests => 5, need_lwp;
Require LWP support.
=item need_cgi
plan tests => 5, need_cgi;
Requires mod_cgi or mod_cgid to be installed.
=item need_cache_disk
plan tests => 5, need_cache_disk
Requires mod_cache_disk or mod_disk_cache to be installed.
=item need_php
plan tests => 5, need_php;
Requires a PHP module to be installed (version 4 or 5).
=item need_php4
plan tests => 5, need_php4;
Requires a PHP version 4 module to be installed.
=item need_imagemap
plan tests => 5, need_imagemap;
Requires a mod_imagemap or mod_imap be installed
=item need_apache
plan tests => 5, need_apache 2;
Requires Apache 2nd generation httpd-2.x.xx
plan tests => 5, need_apache 1;
Requires Apache 1st generation (apache-1.3.xx)
See also C<need_min_apache_version()>.
=item need_min_apache_version
Used to require a minimum version of Apache.
For example:
plan tests => 5, need_min_apache_version("2.0.40");
requires Apache 2.0.40 or higher.
=item need_apache_version
Used to require a specific version of Apache.
For example:
plan tests => 5, need_apache_version("2.0.40");
requires Apache 2.0.40.
=item need_min_apache_fix
Used to require a particular micro version from corresponding minor release
For example:
plan tests => 5, need_min_apache_fix("2.0.40", "2.2.30", "2.4.18");
requires Apache 2.0.40 or higher.
=item need_apache_mpm
Used to require a specific Apache Multi-Processing Module.
For example:
plan tests => 5, need_apache_mpm('prefork');
requires the prefork MPM.
=item need_perl
plan tests => 5, need_perl 'iolayers';
plan tests => 5, need_perl 'ithreads';
Requires a perl extension to be present, or perl compiled with certain
capabilities.
The first example tests whether C<PerlIO> is available, the second
whether:
$Config{useithread} eq 'define';
=item need_min_perl_version
Used to require a minimum version of Perl.
For example:
plan tests => 5, need_min_perl_version("5.008001");
requires Perl 5.8.1 or higher.
=item need_fork
Requires the perl built-in function C<fork> to be implemented.
=item need_module
plan tests => 5, need_module 'CGI';
plan tests => 5, need_module qw(CGI Find::File);
plan tests => 5, need_module ['CGI', 'Find::File', 'cgid'];
Requires Apache C and Perl modules. The function accept a list of
arguments or a reference to a list.
In case of C modules, depending on how the module name was passed it
may pass through the following completions:
=over
=item 1 need_module 'proxy_http.c'
If there is the I<.c> extension, the module name will be looked up as
is, i.e. I<'proxy_http.c'>.
=item 2 need_module 'mod_cgi'
The I<.c> extension will be appended before the lookup, turning it into
I<'mod_cgi.c'>.
=item 3 need_module 'cgi'
The I<.c> extension and I<mod_> prefix will be added before the
lookup, turning it into I<'mod_cgi.c'>.
=back
=item need_min_module_version
Used to require a minimum version of a module
For example:
plan tests => 5, need_min_module_version(CGI => 2.81);
requires C<CGI.pm> version 2.81 or higher.
Currently works only for perl modules.
=item need
plan tests => 5,
need 'LWP',
{ "perl >= 5.8.0 and w/ithreads is required" =>
($Config{useperlio} && $] >= 5.008) },
{ "not Win32" => sub { $^O eq 'MSWin32' },
"foo is disabled" => \&is_foo_enabled,
},
'cgid';
need() is more generic function which can impose multiple requirements
at once. All requirements must be satisfied.
need()'s argument is a list of things to test. The list can include
scalars, which are passed to need_module(), and hash references. If
hash references are used, the keys, are strings, containing a reason
for a failure to satisfy this particular entry, the values are the
condition, which are satisfaction if they return true. If the value is
0 or 1, it used to decide whether the requirements very satisfied, so
you can mix special C<need_*()> functions that return 0 or 1. For
example:
plan tests => 1, need 'Compress::Zlib', 'deflate',
need_min_apache_version("2.0.49");
If the scalar value is a string, different from 0 or 1, it's passed to
I<need_module()>. If the value is a code reference, it gets executed
at the time of check and its return value is used to check the
condition. If the condition check fails, the provided (in a key)
reason is used to tell user why the test was skipped.
In the presented example, we require the presence of the C<LWP> Perl
module, C<mod_cgid>, that we run under perl E<gt>= 5.7.3 on Win32.
It's possible to put more than one requirement into a single hash
reference, but be careful that the keys will be different.
It's also important to mention to avoid using:
plan tests => 1, requirement1 && requirement2;
technique. While test-wise that technique is equivalent to:
plan tests => 1, need requirement1, requirement2;
since the test will be skipped, unless all the rules are satisfied,
it's not equivalent for the end users. The second technique, deploying
C<need()> and a list of requirements, always runs all the requirement
checks and reports all the missing requirements. In the case of the
first technique, if the first requirement fails, the second is not
run, and the missing requirement is not reported. So let's say all the
requirements are missing Apache modules, and a user wants to satisfy
all of these and run the test suite again. If all the unsatisfied
requirements are reported at once, she will need to rebuild Apache
once. If only one requirement is reported at a time, she will have to
rebuild Apache as many times as there are elements in the C<&&>
statement.
Also see plan().
=item under_construction
plan tests => 5, under_construction;
skip all tests, noting that the tests are under construction
=item skip_reason
plan tests => 5, skip_reason('my custom reason');
skip all tests. the reason you specify will be given at runtime.
if no reason is given a default reason will be used.
=back
=head1 Additional Configuration Variables
=over 4
=item basic_config
my $basic_cfg = Apache::Test::basic_config();
$basic_cfg->write_perlscript($file, $content);
C<basic_config()> is similar to C<config()>, but doesn't contain any
httpd-specific information and should be used for operations that
don't require any httpd-specific knowledge.
=item config
my $cfg = Apache::Test::config();
my $server_rev = $cfg->{server}->{rev};
...
( run in 0.532 second using v1.01-cache-2.11-cpan-ceb78f64989 )