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/
26 # "not absolute" is the the default, as it saves some fakery within TestInit
27 # which can peturb tests, and takes CPU. Working with the upstream author of
28 # any of these, to figure out how to remove them from this list, considered
31 '../cpan/Archive-Extract' => 1,
32 '../cpan/Archive-Tar' => 1,
33 '../cpan/AutoLoader' => 1,
35 '../cpan/Class-ISA' => 1,
37 '../cpan/Devel-PPPort' => 1,
38 '../cpan/Encode' => 1,
39 '../cpan/ExtUtils-Command' => 1,
40 '../cpan/ExtUtils-Constant' => 1,
41 '../cpan/ExtUtils-MakeMaker' => 1,
42 '../cpan/ExtUtils-Manifest' => 1,
43 '../cpan/ExtUtils-ParseXS' => 1,
44 '../cpan/File-Fetch' => 1,
45 '../cpan/IPC-Cmd' => 1,
46 '../cpan/IPC-SysV' => 1,
47 '../cpan/Locale-Codes' => 1,
48 '../cpan/Log-Message' => 1,
49 '../cpan/Math-BigInt' => 1,
50 '../cpan/Math-BigRat' => 1,
51 '../cpan/Math-Complex' => 1,
52 '../cpan/Module-Build' => 1,
53 '../cpan/Module-Load' => 1,
54 '../cpan/Module-Load-Conditional' => 1,
55 '../cpan/Object-Accessor' => 1,
56 '../cpan/Package-Constants' => 1,
57 '../cpan/Parse-CPAN-Meta' => 1,
58 '../cpan/Pod-Simple' => 1,
59 '../cpan/Term-UI' => 1,
60 '../cpan/Test-Simple' => 1,
61 '../cpan/Tie-File' => 1,
62 '../cpan/bignum' => 1,
63 '../cpan/podlators' => 1,
67 ('../cpan/B-Debug' => 1,
68 '../cpan/Compress-Raw-Bzip2' => 1,
69 '../cpan/Compress-Raw-Zlib' => 1,
70 '../cpan/Devel-PPPort' => 1,
71 '../cpan/Getopt-Long' => 1,
72 '../cpan/IO-Compress' => 1,
73 '../cpan/Math-BigInt' => 1,
74 '../cpan/Math-BigRat' => 1,
75 '../cpan/MIME-Base64' => 1,
77 '../cpan/parent' => 1,
78 '../cpan/Parse-CPAN-Meta' => 1,
79 '../cpan/Pod-Simple' => 1,
80 '../cpan/podlators' => 1,
81 '../cpan/Test-Simple' => 1,
82 '../cpan/Tie-RefHash' => 1,
83 '../cpan/Time-HiRes' => 1,
84 '../cpan/Unicode-Collate' => 1,
85 '../cpan/Unicode-Normalize' => 1,
92 # Location to put the Valgrind log.
93 my $Valgrind_Log = 'current.valgrind';
97 # for testing TEST only
98 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
99 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
101 delete $ENV{PERL5LIB};
102 delete $ENV{PERLLIB};
103 delete $ENV{PERL5OPT};
105 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
106 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
107 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
109 # Cheesy version of Getopt::Std. We can't replace it with that, because we
110 # can't rely on require working.
113 foreach my $idx (0..$#ARGV) {
114 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
115 $::benchmark = 1 if $1 eq 'benchmark';
116 $::core = 1 if $1 eq 'core';
117 $::verbose = 1 if $1 eq 'v';
118 $::torture = 1 if $1 eq 'torture';
119 $::with_utf8 = 1 if $1 eq 'utf8';
120 $::with_utf16 = 1 if $1 eq 'utf16';
121 $::taintwarn = 1 if $1 eq 'taintwarn';
122 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
123 if ($1 =~ /^deparse(,.+)?$/) {
125 $::deparse_opts = $1;
131 chdir 't' if -f 't/TEST';
132 if (-f 'TEST' && -f 'harness' && -d '../lib') {
136 die "You need to run \"make test\" first to set things up.\n"
137 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
139 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
140 unless (-x 'perl.third') {
141 unless (-x '../perl.third') {
142 die "You need to run \"make perl.third first.\n";
145 print "Symlinking ../perl.third as perl.third...\n";
146 die "Failed to symlink: $!\n"
147 unless symlink("../perl.third", "perl.third");
148 die "Symlinked but no executable perl.third: $!\n"
149 unless -x 'perl.third';
154 # check leakage for embedders
155 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
157 $ENV{EMXSHELL} = 'sh'; # For OS/2
159 if ($show_elapsed_time) { require Time::HiRes }
170 # Roll your own File::Find!
173 opendir DIR, $dir or die "Trouble opening $dir: $!";
174 foreach my $f (sort { $a cmp $b } readdir DIR) {
177 my $fullpath = "$dir/$f";
180 _find_tests($fullpath);
181 } elsif ($f =~ /\.t$/) {
182 push @ARGV, $fullpath;
188 # Scan the text of the test program to find switches and special options
189 # we might need to apply.
191 my($test, $type) = @_;
193 open(my $script, "<", $test) or die "Can't read $test.\n";
194 my $first_line = <$script>;
196 $first_line =~ tr/\0//d if $::with_utf16;
199 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
203 # not all tests are expected to pass with this option
211 if ($type eq 'deparse') {
212 # Look for #line directives which change the filename
214 $file_opts = $file_opts . ",-f$3$4"
215 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
226 $test =~ /^(.+)\/[^\/]+/;
228 my $testswitch = $dir_to_switch{$dir};
229 if (!defined $testswitch) {
230 if ($test =~ s!^(\.\./(cpan|dist|ext)/[^/]+)/t!t!) {
232 $return_dir = '../../t';
234 $perl = '../../t/perl';
235 $testswitch = "-I../.. -MTestInit=U2T";
238 $testswitch = $testswitch . ',A';
240 if ($temp_no_core{$run_dir}) {
241 $testswitch = $testswitch . ',NC';
245 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
249 my $utf8 = $::with_utf8 ? "-I$lib -Mutf8" : '';
256 return_dir => $return_dir,
257 testswitch => $testswitch,
267 my($options, $type) = @_;
269 my $test = $options->{test};
272 if ($type eq 'deparse') {
273 my $perl = "$options->{perl} $options->{testswitch}";
274 my $lib = $options->{lib};
277 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
278 "-l$::deparse_opts$options->{file} ".
280 "&& $perl $options->{switch} -I$lib $test.dp"
283 elsif ($type eq 'perl') {
284 my $perl = $options->{perl};
285 my $redir = $^O eq 'VMS' ? '2>&1' : '';
287 if ($ENV{PERL_VALGRIND}) {
288 my $valgrind = $ENV{VALGRIND} // 'valgrind';
289 my $vg_opts = $ENV{VG_OPTS}
290 // "--suppressions=perl.supp --leak-check=yes "
291 . "--leak-resolution=high --show-reachable=yes "
292 . "--num-callers=50";
293 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
294 $redir = "3>$Valgrind_Log";
297 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
298 $cmd = $perl . _quote_args($args) . " $test $redir";
307 if ($options->{run_dir}) {
308 my $run_dir = $options->{run_dir};
309 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
318 if ($options->{return_dir}) {
319 my $return_dir = $options->{return_dir};
321 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
328 my ($test, $type) = @_;
330 my $options = _scan_test($test, $type);
331 # $test might have changed if we're in ext/Foo, so don't use it anymore
332 # from now on. Use $options->{test} instead.
334 _before_fork($options);
336 my $cmd = _cmd($options, $type);
338 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
340 _after_fork($options);
342 # Our environment may force us to use UTF-8, but we can't be sure that
343 # anything we're reading from will be generating (well formed) UTF-8
344 # This may not be the best way - possibly we should unset ${^OPEN} up
355 foreach (split(/\s+/,$args)) {
356 # In VMS protect with doublequotes because otherwise
357 # DCL will lowercase -- unless already doublequoted.
358 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
359 $argstring = $argstring . ' ' . $_;
365 return unless defined $_[0];
366 return map {$_, 1} split /\s+/, $_[0];
369 sub _tests_from_manifest {
370 my ($extensions, $known_extensions) = @_;
372 my %extensions = _populate_hash($extensions);
373 my %known_extensions = _populate_hash($known_extensions);
375 foreach (keys %known_extensions) {
376 $skip{$_} = 1 unless $extensions{$_};
380 my $mani = '../MANIFEST';
381 if (open(MANI, $mani)) {
383 if (m!^((?:cpan|dist|ext)/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
386 if (!$::core || $t =~ m!^lib/[a-z]!) {
387 if (defined $extension) {
388 $extension =~ s!/t$!!;
389 # XXX Do I want to warn that I'm skipping these?
390 next if $skip{$extension};
391 my $flat_extension = $extension;
392 $flat_extension =~ s!-!/!g;
393 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
396 push @results, $path;
397 $::path_to_name{$path} = $t;
403 warn "$0: cannot open $mani: $!\n";
409 # base first, as TEST bails out if that can't run
410 # then comp, to validate that require works
411 # then run, to validate that -M works
412 # then we know we can -MTestInit for everything else, making life simpler
413 foreach my $dir (qw(base comp run cmd io re op uni mro)) {
416 _find_tests("lib") unless $::core;
417 # Config.pm may be broken for make minitest. And this is only a refinement
418 # for skipping tests on non-default builds, so it is allowed to fail.
419 # What we want to to is make a list of extensions which we did not build.
420 my $configsh = '../config.sh';
421 my ($extensions, $known_extensions);
423 open FH, $configsh or die "Can't open $configsh: $!";
425 if (/^extensions=['"](.*)['"]$/) {
428 elsif (/^known_extensions=['"](.*)['"]$/) {
429 $known_extensions = $1;
432 if (!defined $known_extensions) {
433 warn "No known_extensions line found in $configsh";
435 if (!defined $extensions) {
436 warn "No extensions line found in $configsh";
439 # The "complex" constructions of list return from a subroutine, and push of
440 # a list, might fail if perl is really hosed, but they aren't needed for
441 # make minitest, and the building of extensions will likely also fail if
442 # something is that badly wrong.
443 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
446 _find_tests('porting');
447 _find_tests('japh') if $::torture;
448 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
453 _testprogs('deparse', '', @ARGV);
455 elsif ($::with_utf16) {
458 print STDERR "# ENDIAN $e BOM $b\n";
461 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
462 my $f = $e ? "v" : "n";
466 if (open(U, ">$u")) {
467 print U pack("$f", 0xFEFF) if $b;
469 print U pack("$f*", unpack("C*", $_));
476 _testprogs('perl', '', @UARGV);
482 _testprogs('perl', '', @ARGV);
486 my ($type, $args, @tests) = @_;
488 print <<'EOT' if ($type eq 'deparse');
489 ------------------------------------------------------------------------------
491 ------------------------------------------------------------------------------
496 foreach my $t (@tests) {
497 unless (exists $::path_to_name{$t}) {
499 $::path_to_name{$t} = $tname;
503 foreach (@::path_to_name{@tests}) {
506 $maxlen = $len if $len > $maxlen;
508 # + 3 : we want three dots between the test name and the "ok"
509 my $dotdotdot = $maxlen + 3 ;
511 my $total_files = @tests;
513 my $tested_files = 0;
517 while (my $test = shift @tests) {
518 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
523 if ($type eq 'deparse') {
524 if ($test eq "comp/redef.t") {
525 # Redefinition happens at compile time
528 elsif ($test =~ m{lib/Switch/t/}) {
529 # B::Deparse doesn't support source filtering
533 my $te = $::path_to_name{$test} . '.'
534 x ($dotdotdot - length($::path_to_name{$test}));
536 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
541 my $results = _run_test($test, $type);
547 my $trailing_leader = 0;
551 next if /^\s*$/; # skip blank lines
552 if (/^1..$/ && ($^O eq 'VMS')) {
553 # VMS pipe bug inserts blank lines.
555 if ($l2 =~ /^\s*$/) {
564 if ($trailing_leader) {
565 # shouldn't be anything following a postfix 1..n
566 $failure = 'FAILED--extra output after trailing 1..n';
569 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
571 $failure = 'FAILED--seen duplicate leader';
575 %todo = map { $_ => 1 } split / /, $3 if $3;
576 $totmax = $totmax + $max;
577 $tested_files = $tested_files + 1;
579 # 1..n appears at end of file
580 $trailing_leader = 1;
582 $failure = "FAILED--expected $max tests, saw $next";
592 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
593 unless ($seen_leader) {
600 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
601 $num = $next unless $num;
605 # SKIP is essentially the same as TODO for t/TEST
606 # this still conforms to TAP:
607 # http://search.cpan.org/dist/TAP/TAP.pm
608 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
609 $istodo = 1 if $todo{$num};
611 if( $not && !$istodo ) {
612 $failure = "FAILED at test $num";
617 $failure ="FAILED--expected test $next, saw test $num";
621 elsif (/^Bail out!\s*(.*)/i) { # magic words
622 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
625 # module tests are allowed extra output,
626 # because Test::Harness allows it
627 next if $test =~ /^\W*(cpan|dist|ext|lib)\b/;
628 $failure = "FAILED--unexpected output at test $next";
636 if (not defined $failure) {
637 $failure = 'FAILED--no leader found' unless $seen_leader;
640 if ($ENV{PERL_VALGRIND}) {
642 if (-e $Valgrind_Log) {
643 if (open(V, $Valgrind_Log)) {
647 warn "$0: Failed to open '$Valgrind_Log': $!\n";
650 if ($ENV{VG_OPTS} =~ /cachegrind/) {
651 if (rename $Valgrind_Log, "$test.valgrind") {
652 $valgrind = $valgrind + 1;
654 warn "$0: Failed to create '$test.valgrind': $!\n";
660 for my $i (0..$#valgrind) {
661 local $_ = $valgrind[$i];
662 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
663 $errors = $errors + $1; # there may be multiple error summaries
664 } elsif (/^==\d+== LEAK SUMMARY:/) {
665 for my $off (1 .. 4) {
666 if ($valgrind[$i+$off] =~
667 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
668 $leaks = $leaks + $1;
673 if ($errors or $leaks) {
674 if (rename $Valgrind_Log, "$test.valgrind") {
675 $valgrind = $valgrind + 1;
677 warn "$0: Failed to create '$test.valgrind': $!\n";
681 warn "No valgrind output?\n";
683 if (-e $Valgrind_Log) {
685 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
688 if ($type eq 'deparse') {
691 if ($ENV{PERL_3LOG}) {
695 $tpp =~ s:\.t$:.3log:;
696 rename("perl.3log", $tpp) ||
697 die "rename: perl3.log to $tpp: $!\n";
699 if (not defined $failure and $next != $max) {
700 $failure="FAILED--expected $max tests, saw $next";
703 if( !defined $failure # don't mask a test failure
706 $failure = "FAILED--non-zero wait status: $?";
709 if (defined $failure) {
710 print "${te}$failure\n";
711 $::bad_files = $::bad_files + 1;
712 if ($test =~ /^base/) {
713 die "Failed a basic test ($test) -- cannot continue.\n";
715 $failed_tests{$test} = 1;
720 if ( $show_elapsed_time ) {
721 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
726 print "${te}ok$elapsed\n";
727 $good_files = $good_files + 1;
730 print "${te}skipped\n";
731 $tested_files = $tested_files - 1;
736 if ($::bad_files == 0) {
738 print "All tests successful.\n";
739 # XXX add mention of 'perlbug -ok' ?
742 die "FAILED--no tests were run for some reason.\n";
746 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
747 my $s = $::bad_files == 1 ? "" : "s";
748 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
749 for my $test ( sort keys %failed_tests ) {
753 ### Since not all tests were successful, you may want to run some of
754 ### them individually and examine any diagnostic messages they produce.
755 ### See the INSTALL document's section on "make test".
757 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
758 ### You have a good chance to get more information by running
760 ### in the 't' directory since most (>=80%) of the tests succeeded.
762 if (eval {require Config; import Config; 1}) {
763 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
765 ### You may have to set your dynamic library search path,
766 ### $p, to point to the build directory:
768 if (exists $ENV{$p} && $ENV{$p} ne '') {
770 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
771 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
772 ### export $p=`pwd`:\$$p; cd t; ./perl harness
776 ### setenv $p `pwd`; cd t; ./perl harness
777 ### $p=`pwd`; export $p; cd t; ./perl harness
778 ### export $p=`pwd`; cd t; ./perl harness
782 ### for csh-style shells, like tcsh; or for traditional/modern
783 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
788 my ($user,$sys,$cuser,$csys) = times;
789 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
790 $user,$sys,$cuser,$csys,$tested_files,$totmax);
791 if ($ENV{PERL_VALGRIND}) {
792 my $s = $valgrind == 1 ? '' : 's';
793 print "$valgrind valgrind report$s created.\n", ;
796 exit ($::bad_files != 0);
798 # ex: set ts=8 sts=4 sw=4 noet: