home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / TestMM.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-05  |  4.8 KB  |  194 lines

  1. package Apache::TestMM;
  2.  
  3. use strict;
  4. use warnings FATAL => 'all';
  5.  
  6. use Config;
  7. use Apache::TestConfig ();
  8. use Apache::TestTrace;
  9.  
  10. sub import {
  11.     my $class = shift;
  12.  
  13.     for my $section (@_) {
  14.         unless (defined &$section) {
  15.             die "unknown Apache::TestMM section: $section";
  16.         }
  17.         no strict 'refs';
  18.         my $sub = "MY::$section";
  19.         # Force aliasing, since previous WriteMakefile might have
  20.         # moved it
  21.         undef &$sub if defined &$sub;
  22.         *$sub = \&{$section};
  23.     }
  24. }
  25.  
  26. sub add_dep {
  27.     my($string, $targ, $add) = @_;
  28.     $$string =~ s/($targ\s+::)/$1 $add /;
  29. }
  30.  
  31. sub clean {
  32.     my $self = shift;
  33.     my $string = $self->MM::clean(@_);
  34.     add_dep(\$string, clean => 'test_clean');
  35.     $string;
  36. }
  37.  
  38. sub test {
  39.  
  40.     my $env = Apache::TestConfig->passenv_makestr();
  41.  
  42.     my $preamble = Apache::TestConfig::WIN32 ? "" : <<EOF;
  43. PASSENV = $env
  44. EOF
  45.  
  46.     return $preamble . <<'EOF';
  47. TEST_VERBOSE = 0
  48. TEST_FILES =
  49.  
  50. test_clean :
  51.     $(FULLPERL) -I$(INST_ARCHLIB) -I$(INST_LIB) \
  52.     t/TEST -clean
  53.  
  54. run_tests : test_clean
  55.     $(PASSENV) \
  56.     $(FULLPERL) -I$(INST_ARCHLIB) -I$(INST_LIB) \
  57.     t/TEST -bugreport -verbose=$(TEST_VERBOSE) $(TEST_FILES)
  58.  
  59. test :: pure_all run_tests test_clean
  60.  
  61. cmodules:
  62.     cd c-modules && $(MAKE) all
  63.  
  64. cmodules_clean:
  65.     cd c-modules && $(MAKE) clean
  66. EOF
  67.  
  68. }
  69.  
  70. sub generate_script {
  71.     my $file = shift;
  72.  
  73.     unlink $file if -e $file;
  74.  
  75.     my $body = "BEGIN { eval { require blib; } }\n";
  76.  
  77.     $body .= Apache::TestConfig->modperl_2_inc_fixup;
  78.  
  79.     if (@Apache::TestMM::Argv) {
  80.         $body .= "\n\%Apache::TestConfig::Argv = qw(@Apache::TestMM::Argv);\n";
  81.     }
  82.  
  83.     my $in = Symbol::gensym();
  84.     open $in, "$file.PL" or die "Couldn't open $file.PL: $!";
  85.     {
  86.         local $/;
  87.         $body .= <$in>;
  88.     }
  89.     close $in;
  90.  
  91.     info "generating script $file";
  92.     Apache::Test::config()->write_perlscript($file, $body);
  93. }
  94.  
  95. sub filter_args {
  96.     my($argv, $vars) =
  97.         Apache::TestConfig::filter_args(\@ARGV, \%Apache::TestConfig::Usage);
  98.     @ARGV = @$argv;
  99.     @Apache::TestMM::Argv = %$vars;
  100. }
  101.  
  102. 1;
  103.  
  104. =head1 NAME
  105.  
  106. Apache::TestMM - Provide MakeMaker Wrapper Methods
  107.  
  108. =head1 SYNOPSIS
  109.  
  110.   require Apache::TestMM;
  111.   
  112.   # import MY::test and MY::clean overrides for MM
  113.   Apache::TestMM->import(qw(test clean));
  114.   
  115.   # parse command line args
  116.   Apache::TestMM::filter_args();
  117.   
  118.   # autogenerate the script
  119.   Apache::TestMM::generate_script('t/TEST');
  120.  
  121. =head1 DESCRIPTION
  122.  
  123. C<Apache::TestMM> provides wrappers for the C<ExtUtils::MakeMaker>
  124. craft, making it easier to extend the autogenerated F<Makefile> with
  125. C<Apache::Test>.
  126.  
  127. =head1 FUNCTIONS
  128.  
  129. =head2 C<import>
  130.  
  131.   use Apache::TestMM qw(test clean);
  132.  
  133. or:
  134.  
  135.   Apache::TestMM->import(qw(test clean));
  136.  
  137. Imports C<MY::> overrides for the default C<ExtUtils::MakeMaker>
  138. I<test> and I<clean> targets, as if you have defined:
  139.  
  140.   sub MY::test {...}
  141.   sub MY::clean {...}
  142.  
  143. in F<Makefile.PL>. C<Apache::TestMM> does this for you so that these Makefile
  144. targets will run the Apache server and the tests for it, and clean up after
  145. its mess.
  146.  
  147. =head2 C<filter_args>
  148.  
  149.   push @ARGV, '-apxs', $apxs_path;
  150.   Apache::TestMM::filter_args();
  151.   WriteMakefile(...);
  152.  
  153. When C<WriteMakefile()> is called it parses C<@ARGV>, hoping to find special
  154. options like C<PREFIX=/home/stas/perl>. C<Apache::Test> accepts a lot of
  155. options of its own. When C<Apache::TestMM::filter_args()> is called, it
  156. removes any C<Apache::Test>-specific options from C<@ARGV> and stores them
  157. internally, so when C<WriteMakefile()> is called they aren't in C<@ARGV> and
  158. thus won't be processed by C<WriteMakefile()>.
  159.  
  160. The options can be set when F<Makefile.PL> is called:
  161.  
  162.   % perl Makefile.PL -apxs /path/to/apxs
  163.  
  164. Or you can push them manually to C<@ARGV> from the code:
  165.  
  166.   push @ARGV, '-apxs', $apxs_path;
  167.  
  168. When:
  169.  
  170.   Apache::TestMM::generate_script('t/TEST');
  171.  
  172. is called, C<Apache::Test>-specific options extracted by
  173. C<Apache::TestMM::filter_args()> are written to the autogenerated
  174. file. In our example, the autogenerated F<t/TEST> will include:
  175.  
  176.   %Apache::TestConfig::Argv = qw(apxs /path/to/apxs);
  177.  
  178. which is going to be used by the C<Apache::Test> runtime.
  179.  
  180. =head2 C<generate_script>
  181.  
  182.   Apache::TestMM::generate_script('t/TEST');
  183.  
  184. C<generate_script()> accepts the name of the script to generate and
  185. will look for a template with the same name and suffix I<.PL>. So in
  186. our example it'll look for F<t/TEST.PL>. The autogenerated script
  187. F<t/TEST> will include the contents of F<t/TEST.PL>, and special
  188. directives, including any configuration options passed via
  189. C<L<filter_args()|/C_filter_args_>> called from F<Makefile.PL>, special
  190. fixup code, etc. If no argument is passed to C<generate_script()>,
  191. it will create a file named F<t/TEST> by default.
  192.  
  193. =cut
  194.