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 / TEST
CommitLineData
63217fa2 1#!./perl
8d063cd8 2
8d063cd8 3# This is written in a peculiar style, since we're trying to avoid
1de9afcd 4# most of the constructs we'll be testing for. (This comment is
8bdd21ca 5# probably obsolete on the avoidance side, though still current
1de9afcd 6# on the peculiarity side.)
8d063cd8 7
c537bcda
NC
8# t/TEST and t/harness need to share code. The logical way to do this would be
9# to have the common code in a file both require or use. However, t/TEST needs
10# to still work, to generate test results, even if require isn't working, so
11# we cannot do that. t/harness has no such restriction, so it is quite
12# acceptable to have it require t/TEST.
13
14# In which case, we need to stop t/TEST actually running tests, as all
15# t/harness needs are its subroutines.
16
94708f6d
JH
17# Measure the elapsed wallclock time.
18my $t0 = time();
19
2722144b
MH
20# If we're doing deparse tests, ignore failures for these
21my $deparse_failures;
22
23# And skip even running these
24my $deparse_skips;
2adbc9b6
NC
25
26# directories with special sets of test switches
27my %dir_to_switch =
28 (base => '',
29 comp => '',
30 run => '',
30b6e591 31 '../ext/File-Glob/t' => '-I.. -MTestInit', # FIXME - tests assume t/
2adbc9b6
NC
32 );
33
cb596ae4 34# "not absolute" is the default, as it saves some fakery within TestInit
8bdd21ca 35# which can perturb tests, and takes CPU. Working with the upstream author of
41239ce7
NC
36# any of these, to figure out how to remove them from this list, considered
37# "a good thing".
38my %abs = (
41239ce7
NC
39 '../cpan/Archive-Tar' => 1,
40 '../cpan/AutoLoader' => 1,
41 '../cpan/CPAN' => 1,
41239ce7
NC
42 '../cpan/Devel-PPPort' => 1,
43 '../cpan/Encode' => 1,
2ca4a82e 44 '../cpan/ExtUtils-Command' => 1,
41239ce7 45 '../cpan/ExtUtils-Constant' => 1,
d393d7e5 46 '../cpan/ExtUtils-Install' => 1,
41239ce7 47 '../cpan/ExtUtils-MakeMaker' => 1,
854a00d8 48 '../cpan/ExtUtils-Manifest' => 1,
41239ce7
NC
49 '../cpan/File-Fetch' => 1,
50 '../cpan/IPC-Cmd' => 1,
51 '../cpan/IPC-SysV' => 1,
52 '../cpan/Locale-Codes' => 1,
41239ce7
NC
53 '../cpan/Module-Load' => 1,
54 '../cpan/Module-Load-Conditional' => 1,
41239ce7
NC
55 '../cpan/Parse-CPAN-Meta' => 1,
56 '../cpan/Pod-Simple' => 1,
41239ce7 57 '../cpan/Test-Simple' => 1,
41239ce7 58 '../cpan/podlators' => 1,
2a6dc374 59 '../dist/Cwd' => 1,
cc79184a 60 '../dist/ExtUtils-ParseXS' => 1,
c0504019 61 '../dist/Tie-File' => 1,
41239ce7 62 );
2a6dc374 63
2adbc9b6 64my %temp_no_core =
dc5320d3 65 ('../cpan/B-Debug' => 1,
d371e02a 66 '../cpan/Compress-Raw-Bzip2' => 1,
c6f84bbd 67 '../cpan/Compress-Raw-Zlib' => 1,
b2861970 68 '../cpan/Devel-PPPort' => 1,
e41cfb92 69 '../cpan/Getopt-Long' => 1,
3fd969f4 70 '../cpan/IO-Compress' => 1,
b7c8007e 71 '../cpan/MIME-Base64' => 1,
a636c943 72 '../cpan/parent' => 1,
832db5b1 73 '../cpan/Parse-CPAN-Meta' => 1,
490d1888 74 '../cpan/Pod-Simple' => 1,
f4e6b692 75 '../cpan/podlators' => 1,
e0ee75a6 76 '../cpan/Test-Simple' => 1,
325914f9 77 '../cpan/Tie-RefHash' => 1,
a3e5f045 78 '../cpan/Unicode-Collate' => 1,
c62c1f54 79 '../cpan/Unicode-Normalize' => 1,
2adbc9b6
NC
80 );
81
2574563e
TB
82# delete env vars that may influence the results
83# but allow override via *_TEST env var if wanted
84# (e.g. PERL5OPT_TEST=-d:NYTProf)
b6646683
DG
85my @bad_env_vars = qw(
86 PERL5LIB PERLLIB PERL5OPT
87 PERL_YAML_BACKEND PERL_JSON_BACKEND
88);
89
90for my $envname (@bad_env_vars) {
2574563e
TB
91 my $override = $ENV{"${envname}_TEST"};
92 if (defined $override) {
93 warn "$0: $envname=$override\n";
94 $ENV{$envname} = $override;
95 }
96 else {
97 delete $ENV{$envname};
98 }
99}
60e23f2f 100
be075caf
MH
101# Location to put the Valgrind log.
102our $Valgrind_Log;
103
104my %skip = (
105 '.' => 1,
106 '..' => 1,
107 'CVS' => 1,
108 'RCS' => 1,
109 'SCCS' => 1,
110 '.svn' => 1,
111 );
112
75e21add 113
63217fa2 114if ($::do_nothing) {
75e21add
JH
115 return 1;
116}
117
a2d3de13
NC
118$| = 1;
119
120# for testing TEST only
121#BEGIN { require '../lib/strict.pm'; "strict"->import() };
122#BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
123
cc6ae9e5
CB
124# remove empty elements due to insertion of empty symbols via "''p1'" syntax
125@ARGV = grep($_,@ARGV) if $^O eq 'VMS';
551405c4 126our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
cc6ae9e5 127
18869dc6
NC
128# Cheesy version of Getopt::Std. We can't replace it with that, because we
129# can't rely on require working.
80ed0dea
DM
130{
131 my @argv = ();
5d9a6404 132 foreach my $idx (0..$#ARGV) {
b03030b8 133 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
7019aa11 134 $::benchmark = 1 if $1 eq 'benchmark';
80ed0dea
DM
135 $::core = 1 if $1 eq 'core';
136 $::verbose = 1 if $1 eq 'v';
137 $::torture = 1 if $1 eq 'torture';
138 $::with_utf8 = 1 if $1 eq 'utf8';
139 $::with_utf16 = 1 if $1 eq 'utf16';
80ed0dea 140 $::taintwarn = 1 if $1 eq 'taintwarn';
485988ae 141 if ($1 =~ /^deparse(,.+)?$/) {
80ed0dea
DM
142 $::deparse = 1;
143 $::deparse_opts = $1;
2722144b 144 _process_deparse_config();
485988ae 145 }
5d9a6404 146 }
80ed0dea 147 @ARGV = @argv;
8d063cd8
LW
148}
149
378cc40b 150chdir 't' if -f 't/TEST';
ab662740
NC
151if (-f 'TEST' && -f 'harness' && -d '../lib') {
152 @INC = '../lib';
153}
378cc40b 154
3e6e8be7 155die "You need to run \"make test\" first to set things up.\n"
196918b0 156 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
4633a7c4 157
3fb91a5e
GS
158# check leakage for embedders
159$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
da6a8325
GG
160# check existence of all symbols
161$ENV{PERL_DL_NONLAZY} = 1 unless exists $ENV{PERL_DL_NONLAZY};
3fb91a5e 162
4633a7c4 163$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 164
28ffa55a 165if ($show_elapsed_time) { require Time::HiRes }
b49055e9 166my %timings = (); # testname => [@et] pairs if $show_elapsed_time.
7ebf5c89 167
18869dc6 168# Roll your own File::Find!
ac681bb3 169sub _find_tests { our @found=(); push @ARGV, _find_files('\.t$', $_[0]) }
c96083ea
JC
170sub _find_files {
171 my($patt, @dirs) = @_;
172 for my $dir (@dirs) {
173 opendir DIR, $dir or die "Trouble opening $dir: $!";
174 foreach my $f (sort { $a cmp $b } readdir DIR) {
175 next if $skip{$f};
176
177 my $fullpath = "$dir/$f";
178
179 if (-d $fullpath) {
180 _find_files($patt, $fullpath);
181 } elsif ($f =~ /$patt/) {
182 push @found, $fullpath;
183 }
7ebf5c89 184 }
24c841ba 185 }
c96083ea 186 @found;
24c841ba
MS
187}
188
3fd4b359
MS
189
190# Scan the text of the test program to find switches and special options
191# we might need to apply.
192sub _scan_test {
193 my($test, $type) = @_;
194
195 open(my $script, "<", $test) or die "Can't read $test.\n";
196 my $first_line = <$script>;
197
198 $first_line =~ tr/\0//d if $::with_utf16;
199
200 my $switch = "";
201 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
79b01a68 202 $switch = "-$1";
3fd4b359
MS
203 } else {
204 if ($::taintwarn) {
205 # not all tests are expected to pass with this option
79b01a68 206 $switch = '-t';
3fd4b359
MS
207 } else {
208 $switch = '';
209 }
210 }
211
212 my $file_opts = "";
213 if ($type eq 'deparse') {
214 # Look for #line directives which change the filename
215 while (<$script>) {
11ea18f2 216 $file_opts = $file_opts . ",-f$3$4"
3fd4b359
MS
217 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
218 }
219 }
220
491c9572 221 close $script;
84650816 222
923e061d
MS
223 my $perl = './perl';
224 my $lib = '../lib';
491c9572
VP
225 my $run_dir;
226 my $return_dir;
227
2adbc9b6
NC
228 $test =~ /^(.+)\/[^\/]+/;
229 my $dir = $1;
2adbc9b6 230 my $testswitch = $dir_to_switch{$dir};
5ed59b83 231 if (!defined $testswitch) {
55d965ca 232 if ($test =~ s!^(\.\./(cpan|dist|ext)/[^/]+)/t!t!) {
491c9572 233 $run_dir = $1;
2adbc9b6
NC
234 $return_dir = '../../t';
235 $lib = '../../lib';
1ff5bc37 236 $perl = '../../t/perl';
6d1e6673 237 $testswitch = "-I../.. -MTestInit=U2T";
4b05cdbd 238 if ($2 eq 'cpan' || $2 eq 'dist') {
41239ce7 239 if($abs{$run_dir}) {
55d965ca
NC
240 $testswitch = $testswitch . ',A';
241 }
242 if ($temp_no_core{$run_dir}) {
243 $testswitch = $testswitch . ',NC';
244 }
2adbc9b6 245 }
76cc22ec
NC
246 } elsif ($test =~ m!^\.\./lib!) {
247 $testswitch = '-I.. -MTestInit=U1'; # -T will remove . from @INC
2adbc9b6 248 } else {
30b6e591 249 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
2adbc9b6 250 }
5ed59b83 251 }
923e061d 252
9fb03e61 253 my $utf8 = ($::with_utf8 || $::with_utf16) ? "-I$lib -Mutf8" : '';
84650816 254
9b37184d 255 my %options = (
491c9572
VP
256 perl => $perl,
257 lib => $lib,
258 test => $test,
259 run_dir => $run_dir,
260 return_dir => $return_dir,
261 testswitch => $testswitch,
262 utf8 => $utf8,
263 file => $file_opts,
264 switch => $switch,
9b37184d
VP
265 );
266
267 return \%options;
491c9572
VP
268}
269
d1fe220a
VP
270sub _cmd {
271 my($options, $type) = @_;
491c9572 272
d1fe220a 273 my $test = $options->{test};
491c9572 274
d1fe220a 275 my $cmd;
84650816 276 if ($type eq 'deparse') {
491c9572
VP
277 my $perl = "$options->{perl} $options->{testswitch}";
278 my $lib = $options->{lib};
d1fe220a
VP
279
280 $cmd = (
491c9572 281 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
84650816
MS
282 "-l$::deparse_opts$options->{file} ".
283 "$test > $test.dp ".
d1fe220a
VP
284 "&& $perl $options->{switch} -I$lib $test.dp"
285 );
84650816
MS
286 }
287 elsif ($type eq 'perl') {
491c9572 288 my $perl = $options->{perl};
84650816
MS
289 my $redir = $^O eq 'VMS' ? '2>&1' : '';
290
291 if ($ENV{PERL_VALGRIND}) {
e07ce2e4 292 my $perl_supp = $options->{return_dir} ? "$options->{return_dir}/perl.supp" : "perl.supp";
c7b956bb 293 my $valgrind_exe = $ENV{VALGRIND} // 'valgrind';
be075caf
MH
294 if ($options->{run_dir}) {
295 $Valgrind_Log = "$options->{run_dir}/$Valgrind_Log";
296 }
84650816 297 my $vg_opts = $ENV{VG_OPTS}
be075caf 298 // "--log-file=$Valgrind_Log "
c7b956bb
JC
299 . "--suppressions=$perl_supp --leak-check=yes "
300 . "--leak-resolution=high --show-reachable=yes "
be075caf
MH
301 . "--num-callers=50 --track-origins=yes";
302 # Force logging if not asked for (so cachegrind reporting works below)
303 if ($vg_opts !~ /--log-file/) {
304 $vg_opts = "--log-file=$Valgrind_Log $vg_opts";
305 }
c7b956bb 306 $perl = "$valgrind_exe $vg_opts $perl";
84650816
MS
307 }
308
491c9572 309 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
d1fe220a 310 $cmd = $perl . _quote_args($args) . " $test $redir";
84650816 311 }
d1fe220a
VP
312 return $cmd;
313}
314
9324df28
VP
315sub _before_fork {
316 my ($options) = @_;
317
318 if ($options->{run_dir}) {
319 my $run_dir = $options->{run_dir};
320 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
321 }
322
be075caf
MH
323 # Remove previous valgrind output otherwise it will interfere
324 my $test = $options->{test};
325
326 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
327
328 if ($ENV{PERL_VALGRIND} && -e $Valgrind_Log) {
329 unlink $Valgrind_Log
330 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
331 }
332
9324df28
VP
333 return;
334}
335
336sub _after_fork {
337 my ($options) = @_;
338
339 if ($options->{return_dir}) {
340 my $return_dir = $options->{return_dir};
341 chdir $return_dir
342 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
343 }
344
345 return;
346}
347
d1fe220a 348sub _run_test {
999051eb 349 my ($test, $type) = @_;
d1fe220a
VP
350
351 my $options = _scan_test($test, $type);
999051eb
VP
352 # $test might have changed if we're in ext/Foo, so don't use it anymore
353 # from now on. Use $options->{test} instead.
d1fe220a 354
9324df28 355 _before_fork($options);
d1fe220a
VP
356
357 my $cmd = _cmd($options, $type);
358
359 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
360
9324df28 361 _after_fork($options);
2adbc9b6 362
84650816
MS
363 # Our environment may force us to use UTF-8, but we can't be sure that
364 # anything we're reading from will be generating (well formed) UTF-8
365 # This may not be the best way - possibly we should unset ${^OPEN} up
366 # top?
367 binmode $results;
368
369 return $results;
370}
371
cc6ae9e5
CB
372sub _quote_args {
373 my ($args) = @_;
374 my $argstring = '';
375
376 foreach (split(/\s+/,$args)) {
377 # In VMS protect with doublequotes because otherwise
378 # DCL will lowercase -- unless already doublequoted.
379 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
11ea18f2 380 $argstring = $argstring . ' ' . $_;
cc6ae9e5
CB
381 }
382 return $argstring;
383}
384
6234cb77 385sub _populate_hash {
a3323f52 386 return unless defined $_[0];
6234cb77
NC
387 return map {$_, 1} split /\s+/, $_[0];
388}
389
a3323f52
NC
390sub _tests_from_manifest {
391 my ($extensions, $known_extensions) = @_;
6234cb77 392 my %skip;
a3323f52
NC
393 my %extensions = _populate_hash($extensions);
394 my %known_extensions = _populate_hash($known_extensions);
395
396 foreach (keys %known_extensions) {
11ea18f2 397 $skip{$_} = 1 unless $extensions{$_};
6234cb77 398 }
a3323f52
NC
399
400 my @results;
7ebf5c89 401 my $mani = '../MANIFEST';
7a315204 402 if (open(MANI, $mani)) {
18869dc6 403 while (<MANI>) {
a193a2db 404 if (m!^((?:cpan|dist|ext)/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
80ed0dea
DM
405 my $t = $1;
406 my $extension = $2;
a3323f52 407 if (!$::core || $t =~ m!^lib/[a-z]!) {
6234cb77 408 if (defined $extension) {
b12cb1ba 409 $extension =~ s!/t(:?/\S+)*$!!;
6234cb77
NC
410 # XXX Do I want to warn that I'm skipping these?
411 next if $skip{$extension};
142f6a0d 412 my $flat_extension = $extension;
6ebb0601
CB
413 $flat_extension =~ s!-!/!g;
414 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
6234cb77 415 }
7ebf5c89 416 my $path = "../$t";
a3323f52 417 push @results, $path;
80ed0dea 418 $::path_to_name{$path} = $t;
5a6e071d 419 }
7a315204
JH
420 }
421 }
35d88760 422 close MANI;
7a315204 423 } else {
f458b6e8 424 warn "$0: cannot open $mani: $!\n";
7a315204 425 }
a3323f52
NC
426 return @results;
427}
428
429unless (@ARGV) {
430 # base first, as TEST bails out if that can't run
431 # then comp, to validate that require works
432 # then run, to validate that -M works
433 # then we know we can -MTestInit for everything else, making life simpler
560a5958 434 foreach my $dir (qw(base comp run cmd io re opbasic op uni mro perf)) {
a3323f52
NC
435 _find_tests($dir);
436 }
cc306f49
NC
437 unless ($::core) {
438 _find_tests('porting');
439 _find_tests("lib");
440 }
a3323f52
NC
441 # Config.pm may be broken for make minitest. And this is only a refinement
442 # for skipping tests on non-default builds, so it is allowed to fail.
443 # What we want to to is make a list of extensions which we did not build.
444 my $configsh = '../config.sh';
445 my ($extensions, $known_extensions);
446 if (-f $configsh) {
447 open FH, $configsh or die "Can't open $configsh: $!";
448 while (<FH>) {
449 if (/^extensions=['"](.*)['"]$/) {
450 $extensions = $1;
451 }
452 elsif (/^known_extensions=['"](.*)['"]$/) {
453 $known_extensions = $1;
454 }
455 }
456 if (!defined $known_extensions) {
457 warn "No known_extensions line found in $configsh";
458 }
459 if (!defined $extensions) {
460 warn "No extensions line found in $configsh";
461 }
462 }
463 # The "complex" constructions of list return from a subroutine, and push of
464 # a list, might fail if perl is really hosed, but they aren't needed for
465 # make minitest, and the building of extensions will likely also fail if
466 # something is that badly wrong.
467 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
80ed0dea 468 unless ($::core) {
80ed0dea 469 _find_tests('japh') if $::torture;
7e71d4a4 470 _find_tests('benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
ff5db609 471 _find_tests('bigmem') if $ENV{PERL_TEST_MEMORY};
e018f8be 472 }
8d063cd8
LW
473}
474
80ed0dea 475if ($::deparse) {
f193aa2f
MS
476 _testprogs('deparse', '', @ARGV);
477}
80ed0dea 478elsif ($::with_utf16) {
1de9afcd
RGS
479 for my $e (0, 1) {
480 for my $b (0, 1) {
481 print STDERR "# ENDIAN $e BOM $b\n";
482 my @UARGV;
483 for my $a (@ARGV) {
484 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
485 my $f = $e ? "v" : "n";
486 push @UARGV, $u;
487 unlink($u);
488 if (open(A, $a)) {
489 if (open(U, ">$u")) {
90f6ca78 490 print U pack("$f", 0xFEFF) if $b;
1de9afcd
RGS
491 while (<A>) {
492 print U pack("$f*", unpack("C*", $_));
493 }
80ed0dea 494 close(U);
1de9afcd 495 }
80ed0dea 496 close(A);
1de9afcd
RGS
497 }
498 }
499 _testprogs('perl', '', @UARGV);
500 unlink(@UARGV);
501 }
502 }
503}
f193aa2f 504else {
f193aa2f 505 _testprogs('perl', '', @ARGV);
485988ae 506}
6ee623d5 507
bb365837 508sub _testprogs {
80ed0dea 509 my ($type, $args, @tests) = @_;
6ee623d5 510
485988ae 511 print <<'EOT' if ($type eq 'deparse');
7a315204 512------------------------------------------------------------------------------
485988ae 513TESTING DEPARSER
7a315204 514------------------------------------------------------------------------------
485988ae
RH
515EOT
516
80ed0dea 517 $::bad_files = 0;
73ddec28 518
cc6ae9e5 519 foreach my $t (@tests) {
80ed0dea 520 unless (exists $::path_to_name{$t}) {
7ebf5c89 521 my $tname = "t/$t";
f458b6e8 522 $::path_to_name{$t} = $tname;
cc6ae9e5 523 }
73ddec28 524 }
908801fe 525 my $maxlen = 0;
80ed0dea 526 foreach (@::path_to_name{@tests}) {
f7b9b043 527 s/\.\w+\z/ /; # space gives easy doubleclick to select fname
73ddec28
RB
528 my $len = length ;
529 $maxlen = $len if $len > $maxlen;
088b5126 530 }
908801fe 531 # + 3 : we want three dots between the test name and the "ok"
80ed0dea 532 my $dotdotdot = $maxlen + 3 ;
c7b956bb 533 my $grind_ct = 0; # count of non-empty valgrind reports
80ed0dea
DM
534 my $total_files = @tests;
535 my $good_files = 0;
33c0d182 536 my $tested_files = 0;
fb7ba3c8 537 my $totmax = 0;
ade55ef4 538 my %failed_tests;
c96083ea 539 my $toolnm; # valgrind, cachegrind, perf
80ed0dea 540
551405c4 541 while (my $test = shift @tests) {
25a2b27f
JC
542 my ($test_start_time, @starttimes) = 0;
543 if ($show_elapsed_time) {
544 $test_start_time = Time::HiRes::time();
545 # times() reports usage by TEST, but we want usage of each
546 # testprog it calls, so record accumulated times now,
547 # subtract them out afterwards. Ideally, we'd take times
548 # in BEGIN/END blocks (giving better visibility of self vs
549 # children of each testprog), but that would require some
550 # IPC to send results back here, or a completely different
8bdd21ca 551 # collection scheme (Storable isn't tuned for incremental use)
25a2b27f
JC
552 @starttimes = times;
553 }
bb365837
GS
554 if ($test =~ /^$/) {
555 next;
6ee623d5 556 }
2722144b
MH
557 if ($type eq 'deparse' && $test =~ $deparse_skips) {
558 next;
485988ae 559 }
80ed0dea 560 my $te = $::path_to_name{$test} . '.'
f7b9b043 561 x ($dotdotdot - length($::path_to_name{$test})) .' ';
cc6ae9e5
CB
562
563 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
564 print $te;
565 $te = '';
566 }
bb365837 567
be075caf
MH
568 (local $Valgrind_Log = "$test.valgrind-current") =~ s/^.*\///;
569
999051eb 570 my $results = _run_test($test, $type);
d638aca2 571
33c0d182 572 my $failure;
e1a5ed7a
JH
573 my $next = 0;
574 my $seen_leader = 0;
575 my $seen_ok = 0;
576 my $trailing_leader = 0;
577 my $max;
43fe0836 578 my %todo;
84650816 579 while (<$results>) {
cc6ae9e5 580 next if /^\s*$/; # skip blank lines
615b7a35
JM
581 if (/^1..$/ && ($^O eq 'VMS')) {
582 # VMS pipe bug inserts blank lines.
5403a9a2 583 my $l2 = <$results>;
615b7a35 584 if ($l2 =~ /^\s*$/) {
5403a9a2 585 $l2 = <$results>;
615b7a35
JM
586 }
587 $_ = '1..' . $l2;
588 }
80ed0dea 589 if ($::verbose) {
bb365837
GS
590 print $_;
591 }
21c74f43 592 unless (/^\#/) {
fb7ba3c8
JH
593 if ($trailing_leader) {
594 # shouldn't be anything following a postfix 1..n
595 $failure = 'FAILED--extra output after trailing 1..n';
596 last;
597 }
809908f7 598 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
fb7ba3c8
JH
599 if ($seen_leader) {
600 $failure = 'FAILED--seen duplicate leader';
601 last;
602 }
603 $max = $1;
604 %todo = map { $_ => 1 } split / /, $3 if $3;
605 $totmax = $totmax + $max;
606 $tested_files = $tested_files + 1;
607 if ($seen_ok) {
608 # 1..n appears at end of file
609 $trailing_leader = 1;
610 if ($next != $max) {
611 $failure = "FAILED--expected $max tests, saw $next";
20f82676
DM
612 last;
613 }
614 }
fb7ba3c8
JH
615 else {
616 $next = 0;
20f82676 617 }
fb7ba3c8 618 $seen_leader = 1;
bb365837
GS
619 }
620 else {
fb7ba3c8
JH
621 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
622 unless ($seen_leader) {
623 unless ($seen_ok) {
624 $next = 0;
625 }
626 }
627 $seen_ok = 1;
11ea18f2 628 $next = $next + 1;
fb7ba3c8
JH
629 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
630 $num = $next unless $num;
631
632 if ($num == $next) {
f458b6e8 633
eac7c728
MB
634 # SKIP is essentially the same as TODO for t/TEST
635 # this still conforms to TAP:
464a08e7 636 # http://testanything.org/wiki/index.php/TAP_specification
eac7c728 637 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
21c74f43
A
638 $istodo = 1 if $todo{$num};
639
640 if( $not && !$istodo ) {
20f82676 641 $failure = "FAILED at test $num";
21c74f43
A
642 last;
643 }
20f82676 644 }
fb7ba3c8
JH
645 else {
646 $failure ="FAILED--expected test $next, saw test $num";
647 last;
648 }
f458b6e8
MS
649 }
650 elsif (/^Bail out!\s*(.*)/i) { # magic words
651 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837
GS
652 }
653 else {
dbf51d07
YST
654 # module tests are allowed extra output,
655 # because Test::Harness allows it
4d834435 656 next if $test =~ /^\W*(cpan|dist|ext|lib)\b/;
a5890677 657 $failure = "FAILED--unexpected output at test $next";
20f82676 658 last;
bb365837 659 }
8d063cd8
LW
660 }
661 }
662 }
84650816 663 close $results;
20f82676 664
33c0d182 665 if (not defined $failure) {
fb7ba3c8 666 $failure = 'FAILED--no leader found' unless $seen_leader;
20f82676
DM
667 }
668
be075caf
MH
669 _check_valgrind(\$toolnm, \$grind_ct, \$test);
670
2722144b 671 if ($type eq 'deparse' && !$ENV{KEEP_DEPARSE_FILES}) {
485988ae
RH
672 unlink "./$test.dp";
673 }
33c0d182 674 if (not defined $failure and $next != $max) {
fb7ba3c8 675 $failure="FAILED--expected $max tests, saw $next";
20f82676
DM
676 }
677
33c0d182
JH
678 if( !defined $failure # don't mask a test failure
679 and $? )
680 {
343bc60d
MS
681 $failure = "FAILED--non-zero wait status: $?";
682 }
683
2722144b
MH
684 # Deparse? Should it have passed or failed?
685 if ($type eq 'deparse' && $test =~ $deparse_failures) {
686 if (!$failure) {
687 # Wait, it didn't fail? Great news! Tell someone!
688 $failure = "FAILED--all tests passed but test should have failed";
689 } else {
690 # Bah, still failing. Mask it.
691 print "${te}skipped\n";
692 $tested_files = $tested_files - 1;
693 next;
694 }
695 }
696
33c0d182 697 if (defined $failure) {
20f82676 698 print "${te}$failure\n";
11ea18f2 699 $::bad_files = $::bad_files + 1;
25a4a90c
KW
700 if ($test =~ /^base/ && ! defined &DynaLoader::boot_DynaLoader) {
701 # Die if running under minitest (no DynaLoader). Otherwise
702 # keep going, as we know that Perl basically works, or we
703 # would not have been able to actually compile it all the way.
704 die "Failed a basic test ($test) under minitest -- cannot continue.\n";
20f82676 705 }
11ea18f2 706 $failed_tests{$test} = 1;
20f82676
DM
707 }
708 else {
fb7ba3c8 709 if ($max) {
b49055e9 710 my ($elapsed, $etms) = ("", 0);
551405c4 711 if ( $show_elapsed_time ) {
b49055e9 712 $etms = (Time::HiRes::time() - $test_start_time) * 1000;
25a2b27f
JC
713 $elapsed = sprintf(" %8.0f ms", $etms);
714
715 my (@endtimes) = times;
716 $endtimes[$_] -= $starttimes[$_] for 0..$#endtimes;
717 splice @endtimes, 0, 2; # drop self/harness times
718 $_ *= 1000 for @endtimes; # and scale to ms
719 $timings{$test} = [$etms,@endtimes];
720 $elapsed .= sprintf(" %5.0f ms", $_) for @endtimes;
551405c4
AL
721 }
722 print "${te}ok$elapsed\n";
11ea18f2 723 $good_files = $good_files + 1;
bb365837
GS
724 }
725 else {
6b202754 726 print "${te}skipped\n";
11ea18f2 727 $tested_files = $tested_files - 1;
bb365837 728 }
bcce72a7 729 }
551405c4 730 } # while tests
8d063cd8 731
80ed0dea 732 if ($::bad_files == 0) {
20f82676 733 if ($good_files) {
bb365837
GS
734 print "All tests successful.\n";
735 # XXX add mention of 'perlbug -ok' ?
736 }
737 else {
738 die "FAILED--no tests were run for some reason.\n";
739 }
8d063cd8 740 }
bb365837 741 else {
80ed0dea 742 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
ade55ef4
AL
743 my $s = $::bad_files == 1 ? "" : "s";
744 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
745 for my $test ( sort keys %failed_tests ) {
746 print "\t$test\n";
bb365837 747 }
4e4732c1 748 warn <<'SHRDLU_1';
f7d228c6
JH
749### Since not all tests were successful, you may want to run some of
750### them individually and examine any diagnostic messages they produce.
751### See the INSTALL document's section on "make test".
4e4732c1 752SHRDLU_1
80ed0dea 753 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
f7d228c6
JH
754### You have a good chance to get more information by running
755### ./perl harness
756### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1 757SHRDLU_2
f458b6e8 758 if (eval {require Config; import Config; 1}) {
80ed0dea 759 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
4e4732c1 760 warn <<SHRDLU_3;
f7d228c6
JH
761### You may have to set your dynamic library search path,
762### $p, to point to the build directory:
4e4732c1 763SHRDLU_3
f458b6e8 764 if (exists $ENV{$p} && $ENV{$p} ne '') {
4e4732c1 765 warn <<SHRDLU_4a;
f7d228c6
JH
766### setenv $p `pwd`:\$$p; cd t; ./perl harness
767### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
768### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1 769SHRDLU_4a
f458b6e8 770 } else {
4e4732c1 771 warn <<SHRDLU_4b;
f7d228c6
JH
772### setenv $p `pwd`; cd t; ./perl harness
773### $p=`pwd`; export $p; cd t; ./perl harness
774### export $p=`pwd`; cd t; ./perl harness
4e4732c1 775SHRDLU_4b
f458b6e8 776 }
4e4732c1 777 warn <<SHRDLU_5;
f7d228c6
JH
778### for csh-style shells, like tcsh; or for traditional/modern
779### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1 780SHRDLU_5
f458b6e8 781 }
afd33fa9 782 }
bb365837 783 }
94708f6d 784 printf "Elapsed: %d sec\n", time() - $t0;
80ed0dea 785 my ($user,$sys,$cuser,$csys) = times;
8e03ad8f
JC
786 my $tot = sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d",
787 $user,$sys,$cuser,$csys,$tested_files,$totmax);
788 print "$tot\n";
789 if ($good_files) {
790 if (-d $show_elapsed_time) {
56e28cb0
JH
791 # HARNESS_TIMER = <a-directory>. Save timings etc to
792 # storable file there. NB: the test cds to ./t/, so
793 # relative path must account for that, ie ../../perf
794 # points to dir next to source tree.
8e03ad8f 795 require Storable;
133d407a
JH
796 my @dt = localtime;
797 $dt[5] += 1900; $dt[4] += 1; # fix year, month
798 my $fn = "$show_elapsed_time/".join('-', @dt[5,4,3,2,1]).".ttimes";
56e28cb0 799 Storable::store({ perf => \%timings,
8e03ad8f
JC
800 gather_conf_platform_info(),
801 total => $tot,
802 }, $fn);
803 print "wrote storable file: $fn\n";
804 }
805 }
be075caf
MH
806
807 _cleanup_valgrind(\$toolnm, \$grind_ct);
6ee623d5 808}
80ed0dea 809exit ($::bad_files != 0);
ade55ef4 810
8e03ad8f
JC
811# Collect platform, config data that should allow comparing
812# performance data between different machines. With enough data,
813# and/or clever statistical analysis, it should be possible to
814# determine the effect of config choices, more memory, etc
815
816sub gather_conf_platform_info {
817 # currently rather quick & dirty, and subject to change
818 # for both content and format.
819 require Config;
820 my (%conf, @platform) = ();
821 $conf{$_} = $Config::Config{$_} for
822 grep /cc|git|config_arg\d+/, keys %Config::Config;
823 if (-f '/proc/cpuinfo') {
824 open my $fh, '/proc/cpuinfo' or warn "$!: /proc/cpuinfo\n";
825 @platform = grep /name|cpu/, <$fh>;
826 chomp $_ for @platform;
827 }
828 unshift @platform, $^O;
829
830 return (
831 conf => \%conf,
832 platform => {cpu => \@platform,
833 mem => [ grep s/\s+/ /,
834 grep chomp, `free` ],
835 load => [ grep chomp, `uptime` ],
836 },
837 host => (grep chomp, `hostname -f`),
56e28cb0 838 version => '0.03', # bump for conf, platform, or data collection changes
8e03ad8f
JC
839 );
840}
841
be075caf
MH
842sub _check_valgrind {
843 return unless $ENV{PERL_VALGRIND};
844
845 my ($toolnm, $grind_ct, $test) = @_;
846
847 $$toolnm = $ENV{VALGRIND};
848 $$toolnm =~ s|.*/||; # keep basename
849 my @valgrind; # gets content of file
850 if (-e $Valgrind_Log) {
851 if (open(V, $Valgrind_Log)) {
852 @valgrind = <V>;
853 close V;
854 } else {
855 warn "$0: Failed to open '$Valgrind_Log': $!\n";
856 }
857 }
858 if ($ENV{VG_OPTS} =~ /(cachegrind)/ or $$toolnm =~ /(perf)/) {
859 $$toolnm = $1;
860 if ($$toolnm eq 'perf') {
e73fd51e 861 # append perfs subcommand, not just stat
be075caf
MH
862 my ($sub) = split /\s/, $ENV{VG_OPTS};
863 $$toolnm .= "-$sub";
864 }
865 if (rename $Valgrind_Log, "$$test.$$toolnm") {
866 $$grind_ct++;
867 } else {
868 warn "$0: Failed to create '$$test.$$toolnm': $!\n";
869 }
870 }
871 elsif (@valgrind) {
872 my $leaks = 0;
873 my $errors = 0;
874 for my $i (0..$#valgrind) {
875 local $_ = $valgrind[$i];
876 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
877 $errors = $errors + $1; # there may be multiple error summaries
878 } elsif (/^==\d+== LEAK SUMMARY:/) {
879 for my $off (1 .. 4) {
880 if ($valgrind[$i+$off] =~
881 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
882 $leaks = $leaks + $1;
883 }
884 }
885 }
886 }
887 if ($errors or $leaks) {
888 if (rename $Valgrind_Log, "$$test.valgrind") {
889 $$grind_ct = $$grind_ct + 1;
890 } else {
891 warn "$0: Failed to create '$$test.valgrind': $!\n";
892 }
893 }
894 } else {
895 # Quiet wasn't asked for? Something may be amiss
896 if ($ENV{VG_OPTS} && $ENV{VG_OPTS} !~ /(^|\s)(-q|--quiet)(\s|$)/) {
897 warn "No valgrind output?\n";
898 }
899 }
900 if (-e $Valgrind_Log) {
901 unlink $Valgrind_Log
902 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
903 }
904}
905
906sub _cleanup_valgrind {
907 return unless $ENV{PERL_VALGRIND};
908
909 my ($toolnm, $grind_ct) = @_;
910 my $s = $$grind_ct == 1 ? '' : 's';
911 print "$$grind_ct valgrind report$s created.\n", ;
912 if ($$toolnm eq 'cachegrind') {
913 # cachegrind leaves a lot of cachegrind.out.$pid litter
914 # around the tree, find and delete them
915 unlink _find_files('cachegrind.out.\d+$',
916 qw ( ../t ../cpan ../ext ../dist/ ));
917 }
918}
919
2722144b
MH
920# Generate regexps of known bad filenames / skips from Porting/deparse-skips.txt
921my $in;
922
923sub _process_deparse_config {
924 my @deparse_failures;
925 my @deparse_skips;
926
927 my $f = '../Porting/deparse-skips.txt';
928
929 my $skips;
930 if (!open($skips, '<', $f)) {
931 warn "Failed to find $f: $!\n";
932 return;
933 }
934
935 while(<$skips>) {
936 if (/__DEPARSE_FAILURES__/) {
937 $in = \@deparse_failures; next;
938 } elsif (/__DEPARSE_SKIPS__/) {
939 $in = \@deparse_skips; next;
940 } elsif (!$in) {
941 next;
942 }
943
944 s/#.*$//; # Kill comments
945 s/\s+$//; # And trailing whitespace
946
947 next unless $_;
948
949 push @$in, $_;
950 }
951
952 for my $f (@deparse_failures, @deparse_skips) {
953 if ($f =~ m|/$|) { # Dir? Skip everything below it
954 $f = qr/\Q$f\E.*/;
955 } else {
956 $f = qr/\Q$f\E/;
957 }
958 }
959
960 $deparse_failures = join('|', @deparse_failures);
961 $deparse_failures = qr/^(?:$deparse_failures)$/;
962
963 $deparse_skips = join('|', @deparse_skips);
964 $deparse_skips = qr/^(?:$deparse_skips)$/;
965}
966
ade55ef4 967# ex: set ts=8 sts=4 sw=4 noet: