Upgrade to CPAN-1.87_62
[p5sagit/p5-mst-13.2.git] / t / TEST
CommitLineData
8d063cd8 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
5# probably obsolete on the avoidance side, though still currrent
6# on the peculiarity side.)
8d063cd8 7
a687059c 8$| = 1;
9
80ed0dea 10# for testing TEST only
11#BEGIN { require '../lib/strict.pm'; strict->import() };
12#BEGIN { require '../lib/warnings.pm'; warnings->import() };
13
60e23f2f 14# Let tests know they're running in the perl core. Useful for modules
15# which live dual lives on CPAN.
16$ENV{PERL_CORE} = 1;
17
cc6ae9e5 18# remove empty elements due to insertion of empty symbols via "''p1'" syntax
19@ARGV = grep($_,@ARGV) if $^O eq 'VMS';
551405c4 20our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
cc6ae9e5 21
5d9a6404 22# Cheesy version of Getopt::Std. Maybe we should replace it with that.
80ed0dea 23{
24 my @argv = ();
5d9a6404 25 foreach my $idx (0..$#ARGV) {
b326da91 26 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
80ed0dea 27 $::core = 1 if $1 eq 'core';
28 $::verbose = 1 if $1 eq 'v';
29 $::torture = 1 if $1 eq 'torture';
30 $::with_utf8 = 1 if $1 eq 'utf8';
31 $::with_utf16 = 1 if $1 eq 'utf16';
80ed0dea 32 $::taintwarn = 1 if $1 eq 'taintwarn';
43651d81 33 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
485988ae 34 if ($1 =~ /^deparse(,.+)?$/) {
80ed0dea 35 $::deparse = 1;
36 $::deparse_opts = $1;
485988ae 37 }
5d9a6404 38 }
80ed0dea 39 @ARGV = @argv;
8d063cd8 40}
41
378cc40b 42chdir 't' if -f 't/TEST';
43
3e6e8be7 44die "You need to run \"make test\" first to set things up.\n"
196918b0 45 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
4633a7c4 46
7a315204 47if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
09187cb1 48 unless (-x 'perl.third') {
49 unless (-x '../perl.third') {
50 die "You need to run \"make perl.third first.\n";
51 }
52 else {
53 print "Symlinking ../perl.third as perl.third...\n";
54 die "Failed to symlink: $!\n"
55 unless symlink("../perl.third", "perl.third");
56 die "Symlinked but no executable perl.third: $!\n"
57 unless -x 'perl.third';
58 }
59 }
60}
61
3fb91a5e 62# check leakage for embedders
63$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
64
4633a7c4 65$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 66
24c841ba 67# Roll your own File::Find!
68use TestInit;
69use File::Spec;
28ffa55a 70if ($show_elapsed_time) { require Time::HiRes }
24c841ba 71my $curdir = File::Spec->curdir;
72my $updir = File::Spec->updir;
73
74sub _find_tests {
75 my($dir) = @_;
93e325a7 76 opendir DIR, $dir or die "Trouble opening $dir: $!";
a1886d87 77 foreach my $f (sort { $a cmp $b } readdir DIR) {
f458b6e8 78 next if $f eq $curdir or $f eq $updir or
0b834283 79 $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
24c841ba 80
f458b6e8 81 my $fullpath = File::Spec->catfile($dir, $f);
24c841ba 82
f458b6e8 83 _find_tests($fullpath) if -d $fullpath;
84 $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
85 push @ARGV, $fullpath if $f =~ /\.t$/;
24c841ba 86 }
87}
88
cc6ae9e5 89sub _quote_args {
90 my ($args) = @_;
91 my $argstring = '';
92
93 foreach (split(/\s+/,$args)) {
94 # In VMS protect with doublequotes because otherwise
95 # DCL will lowercase -- unless already doublequoted.
96 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
97 $argstring .= ' ' . $_;
98 }
99 return $argstring;
100}
101
6234cb77 102sub _populate_hash {
103 return map {$_, 1} split /\s+/, $_[0];
104}
105
24c841ba 106unless (@ARGV) {
9f3d340b 107 foreach my $dir (qw(base comp cmd run io op uni)) {
f458b6e8 108 _find_tests($dir);
24c841ba 109 }
80ed0dea 110 _find_tests("lib") unless $::core;
6234cb77 111 # Config.pm may be broken for make minitest. And this is only a refinement
112 # for skipping tests on non-default builds, so it is allowed to fail.
113 # What we want to to is make a list of extensions which we did not build.
114 my $configsh = File::Spec->catfile($updir, "config.sh");
115 my %skip;
116 if (-f $configsh) {
117 my (%extensions, %known_extensions);
118 open FH, $configsh or die "Can't open $configsh: $!";
119 while (<FH>) {
120 if (/^extensions=['"](.*)['"]$/) {
121 # Deliberate string interpolation to avoid triggering possible
122 # $1 resetting bugs.
123 %extensions = _populate_hash ("$1");
124 }
125 elsif (/^known_extensions=['"](.*)['"]$/) {
126 %known_extensions = _populate_hash ($1);
127 }
128 }
129 if (%extensions) {
130 if (%known_extensions) {
131 foreach (keys %known_extensions) {
132 $skip{$_}++ unless $extensions{$_};
133 }
134 } else {
135 warn "No known_extensions line found in $configsh";
136 }
137 } else {
138 warn "No extensions line found in $configsh";
139 }
140 }
cc6ae9e5 141 my $mani = File::Spec->catfile($updir, "MANIFEST");
7a315204 142 if (open(MANI, $mani)) {
00701878 143 my $ext_pat = $^O eq 'MSWin32' ? '(?:win32/)?ext' : 'ext';
f458b6e8 144 while (<MANI>) { # similar code in t/harness
00701878 145 if (m!^($ext_pat/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
80ed0dea 146 my $t = $1;
147 my $extension = $2;
148 if (!$::core || $t =~ m!^lib/[a-z]!)
5a6e071d 149 {
6234cb77 150 if (defined $extension) {
151 $extension =~ s!/t$!!;
152 # XXX Do I want to warn that I'm skipping these?
153 next if $skip{$extension};
154 }
80ed0dea 155 my $path = File::Spec->catfile($updir, $t);
73ddec28 156 push @ARGV, $path;
80ed0dea 157 $::path_to_name{$path} = $t;
5a6e071d 158 }
7a315204 159 }
160 }
35d88760 161 close MANI;
7a315204 162 } else {
f458b6e8 163 warn "$0: cannot open $mani: $!\n";
7a315204 164 }
80ed0dea 165 unless ($::core) {
d44161bf 166 _find_tests('pod');
e018f8be 167 _find_tests('x2p');
80ed0dea 168 _find_tests('japh') if $::torture;
e018f8be 169 }
8d063cd8 170}
171
80ed0dea 172if ($::deparse) {
f193aa2f 173 _testprogs('deparse', '', @ARGV);
174}
80ed0dea 175elsif ($::with_utf16) {
1de9afcd 176 for my $e (0, 1) {
177 for my $b (0, 1) {
178 print STDERR "# ENDIAN $e BOM $b\n";
179 my @UARGV;
180 for my $a (@ARGV) {
181 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
182 my $f = $e ? "v" : "n";
183 push @UARGV, $u;
184 unlink($u);
185 if (open(A, $a)) {
186 if (open(U, ">$u")) {
90f6ca78 187 print U pack("$f", 0xFEFF) if $b;
1de9afcd 188 while (<A>) {
189 print U pack("$f*", unpack("C*", $_));
190 }
80ed0dea 191 close(U);
1de9afcd 192 }
80ed0dea 193 close(A);
1de9afcd 194 }
195 }
196 _testprogs('perl', '', @UARGV);
197 unlink(@UARGV);
198 }
199 }
200}
f193aa2f 201else {
f193aa2f 202 _testprogs('perl', '', @ARGV);
485988ae 203}
6ee623d5 204
bb365837 205sub _testprogs {
80ed0dea 206 my ($type, $args, @tests) = @_;
6ee623d5 207
485988ae 208 print <<'EOT' if ($type eq 'deparse');
7a315204 209------------------------------------------------------------------------------
485988ae 210TESTING DEPARSER
7a315204 211------------------------------------------------------------------------------
485988ae 212EOT
213
80ed0dea 214 $::bad_files = 0;
73ddec28 215
cc6ae9e5 216 foreach my $t (@tests) {
80ed0dea 217 unless (exists $::path_to_name{$t}) {
f458b6e8 218 my $tname = File::Spec->catfile('t',$t);
219 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
220 $::path_to_name{$t} = $tname;
cc6ae9e5 221 }
73ddec28 222 }
908801fe 223 my $maxlen = 0;
80ed0dea 224 foreach (@::path_to_name{@tests}) {
73ddec28 225 s/\.\w+\z/./;
226 my $len = length ;
227 $maxlen = $len if $len > $maxlen;
088b5126 228 }
908801fe 229 # + 3 : we want three dots between the test name and the "ok"
80ed0dea 230 my $dotdotdot = $maxlen + 3 ;
7a834142 231 my $valgrind = 0;
da51b73c 232 my $valgrind_log = 'current.valgrind';
80ed0dea 233 my $total_files = @tests;
234 my $good_files = 0;
235 my $tested_files = 0;
236 my $totmax = 0;
ade55ef4 237 my %failed_tests;
80ed0dea 238
551405c4 239 while (my $test = shift @tests) {
28ffa55a 240 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
bb365837 241
bb365837 242 if ($test =~ /^$/) {
243 next;
6ee623d5 244 }
485988ae 245 if ($type eq 'deparse') {
246 if ($test eq "comp/redef.t") {
247 # Redefinition happens at compile time
248 next;
249 }
7a834142 250 elsif ($test =~ m{lib/Switch/t/}) {
485988ae 251 # B::Deparse doesn't support source filtering
252 next;
253 }
254 }
80ed0dea 255 my $te = $::path_to_name{$test} . '.'
256 x ($dotdotdot - length($::path_to_name{$test}));
cc6ae9e5 257
258 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
259 print $te;
260 $te = '';
261 }
bb365837 262
80ed0dea 263 # XXX DAPM %OVER not defined anywhere
264 # $test = $OVER{$test} if exists $OVER{$test};
7a315204 265
551405c4 266 open(SCRIPT,"<",$test) or die "Can't run $test.\n";
f458b6e8 267 $_ = <SCRIPT>;
268 close(SCRIPT) unless ($type eq 'deparse');
80ed0dea 269 if ($::with_utf16) {
90f6ca78 270 $_ =~ tr/\0//d;
271 }
80ed0dea 272 my $switch;
f458b6e8 273 if (/#!.*\bperl.*\s-\w*([tT])/) {
274 $switch = qq{"-$1"};
275 }
276 else {
80ed0dea 277 if ($::taintwarn) {
b26492ee 278 # not all tests are expected to pass with this option
279 $switch = '"-t"';
280 }
281 else {
282 $switch = '';
283 }
f458b6e8 284 }
6ee623d5 285
485988ae 286 my $file_opts = "";
287 if ($type eq 'deparse') {
288 # Look for #line directives which change the filename
289 while (<SCRIPT>) {
290 $file_opts .= ",-f$3$4"
291 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
292 }
293 close(SCRIPT);
294 }
7a315204 295
80ed0dea 296 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
4343e7c3 297 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae 298 if ($type eq 'deparse') {
80ed0dea 299 my $deparse_cmd =
127212b2 300 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
80ed0dea 301 "-l$::deparse_opts$file_opts ".
7a315204 302 "$test > $test.dp ".
303 "&& ./perl $testswitch $switch -I../lib $test.dp |";
80ed0dea 304 open(RESULTS, $deparse_cmd)
305 or print "can't deparse '$deparse_cmd': $!.\n";
485988ae 306 }
307 elsif ($type eq 'perl') {
a7da9a42 308 my $perl = $ENV{PERL} || './perl';
da51b73c 309 my $redir = $^O eq 'VMS' ? '2>&1' : '';
7a834142 310 if ($ENV{PERL_VALGRIND}) {
4f9287d3 311 my $valgrind = $ENV{VALGRIND} // 'valgrind';
312 $perl = "$valgrind --suppressions=perl.supp --leak-check=yes "
313 . "--leak-resolution=high --show-reachable=yes "
d76be4ee 314 . "--num-callers=50 --log-fd=3 $perl";
da51b73c 315 $redir = "3>$valgrind_log";
7a834142 316 }
80ed0dea 317 my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
318 . " $test $redir|";
be24517c 319 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2 320 }
9c8d215a 321 # Our environment may force us to use UTF-8, but we can't be sure that
322 # anything we're reading from will be generating (well formed) UTF-8
323 # This may not be the best way - possibly we should unset ${^OPEN} up
324 # top?
325 binmode RESULTS;
d638aca2 326
f458b6e8 327 my $failure;
328 my $next = 0;
329 my $seen_leader = 0;
330 my $seen_ok = 0;
20f82676 331 my $trailing_leader = 0;
80ed0dea 332 my $max;
43fe0836 333 my %todo;
bb365837 334 while (<RESULTS>) {
cc6ae9e5 335 next if /^\s*$/; # skip blank lines
80ed0dea 336 if ($::verbose) {
bb365837 337 print $_;
338 }
21c74f43 339 unless (/^\#/) {
20f82676 340 if ($trailing_leader) {
341 # shouldn't be anything following a postfix 1..n
a5890677 342 $failure = 'FAILED--extra output after trailing 1..n';
20f82676 343 last;
344 }
809908f7 345 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
20f82676 346 if ($seen_leader) {
a5890677 347 $failure = 'FAILED--seen duplicate leader';
20f82676 348 last;
349 }
bb365837 350 $max = $1;
f458b6e8 351 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837 352 $totmax += $max;
80ed0dea 353 $tested_files++;
f458b6e8 354 if ($seen_ok) {
20f82676 355 # 1..n appears at end of file
356 $trailing_leader = 1;
357 if ($next != $max) {
a5890677 358 $failure = "FAILED--expected $max tests, saw $next";
20f82676 359 last;
360 }
361 }
362 else {
363 $next = 0;
364 }
f458b6e8 365 $seen_leader = 1;
bb365837 366 }
367 else {
f458b6e8 368 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
21c74f43 369 unless ($seen_leader) {
370 unless ($seen_ok) {
20f82676 371 $next = 0;
21c74f43 372 }
37ce32a7 373 }
21c74f43 374 $seen_ok = 1;
20f82676 375 $next++;
f458b6e8 376 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
377 $num = $next unless $num;
378
379 if ($num == $next) {
380
eac7c728 381 # SKIP is essentially the same as TODO for t/TEST
382 # this still conforms to TAP:
383 # http://search.cpan.org/dist/Test-Harness/lib/Test/Harness/TAP.pod
384 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
21c74f43 385 $istodo = 1 if $todo{$num};
386
387 if( $not && !$istodo ) {
20f82676 388 $failure = "FAILED at test $num";
21c74f43 389 last;
390 }
20f82676 391 }
392 else {
f458b6e8 393 $failure ="FAILED--expected test $next, saw test $num";
20f82676 394 last;
37ce32a7 395 }
f458b6e8 396 }
397 elsif (/^Bail out!\s*(.*)/i) { # magic words
398 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837 399 }
400 else {
dbf51d07 401 # module tests are allowed extra output,
402 # because Test::Harness allows it
403 next if $test =~ /^\W*(ext|lib)\b/;
a5890677 404 $failure = "FAILED--unexpected output at test $next";
20f82676 405 last;
bb365837 406 }
8d063cd8 407 }
408 }
409 }
bb365837 410 close RESULTS;
20f82676 411
412 if (not defined $failure) {
a5890677 413 $failure = 'FAILED--no leader found' unless $seen_leader;
20f82676 414 }
415
7a834142 416 if ($ENV{PERL_VALGRIND}) {
da51b73c 417 my @valgrind;
418 if (-e $valgrind_log) {
419 if (open(V, $valgrind_log)) {
420 @valgrind = <V>;
421 close V;
422 } else {
423 warn "$0: Failed to open '$valgrind_log': $!\n";
424 }
425 }
7a834142 426 if (@valgrind) {
d44161bf 427 my $leaks = 0;
428 my $errors = 0;
7a834142 429 for my $i (0..$#valgrind) {
430 local $_ = $valgrind[$i];
d44161bf 431 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
432 $errors += $1; # there may be multiple error summaries
433 } elsif (/^==\d+== LEAK SUMMARY:/) {
434 for my $off (1 .. 4) {
435 if ($valgrind[$i+$off] =~
436 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
437 $leaks += $1;
438 }
439 }
7a834142 440 }
441 }
d44161bf 442 if ($errors or $leaks) {
da51b73c 443 if (rename $valgrind_log, "$test.valgrind") {
d44161bf 444 $valgrind++;
445 } else {
446 warn "$0: Failed to create '$test.valgrind': $!\n";
7a834142 447 }
448 }
449 } else {
450 warn "No valgrind output?\n";
451 }
da51b73c 452 if (-e $valgrind_log) {
453 unlink $valgrind_log
454 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
455 }
7a834142 456 }
485988ae 457 if ($type eq 'deparse') {
458 unlink "./$test.dp";
459 }
211f317f 460 if ($ENV{PERL_3LOG}) {
461 my $tpp = $test;
3716a21d 462 $tpp =~ s:^\.\./::;
9c54ecba 463 $tpp =~ s:/:_:g;
3716a21d 464 $tpp =~ s:\.t$:.3log:;
465 rename("perl.3log", $tpp) ||
466 die "rename: perl3.log to $tpp: $!\n";
211f317f 467 }
20f82676 468 if (not defined $failure and $next != $max) {
a5890677 469 $failure="FAILED--expected $max tests, saw $next";
20f82676 470 }
471
472 if (defined $failure) {
473 print "${te}$failure\n";
474 $::bad_files++;
ade55ef4 475 if ($test =~ /^base/) {
476 die "Failed a basic test ($test) -- cannot continue.\n";
20f82676 477 }
ade55ef4 478 ++$failed_tests{$test};
20f82676 479 }
480 else {
bb365837 481 if ($max) {
551405c4 482 my $elapsed;
483 if ( $show_elapsed_time ) {
484 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
485 }
486 else {
487 $elapsed = "";
488 }
489 print "${te}ok$elapsed\n";
80ed0dea 490 $good_files++;
bb365837 491 }
492 else {
cc6ae9e5 493 print "${te}skipping test on this platform\n";
80ed0dea 494 $tested_files -= 1;
bb365837 495 }
bcce72a7 496 }
551405c4 497 } # while tests
8d063cd8 498
80ed0dea 499 if ($::bad_files == 0) {
20f82676 500 if ($good_files) {
bb365837 501 print "All tests successful.\n";
502 # XXX add mention of 'perlbug -ok' ?
503 }
504 else {
505 die "FAILED--no tests were run for some reason.\n";
506 }
8d063cd8 507 }
bb365837 508 else {
80ed0dea 509 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
ade55ef4 510 my $s = $::bad_files == 1 ? "" : "s";
511 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
512 for my $test ( sort keys %failed_tests ) {
513 print "\t$test\n";
bb365837 514 }
4e4732c1 515 warn <<'SHRDLU_1';
f7d228c6 516### Since not all tests were successful, you may want to run some of
517### them individually and examine any diagnostic messages they produce.
518### See the INSTALL document's section on "make test".
4e4732c1 519SHRDLU_1
80ed0dea 520 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
f7d228c6 521### You have a good chance to get more information by running
522### ./perl harness
523### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1 524SHRDLU_2
f458b6e8 525 if (eval {require Config; import Config; 1}) {
80ed0dea 526 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
4e4732c1 527 warn <<SHRDLU_3;
f7d228c6 528### You may have to set your dynamic library search path,
529### $p, to point to the build directory:
4e4732c1 530SHRDLU_3
f458b6e8 531 if (exists $ENV{$p} && $ENV{$p} ne '') {
4e4732c1 532 warn <<SHRDLU_4a;
f7d228c6 533### setenv $p `pwd`:\$$p; cd t; ./perl harness
534### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
535### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1 536SHRDLU_4a
f458b6e8 537 } else {
4e4732c1 538 warn <<SHRDLU_4b;
f7d228c6 539### setenv $p `pwd`; cd t; ./perl harness
540### $p=`pwd`; export $p; cd t; ./perl harness
541### export $p=`pwd`; cd t; ./perl harness
4e4732c1 542SHRDLU_4b
f458b6e8 543 }
4e4732c1 544 warn <<SHRDLU_5;
f7d228c6 545### for csh-style shells, like tcsh; or for traditional/modern
546### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1 547SHRDLU_5
f458b6e8 548 }
afd33fa9 549 }
bb365837 550 }
80ed0dea 551 my ($user,$sys,$cuser,$csys) = times;
f47304e9 552 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
80ed0dea 553 $user,$sys,$cuser,$csys,$tested_files,$totmax);
7a834142 554 if ($ENV{PERL_VALGRIND}) {
555 my $s = $valgrind == 1 ? '' : 's';
556 print "$valgrind valgrind report$s created.\n", ;
557 }
6ee623d5 558}
80ed0dea 559exit ($::bad_files != 0);
ade55ef4 560
561# ex: set ts=8 sts=4 sw=4 noet: