add deprecation warning for my $x if 0
[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
4# most of the constructs we'll be testing for.
5
a687059c 6$| = 1;
7
60e23f2f 8# Let tests know they're running in the perl core. Useful for modules
9# which live dual lives on CPAN.
10$ENV{PERL_CORE} = 1;
11
cc6ae9e5 12# remove empty elements due to insertion of empty symbols via "''p1'" syntax
13@ARGV = grep($_,@ARGV) if $^O eq 'VMS';
14
5d9a6404 15# Cheesy version of Getopt::Std. Maybe we should replace it with that.
b326da91 16@argv = ();
5d9a6404 17if ($#ARGV >= 0) {
18 foreach my $idx (0..$#ARGV) {
b326da91 19 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
5a6e071d 20 $core = 1 if $1 eq 'core';
5d9a6404 21 $verbose = 1 if $1 eq 'v';
e018f8be 22 $torture = 1 if $1 eq 'torture';
5d9a6404 23 $with_utf= 1 if $1 eq 'utf8';
b26492ee 24 $bytecompile = 1 if $1 eq 'bytecompile';
25 $compile = 1 if $1 eq 'compile';
26 $taintwarn = 1 if $1 eq 'taintwarn';
43651d81 27 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
485988ae 28 if ($1 =~ /^deparse(,.+)?$/) {
29 $deparse = 1;
30 $deparse_opts = $1;
31 }
5d9a6404 32 }
8d063cd8 33}
b326da91 34@ARGV = @argv;
8d063cd8 35
378cc40b 36chdir 't' if -f 't/TEST';
37
3e6e8be7 38die "You need to run \"make test\" first to set things up.\n"
196918b0 39 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
4633a7c4 40
7a315204 41if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
09187cb1 42 unless (-x 'perl.third') {
43 unless (-x '../perl.third') {
44 die "You need to run \"make perl.third first.\n";
45 }
46 else {
47 print "Symlinking ../perl.third as perl.third...\n";
48 die "Failed to symlink: $!\n"
49 unless symlink("../perl.third", "perl.third");
50 die "Symlinked but no executable perl.third: $!\n"
51 unless -x 'perl.third';
52 }
53 }
54}
55
3fb91a5e 56# check leakage for embedders
57$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
58
4633a7c4 59$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 60
24c841ba 61# Roll your own File::Find!
62use TestInit;
63use File::Spec;
64my $curdir = File::Spec->curdir;
65my $updir = File::Spec->updir;
66
67sub _find_tests {
68 my($dir) = @_;
93e325a7 69 opendir DIR, $dir or die "Trouble opening $dir: $!";
a1886d87 70 foreach my $f (sort { $a cmp $b } readdir DIR) {
24c841ba 71 next if $f eq $curdir or $f eq $updir;
72
cc6ae9e5 73 my $fullpath = File::Spec->catfile($dir, $f);
24c841ba 74
75 _find_tests($fullpath) if -d $fullpath;
cc6ae9e5 76 $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
24c841ba 77 push @ARGV, $fullpath if $f =~ /\.t$/;
78 }
79}
80
cc6ae9e5 81sub _quote_args {
82 my ($args) = @_;
83 my $argstring = '';
84
85 foreach (split(/\s+/,$args)) {
86 # In VMS protect with doublequotes because otherwise
87 # DCL will lowercase -- unless already doublequoted.
88 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
89 $argstring .= ' ' . $_;
90 }
91 return $argstring;
92}
93
24c841ba 94unless (@ARGV) {
9f3d340b 95 foreach my $dir (qw(base comp cmd run io op uni)) {
24c841ba 96 _find_tests($dir);
97 }
5a6e071d 98 _find_tests("lib") unless $core;
cc6ae9e5 99 my $mani = File::Spec->catfile($updir, "MANIFEST");
7a315204 100 if (open(MANI, $mani)) {
80ffb5f9 101 while (<MANI>) { # similar code in t/harness
9c9537e6 102 if (m!^(ext/\S+/?(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
5a6e071d 103 $t = $1;
104 if (!$core || $t =~ m!^lib/[a-z]!)
105 {
cc6ae9e5 106 $path = File::Spec->catfile($updir, $t);
73ddec28 107 push @ARGV, $path;
108 $name{$path} = $t;
5a6e071d 109 }
7a315204 110 }
111 }
35d88760 112 close MANI;
7a315204 113 } else {
114 warn "$0: cannot open $mani: $!\n";
115 }
e018f8be 116 unless ($core) {
d44161bf 117 _find_tests('pod');
e018f8be 118 _find_tests('x2p');
119 _find_tests('japh') if $torture;
120 }
8d063cd8 121}
122
7a315204 123# Tests known to cause infinite loops for the perlcc tests.
595ae481 124# %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
24c841ba 125%infinite = ();
6ee623d5 126
485988ae 127if ($deparse) {
f193aa2f 128 _testprogs('deparse', '', @ARGV);
129}
1df34986 130elsif( $compile ) {
131 _testprogs('compile', '', @ARGV);
132}
133elsif( $bytecompile ) {
134 _testprogs('bytecompile', '', @ARGV);
f193aa2f 135}
136else {
137 _testprogs('compile', '', @ARGV) if -e "../testcompile";
138 _testprogs('perl', '', @ARGV);
485988ae 139}
6ee623d5 140
bb365837 141sub _testprogs {
142 $type = shift @_;
f193aa2f 143 $args = shift;
bb365837 144 @tests = @_;
6ee623d5 145
bb365837 146 print <<'EOT' if ($type eq 'compile');
7a315204 147------------------------------------------------------------------------------
6ee623d5 148TESTING COMPILER
7a315204 149------------------------------------------------------------------------------
bb365837 150EOT
151
485988ae 152 print <<'EOT' if ($type eq 'deparse');
7a315204 153------------------------------------------------------------------------------
485988ae 154TESTING DEPARSER
7a315204 155------------------------------------------------------------------------------
485988ae 156EOT
157
566ece03 158 print <<EOT if ($type eq 'bytecompile');
1df34986 159------------------------------------------------------------------------------
160TESTING BYTECODE COMPILER
161------------------------------------------------------------------------------
162EOT
163
595ae481 164 $ENV{PERLCC_TIMEOUT} = 120
9636a016 165 if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
ef712cf7 166
bb365837 167 $bad = 0;
168 $good = 0;
169 $total = @tests;
170 $files = 0;
171 $totmax = 0;
73ddec28 172
cc6ae9e5 173 foreach my $t (@tests) {
174 unless (exists $name{$t}) {
175 my $tname = File::Spec->catfile('t',$t);
176 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
177 $name{$t} = $tname;
178 }
73ddec28 179 }
908801fe 180 my $maxlen = 0;
73ddec28 181 foreach (@name{@tests}) {
182 s/\.\w+\z/./;
183 my $len = length ;
184 $maxlen = $len if $len > $maxlen;
088b5126 185 }
908801fe 186 # + 3 : we want three dots between the test name and the "ok"
73ddec28 187 $dotdotdot = $maxlen + 3 ;
7a834142 188 my $valgrind = 0;
da51b73c 189 my $valgrind_log = 'current.valgrind';
bb365837 190 while ($test = shift @tests) {
191
192 if ( $infinite{$test} && $type eq 'compile' ) {
595ae481 193 print STDERR "$test creates infinite loop! Skipping.\n";
bb365837 194 next;
6ee623d5 195 }
bb365837 196 if ($test =~ /^$/) {
197 next;
6ee623d5 198 }
485988ae 199 if ($type eq 'deparse') {
200 if ($test eq "comp/redef.t") {
201 # Redefinition happens at compile time
202 next;
203 }
7a834142 204 elsif ($test =~ m{lib/Switch/t/}) {
485988ae 205 # B::Deparse doesn't support source filtering
206 next;
207 }
208 }
cc6ae9e5 209 $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
210
211 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
212 print $te;
213 $te = '';
214 }
bb365837 215
7a315204 216 $test = $OVER{$test} if exists $OVER{$test};
217
2f6bec1d 218 open(SCRIPT,"<$test") or die "Can't run $test.\n";
219 $_ = <SCRIPT>;
220 close(SCRIPT) unless ($type eq 'deparse');
5dc83c40 221 if (/#!.*\bperl.*\s-\w*([tT])/) {
6537fe72 222 $switch = qq{"-$1"};
2f6bec1d 223 }
224 else {
b26492ee 225 if ($taintwarn) {
226 # not all tests are expected to pass with this option
227 $switch = '"-t"';
228 }
229 else {
230 $switch = '';
231 }
2f6bec1d 232 }
6ee623d5 233
b326da91 234 my $test_executable; # for 'compile' tests
485988ae 235 my $file_opts = "";
236 if ($type eq 'deparse') {
237 # Look for #line directives which change the filename
238 while (<SCRIPT>) {
239 $file_opts .= ",-f$3$4"
240 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
241 }
242 close(SCRIPT);
243 }
7a315204 244
7a315204 245 my $utf = $with_utf ? '-I../lib -Mutf8' : '';
4343e7c3 246 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae 247 if ($type eq 'deparse') {
248 my $deparse =
127212b2 249 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
485988ae 250 "-l$deparse_opts$file_opts ".
7a315204 251 "$test > $test.dp ".
252 "&& ./perl $testswitch $switch -I../lib $test.dp |";
485988ae 253 open(RESULTS, $deparse)
254 or print "can't deparse '$deparse': $!.\n";
255 }
1df34986 256 elsif ($type eq 'bytecompile') {
c7e45529 257 my ($pwd, $null);
258 if( $^O eq 'MSWin32') {
259 $pwd = `cd`;
260 $null = 'nul';
261 } else {
262 $pwd = `pwd`;
263 $null = '/dev/null';
264 }
265 chomp $pwd;
266 my $perl = $ENV{PERL} || "$pwd/perl";
267 my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
566ece03 268 $bswitch .= "-TF$test.plc,"
1df34986 269 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
270 $bswitch .= "-k,"
271 if $test =~ m(deparse|terse|ext/Storable/t/code);
1df34986 272 $bswitch .= "-b,"
273 if $test =~ m(op/getpid);
274 my $bytecompile =
275 "$perl $testswitch $switch -I../lib $bswitch".
c7e45529 276 "-o$test.plc $test 2>$null &&".
277 "$perl $testswitch $switch -I../lib $utf $test.plc |";
1df34986 278 open(RESULTS,$bytecompile)
279 or print "can't byte-compile '$bytecompile': $!.\n";
280 }
485988ae 281 elsif ($type eq 'perl') {
a7da9a42 282 my $perl = $ENV{PERL} || './perl';
da51b73c 283 my $redir = $^O eq 'VMS' ? '2>&1' : '';
7a834142 284 if ($ENV{PERL_VALGRIND}) {
d44161bf 285 $perl = "valgrind --suppressions=perl.supp --leak-check=yes "
286 . "--leak-resolution=high --show-reachable=yes "
da51b73c 287 . "--num-callers=50 --logfile-fd=3 $perl";
288 $redir = "3>$valgrind_log";
7a834142 289 }
cc6ae9e5 290 my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
be24517c 291 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2 292 }
293 else {
b326da91 294 my $compile;
295 my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
9d2bbe64 296 # -O9 for good measure, -fcog is broken ATM
297 "$switch -Wb=-O9,-fno-cog -L .. " .
b326da91 298 "-I \".. ../lib/CORE\" $args $utf $test -o ";
299
300 if( $^O eq 'MSWin32' ) {
301 $test_executable = "$test.exe";
302 # hopefully unused name...
303 open HACK, "> xweghyz.pl";
304 print HACK <<EOT;
305#!./perl
306
307open HACK, '.\\perl $pl2c $test_executable |';
308# cl.exe prints the name of the .c file on stdout (\%^\$^#)
6d73d07f 309while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
b326da91 310open HACK, '$test_executable |';
311while(<HACK>) {print}
312EOT
313 close HACK;
314 $compile = 'xweghyz.pl |';
315 }
316 else {
317 $test_executable = "$test.plc";
318 $compile = "./perl $pl2c $test_executable && $test_executable |";
319 }
320 unlink $test_executable if -f $test_executable;
be24517c 321 open(RESULTS, $compile)
322 or print "can't compile '$compile': $!.\n";
6ee623d5 323 }
d638aca2 324
b326da91 325 $ok = 0;
326 $next = 0;
21c74f43 327 my $seen_leader = 0;
328 my $seen_ok = 0;
bb365837 329 while (<RESULTS>) {
cc6ae9e5 330 next if /^\s*$/; # skip blank lines
bb365837 331 if ($verbose) {
332 print $_;
333 }
21c74f43 334 unless (/^\#/) {
809908f7 335 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
bb365837 336 $max = $1;
809908f7 337 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837 338 $totmax += $max;
339 $files += 1;
21c74f43 340 unless ($seen_ok) {
341 $next = 1;
342 $ok = 1;
343 }
344 $seen_leader = 1;
bb365837 345 }
346 else {
21c74f43 347 if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
348 unless ($seen_leader) {
349 unless ($seen_ok) {
350 $next = 1;
351 $ok = 1;
352 }
37ce32a7 353 }
21c74f43 354 $seen_ok = 1;
355 if ($2 == $next) {
356 my($not, $num, $extra) = ($1, $2, $3);
6c0731c3 357 my($istodo) = $extra =~ /#\s*TODO/ if $extra;
21c74f43 358 $istodo = 1 if $todo{$num};
359
360 if( $not && !$istodo ) {
361 $ok = 0;
362 $next = $num;
363 last;
364 }
365 else {
366 $next = $next + 1;
367 }
37ce32a7 368 }
d667a7e6 369 }
370 elsif (/^Bail out!\s*(.*)/i) { # magic words
371 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837 372 }
373 else {
374 $ok = 0;
375 }
8d063cd8 376 }
377 }
378 }
bb365837 379 close RESULTS;
7a834142 380 if ($ENV{PERL_VALGRIND}) {
da51b73c 381 my @valgrind;
382 if (-e $valgrind_log) {
383 if (open(V, $valgrind_log)) {
384 @valgrind = <V>;
385 close V;
386 } else {
387 warn "$0: Failed to open '$valgrind_log': $!\n";
388 }
389 }
7a834142 390 if (@valgrind) {
d44161bf 391 my $leaks = 0;
392 my $errors = 0;
7a834142 393 for my $i (0..$#valgrind) {
394 local $_ = $valgrind[$i];
d44161bf 395 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
396 $errors += $1; # there may be multiple error summaries
397 } elsif (/^==\d+== LEAK SUMMARY:/) {
398 for my $off (1 .. 4) {
399 if ($valgrind[$i+$off] =~
400 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
401 $leaks += $1;
402 }
403 }
7a834142 404 }
405 }
d44161bf 406 if ($errors or $leaks) {
da51b73c 407 if (rename $valgrind_log, "$test.valgrind") {
d44161bf 408 $valgrind++;
409 } else {
410 warn "$0: Failed to create '$test.valgrind': $!\n";
7a834142 411 }
412 }
413 } else {
414 warn "No valgrind output?\n";
415 }
da51b73c 416 if (-e $valgrind_log) {
417 unlink $valgrind_log
418 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
419 }
7a834142 420 }
485988ae 421 if ($type eq 'deparse') {
422 unlink "./$test.dp";
423 }
211f317f 424 if ($ENV{PERL_3LOG}) {
425 my $tpp = $test;
3716a21d 426 $tpp =~ s:^\.\./::;
9c54ecba 427 $tpp =~ s:/:_:g;
3716a21d 428 $tpp =~ s:\.t$:.3log:;
429 rename("perl.3log", $tpp) ||
430 die "rename: perl3.log to $tpp: $!\n";
211f317f 431 }
bb365837 432 $next = $next - 1;
b326da91 433 # test if the compiler compiled something
434 if( $type eq 'compile' && !-e "$test_executable" ) {
435 $ok = 0;
436 print "Test did not compile\n";
437 }
438 if ($ok && $next == $max ) {
bb365837 439 if ($max) {
cc6ae9e5 440 print "${te}ok\n";
bb365837 441 $good = $good + 1;
442 }
443 else {
cc6ae9e5 444 print "${te}skipping test on this platform\n";
bb365837 445 $files -= 1;
446 }
bcce72a7 447 }
bb365837 448 else {
449 $next += 1;
cc6ae9e5 450 print "${te}FAILED at test $next\n";
bb365837 451 $bad = $bad + 1;
452 $_ = $test;
453 if (/^base/) {
454 die "Failed a basic test--cannot continue.\n";
455 }
8d063cd8 456 }
457 }
8d063cd8 458
bb365837 459 if ($bad == 0) {
460 if ($ok) {
461 print "All tests successful.\n";
462 # XXX add mention of 'perlbug -ok' ?
463 }
464 else {
465 die "FAILED--no tests were run for some reason.\n";
466 }
8d063cd8 467 }
bb365837 468 else {
ba1398cf 469 $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
bb365837 470 if ($bad == 1) {
e824fb2c 471 warn "Failed 1 test script out of $files, $pct% okay.\n";
bb365837 472 }
473 else {
e824fb2c 474 warn "Failed $bad test scripts out of $files, $pct% okay.\n";
bb365837 475 }
4e4732c1 476 warn <<'SHRDLU_1';
f7d228c6 477### Since not all tests were successful, you may want to run some of
478### them individually and examine any diagnostic messages they produce.
479### See the INSTALL document's section on "make test".
4e4732c1 480SHRDLU_1
481 warn <<'SHRDLU_2' if $good / $total > 0.8;
f7d228c6 482### You have a good chance to get more information by running
483### ./perl harness
484### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1 485SHRDLU_2
486 if (eval {require Config; import Config; 1}) {
e6af294e 487 if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
4e4732c1 488 warn <<SHRDLU_3;
f7d228c6 489### You may have to set your dynamic library search path,
490### $p, to point to the build directory:
4e4732c1 491SHRDLU_3
492 if (exists $ENV{$p} && $ENV{$p} ne '') {
493 warn <<SHRDLU_4a;
f7d228c6 494### setenv $p `pwd`:\$$p; cd t; ./perl harness
495### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
496### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1 497SHRDLU_4a
498 } else {
499 warn <<SHRDLU_4b;
f7d228c6 500### setenv $p `pwd`; cd t; ./perl harness
501### $p=`pwd`; export $p; cd t; ./perl harness
502### export $p=`pwd`; cd t; ./perl harness
4e4732c1 503SHRDLU_4b
504 }
505 warn <<SHRDLU_5;
f7d228c6 506### for csh-style shells, like tcsh; or for traditional/modern
507### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1 508SHRDLU_5
509 }
afd33fa9 510 }
bb365837 511 }
512 ($user,$sys,$cuser,$csys) = times;
513 print sprintf("u=%g s=%g cu=%g cs=%g scripts=%d tests=%d\n",
514 $user,$sys,$cuser,$csys,$files,$totmax);
7a834142 515 if ($ENV{PERL_VALGRIND}) {
516 my $s = $valgrind == 1 ? '' : 's';
517 print "$valgrind valgrind report$s created.\n", ;
518 }
6ee623d5 519}
3e6e8be7 520exit ($bad != 0);