3 # We suppose that perl _mostly_ works at this moment, so may use
4 # sophisticated testing.
8 @INC = '../lib'; # pick up only this build's lib
11 my $torture; # torture testing?
13 use TAP::Harness 3.13;
17 $::do_nothing = $::do_nothing = 1;
22 $Verbose++ while @ARGV && $ARGV[0] eq '-v' && shift;
24 # For valgrind summary output
28 if ($ARGV[0] && $ARGV[0] eq '-torture') {
33 # Let tests know they're running in the perl core. Useful for modules
34 # which live dual lives on CPAN.
37 my (@tests, @re, @anti_re);
39 # [.VMS]TEST.COM calls harness with empty arguments, so clean-up @ARGV
40 @ARGV = grep $_ && length( $_ ) => @ARGV;
44 # This can probably be done more tersely with a map, but I doubt that it
50 if ($ref eq 'ARRAY') {
51 push @results, _extract_tests @$_;
52 } elsif ($ref eq 'HASH') {
53 push @results, _extract_tests values %$_;
55 die "Unknown reference type $ref";
58 push @results, glob $_;
64 while ($ARGV[0] && $ARGV[0]=~/^-(n?)re/) {
65 my $ary= $1 ? \@anti_re : \@re;
67 if ( $ARGV[0] !~ /=/ ) {
69 while (@ARGV and $ARGV[0] !~ /^-/) {
70 push @$ary, shift @ARGV;
73 push @$ary, (split/=/,shift @ARGV)[1];
77 my $jobs = $ENV{TEST_JOBS};
78 my ($rules, $state, $color);
79 if ($ENV{HARNESS_OPTIONS}) {
80 for my $opt ( split /:/, $ENV{HARNESS_OPTIONS} ) {
81 if ( $opt =~ /^j(\d*)$/ ) {
84 elsif ( $opt eq 'c' ) {
88 die "Unknown HARNESS_OPTIONS item: $opt\n";
94 # If you want these run in speed order, just use prove
95 if ($^O eq 'MSWin32') {
96 @tests = map(glob($_),@ARGV);
101 # This is a hack to force config_heavy.pl to be loaded, before the
102 # prep work for running a test changes directory.
103 1 if $Config{d_fork};
105 # Ideally we'd get somewhere close to Tux's Oslo rules
108 # { seq => '../ext/DB_File/t/*' },
109 # { seq => '../ext/IO_Compress_Zlib/t/*' },
110 # { seq => '../lib/ExtUtils/t/*' },
115 # but for now, run all directories in sequence.
118 my @seq = <base/*.t>;
120 my @next = qw(comp run cmd io re opbasic op uni mro lib porting perf);
121 push @next, 'japh' if $torture;
122 push @next, 'win32' if $^O eq 'MSWin32';
123 push @next, 'benchmark' if $ENV{PERL_BENCHMARK};
124 push @next, 'bigmem' if $ENV{PERL_TEST_MEMORY};
125 # Hopefully TAP::Parser::Scheduler will support this syntax soon.
126 # my $next = { par => '{' . join (',', @next) . '}/*.t' };
127 my $next = { par => [
128 map { "$_/*.t" } @next
130 @tests = _extract_tests ($next);
132 # This is a bit of a game, because we only want to sort these tests in
133 # speed order. base/*.t wants to run first, and ext,lib etc last and in
136 require App::Prove::State;
137 $state = App::Prove::State->new({ store => 'test_state' });
138 $state->apply_switch('slow', 'save');
139 # For some reason get_tests returns *all* the tests previously run,
140 # (in the right order), not simply the selection in @tests
141 # (in the right order). Not sure if this is a bug or a feature.
142 # Whatever, *we* are only interested in the ones that are in @tests
145 @tests = grep {exists $seen{$_} } $state->get_tests(0, @tests);
147 @tests = (@seq, @tests);
152 _tests_from_manifest($Config{extensions}, $Config{known_extensions});
155 # Where known, collate the elapsed times by test name
156 foreach ($state->results->tests()) {
157 $times{$_->name} = $_->elapsed();
166 # Preprocess the list of tests
168 if ($^O eq 'MSWin32') {
169 s,\\,/,g; # canonicalize path
172 # Keep a list of the distinct directory names, and another list of
173 # those which contain a file whose name begins with a 0
175 ( .*? ) # $1 is the directory path name
177 ( [^/]* \.t ) # $2 is the .t name
182 $all_dirs{$path} = 1;
183 $serials{$path} = 1 if $2 =~ / \A 0 /x;
187 # We assume that the reason a test file's name begins with a 0 is to
188 # order its execution among the tests in its directory. Hence, a
189 # directory containing such files should be tested in serial order.
191 # Add exceptions to the above rule
192 for (qw(ext/Pod-Html/t cpan/IO-Zlib/t ext/File-Find/t)) {
196 my @nonexistent_serials = grep { not exists $all_dirs{$_} } keys %serials;
197 if (@nonexistent_serials) {
198 die "These directories to be run serially don't exist."
199 . " Check your spelling:\n" . join "\n", @nonexistent_serials;
202 # Remove the serial testing directories from the list of all
203 # directories. The remaining ones are testable in parallel. Make the
204 # parallel list a scalar with names separated by '|' so that below
205 # they will be added to a regular expression.
206 my $non_serials = join "|", grep { not exists $serials{$_} } keys %all_dirs;
211 # Treat every file in each non-serial directory as its own
212 # "directory", so that it can be executed in parallel
213 m! \A ( \.\. / (?: $non_serials )
214 / [^/]+ \.t \z | .* [/] ) !x
216 push @{$dir{$1}}, $_;
218 # This file contributes time to the total needed for the directory
220 $total_time{$1} += $times{$_} || 0;
222 #print STDERR __LINE__, join "\n", sort { $total_time{$b} <=> $total_time{$a} } keys %dir, " ";
226 # Generate T::H schedule rules that run the contents of each directory
228 push @seq, { par => [ map { s!/$!/*!; { seq => $_ } } sort {
229 # Directories, ordered by total time descending then name ascending
230 $total_time{$b} <=> $total_time{$a} || lc $a cmp lc $b
233 $rules = { seq => \@seq };
236 if ($^O eq 'MSWin32') {
239 if (@re or @anti_re) {
241 foreach my $test (@tests) {
244 foreach my $re (@re) {
245 $keep = 1 if $test=~/$re/;
251 foreach my $anti_re (@anti_re) {
252 $keep = 0 if $test=~/$anti_re/;
256 push @keepers, $test;
262 # Allow eg ./perl t/harness t/op/lc.t
264 if (! -f $_ && !/^\.\./ && -f "../$_") {
274 # Load TAP::Parser now as otherwise it could be required in the short time span
275 # in which the harness process chdirs into ext/Dist
278 my $h = TAP::Harness->new({
282 verbosity => $Verbose,
283 timer => $ENV{HARNESS_TIMER},
285 my ($harness, $test) = @_;
287 my $options = $options{$test};
288 if (!defined $options) {
289 $options = $options{$test} = _scan_test($test, $type);
292 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
294 return [ split ' ', _cmd($options, $type) ];
298 # Print valgrind output after test completes
299 if ($ENV{PERL_VALGRIND}) {
303 my $test = $job->[0];
304 my $vfile = "$test.valgrind-current";
307 if ( (! -z $vfile) && open(my $voutput, '<', $vfile)) {
308 print "$test: Valgrind output:\n";
309 print "$test: $_" for <$voutput>;
313 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
315 _check_valgrind(\$htoolnm, \$hgrind_ct, \$test);
323 $state->observe_test(@_);
327 after_runtests => sub {
335 my ($args, $job) = @_;
336 my $test = $job->[0];
337 _before_fork($options{$test});
338 push @{ $args->{switches} }, "-I../../lib";
344 my ($parser, $job) = @_;
345 my $test = $job->[0];
346 my $options = delete $options{$test};
347 _after_fork($options);
351 my $agg = $h->runtests(@tests);
352 _cleanup_valgrind(\$htoolnm, \$hgrind_ct);
353 exit $agg->has_errors ? 1 : 0;