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.
21 # Location to put the Valgrind log.
22 my $Valgrind_Log = 'current.valgrind';
26 # for testing TEST only
27 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
28 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
30 delete $ENV{PERL5LIB};
32 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
33 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
34 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
36 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
39 foreach my $idx (0..$#ARGV) {
40 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
41 $::benchmark = 1 if $1 eq 'benchmark';
42 $::core = 1 if $1 eq 'core';
43 $::verbose = 1 if $1 eq 'v';
44 $::torture = 1 if $1 eq 'torture';
45 $::with_utf8 = 1 if $1 eq 'utf8';
46 $::with_utf16 = 1 if $1 eq 'utf16';
47 $::taintwarn = 1 if $1 eq 'taintwarn';
48 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
49 if ($1 =~ /^deparse(,.+)?$/) {
57 chdir 't' if -f 't/TEST';
59 die "You need to run \"make test\" first to set things up.\n"
60 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
62 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
63 unless (-x 'perl.third') {
64 unless (-x '../perl.third') {
65 die "You need to run \"make perl.third first.\n";
68 print "Symlinking ../perl.third as perl.third...\n";
69 die "Failed to symlink: $!\n"
70 unless symlink("../perl.third", "perl.third");
71 die "Symlinked but no executable perl.third: $!\n"
72 unless -x 'perl.third';
77 # check leakage for embedders
78 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
80 $ENV{EMXSHELL} = 'sh'; # For OS/2
82 # Roll your own File::Find!
83 if ($show_elapsed_time) { require Time::HiRes }
96 opendir DIR, $dir or die "Trouble opening $dir: $!";
97 foreach my $f (sort { $a cmp $b } readdir DIR) {
100 my $fullpath = "$dir/$f";
103 _find_tests($fullpath);
104 } elsif ($f =~ /\.t$/) {
105 push @ARGV, $fullpath;
111 # Scan the text of the test program to find switches and special options
112 # we might need to apply.
114 my($test, $type) = @_;
116 open(my $script, "<", $test) or die "Can't read $test.\n";
117 my $first_line = <$script>;
119 $first_line =~ tr/\0//d if $::with_utf16;
122 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
126 # not all tests are expected to pass with this option
134 if ($type eq 'deparse') {
135 # Look for #line directives which change the filename
137 $file_opts .= ",-f$3$4"
138 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
142 return { file => $file_opts, switch => $switch };
145 # directories with special sets of test switches
153 my($harness, $test, $type) = @_;
154 if (!defined $type) {
155 # To conform to the interface expected by exec in TAP::Harness
159 my $options = _scan_test($test, $type);
163 $test =~ /^([^\/]+)/;
165 my $testswitch = $dir_to_switch{$1};
166 if (!defined $testswitch) {
167 $testswitch = '-I. -MTestInit'; # -T will remove . from @INC
170 my $utf8 = $::with_utf8 ? '-I$lib -Mutf8' : '';
173 if ($type eq 'deparse') {
175 "$perl $testswitch $options->{switch} -I$lib -MO=-qq,Deparse,-sv1.,".
176 "-l$::deparse_opts$options->{file} ".
178 "&& $perl $testswitch $options->{switch} -I$lib $test.dp |";
179 open($results, $deparse_cmd)
180 or print "can't deparse '$deparse_cmd': $!.\n";
182 elsif ($type eq 'perl') {
183 my $perl = $ENV{PERL} || $perl;
184 my $redir = $^O eq 'VMS' ? '2>&1' : '';
186 if ($ENV{PERL_VALGRIND}) {
187 my $valgrind = $ENV{VALGRIND} // 'valgrind';
188 my $vg_opts = $ENV{VG_OPTS}
189 // "--suppressions=perl.supp --leak-check=yes "
190 . "--leak-resolution=high --show-reachable=yes "
191 . "--num-callers=50";
192 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
193 $redir = "3>$Valgrind_Log";
196 my $run = $perl . _quote_args("$testswitch $options->{switch} $utf8")
198 open($results, $run) or print "can't run '$run': $!.\n";
201 # Our environment may force us to use UTF-8, but we can't be sure that
202 # anything we're reading from will be generating (well formed) UTF-8
203 # This may not be the best way - possibly we should unset ${^OPEN} up
214 foreach (split(/\s+/,$args)) {
215 # In VMS protect with doublequotes because otherwise
216 # DCL will lowercase -- unless already doublequoted.
217 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
218 $argstring .= ' ' . $_;
224 return unless defined $_[0];
225 return map {$_, 1} split /\s+/, $_[0];
228 sub _tests_from_manifest {
229 my ($extensions, $known_extensions) = @_;
231 my %extensions = _populate_hash($extensions);
232 my %known_extensions = _populate_hash($known_extensions);
234 foreach (keys %known_extensions) {
235 $skip{$_}++ unless $extensions{$_};
239 my $mani = '../MANIFEST';
240 if (open(MANI, $mani)) {
241 while (<MANI>) { # similar code in t/harness
242 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
245 if (!$::core || $t =~ m!^lib/[a-z]!) {
246 if (defined $extension) {
247 $extension =~ s!/t$!!;
248 # XXX Do I want to warn that I'm skipping these?
249 next if $skip{$extension};
250 my $flat_extension = $extension;
251 $flat_extension =~ s!-!/!g;
252 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
255 push @results, $path;
256 $::path_to_name{$path} = $t;
262 warn "$0: cannot open $mani: $!\n";
268 # base first, as TEST bails out if that can't run
269 # then comp, to validate that require works
270 # then run, to validate that -M works
271 # then we know we can -MTestInit for everything else, making life simpler
272 foreach my $dir (qw(base comp run cmd io op uni mro)) {
275 _find_tests("lib") unless $::core;
276 # Config.pm may be broken for make minitest. And this is only a refinement
277 # for skipping tests on non-default builds, so it is allowed to fail.
278 # What we want to to is make a list of extensions which we did not build.
279 my $configsh = '../config.sh';
280 my ($extensions, $known_extensions);
282 open FH, $configsh or die "Can't open $configsh: $!";
284 if (/^extensions=['"](.*)['"]$/) {
287 elsif (/^known_extensions=['"](.*)['"]$/) {
288 $known_extensions = $1;
291 if (!defined $known_extensions) {
292 warn "No known_extensions line found in $configsh";
294 if (!defined $extensions) {
295 warn "No extensions line found in $configsh";
298 # The "complex" constructions of list return from a subroutine, and push of
299 # a list, might fail if perl is really hosed, but they aren't needed for
300 # make minitest, and the building of extensions will likely also fail if
301 # something is that badly wrong.
302 push @ARGV, _tests_from_manifest($extensions, $known_extensions);
306 _find_tests('porting');
307 _find_tests('japh') if $::torture;
308 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
313 _testprogs('deparse', '', @ARGV);
315 elsif ($::with_utf16) {
318 print STDERR "# ENDIAN $e BOM $b\n";
321 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
322 my $f = $e ? "v" : "n";
326 if (open(U, ">$u")) {
327 print U pack("$f", 0xFEFF) if $b;
329 print U pack("$f*", unpack("C*", $_));
336 _testprogs('perl', '', @UARGV);
342 _testprogs('perl', '', @ARGV);
346 my ($type, $args, @tests) = @_;
348 print <<'EOT' if ($type eq 'deparse');
349 ------------------------------------------------------------------------------
351 ------------------------------------------------------------------------------
356 foreach my $t (@tests) {
357 unless (exists $::path_to_name{$t}) {
359 $::path_to_name{$t} = $tname;
363 foreach (@::path_to_name{@tests}) {
366 $maxlen = $len if $len > $maxlen;
368 # + 3 : we want three dots between the test name and the "ok"
369 my $dotdotdot = $maxlen + 3 ;
371 my $total_files = @tests;
373 my $tested_files = 0;
377 while (my $test = shift @tests) {
378 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
383 if ($type eq 'deparse') {
384 if ($test eq "comp/redef.t") {
385 # Redefinition happens at compile time
388 elsif ($test =~ m{lib/Switch/t/}) {
389 # B::Deparse doesn't support source filtering
393 my $te = $::path_to_name{$test} . '.'
394 x ($dotdotdot - length($::path_to_name{$test}));
396 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
401 my $results = _run_test(undef, $test, $type);
407 my $trailing_leader = 0;
411 next if /^\s*$/; # skip blank lines
412 if (/^1..$/ && ($^O eq 'VMS')) {
413 # VMS pipe bug inserts blank lines.
415 if ($l2 =~ /^\s*$/) {
424 if ($trailing_leader) {
425 # shouldn't be anything following a postfix 1..n
426 $failure = 'FAILED--extra output after trailing 1..n';
429 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
431 $failure = 'FAILED--seen duplicate leader';
435 %todo = map { $_ => 1 } split / /, $3 if $3;
439 # 1..n appears at end of file
440 $trailing_leader = 1;
442 $failure = "FAILED--expected $max tests, saw $next";
452 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
453 unless ($seen_leader) {
460 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
461 $num = $next unless $num;
465 # SKIP is essentially the same as TODO for t/TEST
466 # this still conforms to TAP:
467 # http://search.cpan.org/dist/TAP/TAP.pod
468 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
469 $istodo = 1 if $todo{$num};
471 if( $not && !$istodo ) {
472 $failure = "FAILED at test $num";
477 $failure ="FAILED--expected test $next, saw test $num";
481 elsif (/^Bail out!\s*(.*)/i) { # magic words
482 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
485 # module tests are allowed extra output,
486 # because Test::Harness allows it
487 next if $test =~ /^\W*(ext|lib)\b/;
488 $failure = "FAILED--unexpected output at test $next";
496 if (not defined $failure) {
497 $failure = 'FAILED--no leader found' unless $seen_leader;
500 if ($ENV{PERL_VALGRIND}) {
502 if (-e $Valgrind_Log) {
503 if (open(V, $Valgrind_Log)) {
507 warn "$0: Failed to open '$Valgrind_Log': $!\n";
510 if ($ENV{VG_OPTS} =~ /cachegrind/) {
511 if (rename $Valgrind_Log, "$test.valgrind") {
514 warn "$0: Failed to create '$test.valgrind': $!\n";
520 for my $i (0..$#valgrind) {
521 local $_ = $valgrind[$i];
522 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
523 $errors += $1; # there may be multiple error summaries
524 } elsif (/^==\d+== LEAK SUMMARY:/) {
525 for my $off (1 .. 4) {
526 if ($valgrind[$i+$off] =~
527 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
533 if ($errors or $leaks) {
534 if (rename $Valgrind_Log, "$test.valgrind") {
537 warn "$0: Failed to create '$test.valgrind': $!\n";
541 warn "No valgrind output?\n";
543 if (-e $Valgrind_Log) {
545 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
548 if ($type eq 'deparse') {
551 if ($ENV{PERL_3LOG}) {
555 $tpp =~ s:\.t$:.3log:;
556 rename("perl.3log", $tpp) ||
557 die "rename: perl3.log to $tpp: $!\n";
559 if (not defined $failure and $next != $max) {
560 $failure="FAILED--expected $max tests, saw $next";
563 if( !defined $failure # don't mask a test failure
566 $failure = "FAILED--non-zero wait status: $?";
569 if (defined $failure) {
570 print "${te}$failure\n";
572 if ($test =~ /^base/) {
573 die "Failed a basic test ($test) -- cannot continue.\n";
575 ++$failed_tests{$test};
580 if ( $show_elapsed_time ) {
581 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
586 print "${te}ok$elapsed\n";
590 print "${te}skipped\n";
596 if ($::bad_files == 0) {
598 print "All tests successful.\n";
599 # XXX add mention of 'perlbug -ok' ?
602 die "FAILED--no tests were run for some reason.\n";
606 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
607 my $s = $::bad_files == 1 ? "" : "s";
608 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
609 for my $test ( sort keys %failed_tests ) {
613 ### Since not all tests were successful, you may want to run some of
614 ### them individually and examine any diagnostic messages they produce.
615 ### See the INSTALL document's section on "make test".
617 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
618 ### You have a good chance to get more information by running
620 ### in the 't' directory since most (>=80%) of the tests succeeded.
622 if (eval {require Config; import Config; 1}) {
623 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
625 ### You may have to set your dynamic library search path,
626 ### $p, to point to the build directory:
628 if (exists $ENV{$p} && $ENV{$p} ne '') {
630 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
631 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
632 ### export $p=`pwd`:\$$p; cd t; ./perl harness
636 ### setenv $p `pwd`; cd t; ./perl harness
637 ### $p=`pwd`; export $p; cd t; ./perl harness
638 ### export $p=`pwd`; cd t; ./perl harness
642 ### for csh-style shells, like tcsh; or for traditional/modern
643 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
648 my ($user,$sys,$cuser,$csys) = times;
649 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
650 $user,$sys,$cuser,$csys,$tested_files,$totmax);
651 if ($ENV{PERL_VALGRIND}) {
652 my $s = $valgrind == 1 ? '' : 's';
653 print "$valgrind valgrind report$s created.\n", ;
656 exit ($::bad_files != 0);
658 # ex: set ts=8 sts=4 sw=4 noet: