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 / TestMB.pm < prev    next >
Encoding:
Perl POD Document  |  2004-08-06  |  9.6 KB  |  359 lines

  1. package Apache::TestMB;
  2.  
  3. use strict;
  4. use vars qw(@ISA);
  5. use Module::Build;
  6. use Apache::Test ();
  7. use Apache::TestConfig ();
  8. @ISA = qw(Module::Build);
  9.  
  10. sub new {
  11.     my $pkg = shift;
  12.     my($argv, $vars) =
  13.         Apache::TestConfig::filter_args(\@ARGV, \%Apache::TestConfig::Usage);
  14.     @ARGV = @$argv;
  15.     my $self = $pkg->SUPER::new(@_);
  16.     $self->{properties}{apache_test_args} = $vars;
  17.     $self->{properties}{apache_test_script} ||= 't/TEST';
  18.     $self->generate_script;
  19.     return $self;
  20. }
  21.  
  22. sub valid_property {
  23.     return 1 if defined $_[1] &&
  24.         ($_[1] eq 'apache_test_args' || $_[1] eq 'apache_test_script');
  25.     shift->SUPER::valid_property(@_);
  26. }
  27.  
  28. sub apache_test_args {
  29.     my $self = shift;
  30.     $self->{properties}{apache_test_args} = shift if @_;
  31.     return $self->{properties}{apache_test_args};
  32. }
  33.  
  34. sub apache_test_script {
  35.     my $self = shift;
  36.     $self->{properties}{apache_test_script} = shift if @_;
  37.     return $self->{properties}{apache_test_script};
  38. }
  39.  
  40. sub ACTION_test_clean {
  41.     my $self = shift;
  42.     # XXX I'd love to do this without t/TEST.
  43.     $self->do_system( $self->perl, $self->_bliblib,
  44.                       $self->localize_file_path($self->apache_test_script),
  45.                       '-clean');
  46. }
  47.  
  48. sub ACTION_clean {
  49.     my $self = shift;
  50.     $self->depends_on('test_clean');
  51.     $self->SUPER::ACTION_clean(@_);
  52. }
  53.  
  54. sub ACTION_run_tests {
  55.     my $self = shift;
  56.     $self->depends_on('test_clean');
  57.     # XXX I'd love to do this without t/TEST.
  58.     $self->do_system($self->perl, $self->_bliblib,
  59.                      $self->localize_file_path($self->apache_test_script),
  60.                      '-bugreport', '-verbose=' . ($self->verbose || 0));
  61. }
  62.  
  63. sub _bliblib {
  64.     my $self = shift;
  65.     return (
  66.         '-I', File::Spec->catdir($self->base_dir, $self->blib, 'lib'),
  67.         '-I', File::Spec->catdir($self->base_dir, $self->blib, 'arch'),
  68.     );
  69. }
  70.  
  71. sub ACTION_test {
  72.     my $self = shift;
  73.     $self->depends_on('code');
  74.     $self->depends_on('run_tests');
  75.     $self->depends_on('test_clean');
  76. }
  77.  
  78. sub _cmodules {
  79.     my ($self, $action) = @_;
  80.     die "The cmodules" . ( $action ne 'all' ? "_$action" : '')
  81.       . " action is not yet implemented";
  82.     # XXX TBD.
  83.     my $start_dir = $self->cwd;
  84.     chdir $self->localize_file_path('c-modules');
  85.     # XXX How do we get Build.PL to be generated instead of Makefile?
  86.     # Subclass Apache::TestConfigC, perhaps?
  87.     $self->do_system('Build.PL', $action);
  88.     chdir $start_dir;
  89. }
  90.  
  91. sub ACTION_cmodules       { shift->_cmodues('all')   }
  92. sub ACTION_cmodules_clean { shift->_cmodues('clean') }
  93.  
  94. # XXX I'd love to make this optional.
  95. sub generate_script {
  96.     my $self = shift;
  97.  
  98.     # If a file name has been passed in, use it. Otherwise, use the
  99.     # one set up when the Apache::TestMB object was created.
  100.     my $script = $self->localize_file_path($_[0]
  101.         ? $self->apache_test_script(shift)
  102.         : $self->apache_test_script
  103.     );
  104.  
  105.     # We need a class to run the tests from t/TEST.
  106.     my $class = pop || 'Apache::TestRunPerl';
  107.  
  108.     # Delete any existing instance of the file.
  109.     unlink $script if -e $script;
  110.  
  111.     # Start the contents of t/TEST.
  112.     my $body = "BEGIN { eval { require blib; } }\n"
  113.       . Apache::TestConfig->modperl_2_inc_fixup;
  114.  
  115.     # Configure the arguments for t/TEST.
  116.     while (my($k, $v) = each %{ $self->apache_test_args }) {
  117.         $v =~ s/\|/\\|/g;
  118.         $body .= "\n\$Apache::TestConfig::Argv{'$k'} = q|$v|;\n";
  119.     }
  120.  
  121.     my $infile = "$script.PL";
  122.     if (-f $infile) {
  123.         # Use the existing t/TEST.PL.
  124.         my $in = Symbol::gensym();
  125.         open $in, "$infile" or die "Couldn't open $infile: $!";
  126.         local $/;
  127.         $body .= <$in>;
  128.         close $in;
  129.     } else {
  130.         # Create t/TEST from scratch.
  131.         $body .= join "\n",
  132.             Apache::TestConfig->perlscript_header,
  133.             "use $class ();",
  134.             "$class->new->run(\@ARGV);";
  135.     }
  136.  
  137.     # Make it so!
  138.     print "Generating test running script $script\n" if $self->verbose;
  139.     Apache::Test::basic_config()->write_perlscript($script, $body);
  140.     $self->add_to_cleanup($self->apache_test_script);
  141. }
  142.  
  143.  
  144. 1;
  145. __END__
  146.  
  147. =head1 NAME
  148.  
  149. Apache::TestMB - Subclass of Module::Build to support Apache::Test
  150.  
  151. =head1 SYNOPSIS
  152.  
  153. Standard process for building & installing modules:
  154.  
  155.   perl Build.PL
  156.   ./Build
  157.   ./Build test
  158.   ./Build install
  159.  
  160. Or, if you're on a platform (like DOS or Windows) that doesn't like the "./"
  161. notation, you can do this:
  162.  
  163.   perl Build.PL
  164.   perl Build
  165.   perl Build test
  166.   perl Build install
  167.  
  168. =head1 DESCRIPTION
  169.  
  170. This class subclasses C<Module::Build> to add support for testing
  171. Apache integration with Apache::Test. It is broadly based on
  172. C<Apache::TestMM>, and as such adds a number of build actions to a the
  173. F<Build> script, while simplifying the process of creating F<Build.PL>
  174. scripts.
  175.  
  176. Here's how to use C<Apache::TestMB> in a F<Build.PL> script:
  177.  
  178.   use Module::Build;
  179.  
  180.   my $build_pkg = eval { require Apache::TestMB }
  181.       ? 'Apache::TestMB' : 'Module::Build';
  182.  
  183.   my $build = $build_pkg->new(
  184.       module_name => 'My::Module',
  185.   );
  186.   $build->create_build_script;
  187.  
  188. This is identical to how C<Module::Build> is used. Not all target
  189. systems may have C<Apache::Test> (and therefore C<Apache::TestMB>
  190. installed, so we test for it to be installed, first. But otherwise,
  191. its use can be exactly the same. Consult the
  192. L<Module::Build|Module::Build> documentation for more information on
  193. how to use it; L<Module::Build::Cookbook|Module::Build::Cookbook> may
  194. be especially useful for those looking to migrate from
  195. C<ExtUtils::MakeMaker>.
  196.  
  197. =head1 INTERFACE
  198.  
  199. =head2 Build
  200.  
  201. With the above script, users can build your module in the usual
  202. C<Module::Build> way:
  203.  
  204.   perl Build.PL
  205.   ./Build
  206.   ./Build test
  207.   ./Build install
  208.  
  209. If C<Apache::TestMB> is installed, then Apache will be started before
  210. tests are run by the C<test> action, and shut down when the tests
  211. complete. Note that C<Build.PL> can be called C<Apache::Test>-specific
  212. options in addition to the usual C<Module::Build> options. For
  213. example:
  214.  
  215.   perl Build.PL -apxs=/usr/local/apache/bin/apxs
  216.  
  217. Consult the L<Apache::Test|Apache::Test> documentation for a complete
  218. list of options.
  219.  
  220. In addition to the actions provided by C<Module::Build> (C<build>,
  221. C<clean>, C<code>, C<test>, etc.), C<Apache::TestMB> adds a few extra
  222. actions:
  223.  
  224. =over 4
  225.  
  226. =item test_clean
  227.  
  228. This action cleans out the files generated by the test script,
  229. F<t/TEST>. It is also executed by the C<clean> action.
  230.  
  231. =item run_tests
  232.  
  233. This action actually the tests by executing the test script,
  234. F<t/TEST>. It is executed by the C<test> action, so most of the time
  235. it won't be executed directly.
  236.  
  237. =back
  238.  
  239. =head2 Constructor
  240.  
  241. =head3 new
  242.  
  243. The C<new()> constructor takes all the same arguments as its parent in
  244. C<Module::Build>, but can optionally accept one other parameter:
  245.  
  246. =over
  247.  
  248. =item apache_test_script
  249.  
  250. The name of the C<Apache::Test> test script. The default value is
  251. F<t/TEST>, which will work in the vast majority of cases. If you wish
  252. to specify your own file name, do so with a relative file name using
  253. Unix-style paths; the file name will automatically be converted for
  254. the local platform.
  255.  
  256. =back
  257.  
  258. When C<new()> is called it does the following:
  259.  
  260. =over 4
  261.  
  262. =item *
  263.  
  264. Processes the C<Apache::Test>-specific options in C<@ARGV>. See the
  265. L<Apache::Test|Apache::Test> documentation for a complete list of
  266. options.
  267.  
  268. =item *
  269.  
  270. Sets the name of the C<Apache::Test> test script to F<t/TEST>, unless
  271. it was explicitly specified by the C<apache_test_script> parameter.
  272.  
  273. =item *
  274.  
  275. Calls C<generate_script()> to generate C<Apache::Test> test script,
  276. usually F<t/TEST>.
  277.  
  278. =back
  279.  
  280. =head2 Instance Methods
  281.  
  282. =head3 apache_test_args
  283.  
  284. Returns a hash reference containing all of the settings specified by
  285. options passed to F<Build.PL>, or explicitly added to C<@ARGV> in
  286. F<Build.PL>. Consult the L<Apache::Test|Apache::Test> documentation
  287. for a complete list of options.
  288.  
  289. =head3 apache_test_script
  290.  
  291. Gets or sets the file name of the C<Apache::Test> test script.
  292.  
  293. =head3 generate_script
  294.  
  295.   $build->generate_script;
  296.   $build->generate_script('t/FOO');
  297.   $build->generate_script(undef, 'Apache::TestRun');
  298.  
  299. This method is called by C<new()>, so in most cases it can be
  300. ignored. If you'd like it to use other than the default arguments, you
  301. can call it explicitly in F<Build.PL> and pass it the arguments you
  302. desire. It takes two optional arguments:
  303.  
  304. =over 4
  305.  
  306. =item *
  307.  
  308. The name of the C<Apache::Test> test script. Defaults to the value
  309. returned by C<apache_test_script()>.
  310.  
  311. =item *
  312.  
  313. The name of an C<Apache::Test> test running class. Defaults to
  314. C<Apache::TestRunPerl>.
  315.  
  316. =back
  317.  
  318. If there is an existing F<t/TEST.PL> (or a script with the same name
  319. as specified by the C<apache_test_script> parameter but with F<.PL>
  320. appended to it), then that script will be used as the template for the
  321. test script.  Otherwise, a simple test script will be written similar
  322. to what would be written by C<Apache::TestRun::generate_script()>
  323. (although that function is not aware of the arguments passed to
  324. F<Build.PL>, so use this one instead!).
  325.  
  326. =head1 SEE ALSO
  327.  
  328. =over 4
  329.  
  330. =item L<Apache::TestRequest|Apache::TestRequest>
  331.  
  332. Demonstrates how to write tests to send requests to the Apache server
  333. run by C<./Build test>.
  334.  
  335. =item L<Module::Build|Module::Build>
  336.  
  337. The parent class for C<Apache::TestMB>; consult it's documentation for
  338. more on its interface.
  339.  
  340. =item L<http://www.perl.com/pub/a/2003/05/22/testing.html>
  341.  
  342. This article by Geoffrey Young explains how to configure Apache and
  343. write tests for your module using Apache::Test. Just use
  344. C<Apache::TestMB> instead of C<Apache::TestMM> to update it for use
  345. with C<Module::Build>.
  346.  
  347. =back
  348.  
  349. =head1 AUTHOR
  350.  
  351. David Wheeler
  352.  
  353. Questions can be asked at the test-dev <at> httpd.apache.org list. For
  354. more information see: I<http://httpd.apache.org/test/> and
  355. I<http://perl.apache.org/docs/general/testing/testing.html>.
  356.  
  357. =cut
  358.  
  359.