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,
32 '../ext/IO-Compress' => 1,
33 '../ext/IPC-SysV' => 1,
34 '../ext/MIME-Base64' => 1,
35 '../ext/Time-HiRes' => 1,
36 '../ext/Unicode-Normalize' => 1,
43 # Location to put the Valgrind log.
44 my $Valgrind_Log = 'current.valgrind';
48 # for testing TEST only
49 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
50 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
52 delete $ENV{PERL5LIB};
54 delete $ENV{PERL5OPT};
56 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
57 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
58 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
60 # Cheesy version of Getopt::Std. We can't replace it with that, because we
61 # can't rely on require working.
64 foreach my $idx (0..$#ARGV) {
65 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
66 $::benchmark = 1 if $1 eq 'benchmark';
67 $::core = 1 if $1 eq 'core';
68 $::verbose = 1 if $1 eq 'v';
69 $::torture = 1 if $1 eq 'torture';
70 $::with_utf8 = 1 if $1 eq 'utf8';
71 $::with_utf16 = 1 if $1 eq 'utf16';
72 $::taintwarn = 1 if $1 eq 'taintwarn';
73 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
74 if ($1 =~ /^deparse(,.+)?$/) {
82 chdir 't' if -f 't/TEST';
84 die "You need to run \"make test\" first to set things up.\n"
85 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
87 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
88 unless (-x 'perl.third') {
89 unless (-x '../perl.third') {
90 die "You need to run \"make perl.third first.\n";
93 print "Symlinking ../perl.third as perl.third...\n";
94 die "Failed to symlink: $!\n"
95 unless symlink("../perl.third", "perl.third");
96 die "Symlinked but no executable perl.third: $!\n"
97 unless -x 'perl.third';
102 # check leakage for embedders
103 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
105 $ENV{EMXSHELL} = 'sh'; # For OS/2
107 if ($show_elapsed_time) { require Time::HiRes }
118 # Roll your own File::Find!
121 opendir DIR, $dir or die "Trouble opening $dir: $!";
122 foreach my $f (sort { $a cmp $b } readdir DIR) {
125 my $fullpath = "$dir/$f";
128 _find_tests($fullpath);
129 } elsif ($f =~ /\.t$/) {
130 push @ARGV, $fullpath;
136 # Scan the text of the test program to find switches and special options
137 # we might need to apply.
139 my($test, $type) = @_;
141 open(my $script, "<", $test) or die "Can't read $test.\n";
142 my $first_line = <$script>;
144 $first_line =~ tr/\0//d if $::with_utf16;
147 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
151 # not all tests are expected to pass with this option
159 if ($type eq 'deparse') {
160 # Look for #line directives which change the filename
162 $file_opts .= ",-f$3$4"
163 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
174 $test =~ /^(.+)\/[^\/]+/;
176 my $testswitch = $dir_to_switch{$dir};
177 if (!defined $testswitch) {
178 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
180 $return_dir = '../../t';
182 $perl = '../../t/perl';
183 $testswitch = "-I../.. -MTestInit=U2T,A";
184 if ($temp_no_core{$run_dir}) {
185 $testswitch = $testswitch . ',NC';
188 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
192 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
199 return_dir => $return_dir,
200 testswitch => $testswitch,
208 my($options, $type) = @_;
210 my $test = $options->{test};
213 if ($type eq 'deparse') {
214 my $perl = "$options->{perl} $options->{testswitch}";
215 my $lib = $options->{lib};
218 "$perl $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
219 "-l$::deparse_opts$options->{file} ".
221 "&& $perl $options->{switch} -I$lib $test.dp"
224 elsif ($type eq 'perl') {
225 my $perl = $options->{perl};
226 my $redir = $^O eq 'VMS' ? '2>&1' : '';
228 if ($ENV{PERL_VALGRIND}) {
229 my $valgrind = $ENV{VALGRIND} // 'valgrind';
230 my $vg_opts = $ENV{VG_OPTS}
231 // "--suppressions=perl.supp --leak-check=yes "
232 . "--leak-resolution=high --show-reachable=yes "
233 . "--num-callers=50";
234 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
235 $redir = "3>$Valgrind_Log";
238 my $args = "$options->{testswitch} $options->{switch} $options->{utf8}";
239 $cmd = $perl . _quote_args($args) . " $test $redir";
248 if ($options->{run_dir}) {
249 my $run_dir = $options->{run_dir};
250 chdir $run_dir or die "Can't chdir to '$run_dir': $!";
259 if ($options->{return_dir}) {
260 my $return_dir = $options->{return_dir};
262 or die "Can't chdir from '$options->{run_dir}' to '$return_dir': $!";
269 my($harness, $test, $type) = @_;
270 if (!defined $type) {
271 # To conform to the interface expected by exec in TAP::Harness
275 my $options = _scan_test($test, $type);
277 $test = $options->{test}; # Might have changed if we're in ext/Foo
279 _before_fork($options);
281 my $cmd = _cmd($options, $type);
283 open(my $results, "$cmd |") or print "can't run '$cmd': $!.\n";
285 _after_fork($options);
287 # Our environment may force us to use UTF-8, but we can't be sure that
288 # anything we're reading from will be generating (well formed) UTF-8
289 # This may not be the best way - possibly we should unset ${^OPEN} up
300 foreach (split(/\s+/,$args)) {
301 # In VMS protect with doublequotes because otherwise
302 # DCL will lowercase -- unless already doublequoted.
303 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
304 $argstring .= ' ' . $_;
310 return unless defined $_[0];
311 return map {$_, 1} split /\s+/, $_[0];
314 sub _tests_from_manifest {
315 my ($extensions, $known_extensions) = @_;
317 my %extensions = _populate_hash($extensions);
318 my %known_extensions = _populate_hash($known_extensions);
320 foreach (keys %known_extensions) {
321 $skip{$_}++ unless $extensions{$_};
325 my $mani = '../MANIFEST';
326 if (open(MANI, $mani)) {
328 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
331 if (!$::core || $t =~ m!^lib/[a-z]!) {
332 if (defined $extension) {
333 $extension =~ s!/t$!!;
334 # XXX Do I want to warn that I'm skipping these?
335 next if $skip{$extension};
336 my $flat_extension = $extension;
337 $flat_extension =~ s!-!/!g;
338 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
341 push @results, $path;
342 $::path_to_name{$path} = $t;
348 warn "$0: cannot open $mani: $!\n";
354 # base first, as TEST bails out if that can't run
355 # then comp, to validate that require works
356 # then run, to validate that -M works
357 # then we know we can -MTestInit for everything else, making life simpler
358 foreach my $dir (qw(base comp run cmd io op uni mro)) {
361 _find_tests("lib") unless $::core;
362 # Config.pm may be broken for make minitest. And this is only a refinement
363 # for skipping tests on non-default builds, so it is allowed to fail.
364 # What we want to to is make a list of extensions which we did not build.
365 my $configsh = '../config.sh';
366 my ($extensions, $known_extensions);
368 open FH, $configsh or die "Can't open $configsh: $!";
370 if (/^extensions=['"](.*)['"]$/) {
373 elsif (/^known_extensions=['"](.*)['"]$/) {
374 $known_extensions = $1;
377 if (!defined $known_extensions) {
378 warn "No known_extensions line found in $configsh";
380 if (!defined $extensions) {
381 warn "No extensions line found in $configsh";
384 # The "complex" constructions of list return from a subroutine, and push of
385 # a list, might fail if perl is really hosed, but they aren't needed for
386 # make minitest, and the building of extensions will likely also fail if
387 # something is that badly wrong.
388 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
392 _find_tests('porting');
393 _find_tests('japh') if $::torture;
394 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
399 _testprogs('deparse', '', @ARGV);
401 elsif ($::with_utf16) {
404 print STDERR "# ENDIAN $e BOM $b\n";
407 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
408 my $f = $e ? "v" : "n";
412 if (open(U, ">$u")) {
413 print U pack("$f", 0xFEFF) if $b;
415 print U pack("$f*", unpack("C*", $_));
422 _testprogs('perl', '', @UARGV);
428 _testprogs('perl', '', @ARGV);
432 my ($type, $args, @tests) = @_;
434 print <<'EOT' if ($type eq 'deparse');
435 ------------------------------------------------------------------------------
437 ------------------------------------------------------------------------------
442 foreach my $t (@tests) {
443 unless (exists $::path_to_name{$t}) {
445 $::path_to_name{$t} = $tname;
449 foreach (@::path_to_name{@tests}) {
452 $maxlen = $len if $len > $maxlen;
454 # + 3 : we want three dots between the test name and the "ok"
455 my $dotdotdot = $maxlen + 3 ;
457 my $total_files = @tests;
459 my $tested_files = 0;
463 while (my $test = shift @tests) {
464 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
469 if ($type eq 'deparse') {
470 if ($test eq "comp/redef.t") {
471 # Redefinition happens at compile time
474 elsif ($test =~ m{lib/Switch/t/}) {
475 # B::Deparse doesn't support source filtering
479 my $te = $::path_to_name{$test} . '.'
480 x ($dotdotdot - length($::path_to_name{$test}));
482 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
487 my $results = _run_test(undef, $test, $type);
493 my $trailing_leader = 0;
497 next if /^\s*$/; # skip blank lines
498 if (/^1..$/ && ($^O eq 'VMS')) {
499 # VMS pipe bug inserts blank lines.
501 if ($l2 =~ /^\s*$/) {
510 if ($trailing_leader) {
511 # shouldn't be anything following a postfix 1..n
512 $failure = 'FAILED--extra output after trailing 1..n';
515 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
517 $failure = 'FAILED--seen duplicate leader';
521 %todo = map { $_ => 1 } split / /, $3 if $3;
525 # 1..n appears at end of file
526 $trailing_leader = 1;
528 $failure = "FAILED--expected $max tests, saw $next";
538 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
539 unless ($seen_leader) {
546 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
547 $num = $next unless $num;
551 # SKIP is essentially the same as TODO for t/TEST
552 # this still conforms to TAP:
553 # http://search.cpan.org/dist/TAP/TAP.pod
554 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
555 $istodo = 1 if $todo{$num};
557 if( $not && !$istodo ) {
558 $failure = "FAILED at test $num";
563 $failure ="FAILED--expected test $next, saw test $num";
567 elsif (/^Bail out!\s*(.*)/i) { # magic words
568 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
571 # module tests are allowed extra output,
572 # because Test::Harness allows it
573 next if $test =~ /^\W*(ext|lib)\b/;
574 $failure = "FAILED--unexpected output at test $next";
582 if (not defined $failure) {
583 $failure = 'FAILED--no leader found' unless $seen_leader;
586 if ($ENV{PERL_VALGRIND}) {
588 if (-e $Valgrind_Log) {
589 if (open(V, $Valgrind_Log)) {
593 warn "$0: Failed to open '$Valgrind_Log': $!\n";
596 if ($ENV{VG_OPTS} =~ /cachegrind/) {
597 if (rename $Valgrind_Log, "$test.valgrind") {
600 warn "$0: Failed to create '$test.valgrind': $!\n";
606 for my $i (0..$#valgrind) {
607 local $_ = $valgrind[$i];
608 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
609 $errors += $1; # there may be multiple error summaries
610 } elsif (/^==\d+== LEAK SUMMARY:/) {
611 for my $off (1 .. 4) {
612 if ($valgrind[$i+$off] =~
613 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
619 if ($errors or $leaks) {
620 if (rename $Valgrind_Log, "$test.valgrind") {
623 warn "$0: Failed to create '$test.valgrind': $!\n";
627 warn "No valgrind output?\n";
629 if (-e $Valgrind_Log) {
631 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
634 if ($type eq 'deparse') {
637 if ($ENV{PERL_3LOG}) {
641 $tpp =~ s:\.t$:.3log:;
642 rename("perl.3log", $tpp) ||
643 die "rename: perl3.log to $tpp: $!\n";
645 if (not defined $failure and $next != $max) {
646 $failure="FAILED--expected $max tests, saw $next";
649 if( !defined $failure # don't mask a test failure
652 $failure = "FAILED--non-zero wait status: $?";
655 if (defined $failure) {
656 print "${te}$failure\n";
658 if ($test =~ /^base/) {
659 die "Failed a basic test ($test) -- cannot continue.\n";
661 ++$failed_tests{$test};
666 if ( $show_elapsed_time ) {
667 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
672 print "${te}ok$elapsed\n";
676 print "${te}skipped\n";
682 if ($::bad_files == 0) {
684 print "All tests successful.\n";
685 # XXX add mention of 'perlbug -ok' ?
688 die "FAILED--no tests were run for some reason.\n";
692 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
693 my $s = $::bad_files == 1 ? "" : "s";
694 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
695 for my $test ( sort keys %failed_tests ) {
699 ### Since not all tests were successful, you may want to run some of
700 ### them individually and examine any diagnostic messages they produce.
701 ### See the INSTALL document's section on "make test".
703 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
704 ### You have a good chance to get more information by running
706 ### in the 't' directory since most (>=80%) of the tests succeeded.
708 if (eval {require Config; import Config; 1}) {
709 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
711 ### You may have to set your dynamic library search path,
712 ### $p, to point to the build directory:
714 if (exists $ENV{$p} && $ENV{$p} ne '') {
716 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
717 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
718 ### export $p=`pwd`:\$$p; cd t; ./perl harness
722 ### setenv $p `pwd`; cd t; ./perl harness
723 ### $p=`pwd`; export $p; cd t; ./perl harness
724 ### export $p=`pwd`; cd t; ./perl harness
728 ### for csh-style shells, like tcsh; or for traditional/modern
729 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
734 my ($user,$sys,$cuser,$csys) = times;
735 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
736 $user,$sys,$cuser,$csys,$tested_files,$totmax);
737 if ($ENV{PERL_VALGRIND}) {
738 my $s = $valgrind == 1 ? '' : 's';
739 print "$valgrind valgrind report$s created.\n", ;
742 exit ($::bad_files != 0);
744 # ex: set ts=8 sts=4 sw=4 noet: