| 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 | |||
| 17 | package Apache::TestMB; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use vars qw(@ISA); | ||
| 21 | use Module::Build 0.18; | ||
| 22 | use Apache::Test (); | ||
| 23 | use Apache::TestConfig (); | ||
| 24 | @ISA = qw(Module::Build); | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $pkg = shift; | ||
| 28 | my($argv, $vars) = | ||
| 29 | Apache::TestConfig::filter_args(\@ARGV, \%Apache::TestConfig::Usage); | ||
| 30 | @ARGV = @$argv; | ||
| 31 | my $self = $pkg->SUPER::new(@_); | ||
| 32 | $self->{properties}{apache_test_args} = $vars; | ||
| 33 | $self->{properties}{apache_test_script} ||= 't/TEST'; | ||
| 34 | $self->generate_script; | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub valid_property { | ||
| 39 | return 1 if defined $_[1] && | ||
| 40 | ($_[1] eq 'apache_test_args' || $_[1] eq 'apache_test_script'); | ||
| 41 | shift->SUPER::valid_property(@_); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub apache_test_args { | ||
| 45 | my $self = shift; | ||
| 46 | $self->{properties}{apache_test_args} = shift if @_; | ||
| 47 | return $self->{properties}{apache_test_args}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub apache_test_script { | ||
| 51 | my $self = shift; | ||
| 52 | $self->{properties}{apache_test_script} = shift if @_; | ||
| 53 | return $self->{properties}{apache_test_script}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub ACTION_test_clean { | ||
| 57 | my $self = shift; | ||
| 58 | # XXX I'd love to do this without t/TEST. | ||
| 59 | $self->do_system( $self->perl, $self->_bliblib, | ||
| 60 | $self->localize_file_path($self->apache_test_script), | ||
| 61 | '-clean'); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub ACTION_clean { | ||
| 65 | my $self = shift; | ||
| 66 | $self->depends_on('test_clean'); | ||
| 67 | $self->SUPER::ACTION_clean(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub ACTION_run_tests { | ||
| 71 | my $self = shift; | ||
| 72 | $self->depends_on('test_clean'); | ||
| 73 | # XXX I'd love to do this without t/TEST. | ||
| 74 | $self->do_system($self->perl, $self->_bliblib, | ||
| 75 | $self->localize_file_path($self->apache_test_script), | ||
| 76 | '-bugreport', '-verbose=' . ($self->verbose || 0)); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub ACTION_testcover { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | unless ($self->find_module_by_name('Devel::Cover', \@INC)) { | ||
| 83 | warn("Cannot run testcover action unless Devel::Cover " | ||
| 84 | . "is installed.\n" . | ||
| 85 | "Don't forget to rebuild your Makefile after " | ||
| 86 | . "installing Devel::Cover\n"); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | $self->add_to_cleanup('coverage', 'cover_db'); | ||
| 91 | |||
| 92 | my $atdir = $self->localize_file_path("$ENV{HOME}/.apache-test"); | ||
| 93 | local $Test::Harness::switches = | ||
| 94 | local $Test::Harness::Switches = | ||
| 95 | local $ENV{HARNESS_PERL_SWITCHES} = "-MDevel::Cover=+inc,'$atdir'"; | ||
| 96 | local $ENV{APACHE_TEST_EXTRA_ARGS} = "-one-process"; | ||
| 97 | |||
| 98 | $self->depends_on('test'); | ||
| 99 | $self->do_system('cover'); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub ACTION_test_config { | ||
| 103 | my $self = shift; | ||
| 104 | $self->do_system($self->perl, $self->_bliblib, | ||
| 105 | $self->localize_file_path($self->apache_test_script), | ||
| 106 | '-conf', '-verbose=' . ($self->verbose || 0)); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _bliblib { | ||
| 110 | my $self = shift; | ||
| 111 | return ( | ||
| 112 | '-I', File::Spec->catdir($self->base_dir, $self->blib, 'lib'), | ||
| 113 | '-I', File::Spec->catdir($self->base_dir, $self->blib, 'arch'), | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub ACTION_test { | ||
| 118 | my $self = shift; | ||
| 119 | $self->depends_on('code'); | ||
| 120 | $self->depends_on('run_tests'); | ||
| 121 | $self->depends_on('test_clean'); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _cmodules { | ||
| 125 | my ($self, $action) = @_; | ||
| 126 | die "The cmodules" . ( $action ne 'all' ? "_$action" : '') | ||
| 127 | . " action is not yet implemented"; | ||
| 128 | # XXX TBD. | ||
| 129 | $self->depends_on('test_config'); | ||
| 130 | my $start_dir = $self->cwd; | ||
| 131 | chdir $self->localize_file_path('c-modules'); | ||
| 132 | # XXX How do we get Build.PL to be generated instead of Makefile? | ||
| 133 | # Subclass Apache::TestConfigC, perhaps? | ||
| 134 | $self->do_system('Build.PL', $action); | ||
| 135 | chdir $start_dir; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub ACTION_cmodules { shift->_cmodues('all') } | ||
| 139 | sub ACTION_cmodules_clean { shift->_cmodues('clean') } | ||
| 140 | |||
| 141 | # XXX I'd love to make this optional. | ||
| 142 | sub generate_script { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | # If a file name has been passed in, use it. Otherwise, use the | ||
| 146 | # one set up when the Apache::TestMB object was created. | ||
| 147 | my $script = $self->localize_file_path($_[0] | ||
| 148 | ? $self->apache_test_script(shift) | ||
| 149 | : $self->apache_test_script | ||
| 150 | ); | ||
| 151 | |||
| 152 | # We need a class to run the tests from t/TEST. | ||
| 153 | my $class = pop || 'Apache::TestRunPerl'; | ||
| 154 | |||
| 155 | # Delete any existing instance of the file. | ||
| 156 | unlink $script if -e $script; | ||
| 157 | |||
| 158 | # Start the contents of t/TEST. | ||
| 159 | my $body = "BEGIN { eval { require blib && blib->import; } }\n"; | ||
| 160 | |||
| 161 | # Configure the arguments for t/TEST. | ||
| 162 | while (my($k, $v) = each %{ $self->apache_test_args }) { | ||
| 163 | $v =~ s/\|/\\|/g; | ||
| 164 | $body .= "\n\$Apache::TestConfig::Argv{'$k'} = q|$v|;\n"; | ||
| 165 | } | ||
| 166 | |||
| 167 | my $infile = "$script.PL"; | ||
| 168 | if (-f $infile) { | ||
| 169 | # Use the existing t/TEST.PL. | ||
| 170 | my $in = Symbol::gensym(); | ||
| 171 | open $in, "$infile" or die "Couldn't open $infile: $!"; | ||
| 172 | local $/; | ||
| 173 | $body .= <$in>; | ||
| 174 | close $in; | ||
| 175 | } else { | ||
| 176 | # Create t/TEST from scratch. | ||
| 177 | $body .= join "\n", | ||
| 178 | Apache::TestConfig->perlscript_header, | ||
| 179 | "use $class ();", | ||
| 180 | "$class->new->run(\@ARGV);"; | ||
| 181 | } | ||
| 182 | |||
| 183 | # Make it so! | ||
| 184 | print "Generating test running script $script\n" if $self->verbose; | ||
| 185 | Apache::Test::basic_config()->write_perlscript($script, $body); | ||
| 186 | $self->add_to_cleanup($self->apache_test_script); | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | 1; | ||
| 191 | __END__ |