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