File Coverage

blib/lib/Apache/TestMM.pm
Criterion Covered Total %
statement 18 68 26.5
branch 0 16 0.0
condition 0 3 0.0
subroutine 6 12 50.0
pod 2 5 40.0
total 26 104 25.0


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