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 # t/TEST and t/harness need to share code. The logical way to do this would be
9 # to have the common code in a file both require or use. However, t/TEST needs
10 # to still work, to generate test results, even if require isn't working, so
11 # we cannot do that. t/harness has no such restriction, so it is quite
12 # acceptable to have it require t/TEST.
14 # In which case, we need to stop t/TEST actually running tests, as all
15 # t/harness needs are its subroutines.
18 # directories with special sets of test switches
23 '../ext/File-Glob/t' => '-I.. -MTestInit', # FIXME - tests assume t/
27 ('../ext/B-Debug' => 1,
28 '../ext/Compress-Raw-Bzip2' => 1,
29 '../ext/Compress-Raw-Zlib' => 1,
30 '../ext/Devel-PPPort' => 1,
31 '../ext/IO-Compress' => 1,
32 '../ext/IPC-SysV' => 1,
33 '../ext/MIME-Base64' => 1,
34 '../ext/Parse-CPAN-Meta' => 1,
35 '../ext/Time-HiRes' => 1,
36 '../ext/Unicode-Normalize' => 1,
43 # Location to put the Valgrind log.
44 my $Valgrind_Log = 'current.valgrind';
48 # for testing TEST only
49 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
50 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
52 delete $ENV{PERL5LIB};
54 delete $ENV{PERL5OPT};
56 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
57 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
58 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
60 # Cheesy version of Getopt::Std. We can't replace it with that, because we
61 # can't rely on require working.
64 foreach my $idx (0..$#ARGV) {
65 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
66 $::benchmark = 1 if $1 eq 'benchmark';
67 $::core = 1 if $1 eq 'core';
68 $::verbose = 1 if $1 eq 'v';
69 $::torture = 1 if $1 eq 'torture';
70 $::with_utf8 = 1 if $1 eq 'utf8';
71 $::with_utf16 = 1 if $1 eq 'utf16';
72 $::taintwarn = 1 if $1 eq 'taintwarn';
73 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
74 if ($1 =~ /^deparse(,.+)?$/) {
82 chdir 't' if -f 't/TEST';
83 if (-f 'TEST' && -f 'harness' && -d '../lib') {
87 die "You need to run \"make test\" first to set things up.\n"
88 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
90 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
91 unless (-x 'perl.third') {
92 unless (-x '../perl.third') {
93 die "You need to run \"make perl.third first.\n";
96 print "Symlinking ../perl.third as perl.third...\n";
97 die "Failed to symlink: $!\n"
98 unless symlink("../perl.third", "perl.third");
99 die "Symlinked but no executable perl.third: $!\n"
100 unless -x 'perl.third';
105 # check leakage for embedders
106 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
108 $ENV{EMXSHELL} = 'sh'; # For OS/2
110 if ($show_elapsed_time) { require Time::HiRes }
121 # Roll your own File::Find!
124 opendir DIR, $dir or die "Trouble opening $dir: $!";
125 foreach my $f (sort { $a cmp $b } readdir DIR) {
128 my $fullpath = "$dir/$f";
131 _find_tests($fullpath);
132 } elsif ($f =~ /\.t$/) {
133 push @ARGV, $fullpath;
139 # Scan the text of the test program to find switches and special options
140 # we might need to apply.
142 my($test, $type) = @_;
144 open(my $script, "<", $test) or die "Can't read $test.\n";
145 my $first_line = <$script>;
147 $first_line =~ tr/\0//d if $::with_utf16;
150 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
154 # not all tests are expected to pass with this option
162 if ($type eq 'deparse') {
163 # Look for #line directives which change the filename
165 $file_opts .= ",-f$3$4"
166 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
177 $test =~ /^(.+)\/[^\/]+/;
179 my $testswitch = $dir_to_switch{$dir};
180 if (!defined $testswitch) {
181 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
183 $return_dir = '../../t';
185 $perl = '../../t/perl';
186 $testswitch = "-I../.. -MTestInit=U2T,A";
187 if ($temp_no_core{$run_dir}) {
188 $testswitch = $testswitch . ',NC';
191 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
195 my $utf8 = $::with_utf8 ? "-I$lib -Mutf8" : '';
202 return_dir => $return_dir,
203 testswitch => $testswitch,
213 my($options, $type) = @_;
215 my $test = $options->{test};
218 if ($type eq 'deparse') {
219 my $perl = "$options->{perl} $options->{testswitch}";
220 my $lib = $options->{lib};
223 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
224 "-l$::deparse_opts$options->{file} ".
226 "&& $perl $options->{switch} -I$lib $test.dp"
229 elsif ($type eq 'perl') {
230 my $perl = $options->{perl};
231 my $redir = $^O eq 'VMS' ? '2>&1' : '';
233 if ($ENV{PERL_VALGRIND}) {
234 my $valgrind = $ENV{VALGRIND} // 'valgrind';
235 my $vg_opts = $ENV{VG_OPTS}
236 // "--suppressions=perl.supp --leak-check=yes "
237 . "--leak-resolution=high --show-reachable=yes "
238 . "--num-callers=50";
239 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
240 $redir = "3>$Valgrind_Log";
243 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
244 $cmd = $perl . _quote_args($args) . " $test $redir";
253 if ($options->{run_dir}) {
254 my $run_dir = $options->{run_dir};
255 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
264 if ($options->{return_dir}) {
265 my $return_dir = $options->{return_dir};
267 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
274 my ($test, $type) = @_;
276 my $options = _scan_test($test, $type);
277 # $test might have changed if we're in ext/Foo, so don't use it anymore
278 # from now on. Use $options->{test} instead.
280 _before_fork($options);
282 my $cmd = _cmd($options, $type);
284 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
286 _after_fork($options);
288 # Our environment may force us to use UTF-8, but we can't be sure that
289 # anything we're reading from will be generating (well formed) UTF-8
290 # This may not be the best way - possibly we should unset ${^OPEN} up
301 foreach (split(/\s+/,$args)) {
302 # In VMS protect with doublequotes because otherwise
303 # DCL will lowercase -- unless already doublequoted.
304 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
305 $argstring .= ' ' . $_;
311 return unless defined $_[0];
312 return map {$_, 1} split /\s+/, $_[0];
315 sub _tests_from_manifest {
316 my ($extensions, $known_extensions) = @_;
318 my %extensions = _populate_hash($extensions);
319 my %known_extensions = _populate_hash($known_extensions);
321 foreach (keys %known_extensions) {
322 $skip{$_}++ unless $extensions{$_};
326 my $mani = '../MANIFEST';
327 if (open(MANI, $mani)) {
329 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
332 if (!$::core || $t =~ m!^lib/[a-z]!) {
333 if (defined $extension) {
334 $extension =~ s!/t$!!;
335 # XXX Do I want to warn that I'm skipping these?
336 next if $skip{$extension};
337 my $flat_extension = $extension;
338 $flat_extension =~ s!-!/!g;
339 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
342 push @results, $path;
343 $::path_to_name{$path} = $t;
349 warn "$0: cannot open $mani: $!\n";
355 # base first, as TEST bails out if that can't run
356 # then comp, to validate that require works
357 # then run, to validate that -M works
358 # then we know we can -MTestInit for everything else, making life simpler
359 foreach my $dir (qw(base comp run cmd io re op uni mro)) {
362 _find_tests("lib") unless $::core;
363 # Config.pm may be broken for make minitest. And this is only a refinement
364 # for skipping tests on non-default builds, so it is allowed to fail.
365 # What we want to to is make a list of extensions which we did not build.
366 my $configsh = '../config.sh';
367 my ($extensions, $known_extensions);
369 open FH, $configsh or die "Can't open $configsh: $!";
371 if (/^extensions=['"](.*)['"]$/) {
374 elsif (/^known_extensions=['"](.*)['"]$/) {
375 $known_extensions = $1;
378 if (!defined $known_extensions) {
379 warn "No known_extensions line found in $configsh";
381 if (!defined $extensions) {
382 warn "No extensions line found in $configsh";
385 # The "complex" constructions of list return from a subroutine, and push of
386 # a list, might fail if perl is really hosed, but they aren't needed for
387 # make minitest, and the building of extensions will likely also fail if
388 # something is that badly wrong.
389 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
393 _find_tests('porting');
394 _find_tests('japh') if $::torture;
395 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
400 _testprogs('deparse', '', @ARGV);
402 elsif ($::with_utf16) {
405 print STDERR "# ENDIAN $e BOM $b\n";
408 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
409 my $f = $e ? "v" : "n";
413 if (open(U, ">$u")) {
414 print U pack("$f", 0xFEFF) if $b;
416 print U pack("$f*", unpack("C*", $_));
423 _testprogs('perl', '', @UARGV);
429 _testprogs('perl', '', @ARGV);
433 my ($type, $args, @tests) = @_;
435 print <<'EOT' if ($type eq 'deparse');
436 ------------------------------------------------------------------------------
438 ------------------------------------------------------------------------------
443 foreach my $t (@tests) {
444 unless (exists $::path_to_name{$t}) {
446 $::path_to_name{$t} = $tname;
450 foreach (@::path_to_name{@tests}) {
453 $maxlen = $len if $len > $maxlen;
455 # + 3 : we want three dots between the test name and the "ok"
456 my $dotdotdot = $maxlen + 3 ;
458 my $total_files = @tests;
460 my $tested_files = 0;
464 while (my $test = shift @tests) {
465 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
470 if ($type eq 'deparse') {
471 if ($test eq "comp/redef.t") {
472 # Redefinition happens at compile time
475 elsif ($test =~ m{lib/Switch/t/}) {
476 # B::Deparse doesn't support source filtering
480 my $te = $::path_to_name{$test} . '.'
481 x ($dotdotdot - length($::path_to_name{$test}));
483 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
488 my $results = _run_test($test, $type);
494 my $trailing_leader = 0;
498 next if /^\s*$/; # skip blank lines
499 if (/^1..$/ && ($^O eq 'VMS')) {
500 # VMS pipe bug inserts blank lines.
502 if ($l2 =~ /^\s*$/) {
511 if ($trailing_leader) {
512 # shouldn't be anything following a postfix 1..n
513 $failure = 'FAILED--extra output after trailing 1..n';
516 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
518 $failure = 'FAILED--seen duplicate leader';
522 %todo = map { $_ => 1 } split / /, $3 if $3;
526 # 1..n appears at end of file
527 $trailing_leader = 1;
529 $failure = "FAILED--expected $max tests, saw $next";
539 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
540 unless ($seen_leader) {
547 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
548 $num = $next unless $num;
552 # SKIP is essentially the same as TODO for t/TEST
553 # this still conforms to TAP:
554 # http://search.cpan.org/dist/TAP/TAP.pod
555 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
556 $istodo = 1 if $todo{$num};
558 if( $not && !$istodo ) {
559 $failure = "FAILED at test $num";
564 $failure ="FAILED--expected test $next, saw test $num";
568 elsif (/^Bail out!\s*(.*)/i) { # magic words
569 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
572 # module tests are allowed extra output,
573 # because Test::Harness allows it
574 next if $test =~ /^\W*(ext|lib)\b/;
575 $failure = "FAILED--unexpected output at test $next";
583 if (not defined $failure) {
584 $failure = 'FAILED--no leader found' unless $seen_leader;
587 if ($ENV{PERL_VALGRIND}) {
589 if (-e $Valgrind_Log) {
590 if (open(V, $Valgrind_Log)) {
594 warn "$0: Failed to open '$Valgrind_Log': $!\n";
597 if ($ENV{VG_OPTS} =~ /cachegrind/) {
598 if (rename $Valgrind_Log, "$test.valgrind") {
601 warn "$0: Failed to create '$test.valgrind': $!\n";
607 for my $i (0..$#valgrind) {
608 local $_ = $valgrind[$i];
609 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
610 $errors += $1; # there may be multiple error summaries
611 } elsif (/^==\d+== LEAK SUMMARY:/) {
612 for my $off (1 .. 4) {
613 if ($valgrind[$i+$off] =~
614 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
620 if ($errors or $leaks) {
621 if (rename $Valgrind_Log, "$test.valgrind") {
624 warn "$0: Failed to create '$test.valgrind': $!\n";
628 warn "No valgrind output?\n";
630 if (-e $Valgrind_Log) {
632 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
635 if ($type eq 'deparse') {
638 if ($ENV{PERL_3LOG}) {
642 $tpp =~ s:\.t$:.3log:;
643 rename("perl.3log", $tpp) ||
644 die "rename: perl3.log to $tpp: $!\n";
646 if (not defined $failure and $next != $max) {
647 $failure="FAILED--expected $max tests, saw $next";
650 if( !defined $failure # don't mask a test failure
653 $failure = "FAILED--non-zero wait status: $?";
656 if (defined $failure) {
657 print "${te}$failure\n";
659 if ($test =~ /^base/) {
660 die "Failed a basic test ($test) -- cannot continue.\n";
662 ++$failed_tests{$test};
667 if ( $show_elapsed_time ) {
668 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
673 print "${te}ok$elapsed\n";
677 print "${te}skipped\n";
683 if ($::bad_files == 0) {
685 print "All tests successful.\n";
686 # XXX add mention of 'perlbug -ok' ?
689 die "FAILED--no tests were run for some reason.\n";
693 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
694 my $s = $::bad_files == 1 ? "" : "s";
695 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
696 for my $test ( sort keys %failed_tests ) {
700 ### Since not all tests were successful, you may want to run some of
701 ### them individually and examine any diagnostic messages they produce.
702 ### See the INSTALL document's section on "make test".
704 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
705 ### You have a good chance to get more information by running
707 ### in the 't' directory since most (>=80%) of the tests succeeded.
709 if (eval {require Config; import Config; 1}) {
710 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
712 ### You may have to set your dynamic library search path,
713 ### $p, to point to the build directory:
715 if (exists $ENV{$p} && $ENV{$p} ne '') {
717 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
718 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
719 ### export $p=`pwd`:\$$p; cd t; ./perl harness
723 ### setenv $p `pwd`; cd t; ./perl harness
724 ### $p=`pwd`; export $p; cd t; ./perl harness
725 ### export $p=`pwd`; cd t; ./perl harness
729 ### for csh-style shells, like tcsh; or for traditional/modern
730 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
735 my ($user,$sys,$cuser,$csys) = times;
736 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
737 $user,$sys,$cuser,$csys,$tested_files,$totmax);
738 if ($ENV{PERL_VALGRIND}) {
739 my $s = $valgrind == 1 ? '' : 's';
740 print "$valgrind valgrind report$s created.\n", ;
743 exit ($::bad_files != 0);
745 # ex: set ts=8 sts=4 sw=4 noet: