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