6f23caffecb1d3d5170d5d41a0dd2eacdb6cb40b
[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 if ($show_elapsed_time) { require Time::HiRes }
72
73 my %skip = (
74             '.' => 1,
75             '..' => 1,
76             'CVS' => 1,
77             'RCS' => 1,
78             'SCCS' => 1,
79             '.svn' => 1,
80            );
81
82 sub _find_tests {
83     my($dir) = @_;
84     opendir DIR, $dir or die "Trouble opening $dir: $!";
85     foreach my $f (sort { $a cmp $b } readdir DIR) {
86         next if $skip{$f};
87
88         my $fullpath = "$dir/$f";
89
90         if (-d $fullpath) {
91             _find_tests($fullpath);
92         } elsif ($f =~ /\.t$/) {
93             push @ARGV, $fullpath;
94         }
95     }
96 }
97
98 sub _quote_args {
99     my ($args) = @_;
100     my $argstring = '';
101
102     foreach (split(/\s+/,$args)) {
103        # In VMS protect with doublequotes because otherwise
104        # DCL will lowercase -- unless already doublequoted.
105        $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
106        $argstring .= ' ' . $_;
107     }
108     return $argstring;
109 }
110
111 sub _populate_hash {
112     return map {$_, 1} split /\s+/, $_[0];
113 }
114
115 unless (@ARGV) {
116     # base first, as TEST bails out if that can't run
117     # then comp, to validate that require works
118     # then run, to validate that -M works
119     # then we know we can -MTestInit for everything else, making life simpler
120     foreach my $dir (qw(base comp run cmd io op uni mro)) {
121         _find_tests($dir);
122     }
123     _find_tests("lib") unless $::core;
124     # Config.pm may be broken for make minitest. And this is only a refinement
125     # for skipping tests on non-default builds, so it is allowed to fail.
126     # What we want to to is make a list of extensions which we did not build.
127     my $configsh = '../config.sh';
128     my %skip;
129     if (-f $configsh) {
130         my (%extensions, %known_extensions);
131         open FH, $configsh or die "Can't open $configsh: $!";
132         while (<FH>) {
133             if (/^extensions=['"](.*)['"]$/) {
134                 # Deliberate string interpolation to avoid triggering possible
135                 # $1 resetting bugs.
136                 %extensions = _populate_hash ("$1");
137             }
138             elsif (/^known_extensions=['"](.*)['"]$/) {
139                 %known_extensions = _populate_hash ($1);
140             }
141         }
142         if (%extensions) {
143             if (%known_extensions) {
144                 foreach (keys %known_extensions) {
145                     $skip{$_}++ unless $extensions{$_};
146                 }
147             } else {
148                 warn "No known_extensions line found in $configsh";
149             }
150         } else {
151             warn "No extensions line found in $configsh";
152         }
153     }
154     my $mani = '../MANIFEST';
155     if (open(MANI, $mani)) {
156         while (<MANI>) { # similar code in t/harness
157             if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
158                 my $t = $1;
159                 my $extension = $2;
160                 if (!$::core || $t =~ m!^lib/[a-z]!)
161                 {
162                     if (defined $extension) {
163                         $extension =~ s!/t$!!;
164                         # XXX Do I want to warn that I'm skipping these?
165                         next if $skip{$extension};
166                         my $flat_extension = $extension;
167                         $flat_extension =~ s!-!/!g;
168                         next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
169                     }
170                     my $path = "../$t";
171                     push @ARGV, $path;
172                     $::path_to_name{$path} = $t;
173                 }
174             }
175         }
176         close MANI;
177     } else {
178         warn "$0: cannot open $mani: $!\n";
179     }
180     unless ($::core) {
181         _find_tests('pod');
182         _find_tests('x2p');
183         _find_tests('porting');
184         _find_tests('japh') if $::torture;
185         _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
186     }
187 }
188
189 if ($::deparse) {
190     _testprogs('deparse', '',   @ARGV);
191 }
192 elsif ($::with_utf16) {
193     for my $e (0, 1) {
194         for my $b (0, 1) {
195             print STDERR "# ENDIAN $e BOM $b\n";
196             my @UARGV;
197             for my $a (@ARGV) {
198                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
199                 my $f = $e ? "v" : "n";
200                 push @UARGV, $u;
201                 unlink($u);
202                 if (open(A, $a)) {
203                     if (open(U, ">$u")) {
204                         print U pack("$f", 0xFEFF) if $b;
205                         while (<A>) {
206                             print U pack("$f*", unpack("C*", $_));
207                         }
208                         close(U);
209                     }
210                     close(A);
211                 }
212             }
213             _testprogs('perl', '', @UARGV);
214             unlink(@UARGV);
215         }
216     }
217 }
218 else {
219     _testprogs('perl',    '',   @ARGV);
220 }
221
222 sub _testprogs {
223     my ($type, $args, @tests) = @_;
224
225     print <<'EOT' if ($type eq 'deparse');
226 ------------------------------------------------------------------------------
227 TESTING DEPARSER
228 ------------------------------------------------------------------------------
229 EOT
230
231     $::bad_files = 0;
232
233     foreach my $t (@tests) {
234       unless (exists $::path_to_name{$t}) {
235         my $tname = "t/$t";
236         $::path_to_name{$t} = $tname;
237       }
238     }
239     my $maxlen = 0;
240     foreach (@::path_to_name{@tests}) {
241         s/\.\w+\z/./;
242         my $len = length ;
243         $maxlen = $len if $len > $maxlen;
244     }
245     # + 3 : we want three dots between the test name and the "ok"
246     my $dotdotdot = $maxlen + 3 ;
247     my $valgrind = 0;
248     my $valgrind_log = 'current.valgrind';
249     my $total_files = @tests;
250     my $good_files = 0;
251     my $tested_files  = 0;
252     my $totmax = 0;
253     my %failed_tests;
254
255     while (my $test = shift @tests) {
256         my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
257
258         if ($test =~ /^$/) {
259             next;
260         }
261         if ($type eq 'deparse') {
262             if ($test eq "comp/redef.t") {
263                 # Redefinition happens at compile time
264                 next;
265             }
266             elsif ($test =~ m{lib/Switch/t/}) {
267                 # B::Deparse doesn't support source filtering
268                 next;
269             }
270         }
271         my $te = $::path_to_name{$test} . '.'
272                     x ($dotdotdot - length($::path_to_name{$test}));
273
274         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
275             print $te;
276             $te = '';
277         }
278
279         # XXX DAPM %OVER not defined anywhere
280         # $test = $OVER{$test} if exists $OVER{$test};
281
282         open(SCRIPT,"<",$test) or die "Can't run $test.\n";
283         $_ = <SCRIPT>;
284         close(SCRIPT) unless ($type eq 'deparse');
285         if ($::with_utf16) {
286             $_ =~ tr/\0//d;
287         }
288         my $switch;
289         if (/#!.*\bperl.*\s-\w*([tT])/) {
290             $switch = qq{"-$1"};
291         }
292         else {
293             if ($::taintwarn) {
294                 # not all tests are expected to pass with this option
295                 $switch = '"-t"';
296             }
297             else {
298                 $switch = '';
299             }
300         }
301
302         my $file_opts = "";
303         if ($type eq 'deparse') {
304             # Look for #line directives which change the filename
305             while (<SCRIPT>) {
306                 $file_opts .= ",-f$3$4"
307                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
308             }
309             close(SCRIPT);
310         }
311
312         my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
313         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
314         if ($type eq 'deparse') {
315             my $deparse_cmd =
316                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
317                 "-l$::deparse_opts$file_opts ".
318                 "$test > $test.dp ".
319                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
320             open(RESULTS, $deparse_cmd)
321                 or print "can't deparse '$deparse_cmd': $!.\n";
322         }
323         elsif ($type eq 'perl') {
324             my $perl = $ENV{PERL} || './perl';
325             my $redir = $^O eq 'VMS' ? '2>&1' : '';
326             if ($ENV{PERL_VALGRIND}) {
327                 my $valgrind = $ENV{VALGRIND} // 'valgrind';
328                 my $vg_opts = $ENV{VG_OPTS}
329                     //  "--suppressions=perl.supp --leak-check=yes "
330                         . "--leak-resolution=high --show-reachable=yes "
331                         . "--num-callers=50"; 
332                 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
333                 $redir = "3>$valgrind_log";
334             }
335             my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
336                               . " $test $redir|";
337             open(RESULTS,$run) or print "can't run '$run': $!.\n";
338         }
339         # Our environment may force us to use UTF-8, but we can't be sure that
340         # anything we're reading from will be generating (well formed) UTF-8
341         # This may not be the best way - possibly we should unset ${^OPEN} up
342         # top?
343         binmode RESULTS;
344
345         my $failure;
346         my $next = 0;
347         my $seen_leader = 0;
348         my $seen_ok = 0;
349         my $trailing_leader = 0;
350         my $max;
351         my %todo;
352         while (<RESULTS>) {
353             next if /^\s*$/; # skip blank lines
354             if (/^1..$/ && ($^O eq 'VMS')) {
355                 # VMS pipe bug inserts blank lines.
356                 my $l2 = <RESULTS>;
357                 if ($l2 =~ /^\s*$/) {
358                     $l2 = <RESULTS>;
359                 }
360                 $_ = '1..' . $l2;
361             }
362             if ($::verbose) {
363                 print $_;
364             }
365             unless (/^\#/) {
366                 if ($trailing_leader) {
367                     # shouldn't be anything following a postfix 1..n
368                     $failure = 'FAILED--extra output after trailing 1..n';
369                     last;
370                 }
371                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
372                     if ($seen_leader) {
373                         $failure = 'FAILED--seen duplicate leader';
374                         last;
375                     }
376                     $max = $1;
377                     %todo = map { $_ => 1 } split / /, $3 if $3;
378                     $totmax += $max;
379                     $tested_files++;
380                     if ($seen_ok) {
381                         # 1..n appears at end of file
382                         $trailing_leader = 1;
383                         if ($next != $max) {
384                             $failure = "FAILED--expected $max tests, saw $next";
385                             last;
386                         }
387                     }
388                     else {
389                         $next = 0;
390                     }
391                     $seen_leader = 1;
392                 }
393                 else {
394                     if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
395                         unless ($seen_leader) {
396                             unless ($seen_ok) {
397                                 $next = 0;
398                             }
399                         }
400                         $seen_ok = 1;
401                         $next++;
402                         my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
403                         $num = $next unless $num;
404
405                         if ($num == $next) {
406
407                             # SKIP is essentially the same as TODO for t/TEST
408                             # this still conforms to TAP:
409                             # http://search.cpan.org/dist/TAP/TAP.pod
410                             $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
411                             $istodo = 1 if $todo{$num};
412
413                             if( $not && !$istodo ) {
414                                 $failure = "FAILED at test $num";
415                                 last;
416                             }
417                         }
418                         else {
419                             $failure ="FAILED--expected test $next, saw test $num";
420                             last;
421                         }
422                     }
423                     elsif (/^Bail out!\s*(.*)/i) { # magic words
424                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
425                     }
426                     else {
427                         # module tests are allowed extra output,
428                         # because Test::Harness allows it
429                         next if $test =~ /^\W*(ext|lib)\b/;
430                         $failure = "FAILED--unexpected output at test $next";
431                         last;
432                     }
433                 }
434             }
435         }
436         close RESULTS;
437
438         if (not defined $failure) {
439             $failure = 'FAILED--no leader found' unless $seen_leader;
440         }
441
442         if ($ENV{PERL_VALGRIND}) {
443             my @valgrind;
444             if (-e $valgrind_log) {
445                 if (open(V, $valgrind_log)) {
446                     @valgrind = <V>;
447                     close V;
448                 } else {
449                     warn "$0: Failed to open '$valgrind_log': $!\n";
450                 }
451             }
452             if ($ENV{VG_OPTS} =~ /cachegrind/) {
453                 if (rename $valgrind_log, "$test.valgrind") {
454                     $valgrind++;
455                 } else {
456                     warn "$0: Failed to create '$test.valgrind': $!\n";
457                 }
458             }
459             elsif (@valgrind) {
460                 my $leaks = 0;
461                 my $errors = 0;
462                 for my $i (0..$#valgrind) {
463                     local $_ = $valgrind[$i];
464                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
465                         $errors += $1;   # there may be multiple error summaries
466                     } elsif (/^==\d+== LEAK SUMMARY:/) {
467                         for my $off (1 .. 4) {
468                             if ($valgrind[$i+$off] =~
469                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
470                                 $leaks += $1;
471                             }
472                         }
473                     }
474                 }
475                 if ($errors or $leaks) {
476                     if (rename $valgrind_log, "$test.valgrind") {
477                         $valgrind++;
478                     } else {
479                         warn "$0: Failed to create '$test.valgrind': $!\n";
480                     }
481                 }
482             } else {
483                 warn "No valgrind output?\n";
484             }
485             if (-e $valgrind_log) {
486                 unlink $valgrind_log
487                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
488             }
489         }
490         if ($type eq 'deparse') {
491             unlink "./$test.dp";
492         }
493         if ($ENV{PERL_3LOG}) {
494             my $tpp = $test;
495             $tpp =~ s:^\.\./::;
496             $tpp =~ s:/:_:g;
497             $tpp =~ s:\.t$:.3log:;
498             rename("perl.3log", $tpp) ||
499                 die "rename: perl3.log to $tpp: $!\n";
500         }
501         if (not defined $failure and $next != $max) {
502             $failure="FAILED--expected $max tests, saw $next";
503         }
504
505         if( !defined $failure  # don't mask a test failure
506             and $? )
507         {
508             $failure = "FAILED--non-zero wait status: $?";
509         }
510
511         if (defined $failure) {
512             print "${te}$failure\n";
513             $::bad_files++;
514             if ($test =~ /^base/) {
515                 die "Failed a basic test ($test) -- cannot continue.\n";
516             }
517             ++$failed_tests{$test};
518         }
519         else {
520             if ($max) {
521                 my $elapsed;
522                 if ( $show_elapsed_time ) {
523                     $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
524                 }
525                 else {
526                     $elapsed = "";
527                 }
528                 print "${te}ok$elapsed\n";
529                 $good_files++;
530             }
531             else {
532                 print "${te}skipped\n";
533                 $tested_files -= 1;
534             }
535         }
536     } # while tests
537
538     if ($::bad_files == 0) {
539         if ($good_files) {
540             print "All tests successful.\n";
541             # XXX add mention of 'perlbug -ok' ?
542         }
543         else {
544             die "FAILED--no tests were run for some reason.\n";
545         }
546     }
547     else {
548         my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
549         my $s = $::bad_files == 1 ? "" : "s";
550         warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
551         for my $test ( sort keys %failed_tests ) {
552             print "\t$test\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_files / $total_files > 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     my ($user,$sys,$cuser,$csys) = times;
591     print sprintf("u=%.2f  s=%.2f  cu=%.2f  cs=%.2f  scripts=%d  tests=%d\n",
592         $user,$sys,$cuser,$csys,$tested_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_files != 0);
599
600 # ex: set ts=8 sts=4 sw=4 noet: