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/Math-BigInt' => 1,
34 '../ext/Math-BigRat' => 1,
35 '../ext/MIME-Base64' => 1,
37 '../ext/Pod-Simple' => 1,
38 '../ext/Parse-CPAN-Meta' => 1,
39 '../ext/Tie-RefHash' => 1,
40 '../ext/Time-HiRes' => 1,
41 '../ext/Unicode-Normalize' => 1,
42 '../ext/podlators' => 1,
49 # Location to put the Valgrind log.
50 my $Valgrind_Log = 'current.valgrind';
54 # for testing TEST only
55 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
56 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
58 delete $ENV{PERL5LIB};
60 delete $ENV{PERL5OPT};
62 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
63 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
64 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
66 # Cheesy version of Getopt::Std. We can't replace it with that, because we
67 # can't rely on require working.
70 foreach my $idx (0..$#ARGV) {
71 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
72 $::benchmark = 1 if $1 eq 'benchmark';
73 $::core = 1 if $1 eq 'core';
74 $::verbose = 1 if $1 eq 'v';
75 $::torture = 1 if $1 eq 'torture';
76 $::with_utf8 = 1 if $1 eq 'utf8';
77 $::with_utf16 = 1 if $1 eq 'utf16';
78 $::taintwarn = 1 if $1 eq 'taintwarn';
79 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
80 if ($1 =~ /^deparse(,.+)?$/) {
88 chdir 't' if -f 't/TEST';
89 if (-f 'TEST' && -f 'harness' && -d '../lib') {
93 die "You need to run \"make test\" first to set things up.\n"
94 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
96 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
97 unless (-x 'perl.third') {
98 unless (-x '../perl.third') {
99 die "You need to run \"make perl.third first.\n";
102 print "Symlinking ../perl.third as perl.third...\n";
103 die "Failed to symlink: $!\n"
104 unless symlink("../perl.third", "perl.third");
105 die "Symlinked but no executable perl.third: $!\n"
106 unless -x 'perl.third';
111 # check leakage for embedders
112 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
114 $ENV{EMXSHELL} = 'sh'; # For OS/2
116 if ($show_elapsed_time) { require Time::HiRes }
127 # Roll your own File::Find!
130 opendir DIR, $dir or die "Trouble opening $dir: $!";
131 foreach my $f (sort { $a cmp $b } readdir DIR) {
134 my $fullpath = "$dir/$f";
137 _find_tests($fullpath);
138 } elsif ($f =~ /\.t$/) {
139 push @ARGV, $fullpath;
145 # Scan the text of the test program to find switches and special options
146 # we might need to apply.
148 my($test, $type) = @_;
150 open(my $script, "<", $test) or die "Can't read $test.\n";
151 my $first_line = <$script>;
153 $first_line =~ tr/\0//d if $::with_utf16;
156 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
160 # not all tests are expected to pass with this option
168 if ($type eq 'deparse') {
169 # Look for #line directives which change the filename
171 $file_opts .= ",-f$3$4"
172 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
183 $test =~ /^(.+)\/[^\/]+/;
185 my $testswitch = $dir_to_switch{$dir};
186 if (!defined $testswitch) {
187 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
189 $return_dir = '../../t';
191 $perl = '../../t/perl';
192 $testswitch = "-I../.. -MTestInit=U2T,A";
193 if ($temp_no_core{$run_dir}) {
194 $testswitch = $testswitch . ',NC';
197 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
201 my $utf8 = $::with_utf8 ? "-I$lib -Mutf8" : '';
208 return_dir => $return_dir,
209 testswitch => $testswitch,
219 my($options, $type) = @_;
221 my $test = $options->{test};
224 if ($type eq 'deparse') {
225 my $perl = "$options->{perl} $options->{testswitch}";
226 my $lib = $options->{lib};
229 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
230 "-l$::deparse_opts$options->{file} ".
232 "&& $perl $options->{switch} -I$lib $test.dp"
235 elsif ($type eq 'perl') {
236 my $perl = $options->{perl};
237 my $redir = $^O eq 'VMS' ? '2>&1' : '';
239 if ($ENV{PERL_VALGRIND}) {
240 my $valgrind = $ENV{VALGRIND} // 'valgrind';
241 my $vg_opts = $ENV{VG_OPTS}
242 // "--suppressions=perl.supp --leak-check=yes "
243 . "--leak-resolution=high --show-reachable=yes "
244 . "--num-callers=50";
245 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
246 $redir = "3>$Valgrind_Log";
249 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
250 $cmd = $perl . _quote_args($args) . " $test $redir";
259 if ($options->{run_dir}) {
260 my $run_dir = $options->{run_dir};
261 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
270 if ($options->{return_dir}) {
271 my $return_dir = $options->{return_dir};
273 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
280 my ($test, $type) = @_;
282 my $options = _scan_test($test, $type);
283 # $test might have changed if we're in ext/Foo, so don't use it anymore
284 # from now on. Use $options->{test} instead.
286 _before_fork($options);
288 my $cmd = _cmd($options, $type);
290 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
292 _after_fork($options);
294 # Our environment may force us to use UTF-8, but we can't be sure that
295 # anything we're reading from will be generating (well formed) UTF-8
296 # This may not be the best way - possibly we should unset ${^OPEN} up
307 foreach (split(/\s+/,$args)) {
308 # In VMS protect with doublequotes because otherwise
309 # DCL will lowercase -- unless already doublequoted.
310 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
311 $argstring .= ' ' . $_;
317 return unless defined $_[0];
318 return map {$_, 1} split /\s+/, $_[0];
321 sub _tests_from_manifest {
322 my ($extensions, $known_extensions) = @_;
324 my %extensions = _populate_hash($extensions);
325 my %known_extensions = _populate_hash($known_extensions);
327 foreach (keys %known_extensions) {
328 $skip{$_}++ unless $extensions{$_};
332 my $mani = '../MANIFEST';
333 if (open(MANI, $mani)) {
335 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
338 if (!$::core || $t =~ m!^lib/[a-z]!) {
339 if (defined $extension) {
340 $extension =~ s!/t$!!;
341 # XXX Do I want to warn that I'm skipping these?
342 next if $skip{$extension};
343 my $flat_extension = $extension;
344 $flat_extension =~ s!-!/!g;
345 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
348 push @results, $path;
349 $::path_to_name{$path} = $t;
355 warn "$0: cannot open $mani: $!\n";
361 # base first, as TEST bails out if that can't run
362 # then comp, to validate that require works
363 # then run, to validate that -M works
364 # then we know we can -MTestInit for everything else, making life simpler
365 foreach my $dir (qw(base comp run cmd io re op uni mro)) {
368 _find_tests("lib") unless $::core;
369 # Config.pm may be broken for make minitest. And this is only a refinement
370 # for skipping tests on non-default builds, so it is allowed to fail.
371 # What we want to to is make a list of extensions which we did not build.
372 my $configsh = '../config.sh';
373 my ($extensions, $known_extensions);
375 open FH, $configsh or die "Can't open $configsh: $!";
377 if (/^extensions=['"](.*)['"]$/) {
380 elsif (/^known_extensions=['"](.*)['"]$/) {
381 $known_extensions = $1;
384 if (!defined $known_extensions) {
385 warn "No known_extensions line found in $configsh";
387 if (!defined $extensions) {
388 warn "No extensions line found in $configsh";
391 # The "complex" constructions of list return from a subroutine, and push of
392 # a list, might fail if perl is really hosed, but they aren't needed for
393 # make minitest, and the building of extensions will likely also fail if
394 # something is that badly wrong.
395 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
399 _find_tests('porting');
400 _find_tests('japh') if $::torture;
401 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
406 _testprogs('deparse', '', @ARGV);
408 elsif ($::with_utf16) {
411 print STDERR "# ENDIAN $e BOM $b\n";
414 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
415 my $f = $e ? "v" : "n";
419 if (open(U, ">$u")) {
420 print U pack("$f", 0xFEFF) if $b;
422 print U pack("$f*", unpack("C*", $_));
429 _testprogs('perl', '', @UARGV);
435 _testprogs('perl', '', @ARGV);
439 my ($type, $args, @tests) = @_;
441 print <<'EOT' if ($type eq 'deparse');
442 ------------------------------------------------------------------------------
444 ------------------------------------------------------------------------------
449 foreach my $t (@tests) {
450 unless (exists $::path_to_name{$t}) {
452 $::path_to_name{$t} = $tname;
456 foreach (@::path_to_name{@tests}) {
459 $maxlen = $len if $len > $maxlen;
461 # + 3 : we want three dots between the test name and the "ok"
462 my $dotdotdot = $maxlen + 3 ;
464 my $total_files = @tests;
466 my $tested_files = 0;
470 while (my $test = shift @tests) {
471 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
476 if ($type eq 'deparse') {
477 if ($test eq "comp/redef.t") {
478 # Redefinition happens at compile time
481 elsif ($test =~ m{lib/Switch/t/}) {
482 # B::Deparse doesn't support source filtering
486 my $te = $::path_to_name{$test} . '.'
487 x ($dotdotdot - length($::path_to_name{$test}));
489 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
494 my $results = _run_test($test, $type);
500 my $trailing_leader = 0;
504 next if /^\s*$/; # skip blank lines
505 if (/^1..$/ && ($^O eq 'VMS')) {
506 # VMS pipe bug inserts blank lines.
508 if ($l2 =~ /^\s*$/) {
517 if ($trailing_leader) {
518 # shouldn't be anything following a postfix 1..n
519 $failure = 'FAILED--extra output after trailing 1..n';
522 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
524 $failure = 'FAILED--seen duplicate leader';
528 %todo = map { $_ => 1 } split / /, $3 if $3;
532 # 1..n appears at end of file
533 $trailing_leader = 1;
535 $failure = "FAILED--expected $max tests, saw $next";
545 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
546 unless ($seen_leader) {
553 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
554 $num = $next unless $num;
558 # SKIP is essentially the same as TODO for t/TEST
559 # this still conforms to TAP:
560 # http://search.cpan.org/dist/TAP/TAP.pod
561 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
562 $istodo = 1 if $todo{$num};
564 if( $not && !$istodo ) {
565 $failure = "FAILED at test $num";
570 $failure ="FAILED--expected test $next, saw test $num";
574 elsif (/^Bail out!\s*(.*)/i) { # magic words
575 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
578 # module tests are allowed extra output,
579 # because Test::Harness allows it
580 next if $test =~ /^\W*(ext|lib)\b/;
581 $failure = "FAILED--unexpected output at test $next";
589 if (not defined $failure) {
590 $failure = 'FAILED--no leader found' unless $seen_leader;
593 if ($ENV{PERL_VALGRIND}) {
595 if (-e $Valgrind_Log) {
596 if (open(V, $Valgrind_Log)) {
600 warn "$0: Failed to open '$Valgrind_Log': $!\n";
603 if ($ENV{VG_OPTS} =~ /cachegrind/) {
604 if (rename $Valgrind_Log, "$test.valgrind") {
607 warn "$0: Failed to create '$test.valgrind': $!\n";
613 for my $i (0..$#valgrind) {
614 local $_ = $valgrind[$i];
615 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
616 $errors += $1; # there may be multiple error summaries
617 } elsif (/^==\d+== LEAK SUMMARY:/) {
618 for my $off (1 .. 4) {
619 if ($valgrind[$i+$off] =~
620 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
626 if ($errors or $leaks) {
627 if (rename $Valgrind_Log, "$test.valgrind") {
630 warn "$0: Failed to create '$test.valgrind': $!\n";
634 warn "No valgrind output?\n";
636 if (-e $Valgrind_Log) {
638 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
641 if ($type eq 'deparse') {
644 if ($ENV{PERL_3LOG}) {
648 $tpp =~ s:\.t$:.3log:;
649 rename("perl.3log", $tpp) ||
650 die "rename: perl3.log to $tpp: $!\n";
652 if (not defined $failure and $next != $max) {
653 $failure="FAILED--expected $max tests, saw $next";
656 if( !defined $failure # don't mask a test failure
659 $failure = "FAILED--non-zero wait status: $?";
662 if (defined $failure) {
663 print "${te}$failure\n";
665 if ($test =~ /^base/) {
666 die "Failed a basic test ($test) -- cannot continue.\n";
668 ++$failed_tests{$test};
673 if ( $show_elapsed_time ) {
674 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
679 print "${te}ok$elapsed\n";
683 print "${te}skipped\n";
689 if ($::bad_files == 0) {
691 print "All tests successful.\n";
692 # XXX add mention of 'perlbug -ok' ?
695 die "FAILED--no tests were run for some reason.\n";
699 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
700 my $s = $::bad_files == 1 ? "" : "s";
701 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
702 for my $test ( sort keys %failed_tests ) {
706 ### Since not all tests were successful, you may want to run some of
707 ### them individually and examine any diagnostic messages they produce.
708 ### See the INSTALL document's section on "make test".
710 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
711 ### You have a good chance to get more information by running
713 ### in the 't' directory since most (>=80%) of the tests succeeded.
715 if (eval {require Config; import Config; 1}) {
716 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
718 ### You may have to set your dynamic library search path,
719 ### $p, to point to the build directory:
721 if (exists $ENV{$p} && $ENV{$p} ne '') {
723 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
724 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
725 ### export $p=`pwd`:\$$p; cd t; ./perl harness
729 ### setenv $p `pwd`; cd t; ./perl harness
730 ### $p=`pwd`; export $p; cd t; ./perl harness
731 ### export $p=`pwd`; cd t; ./perl harness
735 ### for csh-style shells, like tcsh; or for traditional/modern
736 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
741 my ($user,$sys,$cuser,$csys) = times;
742 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
743 $user,$sys,$cuser,$csys,$tested_files,$totmax);
744 if ($ENV{PERL_VALGRIND}) {
745 my $s = $valgrind == 1 ? '' : 's';
746 print "$valgrind valgrind report$s created.\n", ;
749 exit ($::bad_files != 0);
751 # ex: set ts=8 sts=4 sw=4 noet: