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 ('../ext/B-Debug' => 1,
34 '../ext/Compress-Raw-Bzip2' => 1,
35 '../ext/Compress-Raw-Zlib' => 1,
36 '../cpan/Devel-PPPort' => 1,
37 '../cpan/Getopt-Long' => 1,
38 '../ext/IO-Compress' => 1,
39 '../cpan/IPC-SysV' => 1,
40 '../ext/Math-BigInt' => 1,
41 '../ext/Math-BigRat' => 1,
42 '../ext/MIME-Base64' => 1,
45 '../cpan/Parse-CPAN-Meta' => 1,
46 '../ext/Pod-Simple' => 1,
47 '../cpan/podlators' => 1,
48 '../ext/Test-Simple' => 1,
49 '../cpan/Tie-RefHash' => 1,
50 '../ext/Time-HiRes' => 1,
51 '../ext/Unicode-Collate' => 1,
52 '../ext/Unicode-Normalize' => 1,
59 # Location to put the Valgrind log.
60 my $Valgrind_Log = 'current.valgrind';
64 # for testing TEST only
65 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
66 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
68 delete $ENV{PERL5LIB};
70 delete $ENV{PERL5OPT};
72 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
73 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
74 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
76 # Cheesy version of Getopt::Std. We can't replace it with that, because we
77 # can't rely on require working.
80 foreach my $idx (0..$#ARGV) {
81 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
82 $::benchmark = 1 if $1 eq 'benchmark';
83 $::core = 1 if $1 eq 'core';
84 $::verbose = 1 if $1 eq 'v';
85 $::torture = 1 if $1 eq 'torture';
86 $::with_utf8 = 1 if $1 eq 'utf8';
87 $::with_utf16 = 1 if $1 eq 'utf16';
88 $::taintwarn = 1 if $1 eq 'taintwarn';
89 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
90 if ($1 =~ /^deparse(,.+)?$/) {
98 chdir 't' if -f 't/TEST';
99 if (-f 'TEST' && -f 'harness' && -d '../lib') {
103 die "You need to run \"make test\" first to set things up.\n"
104 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
106 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
107 unless (-x 'perl.third') {
108 unless (-x '../perl.third') {
109 die "You need to run \"make perl.third first.\n";
112 print "Symlinking ../perl.third as perl.third...\n";
113 die "Failed to symlink: $!\n"
114 unless symlink("../perl.third", "perl.third");
115 die "Symlinked but no executable perl.third: $!\n"
116 unless -x 'perl.third';
121 # check leakage for embedders
122 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
124 $ENV{EMXSHELL} = 'sh'; # For OS/2
126 if ($show_elapsed_time) { require Time::HiRes }
137 # Roll your own File::Find!
140 opendir DIR, $dir or die "Trouble opening $dir: $!";
141 foreach my $f (sort { $a cmp $b } readdir DIR) {
144 my $fullpath = "$dir/$f";
147 _find_tests($fullpath);
148 } elsif ($f =~ /\.t$/) {
149 push @ARGV, $fullpath;
155 # Scan the text of the test program to find switches and special options
156 # we might need to apply.
158 my($test, $type) = @_;
160 open(my $script, "<", $test) or die "Can't read $test.\n";
161 my $first_line = <$script>;
163 $first_line =~ tr/\0//d if $::with_utf16;
166 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
170 # not all tests are expected to pass with this option
178 if ($type eq 'deparse') {
179 # Look for #line directives which change the filename
181 $file_opts .= ",-f$3$4"
182 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
193 $test =~ /^(.+)\/[^\/]+/;
195 my $testswitch = $dir_to_switch{$dir};
196 if (!defined $testswitch) {
197 if ($test =~ s!^(\.\./(?:cpan|ext)/[^/]+)/t!t!) {
199 $return_dir = '../../t';
201 $perl = '../../t/perl';
202 $testswitch = "-I../.. -MTestInit=U2T";
203 if (!$no_abs{$run_dir}) {
204 $testswitch = $testswitch . ',A';
206 if ($temp_no_core{$run_dir}) {
207 $testswitch = $testswitch . ',NC';
210 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
214 my $utf8 = $::with_utf8 ? "-I$lib -Mutf8" : '';
221 return_dir => $return_dir,
222 testswitch => $testswitch,
232 my($options, $type) = @_;
234 my $test = $options->{test};
237 if ($type eq 'deparse') {
238 my $perl = "$options->{perl} $options->{testswitch}";
239 my $lib = $options->{lib};
242 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
243 "-l$::deparse_opts$options->{file} ".
245 "&& $perl $options->{switch} -I$lib $test.dp"
248 elsif ($type eq 'perl') {
249 my $perl = $options->{perl};
250 my $redir = $^O eq 'VMS' ? '2>&1' : '';
252 if ($ENV{PERL_VALGRIND}) {
253 my $valgrind = $ENV{VALGRIND} // 'valgrind';
254 my $vg_opts = $ENV{VG_OPTS}
255 // "--suppressions=perl.supp --leak-check=yes "
256 . "--leak-resolution=high --show-reachable=yes "
257 . "--num-callers=50";
258 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
259 $redir = "3>$Valgrind_Log";
262 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
263 $cmd = $perl . _quote_args($args) . " $test $redir";
272 if ($options->{run_dir}) {
273 my $run_dir = $options->{run_dir};
274 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
283 if ($options->{return_dir}) {
284 my $return_dir = $options->{return_dir};
286 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
293 my ($test, $type) = @_;
295 my $options = _scan_test($test, $type);
296 # $test might have changed if we're in ext/Foo, so don't use it anymore
297 # from now on. Use $options->{test} instead.
299 _before_fork($options);
301 my $cmd = _cmd($options, $type);
303 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
305 _after_fork($options);
307 # Our environment may force us to use UTF-8, but we can't be sure that
308 # anything we're reading from will be generating (well formed) UTF-8
309 # This may not be the best way - possibly we should unset ${^OPEN} up
320 foreach (split(/\s+/,$args)) {
321 # In VMS protect with doublequotes because otherwise
322 # DCL will lowercase -- unless already doublequoted.
323 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
324 $argstring .= ' ' . $_;
330 return unless defined $_[0];
331 return map {$_, 1} split /\s+/, $_[0];
334 sub _tests_from_manifest {
335 my ($extensions, $known_extensions) = @_;
337 my %extensions = _populate_hash($extensions);
338 my %known_extensions = _populate_hash($known_extensions);
340 foreach (keys %known_extensions) {
341 $skip{$_}++ unless $extensions{$_};
345 my $mani = '../MANIFEST';
346 if (open(MANI, $mani)) {
348 if (m!^((?:cpan|ext)/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
351 if (!$::core || $t =~ m!^lib/[a-z]!) {
352 if (defined $extension) {
353 $extension =~ s!/t$!!;
354 # XXX Do I want to warn that I'm skipping these?
355 next if $skip{$extension};
356 my $flat_extension = $extension;
357 $flat_extension =~ s!-!/!g;
358 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
361 push @results, $path;
362 $::path_to_name{$path} = $t;
368 warn "$0: cannot open $mani: $!\n";
374 # base first, as TEST bails out if that can't run
375 # then comp, to validate that require works
376 # then run, to validate that -M works
377 # then we know we can -MTestInit for everything else, making life simpler
378 foreach my $dir (qw(base comp run cmd io re op uni mro)) {
381 _find_tests("lib") unless $::core;
382 # Config.pm may be broken for make minitest. And this is only a refinement
383 # for skipping tests on non-default builds, so it is allowed to fail.
384 # What we want to to is make a list of extensions which we did not build.
385 my $configsh = '../config.sh';
386 my ($extensions, $known_extensions);
388 open FH, $configsh or die "Can't open $configsh: $!";
390 if (/^extensions=['"](.*)['"]$/) {
393 elsif (/^known_extensions=['"](.*)['"]$/) {
394 $known_extensions = $1;
397 if (!defined $known_extensions) {
398 warn "No known_extensions line found in $configsh";
400 if (!defined $extensions) {
401 warn "No extensions line found in $configsh";
404 # The "complex" constructions of list return from a subroutine, and push of
405 # a list, might fail if perl is really hosed, but they aren't needed for
406 # make minitest, and the building of extensions will likely also fail if
407 # something is that badly wrong.
408 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
412 _find_tests('porting');
413 _find_tests('japh') if $::torture;
414 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
419 _testprogs('deparse', '', @ARGV);
421 elsif ($::with_utf16) {
424 print STDERR "# ENDIAN $e BOM $b\n";
427 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
428 my $f = $e ? "v" : "n";
432 if (open(U, ">$u")) {
433 print U pack("$f", 0xFEFF) if $b;
435 print U pack("$f*", unpack("C*", $_));
442 _testprogs('perl', '', @UARGV);
448 _testprogs('perl', '', @ARGV);
452 my ($type, $args, @tests) = @_;
454 print <<'EOT' if ($type eq 'deparse');
455 ------------------------------------------------------------------------------
457 ------------------------------------------------------------------------------
462 foreach my $t (@tests) {
463 unless (exists $::path_to_name{$t}) {
465 $::path_to_name{$t} = $tname;
469 foreach (@::path_to_name{@tests}) {
472 $maxlen = $len if $len > $maxlen;
474 # + 3 : we want three dots between the test name and the "ok"
475 my $dotdotdot = $maxlen + 3 ;
477 my $total_files = @tests;
479 my $tested_files = 0;
483 while (my $test = shift @tests) {
484 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
489 if ($type eq 'deparse') {
490 if ($test eq "comp/redef.t") {
491 # Redefinition happens at compile time
494 elsif ($test =~ m{lib/Switch/t/}) {
495 # B::Deparse doesn't support source filtering
499 my $te = $::path_to_name{$test} . '.'
500 x ($dotdotdot - length($::path_to_name{$test}));
502 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
507 my $results = _run_test($test, $type);
513 my $trailing_leader = 0;
517 next if /^\s*$/; # skip blank lines
518 if (/^1..$/ && ($^O eq 'VMS')) {
519 # VMS pipe bug inserts blank lines.
521 if ($l2 =~ /^\s*$/) {
530 if ($trailing_leader) {
531 # shouldn't be anything following a postfix 1..n
532 $failure = 'FAILED--extra output after trailing 1..n';
535 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
537 $failure = 'FAILED--seen duplicate leader';
541 %todo = map { $_ => 1 } split / /, $3 if $3;
545 # 1..n appears at end of file
546 $trailing_leader = 1;
548 $failure = "FAILED--expected $max tests, saw $next";
558 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
559 unless ($seen_leader) {
566 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
567 $num = $next unless $num;
571 # SKIP is essentially the same as TODO for t/TEST
572 # this still conforms to TAP:
573 # http://search.cpan.org/dist/TAP/TAP.pm
574 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
575 $istodo = 1 if $todo{$num};
577 if( $not && !$istodo ) {
578 $failure = "FAILED at test $num";
583 $failure ="FAILED--expected test $next, saw test $num";
587 elsif (/^Bail out!\s*(.*)/i) { # magic words
588 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
591 # module tests are allowed extra output,
592 # because Test::Harness allows it
593 next if $test =~ /^\W*(ext|lib)\b/;
594 $failure = "FAILED--unexpected output at test $next";
602 if (not defined $failure) {
603 $failure = 'FAILED--no leader found' unless $seen_leader;
606 if ($ENV{PERL_VALGRIND}) {
608 if (-e $Valgrind_Log) {
609 if (open(V, $Valgrind_Log)) {
613 warn "$0: Failed to open '$Valgrind_Log': $!\n";
616 if ($ENV{VG_OPTS} =~ /cachegrind/) {
617 if (rename $Valgrind_Log, "$test.valgrind") {
620 warn "$0: Failed to create '$test.valgrind': $!\n";
626 for my $i (0..$#valgrind) {
627 local $_ = $valgrind[$i];
628 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
629 $errors += $1; # there may be multiple error summaries
630 } elsif (/^==\d+== LEAK SUMMARY:/) {
631 for my $off (1 .. 4) {
632 if ($valgrind[$i+$off] =~
633 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
639 if ($errors or $leaks) {
640 if (rename $Valgrind_Log, "$test.valgrind") {
643 warn "$0: Failed to create '$test.valgrind': $!\n";
647 warn "No valgrind output?\n";
649 if (-e $Valgrind_Log) {
651 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
654 if ($type eq 'deparse') {
657 if ($ENV{PERL_3LOG}) {
661 $tpp =~ s:\.t$:.3log:;
662 rename("perl.3log", $tpp) ||
663 die "rename: perl3.log to $tpp: $!\n";
665 if (not defined $failure and $next != $max) {
666 $failure="FAILED--expected $max tests, saw $next";
669 if( !defined $failure # don't mask a test failure
672 $failure = "FAILED--non-zero wait status: $?";
675 if (defined $failure) {
676 print "${te}$failure\n";
678 if ($test =~ /^base/) {
679 die "Failed a basic test ($test) -- cannot continue.\n";
681 ++$failed_tests{$test};
686 if ( $show_elapsed_time ) {
687 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
692 print "${te}ok$elapsed\n";
696 print "${te}skipped\n";
702 if ($::bad_files == 0) {
704 print "All tests successful.\n";
705 # XXX add mention of 'perlbug -ok' ?
708 die "FAILED--no tests were run for some reason.\n";
712 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
713 my $s = $::bad_files == 1 ? "" : "s";
714 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
715 for my $test ( sort keys %failed_tests ) {
719 ### Since not all tests were successful, you may want to run some of
720 ### them individually and examine any diagnostic messages they produce.
721 ### See the INSTALL document's section on "make test".
723 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
724 ### You have a good chance to get more information by running
726 ### in the 't' directory since most (>=80%) of the tests succeeded.
728 if (eval {require Config; import Config; 1}) {
729 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
731 ### You may have to set your dynamic library search path,
732 ### $p, to point to the build directory:
734 if (exists $ENV{$p} && $ENV{$p} ne '') {
736 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
737 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
738 ### export $p=`pwd`:\$$p; cd t; ./perl harness
742 ### setenv $p `pwd`; cd t; ./perl harness
743 ### $p=`pwd`; export $p; cd t; ./perl harness
744 ### export $p=`pwd`; cd t; ./perl harness
748 ### for csh-style shells, like tcsh; or for traditional/modern
749 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
754 my ($user,$sys,$cuser,$csys) = times;
755 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
756 $user,$sys,$cuser,$csys,$tested_files,$totmax);
757 if ($ENV{PERL_VALGRIND}) {
758 my $s = $valgrind == 1 ? '' : 's';
759 print "$valgrind valgrind report$s created.\n", ;
762 exit ($::bad_files != 0);
764 # ex: set ts=8 sts=4 sw=4 noet: