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