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