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-Debug' => 1,
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/Time-HiRes' => 1,
43 '../ext/Unicode-Normalize' => 1,
50 # Location to put the Valgrind log.
51 my $Valgrind_Log = 'current.valgrind';
55 # for testing TEST only
56 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
57 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
59 delete $ENV{PERL5LIB};
61 delete $ENV{PERL5OPT};
63 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
64 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
65 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
67 # Cheesy version of Getopt::Std. We can't replace it with that, because we
68 # can't rely on require working.
71 foreach my $idx (0..$#ARGV) {
72 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
73 $::benchmark = 1 if $1 eq 'benchmark';
74 $::core = 1 if $1 eq 'core';
75 $::verbose = 1 if $1 eq 'v';
76 $::torture = 1 if $1 eq 'torture';
77 $::with_utf8 = 1 if $1 eq 'utf8';
78 $::with_utf16 = 1 if $1 eq 'utf16';
79 $::taintwarn = 1 if $1 eq 'taintwarn';
80 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
81 if ($1 =~ /^deparse(,.+)?$/) {
89 chdir 't' if -f 't/TEST';
91 die "You need to run \"make test\" first to set things up.\n"
92 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
94 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
95 unless (-x 'perl.third') {
96 unless (-x '../perl.third') {
97 die "You need to run \"make perl.third first.\n";
100 print "Symlinking ../perl.third as perl.third...\n";
101 die "Failed to symlink: $!\n"
102 unless symlink("../perl.third", "perl.third");
103 die "Symlinked but no executable perl.third: $!\n"
104 unless -x 'perl.third';
109 # check leakage for embedders
110 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
112 $ENV{EMXSHELL} = 'sh'; # For OS/2
114 if ($show_elapsed_time) { require Time::HiRes }
125 # Roll your own File::Find!
128 opendir DIR, $dir or die "Trouble opening $dir: $!";
129 foreach my $f (sort { $a cmp $b } readdir DIR) {
132 my $fullpath = "$dir/$f";
135 _find_tests($fullpath);
136 } elsif ($f =~ /\.t$/) {
137 push @ARGV, $fullpath;
143 # Scan the text of the test program to find switches and special options
144 # we might need to apply.
146 my($test, $type) = @_;
148 open(my $script, "<", $test) or die "Can't read $test.\n";
149 my $first_line = <$script>;
151 $first_line =~ tr/\0//d if $::with_utf16;
154 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
158 # not all tests are expected to pass with this option
166 if ($type eq 'deparse') {
167 # Look for #line directives which change the filename
169 $file_opts .= ",-f$3$4"
170 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
174 return { file => $file_opts, switch => $switch };
178 my($harness, $test, $type) = @_;
179 if (!defined $type) {
180 # To conform to the interface expected by exec in TAP::Harness
184 my $options = _scan_test($test, $type);
189 $test =~ /^(.+)\/[^\/]+/;
193 my $testswitch = $dir_to_switch{$dir};
194 if (!defined $testswitch) {
195 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
197 $return_dir = '../../t';
199 $perl = '../../t/perl';
200 $testswitch = "-I../.. -MTestInit=U2T,A";
201 if ($temp_no_core{$ext_dir}) {
202 $testswitch = $testswitch . ',NC';
204 chdir $ext_dir or die "Can't chdir to '$ext_dir': $!";
206 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
210 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
213 if ($type eq 'deparse') {
215 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
216 "-l$::deparse_opts$options->{file} ".
218 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
219 open($results, $deparse_cmd)
220 or print "can't deparse '$deparse_cmd': $!.\n";
222 elsif ($type eq 'perl') {
223 my $redir = $^O eq 'VMS' ? '2>&1' : '';
225 if ($ENV{PERL_VALGRIND}) {
226 my $valgrind = $ENV{VALGRIND} // 'valgrind';
227 my $vg_opts = $ENV{VG_OPTS}
228 // "--suppressions=perl.supp --leak-check=yes "
229 . "--leak-resolution=high --show-reachable=yes "
230 . "--num-callers=50";
231 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
232 $redir = "3>$Valgrind_Log";
235 my $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
237 open($results, $run) or print "can't run '$run': $!.\n";
242 or die "Can't chdir from '$ext_dir' to '$return_dir': $!";
245 # Our environment may force us to use UTF-8, but we can't be sure that
246 # anything we're reading from will be generating (well formed) UTF-8
247 # This may not be the best way - possibly we should unset ${^OPEN} up
258 foreach (split(/\s+/,$args)) {
259 # In VMS protect with doublequotes because otherwise
260 # DCL will lowercase -- unless already doublequoted.
261 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
262 $argstring .= ' ' . $_;
268 return unless defined $_[0];
269 return map {$_, 1} split /\s+/, $_[0];
272 sub _tests_from_manifest {
273 my ($extensions, $known_extensions) = @_;
275 my %extensions = _populate_hash($extensions);
276 my %known_extensions = _populate_hash($known_extensions);
278 foreach (keys %known_extensions) {
279 $skip{$_}++ unless $extensions{$_};
283 my $mani = '../MANIFEST';
284 if (open(MANI, $mani)) {
286 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
289 if (!$::core || $t =~ m!^lib/[a-z]!) {
290 if (defined $extension) {
291 $extension =~ s!/t$!!;
292 # XXX Do I want to warn that I'm skipping these?
293 next if $skip{$extension};
294 my $flat_extension = $extension;
295 $flat_extension =~ s!-!/!g;
296 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
299 push @results, $path;
300 $::path_to_name{$path} = $t;
306 warn "$0: cannot open $mani: $!\n";
312 # base first, as TEST bails out if that can't run
313 # then comp, to validate that require works
314 # then run, to validate that -M works
315 # then we know we can -MTestInit for everything else, making life simpler
316 foreach my $dir (qw(base comp run cmd io op uni mro)) {
319 _find_tests("lib") unless $::core;
320 # Config.pm may be broken for make minitest. And this is only a refinement
321 # for skipping tests on non-default builds, so it is allowed to fail.
322 # What we want to to is make a list of extensions which we did not build.
323 my $configsh = '../config.sh';
324 my ($extensions, $known_extensions);
326 open FH, $configsh or die "Can't open $configsh: $!";
328 if (/^extensions=['"](.*)['"]$/) {
331 elsif (/^known_extensions=['"](.*)['"]$/) {
332 $known_extensions = $1;
335 if (!defined $known_extensions) {
336 warn "No known_extensions line found in $configsh";
338 if (!defined $extensions) {
339 warn "No extensions line found in $configsh";
342 # The "complex" constructions of list return from a subroutine, and push of
343 # a list, might fail if perl is really hosed, but they aren't needed for
344 # make minitest, and the building of extensions will likely also fail if
345 # something is that badly wrong.
346 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
350 _find_tests('porting');
351 _find_tests('japh') if $::torture;
352 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
357 _testprogs('deparse', '', @ARGV);
359 elsif ($::with_utf16) {
362 print STDERR "# ENDIAN $e BOM $b\n";
365 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
366 my $f = $e ? "v" : "n";
370 if (open(U, ">$u")) {
371 print U pack("$f", 0xFEFF) if $b;
373 print U pack("$f*", unpack("C*", $_));
380 _testprogs('perl', '', @UARGV);
386 _testprogs('perl', '', @ARGV);
390 my ($type, $args, @tests) = @_;
392 print <<'EOT' if ($type eq 'deparse');
393 ------------------------------------------------------------------------------
395 ------------------------------------------------------------------------------
400 foreach my $t (@tests) {
401 unless (exists $::path_to_name{$t}) {
403 $::path_to_name{$t} = $tname;
407 foreach (@::path_to_name{@tests}) {
410 $maxlen = $len if $len > $maxlen;
412 # + 3 : we want three dots between the test name and the "ok"
413 my $dotdotdot = $maxlen + 3 ;
415 my $total_files = @tests;
417 my $tested_files = 0;
421 while (my $test = shift @tests) {
422 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
427 if ($type eq 'deparse') {
428 if ($test eq "comp/redef.t") {
429 # Redefinition happens at compile time
432 elsif ($test =~ m{lib/Switch/t/}) {
433 # B::Deparse doesn't support source filtering
437 my $te = $::path_to_name{$test} . '.'
438 x ($dotdotdot - length($::path_to_name{$test}));
440 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
445 my $results = _run_test(undef, $test, $type);
451 my $trailing_leader = 0;
455 next if /^\s*$/; # skip blank lines
456 if (/^1..$/ && ($^O eq 'VMS')) {
457 # VMS pipe bug inserts blank lines.
459 if ($l2 =~ /^\s*$/) {
468 if ($trailing_leader) {
469 # shouldn't be anything following a postfix 1..n
470 $failure = 'FAILED--extra output after trailing 1..n';
473 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
475 $failure = 'FAILED--seen duplicate leader';
479 %todo = map { $_ => 1 } split / /, $3 if $3;
483 # 1..n appears at end of file
484 $trailing_leader = 1;
486 $failure = "FAILED--expected $max tests, saw $next";
496 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
497 unless ($seen_leader) {
504 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
505 $num = $next unless $num;
509 # SKIP is essentially the same as TODO for t/TEST
510 # this still conforms to TAP:
511 # http://search.cpan.org/dist/TAP/TAP.pod
512 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
513 $istodo = 1 if $todo{$num};
515 if( $not && !$istodo ) {
516 $failure = "FAILED at test $num";
521 $failure ="FAILED--expected test $next, saw test $num";
525 elsif (/^Bail out!\s*(.*)/i) { # magic words
526 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
529 # module tests are allowed extra output,
530 # because Test::Harness allows it
531 next if $test =~ /^\W*(ext|lib)\b/;
532 $failure = "FAILED--unexpected output at test $next";
540 if (not defined $failure) {
541 $failure = 'FAILED--no leader found' unless $seen_leader;
544 if ($ENV{PERL_VALGRIND}) {
546 if (-e $Valgrind_Log) {
547 if (open(V, $Valgrind_Log)) {
551 warn "$0: Failed to open '$Valgrind_Log': $!\n";
554 if ($ENV{VG_OPTS} =~ /cachegrind/) {
555 if (rename $Valgrind_Log, "$test.valgrind") {
558 warn "$0: Failed to create '$test.valgrind': $!\n";
564 for my $i (0..$#valgrind) {
565 local $_ = $valgrind[$i];
566 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
567 $errors += $1; # there may be multiple error summaries
568 } elsif (/^==\d+== LEAK SUMMARY:/) {
569 for my $off (1 .. 4) {
570 if ($valgrind[$i+$off] =~
571 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
577 if ($errors or $leaks) {
578 if (rename $Valgrind_Log, "$test.valgrind") {
581 warn "$0: Failed to create '$test.valgrind': $!\n";
585 warn "No valgrind output?\n";
587 if (-e $Valgrind_Log) {
589 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
592 if ($type eq 'deparse') {
595 if ($ENV{PERL_3LOG}) {
599 $tpp =~ s:\.t$:.3log:;
600 rename("perl.3log", $tpp) ||
601 die "rename: perl3.log to $tpp: $!\n";
603 if (not defined $failure and $next != $max) {
604 $failure="FAILED--expected $max tests, saw $next";
607 if( !defined $failure # don't mask a test failure
610 $failure = "FAILED--non-zero wait status: $?";
613 if (defined $failure) {
614 print "${te}$failure\n";
616 if ($test =~ /^base/) {
617 die "Failed a basic test ($test) -- cannot continue.\n";
619 ++$failed_tests{$test};
624 if ( $show_elapsed_time ) {
625 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
630 print "${te}ok$elapsed\n";
634 print "${te}skipped\n";
640 if ($::bad_files == 0) {
642 print "All tests successful.\n";
643 # XXX add mention of 'perlbug -ok' ?
646 die "FAILED--no tests were run for some reason.\n";
650 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
651 my $s = $::bad_files == 1 ? "" : "s";
652 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
653 for my $test ( sort keys %failed_tests ) {
657 ### Since not all tests were successful, you may want to run some of
658 ### them individually and examine any diagnostic messages they produce.
659 ### See the INSTALL document's section on "make test".
661 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
662 ### You have a good chance to get more information by running
664 ### in the 't' directory since most (>=80%) of the tests succeeded.
666 if (eval {require Config; import Config; 1}) {
667 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
669 ### You may have to set your dynamic library search path,
670 ### $p, to point to the build directory:
672 if (exists $ENV{$p} && $ENV{$p} ne '') {
674 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
675 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
676 ### export $p=`pwd`:\$$p; cd t; ./perl harness
680 ### setenv $p `pwd`; cd t; ./perl harness
681 ### $p=`pwd`; export $p; cd t; ./perl harness
682 ### export $p=`pwd`; cd t; ./perl harness
686 ### for csh-style shells, like tcsh; or for traditional/modern
687 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
692 my ($user,$sys,$cuser,$csys) = times;
693 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
694 $user,$sys,$cuser,$csys,$tested_files,$totmax);
695 if ($ENV{PERL_VALGRIND}) {
696 my $s = $valgrind == 1 ? '' : 's';
697 print "$valgrind valgrind report$s created.\n", ;
700 exit ($::bad_files != 0);
702 # ex: set ts=8 sts=4 sw=4 noet: