5f95d00740604d037b58c082c9b5e4677ab0f202
[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     while ($test = shift @tests) {
188
189         if ( $infinite{$test} && $type eq 'compile' ) {
190             print STDERR "$test creates infinite loop! Skipping.\n";
191             next;
192         }
193         if ($test =~ /^$/) {
194             next;
195         }
196         if ($type eq 'deparse') {
197             if ($test eq "comp/redef.t") {
198                 # Redefinition happens at compile time
199                 next;
200             }
201             elsif ($test =~ m{lib/Switch/t/}) {
202                 # B::Deparse doesn't support source filtering
203                 next;
204             }
205         }
206         $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
207
208         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
209             print $te;
210             $te = '';
211         }
212
213         $test = $OVER{$test} if exists $OVER{$test};
214
215         open(SCRIPT,"<$test") or die "Can't run $test.\n";
216         $_ = <SCRIPT>;
217         close(SCRIPT) unless ($type eq 'deparse');
218         if (/#!.*\bperl.*\s-\w*([tT])/) {
219             $switch = qq{"-$1"};
220         }
221         else {
222             $switch = '';
223         }
224
225         my $test_executable; # for 'compile' tests
226         my $file_opts = "";
227         if ($type eq 'deparse') {
228             # Look for #line directives which change the filename
229             while (<SCRIPT>) {
230                 $file_opts .= ",-f$3$4"
231                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
232             }
233             close(SCRIPT);
234         }
235
236         my $utf = $with_utf ? '-I../lib -Mutf8' : '';
237         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
238         if ($type eq 'deparse') {
239             my $deparse =
240                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
241                 "-l$deparse_opts$file_opts ".
242                 "$test > $test.dp ".
243                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
244             open(RESULTS, $deparse)
245                 or print "can't deparse '$deparse': $!.\n";
246         }
247         elsif ($type eq 'bytecompile') {
248             my $perl = $ENV{PERL} || './perl';
249             my $redir = ($^O eq 'VMS' ? '2>&1' : '');
250             my $bswitch = "-MO=Bytecode,-H,-TI,-s`pwd`/$test,";
251             $bswitch .= "-TF$test.plc,"
252                 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
253             $bswitch .= "-k,"
254                 if $test =~ m(deparse|terse|ext/Storable/t/code);
255             $bswitch .= "-b,"
256                 if $test =~ m(op/getpid);
257             my $bytecompile =
258                 "$perl $testswitch $switch -I../lib $bswitch". 
259                 "-o$test.plc $test 2>/dev/null &&".
260                 "$perl $testswitch $switch -I../lib $utf $test.plc $redir|";
261             open(RESULTS,$bytecompile)
262                 or print "can't byte-compile '$bytecompile': $!.\n";
263         }
264         elsif ($type eq 'perl') {
265             my $perl = $ENV{PERL} || './perl';
266             my $redir = ($^O eq 'VMS' || $ENV{PERL_VALGRIND} ? '2>&1' : '');
267             if ($ENV{PERL_VALGRIND}) {
268                 $perl = "valgrind --suppressions=perl.supp --leak-check=yes "
269                                . "--leak-resolution=high --show-reachable=yes "
270                                . "--num-callers=50 $perl";
271             }
272             my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
273             open(RESULTS,$run) or print "can't run '$run': $!.\n";
274         }
275         else {
276             my $compile;
277             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
278               # -O9 for good measure, -fcog is broken ATM
279                        "$switch -Wb=-O9,-fno-cog -L .. " .
280                        "-I \".. ../lib/CORE\" $args $utf $test -o ";
281
282             if( $^O eq 'MSWin32' ) {
283                 $test_executable = "$test.exe";
284                 # hopefully unused name...
285                 open HACK, "> xweghyz.pl";
286                 print HACK <<EOT;
287 #!./perl
288
289 open HACK, '.\\perl $pl2c $test_executable |';
290 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
291 while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
292 open HACK, '$test_executable |';
293 while(<HACK>) {print}
294 EOT
295                 close HACK;
296                 $compile = 'xweghyz.pl |';
297             }
298             else {
299                 $test_executable = "$test.plc";
300                 $compile = "./perl $pl2c $test_executable && $test_executable |";
301             }
302             unlink $test_executable if -f $test_executable;
303             open(RESULTS, $compile)
304                 or print "can't compile '$compile': $!.\n";
305         }
306
307         $ok = 0;
308         $next = 0;
309         my $seen_leader = 0;
310         my $seen_ok = 0;
311         my @valgrind;
312         while (<RESULTS>) {
313             next if /^\s*$/; # skip blank lines
314             if ($verbose) {
315                 print $_;
316             }
317             if ($ENV{PERL_VALGRIND} && /^==\d+== /) {
318                 push @valgrind, $_;
319                 next;
320             }
321             unless (/^\#/) {
322                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
323                     $max = $1;
324                     %todo = map { $_ => 1 } split / /, $3 if $3;
325                     $totmax += $max;
326                     $files += 1;
327                     unless ($seen_ok) {
328                       $next = 1;
329                       $ok = 1;
330                     }
331                     $seen_leader = 1;
332                 }
333                 else {
334                     if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
335                         unless ($seen_leader) {
336                             unless ($seen_ok) {
337                                 $next = 1;
338                                 $ok = 1;
339                             }
340                         }
341                         $seen_ok = 1;
342                         if ($2 == $next) {
343                             my($not, $num, $extra) = ($1, $2, $3);
344                             my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
345                             $istodo = 1 if $todo{$num};
346
347                             if( $not && !$istodo ) {
348                                 $ok = 0;
349                                 $next = $num;
350                                 last;
351                             }
352                             else {
353                                 $next = $next + 1;
354                             }
355                         }
356                     }
357                     elsif (/^Bail out!\s*(.*)/i) { # magic words
358                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
359                     }
360                     else {
361                         $ok = 0;
362                     }
363                 }
364             }
365         }
366         close RESULTS;
367         if ($ENV{PERL_VALGRIND}) {
368             if (@valgrind) {
369                 my $leaks = 0;
370                 my $errors = 0;
371                 for my $i (0..$#valgrind) {
372                     local $_ = $valgrind[$i];
373                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
374                         $errors += $1;   # there may be multiple error summaries
375                     } elsif (/^==\d+== LEAK SUMMARY:/) {
376                         for my $off (1 .. 4) {
377                             if ($valgrind[$i+$off] =~
378                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
379                                 $leaks += $1;
380                             }
381                         }
382                     }
383                 }
384                 if ($errors or $leaks) {
385                     if (open(V, ">$test.valgrind")) {
386                         for (@valgrind) {
387                             print V $_;
388                         }
389                         close V;
390                         $valgrind++;
391                     } else {
392                         warn "$0: Failed to create '$test.valgrind': $!\n";
393                     }
394                 }
395             } else {
396                 warn "No valgrind output?\n";
397             }
398         }
399         if ($type eq 'deparse') {
400             unlink "./$test.dp";
401         }
402         if ($ENV{PERL_3LOG}) {
403             my $tpp = $test;
404             $tpp =~ s:^\.\./::;
405             $tpp =~ s:/:_:g;
406             $tpp =~ s:\.t$:.3log:;
407             rename("perl.3log", $tpp) ||
408                 die "rename: perl3.log to $tpp: $!\n";
409         }
410         $next = $next - 1;
411         # test if the compiler compiled something
412         if( $type eq 'compile' && !-e "$test_executable" ) {
413             $ok = 0;
414             print "Test did not compile\n";
415         }
416         if ($ok && $next == $max ) {
417             if ($max) {
418                 print "${te}ok\n";
419                 $good = $good + 1;
420             }
421             else {
422                 print "${te}skipping test on this platform\n";
423                 $files -= 1;
424             }
425         }
426         else {
427             $next += 1;
428             print "${te}FAILED at test $next\n";
429             $bad = $bad + 1;
430             $_ = $test;
431             if (/^base/) {
432                 die "Failed a basic test--cannot continue.\n";
433             }
434         }
435     }
436
437     if ($bad == 0) {
438         if ($ok) {
439             print "All tests successful.\n";
440             # XXX add mention of 'perlbug -ok' ?
441         }
442         else {
443             die "FAILED--no tests were run for some reason.\n";
444         }
445     }
446     else {
447         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
448         if ($bad == 1) {
449             warn "Failed 1 test script out of $files, $pct% okay.\n";
450         }
451         else {
452             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
453         }
454         warn <<'SHRDLU_1';
455 ### Since not all tests were successful, you may want to run some of
456 ### them individually and examine any diagnostic messages they produce.
457 ### See the INSTALL document's section on "make test".
458 SHRDLU_1
459         warn <<'SHRDLU_2' if $good / $total > 0.8;
460 ### You have a good chance to get more information by running
461 ###   ./perl harness
462 ### in the 't' directory since most (>=80%) of the tests succeeded.
463 SHRDLU_2
464         if (eval {require Config; import Config; 1}) {
465             if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
466                 warn <<SHRDLU_3;
467 ### You may have to set your dynamic library search path,
468 ### $p, to point to the build directory:
469 SHRDLU_3
470                 if (exists $ENV{$p} && $ENV{$p} ne '') {
471                     warn <<SHRDLU_4a;
472 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
473 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
474 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
475 SHRDLU_4a
476                 } else {
477                     warn <<SHRDLU_4b;
478 ###   setenv $p `pwd`; cd t; ./perl harness
479 ###   $p=`pwd`; export $p; cd t; ./perl harness
480 ###   export $p=`pwd`; cd t; ./perl harness
481 SHRDLU_4b
482                 }    
483                 warn <<SHRDLU_5;
484 ### for csh-style shells, like tcsh; or for traditional/modern
485 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
486 SHRDLU_5
487             }
488         }
489     }
490     ($user,$sys,$cuser,$csys) = times;
491     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
492         $user,$sys,$cuser,$csys,$files,$totmax);
493     if ($ENV{PERL_VALGRIND}) {
494         my $s = $valgrind == 1 ? '' : 's';
495         print "$valgrind valgrind report$s created.\n", ;
496     }
497 }
498 exit ($bad != 0);