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 # I think in the end I'd like "not absolute" to be the default", as it saves
27 # some fakery within TestInit which can peturb tests, and takes CPU.
29 ('../ext/Pod-Parser' => 1,
33 ('../cpan/B-Debug' => 1,
34 '../cpan/Compress-Raw-Bzip2' => 1,
35 '../cpan/Compress-Raw-Zlib' => 1,
36 '../cpan/Devel-PPPort' => 1,
37 '../cpan/Getopt-Long' => 1,
38 '../cpan/IO-Compress' => 1,
39 '../cpan/Math-BigInt' => 1,
40 '../ext/Math-BigRat' => 1,
41 '../ext/MIME-Base64' => 1,
44 '../cpan/Parse-CPAN-Meta' => 1,
45 '../ext/Pod-Simple' => 1,
46 '../cpan/podlators' => 1,
47 '../ext/Test-Simple' => 1,
48 '../cpan/Tie-RefHash' => 1,
49 '../ext/Time-HiRes' => 1,
50 '../cpan/Unicode-Collate' => 1,
51 '../cpan/Unicode-Normalize' => 1,
58 # Location to put the Valgrind log.
59 my $Valgrind_Log = 'current.valgrind';
63 # for testing TEST only
64 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
65 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
67 delete $ENV{PERL5LIB};
69 delete $ENV{PERL5OPT};
71 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
72 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
73 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
75 # Cheesy version of Getopt::Std. We can't replace it with that, because we
76 # can't rely on require working.
79 foreach my $idx (0..$#ARGV) {
80 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
81 $::benchmark = 1 if $1 eq 'benchmark';
82 $::core = 1 if $1 eq 'core';
83 $::verbose = 1 if $1 eq 'v';
84 $::torture = 1 if $1 eq 'torture';
85 $::with_utf8 = 1 if $1 eq 'utf8';
86 $::with_utf16 = 1 if $1 eq 'utf16';
87 $::taintwarn = 1 if $1 eq 'taintwarn';
88 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
89 if ($1 =~ /^deparse(,.+)?$/) {
97 chdir 't' if -f 't/TEST';
98 if (-f 'TEST' && -f 'harness' && -d '../lib') {
102 die "You need to run \"make test\" first to set things up.\n"
103 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
105 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
106 unless (-x 'perl.third') {
107 unless (-x '../perl.third') {
108 die "You need to run \"make perl.third first.\n";
111 print "Symlinking ../perl.third as perl.third...\n";
112 die "Failed to symlink: $!\n"
113 unless symlink("../perl.third", "perl.third");
114 die "Symlinked but no executable perl.third: $!\n"
115 unless -x 'perl.third';
120 # check leakage for embedders
121 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
123 $ENV{EMXSHELL} = 'sh'; # For OS/2
125 if ($show_elapsed_time) { require Time::HiRes }
136 # Roll your own File::Find!
139 opendir DIR, $dir or die "Trouble opening $dir: $!";
140 foreach my $f (sort { $a cmp $b } readdir DIR) {
143 my $fullpath = "$dir/$f";
146 _find_tests($fullpath);
147 } elsif ($f =~ /\.t$/) {
148 push @ARGV, $fullpath;
154 # Scan the text of the test program to find switches and special options
155 # we might need to apply.
157 my($test, $type) = @_;
159 open(my $script, "<", $test) or die "Can't read $test.\n";
160 my $first_line = <$script>;
162 $first_line =~ tr/\0//d if $::with_utf16;
165 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
169 # not all tests are expected to pass with this option
177 if ($type eq 'deparse') {
178 # Look for #line directives which change the filename
180 $file_opts .= ",-f$3$4"
181 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
192 $test =~ /^(.+)\/[^\/]+/;
194 my $testswitch = $dir_to_switch{$dir};
195 if (!defined $testswitch) {
196 if ($test =~ s!^(\.\./(?:cpan|dist|ext)/[^/]+)/t!t!) {
198 $return_dir = '../../t';
200 $perl = '../../t/perl';
201 $testswitch = "-I../.. -MTestInit=U2T";
202 if (!$no_abs{$run_dir}) {
203 $testswitch = $testswitch . ',A';
205 if ($temp_no_core{$run_dir}) {
206 $testswitch = $testswitch . ',NC';
209 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
213 my $utf8 = $::with_utf8 ? "-I$lib -Mutf8" : '';
220 return_dir => $return_dir,
221 testswitch => $testswitch,
231 my($options, $type) = @_;
233 my $test = $options->{test};
236 if ($type eq 'deparse') {
237 my $perl = "$options->{perl} $options->{testswitch}";
238 my $lib = $options->{lib};
241 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
242 "-l$::deparse_opts$options->{file} ".
244 "&& $perl $options->{switch} -I$lib $test.dp"
247 elsif ($type eq 'perl') {
248 my $perl = $options->{perl};
249 my $redir = $^O eq 'VMS' ? '2>&1' : '';
251 if ($ENV{PERL_VALGRIND}) {
252 my $valgrind = $ENV{VALGRIND} // 'valgrind';
253 my $vg_opts = $ENV{VG_OPTS}
254 // "--suppressions=perl.supp --leak-check=yes "
255 . "--leak-resolution=high --show-reachable=yes "
256 . "--num-callers=50";
257 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
258 $redir = "3>$Valgrind_Log";
261 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
262 $cmd = $perl . _quote_args($args) . " $test $redir";
271 if ($options->{run_dir}) {
272 my $run_dir = $options->{run_dir};
273 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
282 if ($options->{return_dir}) {
283 my $return_dir = $options->{return_dir};
285 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
292 my ($test, $type) = @_;
294 my $options = _scan_test($test, $type);
295 # $test might have changed if we're in ext/Foo, so don't use it anymore
296 # from now on. Use $options->{test} instead.
298 _before_fork($options);
300 my $cmd = _cmd($options, $type);
302 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
304 _after_fork($options);
306 # Our environment may force us to use UTF-8, but we can't be sure that
307 # anything we're reading from will be generating (well formed) UTF-8
308 # This may not be the best way - possibly we should unset ${^OPEN} up
319 foreach (split(/\s+/,$args)) {
320 # In VMS protect with doublequotes because otherwise
321 # DCL will lowercase -- unless already doublequoted.
322 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
323 $argstring .= ' ' . $_;
329 return unless defined $_[0];
330 return map {$_, 1} split /\s+/, $_[0];
333 sub _tests_from_manifest {
334 my ($extensions, $known_extensions) = @_;
336 my %extensions = _populate_hash($extensions);
337 my %known_extensions = _populate_hash($known_extensions);
339 foreach (keys %known_extensions) {
340 $skip{$_}++ unless $extensions{$_};
344 my $mani = '../MANIFEST';
345 if (open(MANI, $mani)) {
347 if (m!^((?:cpan|dist|ext)/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
350 if (!$::core || $t =~ m!^lib/[a-z]!) {
351 if (defined $extension) {
352 $extension =~ s!/t$!!;
353 # XXX Do I want to warn that I'm skipping these?
354 next if $skip{$extension};
355 my $flat_extension = $extension;
356 $flat_extension =~ s!-!/!g;
357 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
360 push @results, $path;
361 $::path_to_name{$path} = $t;
367 warn "$0: cannot open $mani: $!\n";
373 # base first, as TEST bails out if that can't run
374 # then comp, to validate that require works
375 # then run, to validate that -M works
376 # then we know we can -MTestInit for everything else, making life simpler
377 foreach my $dir (qw(base comp run cmd io re op uni mro)) {
380 _find_tests("lib") unless $::core;
381 # Config.pm may be broken for make minitest. And this is only a refinement
382 # for skipping tests on non-default builds, so it is allowed to fail.
383 # What we want to to is make a list of extensions which we did not build.
384 my $configsh = '../config.sh';
385 my ($extensions, $known_extensions);
387 open FH, $configsh or die "Can't open $configsh: $!";
389 if (/^extensions=['"](.*)['"]$/) {
392 elsif (/^known_extensions=['"](.*)['"]$/) {
393 $known_extensions = $1;
396 if (!defined $known_extensions) {
397 warn "No known_extensions line found in $configsh";
399 if (!defined $extensions) {
400 warn "No extensions line found in $configsh";
403 # The "complex" constructions of list return from a subroutine, and push of
404 # a list, might fail if perl is really hosed, but they aren't needed for
405 # make minitest, and the building of extensions will likely also fail if
406 # something is that badly wrong.
407 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
410 _find_tests('porting');
411 _find_tests('japh') if $::torture;
412 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
417 _testprogs('deparse', '', @ARGV);
419 elsif ($::with_utf16) {
422 print STDERR "# ENDIAN $e BOM $b\n";
425 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
426 my $f = $e ? "v" : "n";
430 if (open(U, ">$u")) {
431 print U pack("$f", 0xFEFF) if $b;
433 print U pack("$f*", unpack("C*", $_));
440 _testprogs('perl', '', @UARGV);
446 _testprogs('perl', '', @ARGV);
450 my ($type, $args, @tests) = @_;
452 print <<'EOT' if ($type eq 'deparse');
453 ------------------------------------------------------------------------------
455 ------------------------------------------------------------------------------
460 foreach my $t (@tests) {
461 unless (exists $::path_to_name{$t}) {
463 $::path_to_name{$t} = $tname;
467 foreach (@::path_to_name{@tests}) {
470 $maxlen = $len if $len > $maxlen;
472 # + 3 : we want three dots between the test name and the "ok"
473 my $dotdotdot = $maxlen + 3 ;
475 my $total_files = @tests;
477 my $tested_files = 0;
481 while (my $test = shift @tests) {
482 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
487 if ($type eq 'deparse') {
488 if ($test eq "comp/redef.t") {
489 # Redefinition happens at compile time
492 elsif ($test =~ m{lib/Switch/t/}) {
493 # B::Deparse doesn't support source filtering
497 my $te = $::path_to_name{$test} . '.'
498 x ($dotdotdot - length($::path_to_name{$test}));
500 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
505 my $results = _run_test($test, $type);
511 my $trailing_leader = 0;
515 next if /^\s*$/; # skip blank lines
516 if (/^1..$/ && ($^O eq 'VMS')) {
517 # VMS pipe bug inserts blank lines.
519 if ($l2 =~ /^\s*$/) {
528 if ($trailing_leader) {
529 # shouldn't be anything following a postfix 1..n
530 $failure = 'FAILED--extra output after trailing 1..n';
533 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
535 $failure = 'FAILED--seen duplicate leader';
539 %todo = map { $_ => 1 } split / /, $3 if $3;
543 # 1..n appears at end of file
544 $trailing_leader = 1;
546 $failure = "FAILED--expected $max tests, saw $next";
556 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
557 unless ($seen_leader) {
564 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
565 $num = $next unless $num;
569 # SKIP is essentially the same as TODO for t/TEST
570 # this still conforms to TAP:
571 # http://search.cpan.org/dist/TAP/TAP.pm
572 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
573 $istodo = 1 if $todo{$num};
575 if( $not && !$istodo ) {
576 $failure = "FAILED at test $num";
581 $failure ="FAILED--expected test $next, saw test $num";
585 elsif (/^Bail out!\s*(.*)/i) { # magic words
586 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
589 # module tests are allowed extra output,
590 # because Test::Harness allows it
591 next if $test =~ /^\W*(cpan|dist|ext|lib)\b/;
592 $failure = "FAILED--unexpected output at test $next";
600 if (not defined $failure) {
601 $failure = 'FAILED--no leader found' unless $seen_leader;
604 if ($ENV{PERL_VALGRIND}) {
606 if (-e $Valgrind_Log) {
607 if (open(V, $Valgrind_Log)) {
611 warn "$0: Failed to open '$Valgrind_Log': $!\n";
614 if ($ENV{VG_OPTS} =~ /cachegrind/) {
615 if (rename $Valgrind_Log, "$test.valgrind") {
618 warn "$0: Failed to create '$test.valgrind': $!\n";
624 for my $i (0..$#valgrind) {
625 local $_ = $valgrind[$i];
626 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
627 $errors += $1; # there may be multiple error summaries
628 } elsif (/^==\d+== LEAK SUMMARY:/) {
629 for my $off (1 .. 4) {
630 if ($valgrind[$i+$off] =~
631 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
637 if ($errors or $leaks) {
638 if (rename $Valgrind_Log, "$test.valgrind") {
641 warn "$0: Failed to create '$test.valgrind': $!\n";
645 warn "No valgrind output?\n";
647 if (-e $Valgrind_Log) {
649 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
652 if ($type eq 'deparse') {
655 if ($ENV{PERL_3LOG}) {
659 $tpp =~ s:\.t$:.3log:;
660 rename("perl.3log", $tpp) ||
661 die "rename: perl3.log to $tpp: $!\n";
663 if (not defined $failure and $next != $max) {
664 $failure="FAILED--expected $max tests, saw $next";
667 if( !defined $failure # don't mask a test failure
670 $failure = "FAILED--non-zero wait status: $?";
673 if (defined $failure) {
674 print "${te}$failure\n";
676 if ($test =~ /^base/) {
677 die "Failed a basic test ($test) -- cannot continue.\n";
679 ++$failed_tests{$test};
684 if ( $show_elapsed_time ) {
685 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
690 print "${te}ok$elapsed\n";
694 print "${te}skipped\n";
700 if ($::bad_files == 0) {
702 print "All tests successful.\n";
703 # XXX add mention of 'perlbug -ok' ?
706 die "FAILED--no tests were run for some reason.\n";
710 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
711 my $s = $::bad_files == 1 ? "" : "s";
712 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
713 for my $test ( sort keys %failed_tests ) {
717 ### Since not all tests were successful, you may want to run some of
718 ### them individually and examine any diagnostic messages they produce.
719 ### See the INSTALL document's section on "make test".
721 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
722 ### You have a good chance to get more information by running
724 ### in the 't' directory since most (>=80%) of the tests succeeded.
726 if (eval {require Config; import Config; 1}) {
727 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
729 ### You may have to set your dynamic library search path,
730 ### $p, to point to the build directory:
732 if (exists $ENV{$p} && $ENV{$p} ne '') {
734 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
735 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
736 ### export $p=`pwd`:\$$p; cd t; ./perl harness
740 ### setenv $p `pwd`; cd t; ./perl harness
741 ### $p=`pwd`; export $p; cd t; ./perl harness
742 ### export $p=`pwd`; cd t; ./perl harness
746 ### for csh-style shells, like tcsh; or for traditional/modern
747 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
752 my ($user,$sys,$cuser,$csys) = times;
753 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
754 $user,$sys,$cuser,$csys,$tested_files,$totmax);
755 if ($ENV{PERL_VALGRIND}) {
756 my $s = $valgrind == 1 ? '' : 's';
757 print "$valgrind valgrind report$s created.\n", ;
760 exit ($::bad_files != 0);
762 # ex: set ts=8 sts=4 sw=4 noet: