Loop in S_init_perllib(), only calling S_incpush*() with INCPUSH_ADD_OLD_VERS
[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         $::taintwarn = 1 if $1 eq 'taintwarn';
33         $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
34         if ($1 =~ /^deparse(,.+)?$/) {
35             $::deparse = 1;
36             $::deparse_opts = $1;
37         }
38     }
39     @ARGV = @argv;
40 }
41
42 chdir 't' if -f 't/TEST';
43
44 die "You need to run \"make test\" first to set things up.\n"
45   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
46
47 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
48     unless (-x 'perl.third') {
49         unless (-x '../perl.third') {
50             die "You need to run \"make perl.third first.\n";
51         }
52         else {
53             print "Symlinking ../perl.third as perl.third...\n";
54             die "Failed to symlink: $!\n"
55                 unless symlink("../perl.third", "perl.third");
56             die "Symlinked but no executable perl.third: $!\n"
57                 unless -x 'perl.third';
58         }
59     }
60 }
61
62 # check leakage for embedders
63 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
64
65 $ENV{EMXSHELL} = 'sh';        # For OS/2
66
67 # Roll your own File::Find!
68 use TestInit;
69 use File::Spec;
70 if ($show_elapsed_time) { require Time::HiRes }
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 mro)) {
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                         my $flat_extension = $extension;
154                         $flat_extension =~ s!-!/!g;
155                         next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
156                     }
157                     my $path = File::Spec->catfile($updir, $t);
158                     push @ARGV, $path;
159                     $::path_to_name{$path} = $t;
160                 }
161             }
162         }
163         close MANI;
164     } else {
165         warn "$0: cannot open $mani: $!\n";
166     }
167     unless ($::core) {
168         _find_tests('Module_Pluggable');
169         _find_tests('pod');
170         _find_tests('x2p');
171         _find_tests('japh') if $::torture;
172     }
173 }
174
175 if ($::deparse) {
176     _testprogs('deparse', '',   @ARGV);
177 }
178 elsif ($::with_utf16) {
179     for my $e (0, 1) {
180         for my $b (0, 1) {
181             print STDERR "# ENDIAN $e BOM $b\n";
182             my @UARGV;
183             for my $a (@ARGV) {
184                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
185                 my $f = $e ? "v" : "n";
186                 push @UARGV, $u;
187                 unlink($u);
188                 if (open(A, $a)) {
189                     if (open(U, ">$u")) {
190                         print U pack("$f", 0xFEFF) if $b;
191                         while (<A>) {
192                             print U pack("$f*", unpack("C*", $_));
193                         }
194                         close(U);
195                     }
196                     close(A);
197                 }
198             }
199             _testprogs('perl', '', @UARGV);
200             unlink(@UARGV);
201         }
202     }
203 }
204 else {
205     _testprogs('perl',    '',   @ARGV);
206 }
207
208 sub _testprogs {
209     my ($type, $args, @tests) = @_;
210
211     print <<'EOT' if ($type eq 'deparse');
212 ------------------------------------------------------------------------------
213 TESTING DEPARSER
214 ------------------------------------------------------------------------------
215 EOT
216
217     $::bad_files = 0;
218
219     foreach my $t (@tests) {
220       unless (exists $::path_to_name{$t}) {
221         my $tname = File::Spec->catfile('t',$t);
222         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
223         $::path_to_name{$t} = $tname;
224       }
225     }
226     my $maxlen = 0;
227     foreach (@::path_to_name{@tests}) {
228         s/\.\w+\z/./;
229         my $len = length ;
230         $maxlen = $len if $len > $maxlen;
231     }
232     # + 3 : we want three dots between the test name and the "ok"
233     my $dotdotdot = $maxlen + 3 ;
234     my $valgrind = 0;
235     my $valgrind_log = 'current.valgrind';
236     my $total_files = @tests;
237     my $good_files = 0;
238     my $tested_files  = 0;
239     my $totmax = 0;
240     my %failed_tests;
241
242     while (my $test = shift @tests) {
243         my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
244
245         if ($test =~ /^$/) {
246             next;
247         }
248         if ($type eq 'deparse') {
249             if ($test eq "comp/redef.t") {
250                 # Redefinition happens at compile time
251                 next;
252             }
253             elsif ($test =~ m{lib/Switch/t/}) {
254                 # B::Deparse doesn't support source filtering
255                 next;
256             }
257         }
258         my $te = $::path_to_name{$test} . '.'
259                     x ($dotdotdot - length($::path_to_name{$test}));
260
261         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
262             print $te;
263             $te = '';
264         }
265
266         # XXX DAPM %OVER not defined anywhere
267         # $test = $OVER{$test} if exists $OVER{$test};
268
269         open(SCRIPT,"<",$test) or die "Can't run $test.\n";
270         $_ = <SCRIPT>;
271         close(SCRIPT) unless ($type eq 'deparse');
272         if ($::with_utf16) {
273             $_ =~ tr/\0//d;
274         }
275         my $switch;
276         if (/#!.*\bperl.*\s-\w*([tT])/) {
277             $switch = qq{"-$1"};
278         }
279         else {
280             if ($::taintwarn) {
281                 # not all tests are expected to pass with this option
282                 $switch = '"-t"';
283             }
284             else {
285                 $switch = '';
286             }
287         }
288
289         my $file_opts = "";
290         if ($type eq 'deparse') {
291             # Look for #line directives which change the filename
292             while (<SCRIPT>) {
293                 $file_opts .= ",-f$3$4"
294                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
295             }
296             close(SCRIPT);
297         }
298
299         my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
300         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
301         if ($type eq 'deparse') {
302             my $deparse_cmd =
303                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
304                 "-l$::deparse_opts$file_opts ".
305                 "$test > $test.dp ".
306                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
307             open(RESULTS, $deparse_cmd)
308                 or print "can't deparse '$deparse_cmd': $!.\n";
309         }
310         elsif ($type eq 'perl') {
311             my $perl = $ENV{PERL} || './perl';
312             my $redir = $^O eq 'VMS' ? '2>&1' : '';
313             if ($ENV{PERL_VALGRIND}) {
314                 my $valgrind = $ENV{VALGRIND} // 'valgrind';
315                 my $vg_opts = $ENV{VG_OPTS}
316                     //  "--suppressions=perl.supp --leak-check=yes "
317                         . "--leak-resolution=high --show-reachable=yes "
318                         . "--num-callers=50"; 
319                 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
320                 $redir = "3>$valgrind_log";
321             }
322             my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
323                               . " $test $redir|";
324             open(RESULTS,$run) or print "can't run '$run': $!.\n";
325         }
326         # Our environment may force us to use UTF-8, but we can't be sure that
327         # anything we're reading from will be generating (well formed) UTF-8
328         # This may not be the best way - possibly we should unset ${^OPEN} up
329         # top?
330         binmode RESULTS;
331
332         my $failure;
333         my $next = 0;
334         my $seen_leader = 0;
335         my $seen_ok = 0;
336         my $trailing_leader = 0;
337         my $max;
338         my %todo;
339         while (<RESULTS>) {
340             next if /^\s*$/; # skip blank lines
341             if ($::verbose) {
342                 print $_;
343             }
344             unless (/^\#/) {
345                 if ($trailing_leader) {
346                     # shouldn't be anything following a postfix 1..n
347                     $failure = 'FAILED--extra output after trailing 1..n';
348                     last;
349                 }
350                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
351                     if ($seen_leader) {
352                         $failure = 'FAILED--seen duplicate leader';
353                         last;
354                     }
355                     $max = $1;
356                     %todo = map { $_ => 1 } split / /, $3 if $3;
357                     $totmax += $max;
358                     $tested_files++;
359                     if ($seen_ok) {
360                         # 1..n appears at end of file
361                         $trailing_leader = 1;
362                         if ($next != $max) {
363                             $failure = "FAILED--expected $max tests, saw $next";
364                             last;
365                         }
366                     }
367                     else {
368                         $next = 0;
369                     }
370                     $seen_leader = 1;
371                 }
372                 else {
373                     if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
374                         unless ($seen_leader) {
375                             unless ($seen_ok) {
376                                 $next = 0;
377                             }
378                         }
379                         $seen_ok = 1;
380                         $next++;
381                         my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
382                         $num = $next unless $num;
383
384                         if ($num == $next) {
385
386                             # SKIP is essentially the same as TODO for t/TEST
387                             # this still conforms to TAP:
388                             # http://search.cpan.org/dist/TAP/TAP.pod
389                             $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
390                             $istodo = 1 if $todo{$num};
391
392                             if( $not && !$istodo ) {
393                                 $failure = "FAILED at test $num";
394                                 last;
395                             }
396                         }
397                         else {
398                             $failure ="FAILED--expected test $next, saw test $num";
399                             last;
400                         }
401                     }
402                     elsif (/^Bail out!\s*(.*)/i) { # magic words
403                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
404                     }
405                     else {
406                         # module tests are allowed extra output,
407                         # because Test::Harness allows it
408                         next if $test =~ /^\W*(ext|lib)\b/;
409                         $failure = "FAILED--unexpected output at test $next";
410                         last;
411                     }
412                 }
413             }
414         }
415         close RESULTS;
416
417         if (not defined $failure) {
418             $failure = 'FAILED--no leader found' unless $seen_leader;
419         }
420
421         if ($ENV{PERL_VALGRIND}) {
422             my @valgrind;
423             if (-e $valgrind_log) {
424                 if (open(V, $valgrind_log)) {
425                     @valgrind = <V>;
426                     close V;
427                 } else {
428                     warn "$0: Failed to open '$valgrind_log': $!\n";
429                 }
430             }
431             if ($ENV{VG_OPTS} =~ /cachegrind/) {
432                 if (rename $valgrind_log, "$test.valgrind") {
433                     $valgrind++;
434                 } else {
435                     warn "$0: Failed to create '$test.valgrind': $!\n";
436                 }
437             }
438             elsif (@valgrind) {
439                 my $leaks = 0;
440                 my $errors = 0;
441                 for my $i (0..$#valgrind) {
442                     local $_ = $valgrind[$i];
443                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
444                         $errors += $1;   # there may be multiple error summaries
445                     } elsif (/^==\d+== LEAK SUMMARY:/) {
446                         for my $off (1 .. 4) {
447                             if ($valgrind[$i+$off] =~
448                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
449                                 $leaks += $1;
450                             }
451                         }
452                     }
453                 }
454                 if ($errors or $leaks) {
455                     if (rename $valgrind_log, "$test.valgrind") {
456                         $valgrind++;
457                     } else {
458                         warn "$0: Failed to create '$test.valgrind': $!\n";
459                     }
460                 }
461             } else {
462                 warn "No valgrind output?\n";
463             }
464             if (-e $valgrind_log) {
465                 unlink $valgrind_log
466                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
467             }
468         }
469         if ($type eq 'deparse') {
470             unlink "./$test.dp";
471         }
472         if ($ENV{PERL_3LOG}) {
473             my $tpp = $test;
474             $tpp =~ s:^\.\./::;
475             $tpp =~ s:/:_:g;
476             $tpp =~ s:\.t$:.3log:;
477             rename("perl.3log", $tpp) ||
478                 die "rename: perl3.log to $tpp: $!\n";
479         }
480         if (not defined $failure and $next != $max) {
481             $failure="FAILED--expected $max tests, saw $next";
482         }
483
484         if( !defined $failure  # don't mask a test failure
485             and $? )
486         {
487             $failure = "FAILED--non-zero wait status: $?";
488         }
489
490         if (defined $failure) {
491             print "${te}$failure\n";
492             $::bad_files++;
493             if ($test =~ /^base/) {
494                 die "Failed a basic test ($test) -- cannot continue.\n";
495             }
496             ++$failed_tests{$test};
497         }
498         else {
499             if ($max) {
500                 my $elapsed;
501                 if ( $show_elapsed_time ) {
502                     $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
503                 }
504                 else {
505                     $elapsed = "";
506                 }
507                 print "${te}ok$elapsed\n";
508                 $good_files++;
509             }
510             else {
511                 print "${te}skipped\n";
512                 $tested_files -= 1;
513             }
514         }
515     } # while tests
516
517     if ($::bad_files == 0) {
518         if ($good_files) {
519             print "All tests successful.\n";
520             # XXX add mention of 'perlbug -ok' ?
521         }
522         else {
523             die "FAILED--no tests were run for some reason.\n";
524         }
525     }
526     else {
527         my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
528         my $s = $::bad_files == 1 ? "" : "s";
529         warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
530         for my $test ( sort keys %failed_tests ) {
531             print "\t$test\n";
532         }
533         warn <<'SHRDLU_1';
534 ### Since not all tests were successful, you may want to run some of
535 ### them individually and examine any diagnostic messages they produce.
536 ### See the INSTALL document's section on "make test".
537 SHRDLU_1
538         warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
539 ### You have a good chance to get more information by running
540 ###   ./perl harness
541 ### in the 't' directory since most (>=80%) of the tests succeeded.
542 SHRDLU_2
543         if (eval {require Config; import Config; 1}) {
544             if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
545                 warn <<SHRDLU_3;
546 ### You may have to set your dynamic library search path,
547 ### $p, to point to the build directory:
548 SHRDLU_3
549                 if (exists $ENV{$p} && $ENV{$p} ne '') {
550                     warn <<SHRDLU_4a;
551 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
552 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
553 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
554 SHRDLU_4a
555                 } else {
556                     warn <<SHRDLU_4b;
557 ###   setenv $p `pwd`; cd t; ./perl harness
558 ###   $p=`pwd`; export $p; cd t; ./perl harness
559 ###   export $p=`pwd`; cd t; ./perl harness
560 SHRDLU_4b
561                 }
562                 warn <<SHRDLU_5;
563 ### for csh-style shells, like tcsh; or for traditional/modern
564 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
565 SHRDLU_5
566             }
567         }
568     }
569     my ($user,$sys,$cuser,$csys) = times;
570     print sprintf("u=%.2f  s=%.2f  cu=%.2f  cs=%.2f  scripts=%d  tests=%d\n",
571         $user,$sys,$cuser,$csys,$tested_files,$totmax);
572     if ($ENV{PERL_VALGRIND}) {
573         my $s = $valgrind == 1 ? '' : 's';
574         print "$valgrind valgrind report$s created.\n", ;
575     }
576 }
577 exit ($::bad_files != 0);
578
579 # ex: set ts=8 sts=4 sw=4 noet: