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 # Let tests know they're running in the perl core. Useful for modules
18 # which live dual lives on CPAN.
20 delete $ENV{PERL5LIB};
22 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
23 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
24 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
26 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
29 foreach my $idx (0..$#ARGV) {
30 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
31 $::benchmark = 1 if $1 eq 'benchmark';
32 $::core = 1 if $1 eq 'core';
33 $::verbose = 1 if $1 eq 'v';
34 $::torture = 1 if $1 eq 'torture';
35 $::with_utf8 = 1 if $1 eq 'utf8';
36 $::with_utf16 = 1 if $1 eq 'utf16';
37 $::taintwarn = 1 if $1 eq 'taintwarn';
38 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
39 if ($1 =~ /^deparse(,.+)?$/) {
47 chdir 't' if -f 't/TEST';
49 die "You need to run \"make test\" first to set things up.\n"
50 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
52 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
53 unless (-x 'perl.third') {
54 unless (-x '../perl.third') {
55 die "You need to run \"make perl.third first.\n";
58 print "Symlinking ../perl.third as perl.third...\n";
59 die "Failed to symlink: $!\n"
60 unless symlink("../perl.third", "perl.third");
61 die "Symlinked but no executable perl.third: $!\n"
62 unless -x 'perl.third';
67 # check leakage for embedders
68 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
70 $ENV{EMXSHELL} = 'sh'; # For OS/2
72 # Roll your own File::Find!
74 if ($show_elapsed_time) { require Time::HiRes }
87 opendir DIR, $dir or die "Trouble opening $dir: $!";
88 foreach my $f (sort { $a cmp $b } readdir DIR) {
91 my $fullpath = "$dir/$f";
94 _find_tests($fullpath);
95 } elsif ($f =~ /\.t$/) {
96 push @ARGV, $fullpath;
102 # Scan the text of the test program to find switches and special options
103 # we might need to apply.
105 my($test, $type) = @_;
107 open(my $script, "<", $test) or die "Can't read $test.\n";
108 my $first_line = <$script>;
110 $first_line =~ tr/\0//d if $::with_utf16;
113 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
117 # not all tests are expected to pass with this option
125 if ($type eq 'deparse') {
126 # Look for #line directives which change the filename
128 $file_opts .= ",-f$3$4"
129 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
133 return { file => $file_opts, switch => $switch };
138 my($test, $type) = @_;
140 my $options = _scan_test($test, $type);
142 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
143 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
146 if ($type eq 'deparse') {
148 "./perl $testswitch $options->{switch} -I../lib -MO=-qq,Deparse,-sv1.,".
149 "-l$::deparse_opts$options->{file} ".
151 "&& ./perl $testswitch $options->{switch} -I../lib $test.dp |";
152 open($results, $deparse_cmd)
153 or print "can't deparse '$deparse_cmd': $!.\n";
155 elsif ($type eq 'perl') {
156 my $perl = $ENV{PERL} || './perl';
157 my $redir = $^O eq 'VMS' ? '2>&1' : '';
159 if ($ENV{PERL_VALGRIND}) {
160 my $valgrind = $ENV{VALGRIND} // 'valgrind';
161 my $vg_opts = $ENV{VG_OPTS}
162 // "--suppressions=perl.supp --leak-check=yes "
163 . "--leak-resolution=high --show-reachable=yes "
164 . "--num-callers=50";
165 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
166 $redir = "3>$Valgrind_Log";
169 my $run = "$perl" . _quote_args("$testswitch $options->{switch} $utf8")
171 open($results, $run) or print "can't run '$run': $!.\n";
174 # Our environment may force us to use UTF-8, but we can't be sure that
175 # anything we're reading from will be generating (well formed) UTF-8
176 # This may not be the best way - possibly we should unset ${^OPEN} up
187 foreach (split(/\s+/,$args)) {
188 # In VMS protect with doublequotes because otherwise
189 # DCL will lowercase -- unless already doublequoted.
190 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
191 $argstring .= ' ' . $_;
197 return map {$_, 1} split /\s+/, $_[0];
201 # base first, as TEST bails out if that can't run
202 # then comp, to validate that require works
203 # then run, to validate that -M works
204 # then we know we can -MTestInit for everything else, making life simpler
205 foreach my $dir (qw(base comp run cmd io op uni mro)) {
208 _find_tests("lib") unless $::core;
209 # Config.pm may be broken for make minitest. And this is only a refinement
210 # for skipping tests on non-default builds, so it is allowed to fail.
211 # What we want to to is make a list of extensions which we did not build.
212 my $configsh = '../config.sh';
215 my (%extensions, %known_extensions);
216 open FH, $configsh or die "Can't open $configsh: $!";
218 if (/^extensions=['"](.*)['"]$/) {
219 # Deliberate string interpolation to avoid triggering possible
221 %extensions = _populate_hash ("$1");
223 elsif (/^known_extensions=['"](.*)['"]$/) {
224 %known_extensions = _populate_hash ($1);
228 if (%known_extensions) {
229 foreach (keys %known_extensions) {
230 $skip{$_}++ unless $extensions{$_};
233 warn "No known_extensions line found in $configsh";
236 warn "No extensions line found in $configsh";
239 my $mani = '../MANIFEST';
240 if (open(MANI, $mani)) {
241 while (<MANI>) { # similar code in t/harness
242 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
245 if (!$::core || $t =~ m!^lib/[a-z]!)
247 if (defined $extension) {
248 $extension =~ s!/t$!!;
249 # XXX Do I want to warn that I'm skipping these?
250 next if $skip{$extension};
251 my $flat_extension = $extension;
252 $flat_extension =~ s!-!/!g;
253 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
257 $::path_to_name{$path} = $t;
263 warn "$0: cannot open $mani: $!\n";
268 _find_tests('porting');
269 _find_tests('japh') if $::torture;
270 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
275 _testprogs('deparse', '', @ARGV);
277 elsif ($::with_utf16) {
280 print STDERR "# ENDIAN $e BOM $b\n";
283 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
284 my $f = $e ? "v" : "n";
288 if (open(U, ">$u")) {
289 print U pack("$f", 0xFEFF) if $b;
291 print U pack("$f*", unpack("C*", $_));
298 _testprogs('perl', '', @UARGV);
304 _testprogs('perl', '', @ARGV);
308 my ($type, $args, @tests) = @_;
310 print <<'EOT' if ($type eq 'deparse');
311 ------------------------------------------------------------------------------
313 ------------------------------------------------------------------------------
318 foreach my $t (@tests) {
319 unless (exists $::path_to_name{$t}) {
321 $::path_to_name{$t} = $tname;
325 foreach (@::path_to_name{@tests}) {
328 $maxlen = $len if $len > $maxlen;
330 # + 3 : we want three dots between the test name and the "ok"
331 my $dotdotdot = $maxlen + 3 ;
333 my $total_files = @tests;
335 my $tested_files = 0;
339 while (my $test = shift @tests) {
340 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
345 if ($type eq 'deparse') {
346 if ($test eq "comp/redef.t") {
347 # Redefinition happens at compile time
350 elsif ($test =~ m{lib/Switch/t/}) {
351 # B::Deparse doesn't support source filtering
355 my $te = $::path_to_name{$test} . '.'
356 x ($dotdotdot - length($::path_to_name{$test}));
358 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
363 # XXX DAPM %OVER not defined anywhere
364 # $test = $OVER{$test} if exists $OVER{$test};
366 my $results = _run_test($test, $type);
372 my $trailing_leader = 0;
376 next if /^\s*$/; # skip blank lines
377 if (/^1..$/ && ($^O eq 'VMS')) {
378 # VMS pipe bug inserts blank lines.
380 if ($l2 =~ /^\s*$/) {
389 if ($trailing_leader) {
390 # shouldn't be anything following a postfix 1..n
391 $failure = 'FAILED--extra output after trailing 1..n';
394 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
396 $failure = 'FAILED--seen duplicate leader';
400 %todo = map { $_ => 1 } split / /, $3 if $3;
404 # 1..n appears at end of file
405 $trailing_leader = 1;
407 $failure = "FAILED--expected $max tests, saw $next";
417 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
418 unless ($seen_leader) {
425 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
426 $num = $next unless $num;
430 # SKIP is essentially the same as TODO for t/TEST
431 # this still conforms to TAP:
432 # http://search.cpan.org/dist/TAP/TAP.pod
433 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
434 $istodo = 1 if $todo{$num};
436 if( $not && !$istodo ) {
437 $failure = "FAILED at test $num";
442 $failure ="FAILED--expected test $next, saw test $num";
446 elsif (/^Bail out!\s*(.*)/i) { # magic words
447 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
450 # module tests are allowed extra output,
451 # because Test::Harness allows it
452 next if $test =~ /^\W*(ext|lib)\b/;
453 $failure = "FAILED--unexpected output at test $next";
461 if (not defined $failure) {
462 $failure = 'FAILED--no leader found' unless $seen_leader;
465 if ($ENV{PERL_VALGRIND}) {
467 if (-e $Valgrind_Log) {
468 if (open(V, $Valgrind_Log)) {
472 warn "$0: Failed to open '$Valgrind_Log': $!\n";
475 if ($ENV{VG_OPTS} =~ /cachegrind/) {
476 if (rename $Valgrind_Log, "$test.valgrind") {
479 warn "$0: Failed to create '$test.valgrind': $!\n";
485 for my $i (0..$#valgrind) {
486 local $_ = $valgrind[$i];
487 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
488 $errors += $1; # there may be multiple error summaries
489 } elsif (/^==\d+== LEAK SUMMARY:/) {
490 for my $off (1 .. 4) {
491 if ($valgrind[$i+$off] =~
492 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
498 if ($errors or $leaks) {
499 if (rename $Valgrind_Log, "$test.valgrind") {
502 warn "$0: Failed to create '$test.valgrind': $!\n";
506 warn "No valgrind output?\n";
508 if (-e $Valgrind_Log) {
510 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
513 if ($type eq 'deparse') {
516 if ($ENV{PERL_3LOG}) {
520 $tpp =~ s:\.t$:.3log:;
521 rename("perl.3log", $tpp) ||
522 die "rename: perl3.log to $tpp: $!\n";
524 if (not defined $failure and $next != $max) {
525 $failure="FAILED--expected $max tests, saw $next";
528 if( !defined $failure # don't mask a test failure
531 $failure = "FAILED--non-zero wait status: $?";
534 if (defined $failure) {
535 print "${te}$failure\n";
537 if ($test =~ /^base/) {
538 die "Failed a basic test ($test) -- cannot continue.\n";
540 ++$failed_tests{$test};
545 if ( $show_elapsed_time ) {
546 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
551 print "${te}ok$elapsed\n";
555 print "${te}skipped\n";
561 if ($::bad_files == 0) {
563 print "All tests successful.\n";
564 # XXX add mention of 'perlbug -ok' ?
567 die "FAILED--no tests were run for some reason.\n";
571 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
572 my $s = $::bad_files == 1 ? "" : "s";
573 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
574 for my $test ( sort keys %failed_tests ) {
578 ### Since not all tests were successful, you may want to run some of
579 ### them individually and examine any diagnostic messages they produce.
580 ### See the INSTALL document's section on "make test".
582 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
583 ### You have a good chance to get more information by running
585 ### in the 't' directory since most (>=80%) of the tests succeeded.
587 if (eval {require Config; import Config; 1}) {
588 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
590 ### You may have to set your dynamic library search path,
591 ### $p, to point to the build directory:
593 if (exists $ENV{$p} && $ENV{$p} ne '') {
595 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
596 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
597 ### export $p=`pwd`:\$$p; cd t; ./perl harness
601 ### setenv $p `pwd`; cd t; ./perl harness
602 ### $p=`pwd`; export $p; cd t; ./perl harness
603 ### export $p=`pwd`; cd t; ./perl harness
607 ### for csh-style shells, like tcsh; or for traditional/modern
608 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
613 my ($user,$sys,$cuser,$csys) = times;
614 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
615 $user,$sys,$cuser,$csys,$tested_files,$totmax);
616 if ($ENV{PERL_VALGRIND}) {
617 my $s = $valgrind == 1 ? '' : 's';
618 print "$valgrind valgrind report$s created.\n", ;
621 exit ($::bad_files != 0);
623 # ex: set ts=8 sts=4 sw=4 noet: