This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Scalar-List-Utils customization bump
[perl5.git] / t / harness
CommitLineData
a5f75d66
AD
1#!./perl
2
3# We suppose that perl _mostly_ works at this moment, so may use
4# sophisticated testing.
5
aa689395 6BEGIN {
7 chdir 't' if -d 't';
122a0375 8 @INC = '../lib'; # pick up only this build's lib
aa689395 9}
c537bcda 10
e018f8be
JH
11my $torture; # torture testing?
12
abd39864 13use TAP::Harness 3.13;
9a4933c3 14use strict;
68bb50b0 15use Config;
a5f75d66 16
c537bcda
NC
17$::do_nothing = $::do_nothing = 1;
18require './TEST';
be075caf 19our $Valgrind_Log;
c537bcda 20
abd39864
MS
21my $Verbose = 0;
22$Verbose++ while @ARGV && $ARGV[0] eq '-v' && shift;
a5f75d66 23
be075caf
MH
24# For valgrind summary output
25my $htoolnm;
26my $hgrind_ct;
27
12558422 28if ($ARGV[0] && $ARGV[0] eq '-torture') {
e018f8be
JH
29 shift;
30 $torture = 1;
31}
32
60e23f2f
MS
33# Let tests know they're running in the perl core. Useful for modules
34# which live dual lives on CPAN.
35$ENV{PERL_CORE} = 1;
36
0279961e 37my (@tests, $re);
122a0375 38
40996b78
AT
39# [.VMS]TEST.COM calls harness with empty arguments, so clean-up @ARGV
40@ARGV = grep $_ && length( $_ ) => @ARGV;
41
9ae5a6c3
NC
42sub _extract_tests;
43sub _extract_tests {
44 # This can probably be done more tersely with a map, but I doubt that it
45 # would be as clear
46 my @results;
47 foreach (@_) {
48 my $ref = ref $_;
49 if ($ref) {
50 if ($ref eq 'ARRAY') {
51 push @results, _extract_tests @$_;
52 } elsif ($ref eq 'HASH') {
53 push @results, _extract_tests values %$_;
54 } else {
55 die "Unknown reference type $ref";
56 }
57 } else {
0ae187c2 58 push @results, glob $_;
9ae5a6c3
NC
59 }
60 }
61 @results;
62}
63
12558422 64if ($ARGV[0] && $ARGV[0]=~/^-re/) {
8a76aa1f
YO
65 if ($ARGV[0]!~/=/) {
66 shift;
67 $re=join "|",@ARGV;
68 @ARGV=();
69 } else {
70 (undef,$re)=split/=/,shift;
71 }
72}
73
0279961e 74my $jobs = $ENV{TEST_JOBS};
abd39864 75my ($rules, $state, $color);
cd1b270f
B
76if ($ENV{HARNESS_OPTIONS}) {
77 for my $opt ( split /:/, $ENV{HARNESS_OPTIONS} ) {
78 if ( $opt =~ /^j(\d*)$/ ) {
79 $jobs ||= $1 || 9;
80 }
cd1b270f 81 elsif ( $opt eq 'c' ) {
abd39864 82 $color = 1;
cd1b270f
B
83 }
84 else {
85 die "Unknown HARNESS_OPTIONS item: $opt\n";
86 }
87 }
88}
0279961e 89
7a315204 90if (@ARGV) {
0279961e 91 # If you want these run in speed order, just use prove
4efb34a6
NIS
92 if ($^O eq 'MSWin32') {
93 @tests = map(glob($_),@ARGV);
94 }
95 else {
96 @tests = @ARGV;
97 }
68bb50b0
NC
98 # This is a hack to force config_heavy.pl to be loaded, before the
99 # prep work for running a test changes directory.
100 1 if $Config{d_fork};
7a315204 101} else {
9ae5a6c3
NC
102 # Ideally we'd get somewhere close to Tux's Oslo rules
103 # my $rules = {
104 # par => [
105 # { seq => '../ext/DB_File/t/*' },
106 # { seq => '../ext/IO_Compress_Zlib/t/*' },
9ae5a6c3
NC
107 # { seq => '../lib/ExtUtils/t/*' },
108 # '*'
109 # ]
110 # };
111
ee901278 112 # but for now, run all directories in sequence.
9ae5a6c3 113
b695f709 114 unless (@tests) {
0279961e 115 my @seq = <base/*.t>;
9ae5a6c3 116
560a5958 117 my @next = qw(comp run cmd io re opbasic op uni mro lib porting perf);
9ae5a6c3
NC
118 push @next, 'japh' if $torture;
119 push @next, 'win32' if $^O eq 'MSWin32';
7019aa11 120 push @next, 'benchmark' if $ENV{PERL_BENCHMARK};
ff5db609 121 push @next, 'bigmem' if $ENV{PERL_TEST_MEMORY};
0279961e
NC
122 # Hopefully TAP::Parser::Scheduler will support this syntax soon.
123 # my $next = { par => '{' . join (',', @next) . '}/*.t' };
124 my $next = { par => [
2f4cffa7 125 map { "$_/*.t" } @next
e6867818 126 ] };
0279961e
NC
127 @tests = _extract_tests ($next);
128
129 # This is a bit of a game, because we only want to sort these tests in
130 # speed order. base/*.t wants to run first, and ext,lib etc last and in
131 # MANIFEST order
132 if ($jobs) {
133 require App::Prove::State;
134 $state = App::Prove::State->new({ store => 'test_state' });
135 $state->apply_switch('slow', 'save');
136 # For some reason get_tests returns *all* the tests previously run,
137 # (in the right order), not simply the selection in @tests
138 # (in the right order). Not sure if this is a bug or a feature.
139 # Whatever, *we* are only interested in the ones that are in @tests
140 my %seen;
141 @seen{@tests} = ();
142 @tests = grep {exists $seen{$_} } $state->get_tests(0, @tests);
143 }
144 @tests = (@seq, @tests);
145 push @seq, $next;
9ae5a6c3
NC
146
147 my @last;
a3323f52
NC
148 push @last, sort { lc $a cmp lc $b }
149 _tests_from_manifest($Config{extensions}, $Config{known_extensions});
226de479
NC
150 my %times;
151 if ($state) {
152 # Where known, collate the elapsed times by test name
153 foreach ($state->results->tests()) {
154 $times{$_->name} = $_->elapsed();
155 }
156 }
157
158 my %dir;
159 my %total_time;
160
161 for (@last) {
133fac12
MM
162 if ($^O eq 'MSWin32') {
163 s,\\,/,g; # canonicalize path
164 };
8f776eae
NC
165 # Treat every file matching lib/*.t as a "directory"
166 m!\A(\.\./lib/[^/]+\.t\z|.*[/])! or die "'$_'";
226de479
NC
167 push @{$dir{$1}}, $_;
168 $total_time{$1} += $times{$_} || 0;
169 }
170
0279961e 171 push @tests, @last;
9ae5a6c3 172
fc279e46
NC
173 # Generate T::H schedule rules that run the contents of each directory
174 # sequentially.
8f776eae 175 push @seq, { par => [ map { s!/$!/*!; { seq => $_ } } sort {
226de479
NC
176 # Directories, ordered by total time descending then name ascending
177 $total_time{$b} <=> $total_time{$a} || $a cmp $b
178 } keys %dir ] };
9ae5a6c3
NC
179
180 $rules = { seq => \@seq };
7a315204
JH
181 }
182}
22a65f1e
GS
183if ($^O eq 'MSWin32') {
184 s,\\,/,g for @tests;
185}
8a76aa1f
YO
186@tests=grep /$re/, @tests
187 if $re;
9ae5a6c3 188
cfa56252
JH
189# Allow eg ./perl t/harness t/op/lc.t
190for (@tests) {
191 if (-f "../$_") {
192 $_ = "../$_";
193 s{^\.\./t/}{};
194 }
195}
196
4013a0e1
VP
197my %options;
198
199my $type = 'perl';
200
201# Load TAP::Parser now as otherwise it could be required in the short time span
202# in which the harness process chdirs into ext/Dist
203require TAP::Parser;
204
abd39864
MS
205my $h = TAP::Harness->new({
206 rules => $rules,
207 color => $color,
208 jobs => $jobs,
209 verbosity => $Verbose,
640eedb9 210 timer => $ENV{HARNESS_TIMER},
4013a0e1
VP
211 exec => sub {
212 my ($harness, $test) = @_;
213
214 my $options = $options{$test};
215 if (!defined $options) {
216 $options = $options{$test} = _scan_test($test, $type);
217 }
218
be075caf
MH
219 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
220
4013a0e1
VP
221 return [ split ' ', _cmd($options, $type) ];
222 },
abd39864 223});
a0f20b65 224
be075caf
MH
225# Print valgrind output after test completes
226if ($ENV{PERL_VALGRIND}) {
227 $h->callback(
228 after_test => sub {
229 my ($job) = @_;
230 my $test = $job->[0];
231 my $vfile = "$test.valgrind-current";
232 $vfile =~ s/^.*\///;
233
234 if ( (! -z $vfile) && open(my $voutput, '<', $vfile)) {
235 print "$test: Valgrind output:\n";
236 print "$test: $_" for <$voutput>;
237 close($voutput);
238 }
239
240 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
241
242 _check_valgrind(\$htoolnm, \$hgrind_ct, \$test);
243 }
244 );
245}
246
a0f20b65 247if ($state) {
e8fb11d7 248 $h->callback(
a0f20b65
NC
249 after_test => sub {
250 $state->observe_test(@_);
e8fb11d7 251 }
a0f20b65
NC
252 );
253 $h->callback(
254 after_runtests => sub {
255 $state->commit(@_);
256 }
257 );
9ae5a6c3 258}
a0f20b65
NC
259
260$h->callback(
261 parser_args => sub {
4013a0e1
VP
262 my ($args, $job) = @_;
263 my $test = $job->[0];
264 _before_fork($options{$test});
265 push @{ $args->{switches} }, "-I../../lib";
a0f20b65
NC
266 }
267 );
4013a0e1
VP
268
269$h->callback(
270 made_parser => sub {
271 my ($parser, $job) = @_;
272 my $test = $job->[0];
273 my $options = delete $options{$test};
274 _after_fork($options);
275 }
276 );
277
f483babb 278my $agg = $h->runtests(@tests);
be075caf 279_cleanup_valgrind(\$htoolnm, \$hgrind_ct);
f483babb 280exit $agg->has_errors ? 1 : 0;