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