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