📄 testmb.pm
字号:
package Apache::TestMB;use strict;use vars qw(@ISA);use Module::Build;use Apache::Test ();use Apache::TestConfig ();@ISA = qw(Module::Build);sub new { my $pkg = shift; my($argv, $vars) = Apache::TestConfig::filter_args(\@ARGV, \%Apache::TestConfig::Usage); @ARGV = @$argv; my $self = $pkg->SUPER::new(@_); $self->{properties}{apache_test_args} = $vars; $self->{properties}{apache_test_script} ||= 't/TEST'; $self->generate_script; return $self;}sub valid_property { return 1 if defined $_[1] && ($_[1] eq 'apache_test_args' || $_[1] eq 'apache_test_script'); shift->SUPER::valid_property(@_);}sub apache_test_args { my $self = shift; $self->{properties}{apache_test_args} = shift if @_; return $self->{properties}{apache_test_args};}sub apache_test_script { my $self = shift; $self->{properties}{apache_test_script} = shift if @_; return $self->{properties}{apache_test_script};}sub ACTION_test_clean { my $self = shift; # XXX I'd love to do this without t/TEST. $self->do_system( $self->perl, $self->_bliblib, $self->localize_file_path($self->apache_test_script), '-clean');}sub ACTION_clean { my $self = shift; $self->depends_on('test_clean'); $self->SUPER::ACTION_clean(@_);}sub ACTION_run_tests { my $self = shift; $self->depends_on('test_clean'); # XXX I'd love to do this without t/TEST. $self->do_system($self->perl, $self->_bliblib, $self->localize_file_path($self->apache_test_script), '-bugreport', '-verbose=' . ($self->verbose || 0));}sub ACTION_testcover { my $self = shift; unless ($self->find_module_by_name('Devel::Cover', \@INC)) { warn("Cannot run testcover action unless Devel::Cover " . "is installed.\n"); return; } $self->add_to_cleanup('coverage', 'cover_db'); my $atdir = $self->localize_file_path("$ENV{HOME}/.apache-test"); local $Test::Harness::switches = local $Test::Harness::Switches = local $ENV{HARNESS_PERL_SWITCHES} = "-MDevel::Cover=+inc,'$atdir'"; local $ENV{APACHE_TEST_EXTRA_ARGS} = "-one-process"; $self->depends_on('test'); $self->do_system('cover');}sub ACTION_test_config { my $self = shift; $self->do_system($self->perl, $self->_bliblib, $self->localize_file_path($self->apache_test_script), '-conf', '-verbose=' . ($self->verbose || 0));}sub _bliblib { my $self = shift; return ( '-I', File::Spec->catdir($self->base_dir, $self->blib, 'lib'), '-I', File::Spec->catdir($self->base_dir, $self->blib, 'arch'), );}sub ACTION_test { my $self = shift; $self->depends_on('code'); $self->depends_on('run_tests'); $self->depends_on('test_clean');}sub _cmodules { my ($self, $action) = @_; die "The cmodules" . ( $action ne 'all' ? "_$action" : '') . " action is not yet implemented"; # XXX TBD. $self->depends_on('test_config'); my $start_dir = $self->cwd; chdir $self->localize_file_path('c-modules'); # XXX How do we get Build.PL to be generated instead of Makefile? # Subclass Apache::TestConfigC, perhaps? $self->do_system('Build.PL', $action); chdir $start_dir;}sub ACTION_cmodules { shift->_cmodues('all') }sub ACTION_cmodules_clean { shift->_cmodues('clean') }# XXX I'd love to make this optional.sub generate_script { my $self = shift; # If a file name has been passed in, use it. Otherwise, use the # one set up when the Apache::TestMB object was created. my $script = $self->localize_file_path($_[0] ? $self->apache_test_script(shift) : $self->apache_test_script ); # We need a class to run the tests from t/TEST. my $class = pop || 'Apache::TestRunPerl'; # Delete any existing instance of the file. unlink $script if -e $script; # Start the contents of t/TEST. my $body = "BEGIN { eval { require blib && blib->import; } }\n"; # Configure the arguments for t/TEST. while (my($k, $v) = each %{ $self->apache_test_args }) { $v =~ s/\|/\\|/g; $body .= "\n\$Apache::TestConfig::Argv{'$k'} = q|$v|;\n"; } my $infile = "$script.PL"; if (-f $infile) { # Use the existing t/TEST.PL. my $in = Symbol::gensym(); open $in, "$infile" or die "Couldn't open $infile: $!"; local $/; $body .= <$in>; close $in; } else { # Create t/TEST from scratch. $body .= join "\n", Apache::TestConfig->perlscript_header, "use $class ();", "$class->new->run(\@ARGV);"; } # Make it so! print "Generating test running script $script\n" if $self->verbose; Apache::Test::basic_config()->write_perlscript($script, $body); $self->add_to_cleanup($self->apache_test_script);}1;__END__=head1 NAMEApache::TestMB - Subclass of Module::Build to support Apache::Test=head1 SYNOPSISStandard process for building & installing modules: perl Build.PL ./Build ./Build test ./Build installOr, if you're on a platform (like DOS or Windows) that doesn't like the "./"notation, you can do this: perl Build.PL perl Build perl Build test perl Build install=head1 DESCRIPTIONThis class subclasses C<Module::Build> to add support for testingApache integration with Apache::Test. It is broadly based onC<Apache::TestMM>, and as such adds a number of build actions to a theF<Build> script, while simplifying the process of creating F<Build.PL>scripts.Here's how to use C<Apache::TestMB> in a F<Build.PL> script: use Module::Build; my $build_pkg = eval { require Apache::TestMB } ? 'Apache::TestMB' : 'Module::Build'; my $build = $build_pkg->new( module_name => 'My::Module', ); $build->create_build_script;This is identical to how C<Module::Build> is used. Not all targetsystems may have C<Apache::Test> (and therefore C<Apache::TestMB>installed, so we test for it to be installed, first. But otherwise,its use can be exactly the same. Consult theL<Module::Build|Module::Build> documentation for more information onhow to use it; L<Module::Build::Cookbook|Module::Build::Cookbook> maybe especially useful for those looking to migrate fromC<ExtUtils::MakeMaker>.=head1 INTERFACE=head2 BuildWith the above script, users can build your module in the usualC<Module::Build> way: perl Build.PL ./Build ./Build test ./Build installIf C<Apache::TestMB> is installed, then Apache will be started beforetests are run by the C<test> action, and shut down when the testscomplete. Note that C<Build.PL> can be called C<Apache::Test>-specificoptions in addition to the usual C<Module::Build> options. Forexample: perl Build.PL -apxs=/usr/local/apache/bin/apxsConsult the L<Apache::Test|Apache::Test> documentation for a completelist of options.In addition to the actions provided by C<Module::Build> (C<build>,C<clean>, C<code>, C<test>, etc.), C<Apache::TestMB> adds a few extraactions:=over 4=item test_cleanThis action cleans out the files generated by the test script,F<t/TEST>. It is also executed by the C<clean> action.=item run_testsThis action actually the tests by executing the test script,F<t/TEST>. It is executed by the C<test> action, so most of the timeit won't be executed directly.=item testcoverC<Apache::TestMB> overrides this action from C<Module::Build> in order toprevent the C<Apache::Test> preference files from being included in the testcoverage.=back=head2 Constructor=head3 newThe C<new()> constructor takes all the same arguments as its parent inC<Module::Build>, but can optionally accept one other parameter:=over=item apache_test_scriptThe name of the C<Apache::Test> test script. The default value isF<t/TEST>, which will work in the vast majority of cases. If you wishto specify your own file name, do so with a relative file name usingUnix-style paths; the file name will automatically be converted forthe local platform.=backWhen C<new()> is called it does the following:=over 4=item *Processes the C<Apache::Test>-specific options in C<@ARGV>. See theL<Apache::Test|Apache::Test> documentation for a complete list ofoptions.=item *Sets the name of the C<Apache::Test> test script to F<t/TEST>, unlessit was explicitly specified by the C<apache_test_script> parameter.=item *Calls C<generate_script()> to generate C<Apache::Test> test script,usually F<t/TEST>.=back=head2 Instance Methods=head3 apache_test_argsReturns a hash reference containing all of the settings specified byoptions passed to F<Build.PL>, or explicitly added to C<@ARGV> inF<Build.PL>. Consult the L<Apache::Test|Apache::Test> documentationfor a complete list of options.=head3 apache_test_scriptGets or sets the file name of the C<Apache::Test> test script.=head3 generate_script $build->generate_script; $build->generate_script('t/FOO'); $build->generate_script(undef, 'Apache::TestRun');This method is called by C<new()>, so in most cases it can beignored. If you'd like it to use other than the default arguments, youcan call it explicitly in F<Build.PL> and pass it the arguments youdesire. It takes two optional arguments:=over 4=item *The name of the C<Apache::Test> test script. Defaults to the valuereturned by C<apache_test_script()>.=item *The name of an C<Apache::Test> test running class. Defaults toC<Apache::TestRunPerl>.=backIf there is an existing F<t/TEST.PL> (or a script with the same nameas specified by the C<apache_test_script> parameter but with F<.PL>appended to it), then that script will be used as the template for thetest script. Otherwise, a simple test script will be written similarto what would be written by C<Apache::TestRun::generate_script()>(although that function is not aware of the arguments passed toF<Build.PL>, so use this one instead!).=head1 SEE ALSO=over 4=item L<Apache::TestRequest|Apache::TestRequest>Demonstrates how to write tests to send requests to the Apache serverrun by C<./Build test>.=item L<Module::Build|Module::Build>The parent class for C<Apache::TestMB>; consult it's documentation formore on its interface.=item L<http://www.perl.com/pub/a/2003/05/22/testing.html>This article by Geoffrey Young explains how to configure Apache andwrite tests for your module using Apache::Test. Just useC<Apache::TestMB> instead of C<Apache::TestMM> to update it for usewith C<Module::Build>.=back=head1 AUTHORDavid WheelerQuestions can be asked at the test-dev <at> httpd.apache.org list. Formore information see: I<http://httpd.apache.org/test/> andI<http://perl.apache.org/docs/general/testing/testing.html>.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -