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/Devel-PPPort' => 1,
33 '../ext/Hash-Util' => 'fixme',
34 '../ext/Hash-Util-FieldHash' => 'fixme',
35 '../ext/I18N-Langinfo' => 'fixme',
36 '../ext/IO-Compress' => 1,
37 '../ext/IPC-SysV' => 1,
38 '../ext/MIME-Base64' => 1,
39 '../ext/Safe' => 'fixme',
40 '../ext/Time-HiRes' => 1,
41 '../ext/Unicode-Normalize' => 1,
48 # Location to put the Valgrind log.
49 my $Valgrind_Log = 'current.valgrind';
53 # for testing TEST only
54 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
55 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
57 delete $ENV{PERL5LIB};
59 delete $ENV{PERL5OPT};
61 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
62 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
63 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
65 # Cheesy version of Getopt::Std. We can't replace it with that, because we
66 # can't rely on require working.
69 foreach my $idx (0..$#ARGV) {
70 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
71 $::benchmark = 1 if $1 eq 'benchmark';
72 $::core = 1 if $1 eq 'core';
73 $::verbose = 1 if $1 eq 'v';
74 $::torture = 1 if $1 eq 'torture';
75 $::with_utf8 = 1 if $1 eq 'utf8';
76 $::with_utf16 = 1 if $1 eq 'utf16';
77 $::taintwarn = 1 if $1 eq 'taintwarn';
78 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
79 if ($1 =~ /^deparse(,.+)?$/) {
87 chdir 't' if -f 't/TEST';
89 die "You need to run \"make test\" first to set things up.\n"
90 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
92 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
93 unless (-x 'perl.third') {
94 unless (-x '../perl.third') {
95 die "You need to run \"make perl.third first.\n";
98 print "Symlinking ../perl.third as perl.third...\n";
99 die "Failed to symlink: $!\n"
100 unless symlink("../perl.third", "perl.third");
101 die "Symlinked but no executable perl.third: $!\n"
102 unless -x 'perl.third';
107 # check leakage for embedders
108 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
110 $ENV{EMXSHELL} = 'sh'; # For OS/2
112 if ($show_elapsed_time) { require Time::HiRes }
123 # Roll your own File::Find!
126 opendir DIR, $dir or die "Trouble opening $dir: $!";
127 foreach my $f (sort { $a cmp $b } readdir DIR) {
130 my $fullpath = "$dir/$f";
133 _find_tests($fullpath);
134 } elsif ($f =~ /\.t$/) {
135 push @ARGV, $fullpath;
141 # Scan the text of the test program to find switches and special options
142 # we might need to apply.
144 my($test, $type) = @_;
146 open(my $script, "<", $test) or die "Can't read $test.\n";
147 my $first_line = <$script>;
149 $first_line =~ tr/\0//d if $::with_utf16;
152 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
156 # not all tests are expected to pass with this option
164 if ($type eq 'deparse') {
165 # Look for #line directives which change the filename
167 $file_opts .= ",-f$3$4"
168 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
172 return { file => $file_opts, switch => $switch };
176 my($harness, $test, $type) = @_;
177 if (!defined $type) {
178 # To conform to the interface expected by exec in TAP::Harness
182 my $options = _scan_test($test, $type);
187 $test =~ /^(.+)\/[^\/]+/;
191 my $testswitch = $dir_to_switch{$dir};
192 if (!defined $testswitch) {
193 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
195 $return_dir = '../../t';
197 $perl = '../../t/perl';
198 $testswitch = "-I../.. -MTestInit=U2T,A";
199 if ($temp_no_core{$ext_dir}) {
200 $testswitch = $testswitch . ',NC';
202 chdir $ext_dir or die "Can't chdir to '$ext_dir': $!";
204 $testswitch = '-I.. -MTestInit'; # -T will remove . from @INC
208 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
211 if ($type eq 'deparse') {
213 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
214 "-l$::deparse_opts$options->{file} ".
216 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
217 open($results, $deparse_cmd)
218 or print "can't deparse '$deparse_cmd': $!.\n";
220 elsif ($type eq 'perl') {
221 my $redir = $^O eq 'VMS' ? '2>&1' : '';
223 if ($ENV{PERL_VALGRIND}) {
224 my $valgrind = $ENV{VALGRIND} // 'valgrind';
225 my $vg_opts = $ENV{VG_OPTS}
226 // "--suppressions=perl.supp --leak-check=yes "
227 . "--leak-resolution=high --show-reachable=yes "
228 . "--num-callers=50";
229 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
230 $redir = "3>$Valgrind_Log";
233 my $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
235 open($results, $run) or print "can't run '$run': $!.\n";
240 or die "Can't chdir from '$ext_dir' to '$return_dir': $!";
243 # Our environment may force us to use UTF-8, but we can't be sure that
244 # anything we're reading from will be generating (well formed) UTF-8
245 # This may not be the best way - possibly we should unset ${^OPEN} up
256 foreach (split(/\s+/,$args)) {
257 # In VMS protect with doublequotes because otherwise
258 # DCL will lowercase -- unless already doublequoted.
259 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
260 $argstring .= ' ' . $_;
266 return unless defined $_[0];
267 return map {$_, 1} split /\s+/, $_[0];
270 sub _tests_from_manifest {
271 my ($extensions, $known_extensions) = @_;
273 my %extensions = _populate_hash($extensions);
274 my %known_extensions = _populate_hash($known_extensions);
276 foreach (keys %known_extensions) {
277 $skip{$_}++ unless $extensions{$_};
281 my $mani = '../MANIFEST';
282 if (open(MANI, $mani)) {
284 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
287 if (!$::core || $t =~ m!^lib/[a-z]!) {
288 if (defined $extension) {
289 $extension =~ s!/t$!!;
290 # XXX Do I want to warn that I'm skipping these?
291 next if $skip{$extension};
292 my $flat_extension = $extension;
293 $flat_extension =~ s!-!/!g;
294 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
297 push @results, $path;
298 $::path_to_name{$path} = $t;
304 warn "$0: cannot open $mani: $!\n";
310 # base first, as TEST bails out if that can't run
311 # then comp, to validate that require works
312 # then run, to validate that -M works
313 # then we know we can -MTestInit for everything else, making life simpler
314 foreach my $dir (qw(base comp run cmd io op uni mro)) {
317 _find_tests("lib") unless $::core;
318 # Config.pm may be broken for make minitest. And this is only a refinement
319 # for skipping tests on non-default builds, so it is allowed to fail.
320 # What we want to to is make a list of extensions which we did not build.
321 my $configsh = '../config.sh';
322 my ($extensions, $known_extensions);
324 open FH, $configsh or die "Can't open $configsh: $!";
326 if (/^extensions=['"](.*)['"]$/) {
329 elsif (/^known_extensions=['"](.*)['"]$/) {
330 $known_extensions = $1;
333 if (!defined $known_extensions) {
334 warn "No known_extensions line found in $configsh";
336 if (!defined $extensions) {
337 warn "No extensions line found in $configsh";
340 # The "complex" constructions of list return from a subroutine, and push of
341 # a list, might fail if perl is really hosed, but they aren't needed for
342 # make minitest, and the building of extensions will likely also fail if
343 # something is that badly wrong.
344 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
348 _find_tests('porting');
349 _find_tests('japh') if $::torture;
350 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
355 _testprogs('deparse', '', @ARGV);
357 elsif ($::with_utf16) {
360 print STDERR "# ENDIAN $e BOM $b\n";
363 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
364 my $f = $e ? "v" : "n";
368 if (open(U, ">$u")) {
369 print U pack("$f", 0xFEFF) if $b;
371 print U pack("$f*", unpack("C*", $_));
378 _testprogs('perl', '', @UARGV);
384 _testprogs('perl', '', @ARGV);
388 my ($type, $args, @tests) = @_;
390 print <<'EOT' if ($type eq 'deparse');
391 ------------------------------------------------------------------------------
393 ------------------------------------------------------------------------------
398 foreach my $t (@tests) {
399 unless (exists $::path_to_name{$t}) {
401 $::path_to_name{$t} = $tname;
405 foreach (@::path_to_name{@tests}) {
408 $maxlen = $len if $len > $maxlen;
410 # + 3 : we want three dots between the test name and the "ok"
411 my $dotdotdot = $maxlen + 3 ;
413 my $total_files = @tests;
415 my $tested_files = 0;
419 while (my $test = shift @tests) {
420 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
425 if ($type eq 'deparse') {
426 if ($test eq "comp/redef.t") {
427 # Redefinition happens at compile time
430 elsif ($test =~ m{lib/Switch/t/}) {
431 # B::Deparse doesn't support source filtering
435 my $te = $::path_to_name{$test} . '.'
436 x ($dotdotdot - length($::path_to_name{$test}));
438 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
443 my $results = _run_test(undef, $test, $type);
449 my $trailing_leader = 0;
453 next if /^\s*$/; # skip blank lines
454 if (/^1..$/ && ($^O eq 'VMS')) {
455 # VMS pipe bug inserts blank lines.
457 if ($l2 =~ /^\s*$/) {
466 if ($trailing_leader) {
467 # shouldn't be anything following a postfix 1..n
468 $failure = 'FAILED--extra output after trailing 1..n';
471 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
473 $failure = 'FAILED--seen duplicate leader';
477 %todo = map { $_ => 1 } split / /, $3 if $3;
481 # 1..n appears at end of file
482 $trailing_leader = 1;
484 $failure = "FAILED--expected $max tests, saw $next";
494 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
495 unless ($seen_leader) {
502 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
503 $num = $next unless $num;
507 # SKIP is essentially the same as TODO for t/TEST
508 # this still conforms to TAP:
509 # http://search.cpan.org/dist/TAP/TAP.pod
510 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
511 $istodo = 1 if $todo{$num};
513 if( $not && !$istodo ) {
514 $failure = "FAILED at test $num";
519 $failure ="FAILED--expected test $next, saw test $num";
523 elsif (/^Bail out!\s*(.*)/i) { # magic words
524 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
527 # module tests are allowed extra output,
528 # because Test::Harness allows it
529 next if $test =~ /^\W*(ext|lib)\b/;
530 $failure = "FAILED--unexpected output at test $next";
538 if (not defined $failure) {
539 $failure = 'FAILED--no leader found' unless $seen_leader;
542 if ($ENV{PERL_VALGRIND}) {
544 if (-e $Valgrind_Log) {
545 if (open(V, $Valgrind_Log)) {
549 warn "$0: Failed to open '$Valgrind_Log': $!\n";
552 if ($ENV{VG_OPTS} =~ /cachegrind/) {
553 if (rename $Valgrind_Log, "$test.valgrind") {
556 warn "$0: Failed to create '$test.valgrind': $!\n";
562 for my $i (0..$#valgrind) {
563 local $_ = $valgrind[$i];
564 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
565 $errors += $1; # there may be multiple error summaries
566 } elsif (/^==\d+== LEAK SUMMARY:/) {
567 for my $off (1 .. 4) {
568 if ($valgrind[$i+$off] =~
569 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
575 if ($errors or $leaks) {
576 if (rename $Valgrind_Log, "$test.valgrind") {
579 warn "$0: Failed to create '$test.valgrind': $!\n";
583 warn "No valgrind output?\n";
585 if (-e $Valgrind_Log) {
587 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
590 if ($type eq 'deparse') {
593 if ($ENV{PERL_3LOG}) {
597 $tpp =~ s:\.t$:.3log:;
598 rename("perl.3log", $tpp) ||
599 die "rename: perl3.log to $tpp: $!\n";
601 if (not defined $failure and $next != $max) {
602 $failure="FAILED--expected $max tests, saw $next";
605 if( !defined $failure # don't mask a test failure
608 $failure = "FAILED--non-zero wait status: $?";
611 if (defined $failure) {
612 print "${te}$failure\n";
614 if ($test =~ /^base/) {
615 die "Failed a basic test ($test) -- cannot continue.\n";
617 ++$failed_tests{$test};
622 if ( $show_elapsed_time ) {
623 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
628 print "${te}ok$elapsed\n";
632 print "${te}skipped\n";
638 if ($::bad_files == 0) {
640 print "All tests successful.\n";
641 # XXX add mention of 'perlbug -ok' ?
644 die "FAILED--no tests were run for some reason.\n";
648 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
649 my $s = $::bad_files == 1 ? "" : "s";
650 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
651 for my $test ( sort keys %failed_tests ) {
655 ### Since not all tests were successful, you may want to run some of
656 ### them individually and examine any diagnostic messages they produce.
657 ### See the INSTALL document's section on "make test".
659 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
660 ### You have a good chance to get more information by running
662 ### in the 't' directory since most (>=80%) of the tests succeeded.
664 if (eval {require Config; import Config; 1}) {
665 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
667 ### You may have to set your dynamic library search path,
668 ### $p, to point to the build directory:
670 if (exists $ENV{$p} && $ENV{$p} ne '') {
672 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
673 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
674 ### export $p=`pwd`:\$$p; cd t; ./perl harness
678 ### setenv $p `pwd`; cd t; ./perl harness
679 ### $p=`pwd`; export $p; cd t; ./perl harness
680 ### export $p=`pwd`; cd t; ./perl harness
684 ### for csh-style shells, like tcsh; or for traditional/modern
685 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
690 my ($user,$sys,$cuser,$csys) = times;
691 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
692 $user,$sys,$cuser,$csys,$tested_files,$totmax);
693 if ($ENV{PERL_VALGRIND}) {
694 my $s = $valgrind == 1 ? '' : 's';
695 print "$valgrind valgrind report$s created.\n", ;
698 exit ($::bad_files != 0);
700 # ex: set ts=8 sts=4 sw=4 noet: