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.)
10 # for testing TEST only
11 #BEGIN { require '../lib/strict.pm'; strict->import() };
12 #BEGIN { require '../lib/warnings.pm'; warnings->import() };
14 # Let tests know they're running in the perl core. Useful for modules
15 # which live dual lives on CPAN.
17 delete $ENV{PERL5LIB};
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;
23 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
26 foreach my $idx (0..$#ARGV) {
27 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
28 $::core = 1 if $1 eq 'core';
29 $::verbose = 1 if $1 eq 'v';
30 $::torture = 1 if $1 eq 'torture';
31 $::with_utf8 = 1 if $1 eq 'utf8';
32 $::with_utf16 = 1 if $1 eq 'utf16';
33 $::taintwarn = 1 if $1 eq 'taintwarn';
34 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
35 if ($1 =~ /^deparse(,.+)?$/) {
43 chdir 't' if -f 't/TEST';
45 die "You need to run \"make test\" first to set things up.\n"
46 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
48 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
49 unless (-x 'perl.third') {
50 unless (-x '../perl.third') {
51 die "You need to run \"make perl.third first.\n";
54 print "Symlinking ../perl.third as perl.third...\n";
55 die "Failed to symlink: $!\n"
56 unless symlink("../perl.third", "perl.third");
57 die "Symlinked but no executable perl.third: $!\n"
58 unless -x 'perl.third';
63 # check leakage for embedders
64 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
66 $ENV{EMXSHELL} = 'sh'; # For OS/2
68 # Roll your own File::Find!
71 if ($show_elapsed_time) { require Time::HiRes }
72 my $curdir = File::Spec->curdir;
73 my $updir = File::Spec->updir;
77 opendir DIR, $dir or die "Trouble opening $dir: $!";
78 foreach my $f (sort { $a cmp $b } readdir DIR) {
79 next if $f eq $curdir or $f eq $updir or
80 $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
82 my $fullpath = File::Spec->catfile($dir, $f);
84 _find_tests($fullpath) if -d $fullpath;
85 $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
86 push @ARGV, $fullpath if $f =~ /\.t$/;
94 foreach (split(/\s+/,$args)) {
95 # In VMS protect with doublequotes because otherwise
96 # DCL will lowercase -- unless already doublequoted.
97 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
98 $argstring .= ' ' . $_;
104 return map {$_, 1} split /\s+/, $_[0];
108 foreach my $dir (qw(base comp cmd run io op uni mro)) {
111 _find_tests("lib") unless $::core;
112 # Config.pm may be broken for make minitest. And this is only a refinement
113 # for skipping tests on non-default builds, so it is allowed to fail.
114 # What we want to to is make a list of extensions which we did not build.
115 my $configsh = File::Spec->catfile($updir, "config.sh");
118 my (%extensions, %known_extensions);
119 open FH, $configsh or die "Can't open $configsh: $!";
121 if (/^extensions=['"](.*)['"]$/) {
122 # Deliberate string interpolation to avoid triggering possible
124 %extensions = _populate_hash ("$1");
126 elsif (/^known_extensions=['"](.*)['"]$/) {
127 %known_extensions = _populate_hash ($1);
131 if (%known_extensions) {
132 foreach (keys %known_extensions) {
133 $skip{$_}++ unless $extensions{$_};
136 warn "No known_extensions line found in $configsh";
139 warn "No extensions line found in $configsh";
142 my $mani = File::Spec->catfile($updir, "MANIFEST");
143 if (open(MANI, $mani)) {
144 while (<MANI>) { # similar code in t/harness
145 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
148 if (!$::core || $t =~ m!^lib/[a-z]!)
150 if (defined $extension) {
151 $extension =~ s!/t$!!;
152 # XXX Do I want to warn that I'm skipping these?
153 next if $skip{$extension};
154 my $flat_extension = $extension;
155 $flat_extension =~ s!-!/!g;
156 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
158 my $path = File::Spec->catfile($updir, $t);
160 $::path_to_name{$path} = $t;
166 warn "$0: cannot open $mani: $!\n";
169 _find_tests('Module_Pluggable');
172 _find_tests('japh') if $::torture;
177 _testprogs('deparse', '', @ARGV);
179 elsif ($::with_utf16) {
182 print STDERR "# ENDIAN $e BOM $b\n";
185 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
186 my $f = $e ? "v" : "n";
190 if (open(U, ">$u")) {
191 print U pack("$f", 0xFEFF) if $b;
193 print U pack("$f*", unpack("C*", $_));
200 _testprogs('perl', '', @UARGV);
206 _testprogs('perl', '', @ARGV);
210 my ($type, $args, @tests) = @_;
212 print <<'EOT' if ($type eq 'deparse');
213 ------------------------------------------------------------------------------
215 ------------------------------------------------------------------------------
220 foreach my $t (@tests) {
221 unless (exists $::path_to_name{$t}) {
222 my $tname = File::Spec->catfile('t',$t);
223 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
224 $::path_to_name{$t} = $tname;
228 foreach (@::path_to_name{@tests}) {
231 $maxlen = $len if $len > $maxlen;
233 # + 3 : we want three dots between the test name and the "ok"
234 my $dotdotdot = $maxlen + 3 ;
236 my $valgrind_log = 'current.valgrind';
237 my $total_files = @tests;
239 my $tested_files = 0;
243 while (my $test = shift @tests) {
244 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
249 if ($type eq 'deparse') {
250 if ($test eq "comp/redef.t") {
251 # Redefinition happens at compile time
254 elsif ($test =~ m{lib/Switch/t/}) {
255 # B::Deparse doesn't support source filtering
259 my $te = $::path_to_name{$test} . '.'
260 x ($dotdotdot - length($::path_to_name{$test}));
262 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
267 # XXX DAPM %OVER not defined anywhere
268 # $test = $OVER{$test} if exists $OVER{$test};
270 open(SCRIPT,"<",$test) or die "Can't run $test.\n";
272 close(SCRIPT) unless ($type eq 'deparse');
277 if (/#!.*\bperl.*\s-\w*([tT])/) {
282 # not all tests are expected to pass with this option
291 if ($type eq 'deparse') {
292 # Look for #line directives which change the filename
294 $file_opts .= ",-f$3$4"
295 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
300 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
301 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
302 if ($type eq 'deparse') {
304 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
305 "-l$::deparse_opts$file_opts ".
307 "&& ./perl $testswitch $switch -I../lib $test.dp |";
308 open(RESULTS, $deparse_cmd)
309 or print "can't deparse '$deparse_cmd': $!.\n";
311 elsif ($type eq 'perl') {
312 my $perl = $ENV{PERL} || './perl';
313 my $redir = $^O eq 'VMS' ? '2>&1' : '';
314 if ($ENV{PERL_VALGRIND}) {
315 my $valgrind = $ENV{VALGRIND} // 'valgrind';
316 my $vg_opts = $ENV{VG_OPTS}
317 // "--suppressions=perl.supp --leak-check=yes "
318 . "--leak-resolution=high --show-reachable=yes "
319 . "--num-callers=50";
320 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
321 $redir = "3>$valgrind_log";
323 my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
325 open(RESULTS,$run) or print "can't run '$run': $!.\n";
327 # Our environment may force us to use UTF-8, but we can't be sure that
328 # anything we're reading from will be generating (well formed) UTF-8
329 # This may not be the best way - possibly we should unset ${^OPEN} up
337 my $trailing_leader = 0;
341 next if /^\s*$/; # skip blank lines
346 if ($trailing_leader) {
347 # shouldn't be anything following a postfix 1..n
348 $failure = 'FAILED--extra output after trailing 1..n';
351 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
353 $failure = 'FAILED--seen duplicate leader';
357 %todo = map { $_ => 1 } split / /, $3 if $3;
361 # 1..n appears at end of file
362 $trailing_leader = 1;
364 $failure = "FAILED--expected $max tests, saw $next";
374 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
375 unless ($seen_leader) {
382 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
383 $num = $next unless $num;
387 # SKIP is essentially the same as TODO for t/TEST
388 # this still conforms to TAP:
389 # http://search.cpan.org/dist/TAP/TAP.pod
390 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
391 $istodo = 1 if $todo{$num};
393 if( $not && !$istodo ) {
394 $failure = "FAILED at test $num";
399 $failure ="FAILED--expected test $next, saw test $num";
403 elsif (/^Bail out!\s*(.*)/i) { # magic words
404 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
407 # module tests are allowed extra output,
408 # because Test::Harness allows it
409 next if $test =~ /^\W*(ext|lib)\b/;
410 $failure = "FAILED--unexpected output at test $next";
418 if (not defined $failure) {
419 $failure = 'FAILED--no leader found' unless $seen_leader;
422 if ($ENV{PERL_VALGRIND}) {
424 if (-e $valgrind_log) {
425 if (open(V, $valgrind_log)) {
429 warn "$0: Failed to open '$valgrind_log': $!\n";
432 if ($ENV{VG_OPTS} =~ /cachegrind/) {
433 if (rename $valgrind_log, "$test.valgrind") {
436 warn "$0: Failed to create '$test.valgrind': $!\n";
442 for my $i (0..$#valgrind) {
443 local $_ = $valgrind[$i];
444 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
445 $errors += $1; # there may be multiple error summaries
446 } elsif (/^==\d+== LEAK SUMMARY:/) {
447 for my $off (1 .. 4) {
448 if ($valgrind[$i+$off] =~
449 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
455 if ($errors or $leaks) {
456 if (rename $valgrind_log, "$test.valgrind") {
459 warn "$0: Failed to create '$test.valgrind': $!\n";
463 warn "No valgrind output?\n";
465 if (-e $valgrind_log) {
467 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
470 if ($type eq 'deparse') {
473 if ($ENV{PERL_3LOG}) {
477 $tpp =~ s:\.t$:.3log:;
478 rename("perl.3log", $tpp) ||
479 die "rename: perl3.log to $tpp: $!\n";
481 if (not defined $failure and $next != $max) {
482 $failure="FAILED--expected $max tests, saw $next";
485 if( !defined $failure # don't mask a test failure
488 $failure = "FAILED--non-zero wait status: $?";
491 if (defined $failure) {
492 print "${te}$failure\n";
494 if ($test =~ /^base/) {
495 die "Failed a basic test ($test) -- cannot continue.\n";
497 ++$failed_tests{$test};
502 if ( $show_elapsed_time ) {
503 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
508 print "${te}ok$elapsed\n";
512 print "${te}skipped\n";
518 if ($::bad_files == 0) {
520 print "All tests successful.\n";
521 # XXX add mention of 'perlbug -ok' ?
524 die "FAILED--no tests were run for some reason.\n";
528 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
529 my $s = $::bad_files == 1 ? "" : "s";
530 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
531 for my $test ( sort keys %failed_tests ) {
535 ### Since not all tests were successful, you may want to run some of
536 ### them individually and examine any diagnostic messages they produce.
537 ### See the INSTALL document's section on "make test".
539 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
540 ### You have a good chance to get more information by running
542 ### in the 't' directory since most (>=80%) of the tests succeeded.
544 if (eval {require Config; import Config; 1}) {
545 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
547 ### You may have to set your dynamic library search path,
548 ### $p, to point to the build directory:
550 if (exists $ENV{$p} && $ENV{$p} ne '') {
552 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
553 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
554 ### export $p=`pwd`:\$$p; cd t; ./perl harness
558 ### setenv $p `pwd`; cd t; ./perl harness
559 ### $p=`pwd`; export $p; cd t; ./perl harness
560 ### export $p=`pwd`; cd t; ./perl harness
564 ### for csh-style shells, like tcsh; or for traditional/modern
565 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
570 my ($user,$sys,$cuser,$csys) = times;
571 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
572 $user,$sys,$cuser,$csys,$tested_files,$totmax);
573 if ($ENV{PERL_VALGRIND}) {
574 my $s = $valgrind == 1 ? '' : 's';
575 print "$valgrind valgrind report$s created.\n", ;
578 exit ($::bad_files != 0);
580 # ex: set ts=8 sts=4 sw=4 noet: