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/Attribute-Handlers' => 1,
28 '../ext/B' => 'fixme',
29 '../ext/Compress-Raw-Bzip2' => 1,
30 '../ext/Compress-Raw-Zlib' => 1,
31 '../ext/Data-Dumper' => 'fixme',
32 '../ext/Devel-PPPort' => 1,
33 '../ext/DynaLoader' => 'fixme',
35 '../ext/Hash-Util' => 'fixme',
36 '../ext/Hash-Util-FieldHash' => 'fixme',
37 '../ext/I18N-Langinfo' => 'fixme',
38 '../ext/IO-Compress' => 1,
39 '../ext/IPC-SysV' => 1,
40 '../ext/MIME-Base64' => 1,
41 '../ext/Safe' => 'fixme',
42 '../ext/Storable' => 'fixme',
43 '../ext/Time-HiRes' => 1,
44 '../ext/Unicode-Normalize' => 1,
51 # Location to put the Valgrind log.
52 my $Valgrind_Log = 'current.valgrind';
56 # for testing TEST only
57 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
58 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
60 delete $ENV{PERL5LIB};
62 delete $ENV{PERL5OPT};
64 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
65 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
66 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
68 # Cheesy version of Getopt::Std. We can't replace it with that, because we
69 # can't rely on require working.
72 foreach my $idx (0..$#ARGV) {
73 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
74 $::benchmark = 1 if $1 eq 'benchmark';
75 $::core = 1 if $1 eq 'core';
76 $::verbose = 1 if $1 eq 'v';
77 $::torture = 1 if $1 eq 'torture';
78 $::with_utf8 = 1 if $1 eq 'utf8';
79 $::with_utf16 = 1 if $1 eq 'utf16';
80 $::taintwarn = 1 if $1 eq 'taintwarn';
81 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
82 if ($1 =~ /^deparse(,.+)?$/) {
90 chdir 't' if -f 't/TEST';
92 die "You need to run \"make test\" first to set things up.\n"
93 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
95 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
96 unless (-x 'perl.third') {
97 unless (-x '../perl.third') {
98 die "You need to run \"make perl.third first.\n";
101 print "Symlinking ../perl.third as perl.third...\n";
102 die "Failed to symlink: $!\n"
103 unless symlink("../perl.third", "perl.third");
104 die "Symlinked but no executable perl.third: $!\n"
105 unless -x 'perl.third';
110 # check leakage for embedders
111 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
113 $ENV{EMXSHELL} = 'sh'; # For OS/2
115 if ($show_elapsed_time) { require Time::HiRes }
126 # Roll your own File::Find!
129 opendir DIR, $dir or die "Trouble opening $dir: $!";
130 foreach my $f (sort { $a cmp $b } readdir DIR) {
133 my $fullpath = "$dir/$f";
136 _find_tests($fullpath);
137 } elsif ($f =~ /\.t$/) {
138 push @ARGV, $fullpath;
144 # Scan the text of the test program to find switches and special options
145 # we might need to apply.
147 my($test, $type) = @_;
149 open(my $script, "<", $test) or die "Can't read $test.\n";
150 my $first_line = <$script>;
152 $first_line =~ tr/\0//d if $::with_utf16;
155 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
159 # not all tests are expected to pass with this option
167 if ($type eq 'deparse') {
168 # Look for #line directives which change the filename
170 $file_opts .= ",-f$3$4"
171 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
175 return { file => $file_opts, switch => $switch };
179 my($harness, $test, $type) = @_;
180 if (!defined $type) {
181 # To conform to the interface expected by exec in TAP::Harness
185 my $options = _scan_test($test, $type);
190 $test =~ /^(.+)\/[^\/]+/;
194 my $testswitch = $dir_to_switch{$dir};
195 if (!defined $testswitch) {
196 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
198 $return_dir = '../../t';
200 $perl = '../../t/perl';
201 $testswitch = "-I../.. -MTestInit=U2T,A";
202 if ($temp_no_core{$ext_dir}) {
203 $testswitch = $testswitch . ',NC';
205 chdir $ext_dir or die "Can't chdir to '$ext_dir': $!";
207 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
211 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
214 if ($type eq 'deparse') {
216 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
217 "-l$::deparse_opts$options->{file} ".
219 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
220 open($results, $deparse_cmd)
221 or print "can't deparse '$deparse_cmd': $!.\n";
223 elsif ($type eq 'perl') {
224 my $redir = $^O eq 'VMS' ? '2>&1' : '';
226 if ($ENV{PERL_VALGRIND}) {
227 my $valgrind = $ENV{VALGRIND} // 'valgrind';
228 my $vg_opts = $ENV{VG_OPTS}
229 // "--suppressions=perl.supp --leak-check=yes "
230 . "--leak-resolution=high --show-reachable=yes "
231 . "--num-callers=50";
232 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
233 $redir = "3>$Valgrind_Log";
236 my $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
238 open($results, $run) or print "can't run '$run': $!.\n";
243 or die "Can't chdir from '$ext_dir' to '$return_dir': $!";
246 # Our environment may force us to use UTF-8, but we can't be sure that
247 # anything we're reading from will be generating (well formed) UTF-8
248 # This may not be the best way - possibly we should unset ${^OPEN} up
259 foreach (split(/\s+/,$args)) {
260 # In VMS protect with doublequotes because otherwise
261 # DCL will lowercase -- unless already doublequoted.
262 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
263 $argstring .= ' ' . $_;
269 return unless defined $_[0];
270 return map {$_, 1} split /\s+/, $_[0];
273 sub _tests_from_manifest {
274 my ($extensions, $known_extensions) = @_;
276 my %extensions = _populate_hash($extensions);
277 my %known_extensions = _populate_hash($known_extensions);
279 foreach (keys %known_extensions) {
280 $skip{$_}++ unless $extensions{$_};
284 my $mani = '../MANIFEST';
285 if (open(MANI, $mani)) {
287 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
290 if (!$::core || $t =~ m!^lib/[a-z]!) {
291 if (defined $extension) {
292 $extension =~ s!/t$!!;
293 # XXX Do I want to warn that I'm skipping these?
294 next if $skip{$extension};
295 my $flat_extension = $extension;
296 $flat_extension =~ s!-!/!g;
297 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
300 push @results, $path;
301 $::path_to_name{$path} = $t;
307 warn "$0: cannot open $mani: $!\n";
313 # base first, as TEST bails out if that can't run
314 # then comp, to validate that require works
315 # then run, to validate that -M works
316 # then we know we can -MTestInit for everything else, making life simpler
317 foreach my $dir (qw(base comp run cmd io op uni mro)) {
320 _find_tests("lib") unless $::core;
321 # Config.pm may be broken for make minitest. And this is only a refinement
322 # for skipping tests on non-default builds, so it is allowed to fail.
323 # What we want to to is make a list of extensions which we did not build.
324 my $configsh = '../config.sh';
325 my ($extensions, $known_extensions);
327 open FH, $configsh or die "Can't open $configsh: $!";
329 if (/^extensions=['"](.*)['"]$/) {
332 elsif (/^known_extensions=['"](.*)['"]$/) {
333 $known_extensions = $1;
336 if (!defined $known_extensions) {
337 warn "No known_extensions line found in $configsh";
339 if (!defined $extensions) {
340 warn "No extensions line found in $configsh";
343 # The "complex" constructions of list return from a subroutine, and push of
344 # a list, might fail if perl is really hosed, but they aren't needed for
345 # make minitest, and the building of extensions will likely also fail if
346 # something is that badly wrong.
347 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
351 _find_tests('porting');
352 _find_tests('japh') if $::torture;
353 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
358 _testprogs('deparse', '', @ARGV);
360 elsif ($::with_utf16) {
363 print STDERR "# ENDIAN $e BOM $b\n";
366 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
367 my $f = $e ? "v" : "n";
371 if (open(U, ">$u")) {
372 print U pack("$f", 0xFEFF) if $b;
374 print U pack("$f*", unpack("C*", $_));
381 _testprogs('perl', '', @UARGV);
387 _testprogs('perl', '', @ARGV);
391 my ($type, $args, @tests) = @_;
393 print <<'EOT' if ($type eq 'deparse');
394 ------------------------------------------------------------------------------
396 ------------------------------------------------------------------------------
401 foreach my $t (@tests) {
402 unless (exists $::path_to_name{$t}) {
404 $::path_to_name{$t} = $tname;
408 foreach (@::path_to_name{@tests}) {
411 $maxlen = $len if $len > $maxlen;
413 # + 3 : we want three dots between the test name and the "ok"
414 my $dotdotdot = $maxlen + 3 ;
416 my $total_files = @tests;
418 my $tested_files = 0;
422 while (my $test = shift @tests) {
423 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
428 if ($type eq 'deparse') {
429 if ($test eq "comp/redef.t") {
430 # Redefinition happens at compile time
433 elsif ($test =~ m{lib/Switch/t/}) {
434 # B::Deparse doesn't support source filtering
438 my $te = $::path_to_name{$test} . '.'
439 x ($dotdotdot - length($::path_to_name{$test}));
441 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
446 my $results = _run_test(undef, $test, $type);
452 my $trailing_leader = 0;
456 next if /^\s*$/; # skip blank lines
457 if (/^1..$/ && ($^O eq 'VMS')) {
458 # VMS pipe bug inserts blank lines.
460 if ($l2 =~ /^\s*$/) {
469 if ($trailing_leader) {
470 # shouldn't be anything following a postfix 1..n
471 $failure = 'FAILED--extra output after trailing 1..n';
474 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
476 $failure = 'FAILED--seen duplicate leader';
480 %todo = map { $_ => 1 } split / /, $3 if $3;
484 # 1..n appears at end of file
485 $trailing_leader = 1;
487 $failure = "FAILED--expected $max tests, saw $next";
497 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
498 unless ($seen_leader) {
505 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
506 $num = $next unless $num;
510 # SKIP is essentially the same as TODO for t/TEST
511 # this still conforms to TAP:
512 # http://search.cpan.org/dist/TAP/TAP.pod
513 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
514 $istodo = 1 if $todo{$num};
516 if( $not && !$istodo ) {
517 $failure = "FAILED at test $num";
522 $failure ="FAILED--expected test $next, saw test $num";
526 elsif (/^Bail out!\s*(.*)/i) { # magic words
527 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
530 # module tests are allowed extra output,
531 # because Test::Harness allows it
532 next if $test =~ /^\W*(ext|lib)\b/;
533 $failure = "FAILED--unexpected output at test $next";
541 if (not defined $failure) {
542 $failure = 'FAILED--no leader found' unless $seen_leader;
545 if ($ENV{PERL_VALGRIND}) {
547 if (-e $Valgrind_Log) {
548 if (open(V, $Valgrind_Log)) {
552 warn "$0: Failed to open '$Valgrind_Log': $!\n";
555 if ($ENV{VG_OPTS} =~ /cachegrind/) {
556 if (rename $Valgrind_Log, "$test.valgrind") {
559 warn "$0: Failed to create '$test.valgrind': $!\n";
565 for my $i (0..$#valgrind) {
566 local $_ = $valgrind[$i];
567 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
568 $errors += $1; # there may be multiple error summaries
569 } elsif (/^==\d+== LEAK SUMMARY:/) {
570 for my $off (1 .. 4) {
571 if ($valgrind[$i+$off] =~
572 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
578 if ($errors or $leaks) {
579 if (rename $Valgrind_Log, "$test.valgrind") {
582 warn "$0: Failed to create '$test.valgrind': $!\n";
586 warn "No valgrind output?\n";
588 if (-e $Valgrind_Log) {
590 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
593 if ($type eq 'deparse') {
596 if ($ENV{PERL_3LOG}) {
600 $tpp =~ s:\.t$:.3log:;
601 rename("perl.3log", $tpp) ||
602 die "rename: perl3.log to $tpp: $!\n";
604 if (not defined $failure and $next != $max) {
605 $failure="FAILED--expected $max tests, saw $next";
608 if( !defined $failure # don't mask a test failure
611 $failure = "FAILED--non-zero wait status: $?";
614 if (defined $failure) {
615 print "${te}$failure\n";
617 if ($test =~ /^base/) {
618 die "Failed a basic test ($test) -- cannot continue.\n";
620 ++$failed_tests{$test};
625 if ( $show_elapsed_time ) {
626 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
631 print "${te}ok$elapsed\n";
635 print "${te}skipped\n";
641 if ($::bad_files == 0) {
643 print "All tests successful.\n";
644 # XXX add mention of 'perlbug -ok' ?
647 die "FAILED--no tests were run for some reason.\n";
651 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
652 my $s = $::bad_files == 1 ? "" : "s";
653 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
654 for my $test ( sort keys %failed_tests ) {
658 ### Since not all tests were successful, you may want to run some of
659 ### them individually and examine any diagnostic messages they produce.
660 ### See the INSTALL document's section on "make test".
662 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
663 ### You have a good chance to get more information by running
665 ### in the 't' directory since most (>=80%) of the tests succeeded.
667 if (eval {require Config; import Config; 1}) {
668 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
670 ### You may have to set your dynamic library search path,
671 ### $p, to point to the build directory:
673 if (exists $ENV{$p} && $ENV{$p} ne '') {
675 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
676 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
677 ### export $p=`pwd`:\$$p; cd t; ./perl harness
681 ### setenv $p `pwd`; cd t; ./perl harness
682 ### $p=`pwd`; export $p; cd t; ./perl harness
683 ### export $p=`pwd`; cd t; ./perl harness
687 ### for csh-style shells, like tcsh; or for traditional/modern
688 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
693 my ($user,$sys,$cuser,$csys) = times;
694 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
695 $user,$sys,$cuser,$csys,$tested_files,$totmax);
696 if ($ENV{PERL_VALGRIND}) {
697 my $s = $valgrind == 1 ? '' : 's';
698 print "$valgrind valgrind report$s created.\n", ;
701 exit ($::bad_files != 0);
703 # ex: set ts=8 sts=4 sw=4 noet: