home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / TestMM.pm < prev    next >
Encoding:
Perl POD Document  |  2004-08-06  |  5.9 KB  |  225 lines

  1. # Copyright 2001-2004 The Apache Software Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. package Apache::TestMM;
  16.  
  17. use strict;
  18. use warnings FATAL => 'all';
  19.  
  20. use Config;
  21. use Apache::TestConfig ();
  22. use Apache::TestTrace;
  23.  
  24. sub import {
  25.     my $class = shift;
  26.  
  27.     for my $section (@_) {
  28.         unless (defined &$section) {
  29.             die "unknown Apache::TestMM section: $section";
  30.         }
  31.         no strict 'refs';
  32.         my $sub = "MY::$section";
  33.         # Force aliasing, since previous WriteMakefile might have
  34.         # moved it
  35.         undef &$sub if defined &$sub;
  36.         *$sub = \&{$section};
  37.     }
  38. }
  39.  
  40. sub add_dep {
  41.     my($string, $targ, $add) = @_;
  42.     $$string =~ s/($targ\s+::)/$1 $add /;
  43. }
  44.  
  45. sub clean {
  46.     my $self = shift;
  47.     my $string = $self->MM::clean(@_);
  48.     add_dep(\$string, clean => 'test_clean');
  49.     $string;
  50. }
  51.  
  52. sub test {
  53.  
  54.     my $env = Apache::TestConfig->passenv_makestr();
  55.  
  56.     my $preamble = Apache::TestConfig::WIN32 ? "" : <<EOF;
  57. PASSENV = $env
  58. EOF
  59.  
  60.     return $preamble . <<'EOF';
  61. TEST_VERBOSE = 0
  62. TEST_FILES =
  63.  
  64. test_clean :
  65.     $(FULLPERL) -I$(INST_ARCHLIB) -I$(INST_LIB) \
  66.     t/TEST $(APACHE_TEST_EXTRA_ARGS) -clean
  67.  
  68. run_tests : test_clean
  69.     $(PASSENV) \
  70.     $(FULLPERL) -I$(INST_ARCHLIB) -I$(INST_LIB) \
  71.     t/TEST $(APACHE_TEST_EXTRA_ARGS) -bugreport -verbose=$(TEST_VERBOSE) $(TEST_FILES)
  72.  
  73. test :: pure_all run_tests test_clean
  74.  
  75. cmodules:
  76.     cd c-modules && $(MAKE) all
  77.  
  78. cmodules_clean:
  79.     cd c-modules && $(MAKE) clean
  80. EOF
  81.  
  82. }
  83.  
  84. sub generate_script {
  85.     my $file = shift;
  86.  
  87.     unlink $file if -e $file;
  88.  
  89.     my $body = "BEGIN { eval { require blib; } }\n";
  90.  
  91.     $body .= Apache::TestConfig->modperl_2_inc_fixup;
  92.  
  93.     my %args = @Apache::TestMM::Argv;
  94.     while (my($k, $v) = each %args) {
  95.         $v =~ s/\|/\\|/g;
  96.         $body .= "\n\$Apache::TestConfig::Argv{'$k'} = q|$v|;\n";
  97.     }
  98.  
  99.     my $in = Symbol::gensym();
  100.     open $in, "$file.PL" or die "Couldn't open $file.PL: $!";
  101.     {
  102.         local $/;
  103.         $body .= <$in>;
  104.     }
  105.     close $in;
  106.  
  107.     info "generating script $file";
  108.     Apache::Test::basic_config()->write_perlscript($file, $body);
  109. }
  110.  
  111. sub filter_args {
  112.     my($argv, $vars) =
  113.         Apache::TestConfig::filter_args(\@ARGV, \%Apache::TestConfig::Usage);
  114.     @ARGV = @$argv;
  115.     @Apache::TestMM::Argv = %$vars;
  116. }
  117.  
  118. 1;
  119.  
  120. =head1 NAME
  121.  
  122. Apache::TestMM - Provide MakeMaker Wrapper Methods
  123.  
  124. =head1 SYNOPSIS
  125.  
  126.   require Apache::TestMM;
  127.   
  128.   # import MY::test and MY::clean overrides for MM
  129.   Apache::TestMM->import(qw(test clean));
  130.   
  131.   # parse command line args
  132.   Apache::TestMM::filter_args();
  133.   
  134.   # autogenerate the script
  135.   Apache::TestMM::generate_script('t/TEST');
  136.  
  137. =head1 DESCRIPTION
  138.  
  139. C<Apache::TestMM> provides wrappers for the C<ExtUtils::MakeMaker>
  140. craft, making it easier to extend the autogenerated F<Makefile> with
  141. C<Apache::Test>.
  142.  
  143. =head1 FUNCTIONS
  144.  
  145. =head2 C<import>
  146.  
  147.   use Apache::TestMM qw(test clean);
  148.  
  149. or:
  150.  
  151.   Apache::TestMM->import(qw(test clean));
  152.  
  153. Imports C<MY::> overrides for the default C<ExtUtils::MakeMaker>
  154. I<test> and I<clean> targets, as if you have defined:
  155.  
  156.   sub MY::test {...}
  157.   sub MY::clean {...}
  158.  
  159. in F<Makefile.PL>. C<Apache::TestMM> does this for you so that these Makefile
  160. targets will run the Apache server and the tests for it, and clean up after
  161. its mess.
  162.  
  163. =head2 C<filter_args>
  164.  
  165.   push @ARGV, '-apxs', $apxs_path;
  166.   Apache::TestMM::filter_args();
  167.   WriteMakefile(...);
  168.  
  169. When C<WriteMakefile()> is called it parses C<@ARGV>, hoping to find
  170. special options like C<PREFIX=/home/stas/perl>. C<Apache::Test>
  171. accepts a lot of configuration options of its own. When
  172. C<Apache::TestMM::filter_args()> is called, it removes any
  173. C<Apache::Test>-specific options from C<@ARGV> and stores them
  174. internally, so when C<WriteMakefile()> is called they aren't in
  175. C<@ARGV> and thus won't be processed by C<WriteMakefile()>.
  176.  
  177. The options can be set when F<Makefile.PL> is called:
  178.  
  179.   % perl Makefile.PL -apxs /path/to/apxs
  180.  
  181. Or you can push them manually to C<@ARGV> from the code:
  182.  
  183.   push @ARGV, '-apxs', $apxs_path;
  184.  
  185. When:
  186.  
  187.   Apache::TestMM::generate_script('t/TEST');
  188.  
  189. is called, C<Apache::Test>-specific options extracted by
  190. C<Apache::TestMM::filter_args()> are written to the autogenerated
  191. file. In our example, the autogenerated F<t/TEST> will include:
  192.  
  193.   %Apache::TestConfig::Argv = qw(apxs /path/to/apxs);
  194.  
  195. which is going to be used by the C<Apache::Test> runtime.
  196.  
  197. The other frequently used options are: C<-httpd>, telling where to
  198. find the httpd (usually when the C<-apxs> option is not used),
  199. C<-libmodperl> to use a specific mod_perl shared object (if your
  200. mod_perl is built as DSO), C<-maxclients> to change the default number
  201. of the configured C<MaxClients> directive, C<-port> to start the
  202. server on a specific port, etc. To get the complete list of available
  203. configuration options and their purpose and syntax, run:
  204.  
  205.   % perl -MApache::TestConfig -le 'Apache::TestConfig::usage()'
  206.  
  207. You may wish to document some of these in your application's F<README>
  208. file, especially the C<-apxs> and C<-httpd> options.
  209.  
  210.  
  211. =head2 C<generate_script>
  212.  
  213.   Apache::TestMM::generate_script('t/TEST');
  214.  
  215. C<generate_script()> accepts the name of the script to generate and
  216. will look for a template with the same name and suffix I<.PL>. So in
  217. our example it'll look for F<t/TEST.PL>. The autogenerated script
  218. F<t/TEST> will include the contents of F<t/TEST.PL>, and special
  219. directives, including any configuration options passed via
  220. C<L<filter_args()|/C_filter_args_>> called from F<Makefile.PL>, special
  221. fixup code, etc. If no argument is passed to C<generate_script()>,
  222. it will create a file named F<t/TEST> by default.
  223.  
  224. =cut
  225.