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