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,
54 # Location to put the Valgrind log.
55 my $Valgrind_Log = 'current.valgrind';
59 # for testing TEST only
60 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
61 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
63 delete $ENV{PERL5LIB};
65 delete $ENV{PERL5OPT};
67 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
68 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
69 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
71 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
74 foreach my $idx (0..$#ARGV) {
75 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
76 $::benchmark = 1 if $1 eq 'benchmark';
77 $::core = 1 if $1 eq 'core';
78 $::verbose = 1 if $1 eq 'v';
79 $::torture = 1 if $1 eq 'torture';
80 $::with_utf8 = 1 if $1 eq 'utf8';
81 $::with_utf16 = 1 if $1 eq 'utf16';
82 $::taintwarn = 1 if $1 eq 'taintwarn';
83 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
84 if ($1 =~ /^deparse(,.+)?$/) {
92 chdir 't' if -f 't/TEST';
94 die "You need to run \"make test\" first to set things up.\n"
95 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
97 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
98 unless (-x 'perl.third') {
99 unless (-x '../perl.third') {
100 die "You need to run \"make perl.third first.\n";
103 print "Symlinking ../perl.third as perl.third...\n";
104 die "Failed to symlink: $!\n"
105 unless symlink("../perl.third", "perl.third");
106 die "Symlinked but no executable perl.third: $!\n"
107 unless -x 'perl.third';
112 # check leakage for embedders
113 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
115 $ENV{EMXSHELL} = 'sh'; # For OS/2
117 # Roll your own File::Find!
118 if ($show_elapsed_time) { require Time::HiRes }
131 opendir DIR, $dir or die "Trouble opening $dir: $!";
132 foreach my $f (sort { $a cmp $b } readdir DIR) {
135 my $fullpath = "$dir/$f";
138 _find_tests($fullpath);
139 } elsif ($f =~ /\.t$/) {
140 push @ARGV, $fullpath;
146 # Scan the text of the test program to find switches and special options
147 # we might need to apply.
149 my($test, $type) = @_;
151 open(my $script, "<", $test) or die "Can't read $test.\n";
152 my $first_line = <$script>;
154 $first_line =~ tr/\0//d if $::with_utf16;
157 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
161 # not all tests are expected to pass with this option
169 if ($type eq 'deparse') {
170 # Look for #line directives which change the filename
172 $file_opts .= ",-f$3$4"
173 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
177 return { file => $file_opts, switch => $switch };
181 my($harness, $test, $type) = @_;
182 if (!defined $type) {
183 # To conform to the interface expected by exec in TAP::Harness
187 my $options = _scan_test($test, $type);
192 $test =~ /^(.+)\/[^\/]+/;
196 my $testswitch = $dir_to_switch{$dir};
197 if (!defined $testswitch) {
198 if ($test =~ s!^(\.\./ext/[^/]+)/t!t!) {
200 $return_dir = '../../t';
202 $perl = '../../t/perl';
203 $testswitch = "-I$return_dir -MTestInit=U2T,A";
204 if ($temp_no_core{$ext_dir}) {
205 $testswitch = $testswitch . ',NC';
207 chdir $ext_dir or die "Can't chdir to '$ext_dir': $!";
209 $testswitch = '-I. -MTestInit'; # -T will remove . from @INC
213 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
216 if ($type eq 'deparse') {
218 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
219 "-l$::deparse_opts$options->{file} ".
221 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
222 open($results, $deparse_cmd)
223 or print "can't deparse '$deparse_cmd': $!.\n";
225 elsif ($type eq '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 $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
240 open($results, $run) or print "can't run '$run': $!.\n";
245 or die "Can't chdir from '$ext_dir' to '$return_dir': $!";
248 # Our environment may force us to use UTF-8, but we can't be sure that
249 # anything we're reading from will be generating (well formed) UTF-8
250 # This may not be the best way - possibly we should unset ${^OPEN} up
261 foreach (split(/\s+/,$args)) {
262 # In VMS protect with doublequotes because otherwise
263 # DCL will lowercase -- unless already doublequoted.
264 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
265 $argstring .= ' ' . $_;
271 return unless defined $_[0];
272 return map {$_, 1} split /\s+/, $_[0];
275 sub _tests_from_manifest {
276 my ($extensions, $known_extensions) = @_;
278 my %extensions = _populate_hash($extensions);
279 my %known_extensions = _populate_hash($known_extensions);
281 foreach (keys %known_extensions) {
282 $skip{$_}++ unless $extensions{$_};
286 my $mani = '../MANIFEST';
287 if (open(MANI, $mani)) {
288 while (<MANI>) { # similar code in t/harness
289 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
292 if (!$::core || $t =~ m!^lib/[a-z]!) {
293 if (defined $extension) {
294 $extension =~ s!/t$!!;
295 # XXX Do I want to warn that I'm skipping these?
296 next if $skip{$extension};
297 my $flat_extension = $extension;
298 $flat_extension =~ s!-!/!g;
299 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
302 push @results, $path;
303 $::path_to_name{$path} = $t;
309 warn "$0: cannot open $mani: $!\n";
315 # base first, as TEST bails out if that can't run
316 # then comp, to validate that require works
317 # then run, to validate that -M works
318 # then we know we can -MTestInit for everything else, making life simpler
319 foreach my $dir (qw(base comp run cmd io op uni mro)) {
322 _find_tests("lib") unless $::core;
323 # Config.pm may be broken for make minitest. And this is only a refinement
324 # for skipping tests on non-default builds, so it is allowed to fail.
325 # What we want to to is make a list of extensions which we did not build.
326 my $configsh = '../config.sh';
327 my ($extensions, $known_extensions);
329 open FH, $configsh or die "Can't open $configsh: $!";
331 if (/^extensions=['"](.*)['"]$/) {
334 elsif (/^known_extensions=['"](.*)['"]$/) {
335 $known_extensions = $1;
338 if (!defined $known_extensions) {
339 warn "No known_extensions line found in $configsh";
341 if (!defined $extensions) {
342 warn "No extensions line found in $configsh";
345 # The "complex" constructions of list return from a subroutine, and push of
346 # a list, might fail if perl is really hosed, but they aren't needed for
347 # make minitest, and the building of extensions will likely also fail if
348 # something is that badly wrong.
349 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
353 _find_tests('porting');
354 _find_tests('japh') if $::torture;
355 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
360 _testprogs('deparse', '', @ARGV);
362 elsif ($::with_utf16) {
365 print STDERR "# ENDIAN $e BOM $b\n";
368 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
369 my $f = $e ? "v" : "n";
373 if (open(U, ">$u")) {
374 print U pack("$f", 0xFEFF) if $b;
376 print U pack("$f*", unpack("C*", $_));
383 _testprogs('perl', '', @UARGV);
389 _testprogs('perl', '', @ARGV);
393 my ($type, $args, @tests) = @_;
395 print <<'EOT' if ($type eq 'deparse');
396 ------------------------------------------------------------------------------
398 ------------------------------------------------------------------------------
403 foreach my $t (@tests) {
404 unless (exists $::path_to_name{$t}) {
406 $::path_to_name{$t} = $tname;
410 foreach (@::path_to_name{@tests}) {
413 $maxlen = $len if $len > $maxlen;
415 # + 3 : we want three dots between the test name and the "ok"
416 my $dotdotdot = $maxlen + 3 ;
418 my $total_files = @tests;
420 my $tested_files = 0;
424 while (my $test = shift @tests) {
425 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
430 if ($type eq 'deparse') {
431 if ($test eq "comp/redef.t") {
432 # Redefinition happens at compile time
435 elsif ($test =~ m{lib/Switch/t/}) {
436 # B::Deparse doesn't support source filtering
440 my $te = $::path_to_name{$test} . '.'
441 x ($dotdotdot - length($::path_to_name{$test}));
443 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
448 my $results = _run_test(undef, $test, $type);
454 my $trailing_leader = 0;
458 next if /^\s*$/; # skip blank lines
459 if (/^1..$/ && ($^O eq 'VMS')) {
460 # VMS pipe bug inserts blank lines.
462 if ($l2 =~ /^\s*$/) {
471 if ($trailing_leader) {
472 # shouldn't be anything following a postfix 1..n
473 $failure = 'FAILED--extra output after trailing 1..n';
476 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
478 $failure = 'FAILED--seen duplicate leader';
482 %todo = map { $_ => 1 } split / /, $3 if $3;
486 # 1..n appears at end of file
487 $trailing_leader = 1;
489 $failure = "FAILED--expected $max tests, saw $next";
499 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
500 unless ($seen_leader) {
507 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
508 $num = $next unless $num;
512 # SKIP is essentially the same as TODO for t/TEST
513 # this still conforms to TAP:
514 # http://search.cpan.org/dist/TAP/TAP.pod
515 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
516 $istodo = 1 if $todo{$num};
518 if( $not && !$istodo ) {
519 $failure = "FAILED at test $num";
524 $failure ="FAILED--expected test $next, saw test $num";
528 elsif (/^Bail out!\s*(.*)/i) { # magic words
529 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
532 # module tests are allowed extra output,
533 # because Test::Harness allows it
534 next if $test =~ /^\W*(ext|lib)\b/;
535 $failure = "FAILED--unexpected output at test $next";
543 if (not defined $failure) {
544 $failure = 'FAILED--no leader found' unless $seen_leader;
547 if ($ENV{PERL_VALGRIND}) {
549 if (-e $Valgrind_Log) {
550 if (open(V, $Valgrind_Log)) {
554 warn "$0: Failed to open '$Valgrind_Log': $!\n";
557 if ($ENV{VG_OPTS} =~ /cachegrind/) {
558 if (rename $Valgrind_Log, "$test.valgrind") {
561 warn "$0: Failed to create '$test.valgrind': $!\n";
567 for my $i (0..$#valgrind) {
568 local $_ = $valgrind[$i];
569 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
570 $errors += $1; # there may be multiple error summaries
571 } elsif (/^==\d+== LEAK SUMMARY:/) {
572 for my $off (1 .. 4) {
573 if ($valgrind[$i+$off] =~
574 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
580 if ($errors or $leaks) {
581 if (rename $Valgrind_Log, "$test.valgrind") {
584 warn "$0: Failed to create '$test.valgrind': $!\n";
588 warn "No valgrind output?\n";
590 if (-e $Valgrind_Log) {
592 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
595 if ($type eq 'deparse') {
598 if ($ENV{PERL_3LOG}) {
602 $tpp =~ s:\.t$:.3log:;
603 rename("perl.3log", $tpp) ||
604 die "rename: perl3.log to $tpp: $!\n";
606 if (not defined $failure and $next != $max) {
607 $failure="FAILED--expected $max tests, saw $next";
610 if( !defined $failure # don't mask a test failure
613 $failure = "FAILED--non-zero wait status: $?";
616 if (defined $failure) {
617 print "${te}$failure\n";
619 if ($test =~ /^base/) {
620 die "Failed a basic test ($test) -- cannot continue.\n";
622 ++$failed_tests{$test};
627 if ( $show_elapsed_time ) {
628 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
633 print "${te}ok$elapsed\n";
637 print "${te}skipped\n";
643 if ($::bad_files == 0) {
645 print "All tests successful.\n";
646 # XXX add mention of 'perlbug -ok' ?
649 die "FAILED--no tests were run for some reason.\n";
653 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
654 my $s = $::bad_files == 1 ? "" : "s";
655 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
656 for my $test ( sort keys %failed_tests ) {
660 ### Since not all tests were successful, you may want to run some of
661 ### them individually and examine any diagnostic messages they produce.
662 ### See the INSTALL document's section on "make test".
664 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
665 ### You have a good chance to get more information by running
667 ### in the 't' directory since most (>=80%) of the tests succeeded.
669 if (eval {require Config; import Config; 1}) {
670 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
672 ### You may have to set your dynamic library search path,
673 ### $p, to point to the build directory:
675 if (exists $ENV{$p} && $ENV{$p} ne '') {
677 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
678 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
679 ### export $p=`pwd`:\$$p; cd t; ./perl harness
683 ### setenv $p `pwd`; cd t; ./perl harness
684 ### $p=`pwd`; export $p; cd t; ./perl harness
685 ### export $p=`pwd`; cd t; ./perl harness
689 ### for csh-style shells, like tcsh; or for traditional/modern
690 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
695 my ($user,$sys,$cuser,$csys) = times;
696 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
697 $user,$sys,$cuser,$csys,$tested_files,$totmax);
698 if ($ENV{PERL_VALGRIND}) {
699 my $s = $valgrind == 1 ? '' : 's';
700 print "$valgrind valgrind report$s created.\n", ;
703 exit ($::bad_files != 0);
705 # ex: set ts=8 sts=4 sw=4 noet: