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.)
8 # Location to put the Valgrind log.
9 my $Valgrind_Log = 'current.valgrind';
13 # for testing TEST only
14 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
15 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
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 $::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(,.+)?$/) {
44 chdir 't' if -f 't/TEST';
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';
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";
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';
64 # check leakage for embedders
65 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
67 $ENV{EMXSHELL} = 'sh'; # For OS/2
69 # Roll your own File::Find!
70 if ($show_elapsed_time) { require Time::HiRes }
83 opendir DIR, $dir or die "Trouble opening $dir: $!";
84 foreach my $f (sort { $a cmp $b } readdir DIR) {
87 my $fullpath = "$dir/$f";
90 _find_tests($fullpath);
91 } elsif ($f =~ /\.t$/) {
92 push @ARGV, $fullpath;
98 # Scan the text of the test program to find switches and special options
99 # we might need to apply.
101 my($test, $type) = @_;
103 open(my $script, "<", $test) or die "Can't read $test.\n";
104 my $first_line = <$script>;
106 $first_line =~ tr/\0//d if $::with_utf16;
109 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
113 # not all tests are expected to pass with this option
121 if ($type eq 'deparse') {
122 # Look for #line directives which change the filename
124 $file_opts .= ",-f$3$4"
125 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
129 return { file => $file_opts, switch => $switch };
132 # directories with special sets of test switches
140 my($test, $type) = @_;
142 my $options = _scan_test($test, $type);
146 $test =~ /^([^\/]+)/;
148 my $testswitch = $dir_to_switch{$1};
149 if (!defined $testswitch) {
150 $testswitch = '-I. -MTestInit'; # -T will remove . from @INC
153 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
156 if ($type eq 'deparse') {
158 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
159 "-l$::deparse_opts$options->{file} ".
161 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
162 open($results, $deparse_cmd)
163 or print "can't deparse '$deparse_cmd': $!.\n";
165 elsif ($type eq 'perl') {
166 my $perl = $ENV{PERL} || $perl;
167 my $redir = $^O eq 'VMS' ? '2>&1' : '';
169 if ($ENV{PERL_VALGRIND}) {
170 my $valgrind = $ENV{VALGRIND} // 'valgrind';
171 my $vg_opts = $ENV{VG_OPTS}
172 // "--suppressions=perl.supp --leak-check=yes "
173 . "--leak-resolution=high --show-reachable=yes "
174 . "--num-callers=50";
175 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
176 $redir = "3>$Valgrind_Log";
179 my $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
181 open($results, $run) or print "can't run '$run': $!.\n";
184 # Our environment may force us to use UTF-8, but we can't be sure that
185 # anything we're reading from will be generating (well formed) UTF-8
186 # This may not be the best way - possibly we should unset ${^OPEN} up
197 foreach (split(/\s+/,$args)) {
198 # In VMS protect with doublequotes because otherwise
199 # DCL will lowercase -- unless already doublequoted.
200 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
201 $argstring .= ' ' . $_;
207 return unless defined $_[0];
208 return map {$_, 1} split /\s+/, $_[0];
211 sub _tests_from_manifest {
212 my ($extensions, $known_extensions) = @_;
214 my %extensions = _populate_hash($extensions);
215 my %known_extensions = _populate_hash($known_extensions);
217 foreach (keys %known_extensions) {
218 $skip{$_}++ unless $extensions{$_};
222 my $mani = '../MANIFEST';
223 if (open(MANI, $mani)) {
224 while (<MANI>) { # similar code in t/harness
225 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
228 if (!$::core || $t =~ m!^lib/[a-z]!) {
229 if (defined $extension) {
230 $extension =~ s!/t$!!;
231 # XXX Do I want to warn that I'm skipping these?
232 next if $skip{$extension};
233 my $flat_extension = $extension;
234 $flat_extension =~ s!-!/!g;
235 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
238 push @results, $path;
239 $::path_to_name{$path} = $t;
245 warn "$0: cannot open $mani: $!\n";
251 # base first, as TEST bails out if that can't run
252 # then comp, to validate that require works
253 # then run, to validate that -M works
254 # then we know we can -MTestInit for everything else, making life simpler
255 foreach my $dir (qw(base comp run cmd io op uni mro)) {
258 _find_tests("lib") unless $::core;
259 # Config.pm may be broken for make minitest. And this is only a refinement
260 # for skipping tests on non-default builds, so it is allowed to fail.
261 # What we want to to is make a list of extensions which we did not build.
262 my $configsh = '../config.sh';
263 my ($extensions, $known_extensions);
265 open FH, $configsh or die "Can't open $configsh: $!";
267 if (/^extensions=['"](.*)['"]$/) {
270 elsif (/^known_extensions=['"](.*)['"]$/) {
271 $known_extensions = $1;
274 if (!defined $known_extensions) {
275 warn "No known_extensions line found in $configsh";
277 if (!defined $extensions) {
278 warn "No extensions line found in $configsh";
281 # The "complex" constructions of list return from a subroutine, and push of
282 # a list, might fail if perl is really hosed, but they aren't needed for
283 # make minitest, and the building of extensions will likely also fail if
284 # something is that badly wrong.
285 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
289 _find_tests('porting');
290 _find_tests('japh') if $::torture;
291 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
296 _testprogs('deparse', '', @ARGV);
298 elsif ($::with_utf16) {
301 print STDERR "# ENDIAN $e BOM $b\n";
304 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
305 my $f = $e ? "v" : "n";
309 if (open(U, ">$u")) {
310 print U pack("$f", 0xFEFF) if $b;
312 print U pack("$f*", unpack("C*", $_));
319 _testprogs('perl', '', @UARGV);
325 _testprogs('perl', '', @ARGV);
329 my ($type, $args, @tests) = @_;
331 print <<'EOT' if ($type eq 'deparse');
332 ------------------------------------------------------------------------------
334 ------------------------------------------------------------------------------
339 foreach my $t (@tests) {
340 unless (exists $::path_to_name{$t}) {
342 $::path_to_name{$t} = $tname;
346 foreach (@::path_to_name{@tests}) {
349 $maxlen = $len if $len > $maxlen;
351 # + 3 : we want three dots between the test name and the "ok"
352 my $dotdotdot = $maxlen + 3 ;
354 my $total_files = @tests;
356 my $tested_files = 0;
360 while (my $test = shift @tests) {
361 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
366 if ($type eq 'deparse') {
367 if ($test eq "comp/redef.t") {
368 # Redefinition happens at compile time
371 elsif ($test =~ m{lib/Switch/t/}) {
372 # B::Deparse doesn't support source filtering
376 my $te = $::path_to_name{$test} . '.'
377 x ($dotdotdot - length($::path_to_name{$test}));
379 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
384 my $results = _run_test($test, $type);
390 my $trailing_leader = 0;
394 next if /^\s*$/; # skip blank lines
395 if (/^1..$/ && ($^O eq 'VMS')) {
396 # VMS pipe bug inserts blank lines.
398 if ($l2 =~ /^\s*$/) {
407 if ($trailing_leader) {
408 # shouldn't be anything following a postfix 1..n
409 $failure = 'FAILED--extra output after trailing 1..n';
412 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
414 $failure = 'FAILED--seen duplicate leader';
418 %todo = map { $_ => 1 } split / /, $3 if $3;
422 # 1..n appears at end of file
423 $trailing_leader = 1;
425 $failure = "FAILED--expected $max tests, saw $next";
435 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
436 unless ($seen_leader) {
443 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
444 $num = $next unless $num;
448 # SKIP is essentially the same as TODO for t/TEST
449 # this still conforms to TAP:
450 # http://search.cpan.org/dist/TAP/TAP.pod
451 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
452 $istodo = 1 if $todo{$num};
454 if( $not && !$istodo ) {
455 $failure = "FAILED at test $num";
460 $failure ="FAILED--expected test $next, saw test $num";
464 elsif (/^Bail out!\s*(.*)/i) { # magic words
465 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
468 # module tests are allowed extra output,
469 # because Test::Harness allows it
470 next if $test =~ /^\W*(ext|lib)\b/;
471 $failure = "FAILED--unexpected output at test $next";
479 if (not defined $failure) {
480 $failure = 'FAILED--no leader found' unless $seen_leader;
483 if ($ENV{PERL_VALGRIND}) {
485 if (-e $Valgrind_Log) {
486 if (open(V, $Valgrind_Log)) {
490 warn "$0: Failed to open '$Valgrind_Log': $!\n";
493 if ($ENV{VG_OPTS} =~ /cachegrind/) {
494 if (rename $Valgrind_Log, "$test.valgrind") {
497 warn "$0: Failed to create '$test.valgrind': $!\n";
503 for my $i (0..$#valgrind) {
504 local $_ = $valgrind[$i];
505 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
506 $errors += $1; # there may be multiple error summaries
507 } elsif (/^==\d+== LEAK SUMMARY:/) {
508 for my $off (1 .. 4) {
509 if ($valgrind[$i+$off] =~
510 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
516 if ($errors or $leaks) {
517 if (rename $Valgrind_Log, "$test.valgrind") {
520 warn "$0: Failed to create '$test.valgrind': $!\n";
524 warn "No valgrind output?\n";
526 if (-e $Valgrind_Log) {
528 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
531 if ($type eq 'deparse') {
534 if ($ENV{PERL_3LOG}) {
538 $tpp =~ s:\.t$:.3log:;
539 rename("perl.3log", $tpp) ||
540 die "rename: perl3.log to $tpp: $!\n";
542 if (not defined $failure and $next != $max) {
543 $failure="FAILED--expected $max tests, saw $next";
546 if( !defined $failure # don't mask a test failure
549 $failure = "FAILED--non-zero wait status: $?";
552 if (defined $failure) {
553 print "${te}$failure\n";
555 if ($test =~ /^base/) {
556 die "Failed a basic test ($test) -- cannot continue.\n";
558 ++$failed_tests{$test};
563 if ( $show_elapsed_time ) {
564 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
569 print "${te}ok$elapsed\n";
573 print "${te}skipped\n";
579 if ($::bad_files == 0) {
581 print "All tests successful.\n";
582 # XXX add mention of 'perlbug -ok' ?
585 die "FAILED--no tests were run for some reason.\n";
589 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
590 my $s = $::bad_files == 1 ? "" : "s";
591 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
592 for my $test ( sort keys %failed_tests ) {
596 ### Since not all tests were successful, you may want to run some of
597 ### them individually and examine any diagnostic messages they produce.
598 ### See the INSTALL document's section on "make test".
600 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
601 ### You have a good chance to get more information by running
603 ### in the 't' directory since most (>=80%) of the tests succeeded.
605 if (eval {require Config; import Config; 1}) {
606 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
608 ### You may have to set your dynamic library search path,
609 ### $p, to point to the build directory:
611 if (exists $ENV{$p} && $ENV{$p} ne '') {
613 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
614 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
615 ### export $p=`pwd`:\$$p; cd t; ./perl harness
619 ### setenv $p `pwd`; cd t; ./perl harness
620 ### $p=`pwd`; export $p; cd t; ./perl harness
621 ### export $p=`pwd`; cd t; ./perl harness
625 ### for csh-style shells, like tcsh; or for traditional/modern
626 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
631 my ($user,$sys,$cuser,$csys) = times;
632 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
633 $user,$sys,$cuser,$csys,$tested_files,$totmax);
634 if ($ENV{PERL_VALGRIND}) {
635 my $s = $valgrind == 1 ? '' : 's';
636 print "$valgrind valgrind report$s created.\n", ;
639 exit ($::bad_files != 0);
641 # ex: set ts=8 sts=4 sw=4 noet: