7dd688d984380aaf6ec71b505f2cbb4daa2bf1c2
[p5sagit/p5-mst-13.2.git] / t / TEST
1 #!./perl
2
3 # This is written in a peculiar style, since we're trying to avoid
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.)
7
8 $| = 1;
9
10 # for testing TEST only
11 #BEGIN { require '../lib/strict.pm'; strict->import() };
12 #BEGIN { require '../lib/warnings.pm'; warnings->import() };
13
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
18 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
19 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
20
21 # Cheesy version of Getopt::Std.  Maybe we should replace it with that.
22 {
23     my @argv = ();
24     foreach my $idx (0..$#ARGV) {
25         push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
26         $::core    = 1 if $1 eq 'core';
27         $::verbose = 1 if $1 eq 'v';
28         $::torture = 1 if $1 eq 'torture';
29         $::with_utf8 = 1 if $1 eq 'utf8';
30         $::with_utf16 = 1 if $1 eq 'utf16';
31         $::bytecompile = 1 if $1 eq 'bytecompile';
32         $::compile = 1 if $1 eq 'compile';
33         $::taintwarn = 1 if $1 eq 'taintwarn';
34         $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
35         if ($1 =~ /^deparse(,.+)?$/) {
36             $::deparse = 1;
37             $::deparse_opts = $1;
38         }
39     }
40     @ARGV = @argv;
41 }
42
43 chdir 't' if -f 't/TEST';
44
45 die "You need to run \"make test\" first to set things up.\n"
46   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
47
48 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
49     unless (-x 'perl.third') {
50         unless (-x '../perl.third') {
51             die "You need to run \"make perl.third first.\n";
52         }
53         else {
54             print "Symlinking ../perl.third as perl.third...\n";
55             die "Failed to symlink: $!\n"
56                 unless symlink("../perl.third", "perl.third");
57             die "Symlinked but no executable perl.third: $!\n"
58                 unless -x 'perl.third';
59         }
60     }
61 }
62
63 # check leakage for embedders
64 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
65
66 $ENV{EMXSHELL} = 'sh';        # For OS/2
67
68 # Roll your own File::Find!
69 use TestInit;
70 use File::Spec;
71 my $curdir = File::Spec->curdir;
72 my $updir  = File::Spec->updir;
73
74 sub _find_tests {
75     my($dir) = @_;
76     opendir DIR, $dir or die "Trouble opening $dir: $!";
77     foreach my $f (sort { $a cmp $b } readdir DIR) {
78         next if $f eq $curdir or $f eq $updir or
79             $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
80
81         my $fullpath = File::Spec->catfile($dir, $f);
82
83         _find_tests($fullpath) if -d $fullpath;
84         $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
85         push @ARGV, $fullpath if $f =~ /\.t$/;
86     }
87 }
88
89 sub _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
102 sub _populate_hash {
103     return map {$_, 1} split /\s+/, $_[0];
104 }
105
106 unless (@ARGV) {
107     foreach my $dir (qw(base comp cmd run io op uni)) {
108         _find_tests($dir);
109     }
110     _find_tests("lib") unless $::core;
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     }
141     my $mani = File::Spec->catfile($updir, "MANIFEST");
142     if (open(MANI, $mani)) {
143         while (<MANI>) { # similar code in t/harness
144             if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
145                 my $t = $1;
146                 my $extension = $2;
147                 if (!$::core || $t =~ m!^lib/[a-z]!)
148                 {
149                     if (defined $extension) {
150                         $extension =~ s!/t$!!;
151                         # XXX Do I want to warn that I'm skipping these?
152                         next if $skip{$extension};
153                     }
154                     my $path = File::Spec->catfile($updir, $t);
155                     push @ARGV, $path;
156                     $::path_to_name{$path} = $t;
157                 }
158             }
159         }
160         close MANI;
161     } else {
162         warn "$0: cannot open $mani: $!\n";
163     }
164     unless ($::core) {
165         _find_tests('pod');
166         _find_tests('x2p');
167         _find_tests('japh') if $::torture;
168     }
169 }
170
171 # Tests known to cause infinite loops for the perlcc tests.
172 # %::infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
173 %::infinite = ();
174
175 if ($::deparse) {
176     _testprogs('deparse', '',   @ARGV);
177 }
178 elsif( $::compile ) { 
179     _testprogs('compile', '',   @ARGV);
180 }
181 elsif( $::bytecompile ) {
182     _testprogs('bytecompile', '', @ARGV);
183 }
184 elsif ($::with_utf16) {
185     for my $e (0, 1) {
186         for my $b (0, 1) {
187             print STDERR "# ENDIAN $e BOM $b\n";
188             my @UARGV;
189             for my $a (@ARGV) {
190                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
191                 my $f = $e ? "v" : "n";
192                 push @UARGV, $u;
193                 unlink($u);
194                 if (open(A, $a)) {
195                     if (open(U, ">$u")) {
196                         print U pack("$f", 0xFEFF) if $b;
197                         while (<A>) {
198                             print U pack("$f*", unpack("C*", $_));
199                         }
200                         close(U);
201                     }
202                     close(A);
203                 }
204             }
205             _testprogs('perl', '', @UARGV);
206             unlink(@UARGV);
207         }
208     }
209 }
210 else {
211     _testprogs('compile', '',   @ARGV) if -e "../testcompile";
212     _testprogs('perl',    '',   @ARGV);
213 }
214
215 sub _testprogs {
216     my ($type, $args, @tests) = @_;
217
218     print <<'EOT' if ($type eq 'compile');
219 ------------------------------------------------------------------------------
220 TESTING COMPILER
221 ------------------------------------------------------------------------------
222 EOT
223
224     print <<'EOT' if ($type eq 'deparse');
225 ------------------------------------------------------------------------------
226 TESTING DEPARSER
227 ------------------------------------------------------------------------------
228 EOT
229
230     print <<EOT if ($type eq 'bytecompile');
231 ------------------------------------------------------------------------------
232 TESTING BYTECODE COMPILER
233 ------------------------------------------------------------------------------
234 EOT
235
236     $ENV{PERLCC_TIMEOUT} = 120
237           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
238
239     $::bad_files = 0;
240
241     foreach my $t (@tests) {
242       unless (exists $::path_to_name{$t}) {
243         my $tname = File::Spec->catfile('t',$t);
244         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
245         $::path_to_name{$t} = $tname;
246       }
247     }
248     my $maxlen = 0;
249     foreach (@::path_to_name{@tests}) {
250         s/\.\w+\z/./;
251         my $len = length ;
252         $maxlen = $len if $len > $maxlen;
253     }
254     # + 3 : we want three dots between the test name and the "ok"
255     my $dotdotdot = $maxlen + 3 ;
256     my $valgrind = 0;
257     my $valgrind_log = 'current.valgrind';
258     my $total_files = @tests;
259     my $good_files = 0;
260     my $tested_files  = 0;
261     my $totmax = 0;
262     my $ok;
263
264     my $test;
265     while ($test = shift @tests) {
266
267         if ( $::infinite{$test} && $type eq 'compile' ) {
268             print STDERR "$test creates infinite loop! Skipping.\n";
269             next;
270         }
271         if ($test =~ /^$/) {
272             next;
273         }
274         if ($type eq 'deparse') {
275             if ($test eq "comp/redef.t") {
276                 # Redefinition happens at compile time
277                 next;
278             }
279             elsif ($test =~ m{lib/Switch/t/}) {
280                 # B::Deparse doesn't support source filtering
281                 next;
282             }
283         }
284         my $te = $::path_to_name{$test} . '.'
285                     x ($dotdotdot - length($::path_to_name{$test}));
286
287         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
288             print $te;
289             $te = '';
290         }
291
292         # XXX DAPM %OVER not defined anywhere
293         # $test = $OVER{$test} if exists $OVER{$test};
294
295         open(SCRIPT,"<$test") or die "Can't run $test.\n";
296         $_ = <SCRIPT>;
297         close(SCRIPT) unless ($type eq 'deparse');
298         if ($::with_utf16) {
299             $_ =~ tr/\0//d;
300         }
301         my $switch;
302         if (/#!.*\bperl.*\s-\w*([tT])/) {
303             $switch = qq{"-$1"};
304         }
305         else {
306             if ($::taintwarn) {
307                 # not all tests are expected to pass with this option
308                 $switch = '"-t"';
309             }
310             else {
311                 $switch = '';
312             }
313         }
314
315         my $test_executable; # for 'compile' tests
316         my $file_opts = "";
317         if ($type eq 'deparse') {
318             # Look for #line directives which change the filename
319             while (<SCRIPT>) {
320                 $file_opts .= ",-f$3$4"
321                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
322             }
323             close(SCRIPT);
324         }
325
326         my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
327         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
328         if ($type eq 'deparse') {
329             my $deparse_cmd =
330                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
331                 "-l$::deparse_opts$file_opts ".
332                 "$test > $test.dp ".
333                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
334             open(RESULTS, $deparse_cmd)
335                 or print "can't deparse '$deparse_cmd': $!.\n";
336         }
337         elsif ($type eq 'bytecompile') {
338             my ($pwd, $null);
339             if( $^O eq 'MSWin32') {
340                 $pwd = `cd`;
341                 $null = 'nul';
342             } else {
343                 $pwd = `pwd`;
344                 $null = '/dev/null';
345             }
346             chomp $pwd;
347             my $perl = $ENV{PERL} || "$pwd/perl";
348             my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
349             $bswitch .= "-TF$test.plc,"
350                 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
351             $bswitch .= "-k,"
352                 if $test =~ m(deparse|terse|ext/Storable/t/code);
353             $bswitch .= "-b,"
354                 if $test =~ m(op/getpid);
355             my $bytecompile_cmd =
356                 "$perl $testswitch $switch -I../lib $bswitch". 
357                 "-o$test.plc $test 2>$null &&".
358                 "$perl $testswitch $switch -I../lib $utf8 $test.plc |";
359             open(RESULTS,$bytecompile_cmd)
360                 or print "can't byte-compile '$bytecompile_cmd': $!.\n";
361         }
362         elsif ($type eq 'perl') {
363             my $perl = $ENV{PERL} || './perl';
364             my $redir = $^O eq 'VMS' ? '2>&1' : '';
365             if ($ENV{PERL_VALGRIND}) {
366                 $perl = "valgrind --suppressions=perl.supp --leak-check=yes "
367                                . "--leak-resolution=high --show-reachable=yes "
368                                . "--num-callers=50 --logfile-fd=3 $perl";
369                 $redir = "3>$valgrind_log";
370             }
371             my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
372                               . " $test $redir|";
373             open(RESULTS,$run) or print "can't run '$run': $!.\n";
374         }
375         else {
376             my $compile_cmd;
377             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
378               # -O9 for good measure, -fcog is broken ATM
379                        "$switch -Wb=-O9,-fno-cog -L .. " .
380                        "-I \".. ../lib/CORE\" $args $utf8 $test -o ";
381
382             if( $^O eq 'MSWin32' ) {
383                 $test_executable = "$test.exe";
384                 # hopefully unused name...
385                 open HACK, "> xweghyz.pl";
386                 print HACK <<EOT;
387 #!./perl
388
389 open HACK, '.\\perl $pl2c $test_executable |';
390 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
391 while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
392 open HACK, '$test_executable |';
393 while(<HACK>) {print}
394 EOT
395                 close HACK;
396                 $compile_cmd = 'xweghyz.pl |';
397             }
398             else {
399                 $test_executable = "$test.plc";
400                 $compile_cmd
401                     = "./perl $pl2c $test_executable && $test_executable |";
402             }
403             unlink $test_executable if -f $test_executable;
404             open(RESULTS, $compile_cmd)
405                 or print "can't compile '$compile_cmd': $!.\n";
406         }
407
408         $ok = 0;
409         my $next = 0;
410         my $seen_leader = 0;
411         my $seen_ok = 0;
412         my $max;
413         while (<RESULTS>) {
414             next if /^\s*$/; # skip blank lines
415             if ($::verbose) {
416                 print $_;
417             }
418             unless (/^\#/) {
419                 my %todo;
420                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
421                     $max = $1;
422                     %todo = map { $_ => 1 } split / /, $3 if $3;
423                     $totmax += $max;
424                     $tested_files++;
425                     unless ($seen_ok) {
426                       $next = 1;
427                       $ok = 1;
428                     }
429                     $seen_leader = 1;
430                 }
431                 else {
432                     if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
433                         unless ($seen_leader) {
434                             unless ($seen_ok) {
435                                 $next = 1;
436                                 $ok = 1;
437                             }
438                         }
439                         $seen_ok = 1;
440                         if ($2 == $next) {
441                             my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
442                             # SKIP is essentially the same as TODO for t/TEST
443                             # this still conforms to TAP:
444                             # http://search.cpan.org/dist/Test-Harness/lib/Test/Harness/TAP.pod
445                             $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
446                             $istodo = 1 if $todo{$num};
447
448                             if( $not && !$istodo ) {
449                                 $ok = 0;
450                                 $next = $num;
451                                 last;
452                             }
453                             else {
454                                 $next = $next + 1;
455                             }
456                         }
457                     }
458                     elsif (/^Bail out!\s*(.*)/i) { # magic words
459                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
460                     }
461                     else {
462                         $ok = 0;
463                     }
464                 }
465             }
466         }
467         close RESULTS;
468         if ($ENV{PERL_VALGRIND}) {
469             my @valgrind;
470             if (-e $valgrind_log) {
471                 if (open(V, $valgrind_log)) {
472                     @valgrind = <V>;
473                     close V;
474                 } else {
475                     warn "$0: Failed to open '$valgrind_log': $!\n";
476                 }
477             }
478             if (@valgrind) {
479                 my $leaks = 0;
480                 my $errors = 0;
481                 for my $i (0..$#valgrind) {
482                     local $_ = $valgrind[$i];
483                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
484                         $errors += $1;   # there may be multiple error summaries
485                     } elsif (/^==\d+== LEAK SUMMARY:/) {
486                         for my $off (1 .. 4) {
487                             if ($valgrind[$i+$off] =~
488                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
489                                 $leaks += $1;
490                             }
491                         }
492                     }
493                 }
494                 if ($errors or $leaks) {
495                     if (rename $valgrind_log, "$test.valgrind") {
496                         $valgrind++;
497                     } else {
498                         warn "$0: Failed to create '$test.valgrind': $!\n";
499                     }
500                 }
501             } else {
502                 warn "No valgrind output?\n";
503             }
504             if (-e $valgrind_log) {
505                 unlink $valgrind_log
506                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
507             }
508         }
509         if ($type eq 'deparse') {
510             unlink "./$test.dp";
511         }
512         if ($ENV{PERL_3LOG}) {
513             my $tpp = $test;
514             $tpp =~ s:^\.\./::;
515             $tpp =~ s:/:_:g;
516             $tpp =~ s:\.t$:.3log:;
517             rename("perl.3log", $tpp) ||
518                 die "rename: perl3.log to $tpp: $!\n";
519         }
520         $next = $next - 1;
521         # test if the compiler compiled something
522         if( $type eq 'compile' && !-e "$test_executable" ) {
523             $ok = 0;
524             print "Test did not compile\n";
525         }
526         if ($ok && $next == $max ) {
527             if ($max) {
528                 print "${te}ok\n";
529                 $good_files++;
530             }
531             else {
532                 print "${te}skipping test on this platform\n";
533                 $tested_files -= 1;
534             }
535         }
536         else {
537             $next += 1;
538             if ($next > $max) {
539                 print "${te}FAILED at test $next\tpossibly due to extra output\n";
540             }
541             else {
542                 print "${te}FAILED at test $next\n";
543             }
544             $::bad_files++;
545             $_ = $test;
546             if (/^base/) {
547                 die "Failed a basic test--cannot continue.\n";
548             }
549         }
550     }
551
552     if ($::bad_files == 0) {
553         if ($ok) {
554             print "All tests successful.\n";
555             # XXX add mention of 'perlbug -ok' ?
556         }
557         else {
558             die "FAILED--no tests were run for some reason.\n";
559         }
560     }
561     else {
562         my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
563         if ($::bad_files == 1) {
564             warn "Failed 1 test script out of $tested_files, $pct% okay.\n";
565         }
566         else {
567             warn "Failed $::bad_files test scripts out of $tested_files, $pct% okay.\n";
568         }
569         warn <<'SHRDLU_1';
570 ### Since not all tests were successful, you may want to run some of
571 ### them individually and examine any diagnostic messages they produce.
572 ### See the INSTALL document's section on "make test".
573 SHRDLU_1
574         warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
575 ### You have a good chance to get more information by running
576 ###   ./perl harness
577 ### in the 't' directory since most (>=80%) of the tests succeeded.
578 SHRDLU_2
579         if (eval {require Config; import Config; 1}) {
580             if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
581                 warn <<SHRDLU_3;
582 ### You may have to set your dynamic library search path,
583 ### $p, to point to the build directory:
584 SHRDLU_3
585                 if (exists $ENV{$p} && $ENV{$p} ne '') {
586                     warn <<SHRDLU_4a;
587 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
588 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
589 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
590 SHRDLU_4a
591                 } else {
592                     warn <<SHRDLU_4b;
593 ###   setenv $p `pwd`; cd t; ./perl harness
594 ###   $p=`pwd`; export $p; cd t; ./perl harness
595 ###   export $p=`pwd`; cd t; ./perl harness
596 SHRDLU_4b
597                 }    
598                 warn <<SHRDLU_5;
599 ### for csh-style shells, like tcsh; or for traditional/modern
600 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
601 SHRDLU_5
602             }
603         }
604     }
605     my ($user,$sys,$cuser,$csys) = times;
606     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
607         $user,$sys,$cuser,$csys,$tested_files,$totmax);
608     if ($ENV{PERL_VALGRIND}) {
609         my $s = $valgrind == 1 ? '' : 's';
610         print "$valgrind valgrind report$s created.\n", ;
611     }
612 }
613 exit ($::bad_files != 0);