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.)
10 # for testing TEST only
11 #BEGIN { require '../lib/strict.pm'; strict->import() };
12 #BEGIN { require '../lib/warnings.pm'; warnings->import() };
14 # Let tests know they're running in the perl core. Useful for modules
15 # which live dual lives on CPAN.
17 delete $ENV{PERL5LIB};
19 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
20 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
21 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
23 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
26 foreach my $idx (0..$#ARGV) {
27 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
28 $::benchmark = 1 if $1 eq 'benchmark';
29 $::core = 1 if $1 eq 'core';
30 $::verbose = 1 if $1 eq 'v';
31 $::torture = 1 if $1 eq 'torture';
32 $::with_utf8 = 1 if $1 eq 'utf8';
33 $::with_utf16 = 1 if $1 eq 'utf16';
34 $::taintwarn = 1 if $1 eq 'taintwarn';
35 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
36 if ($1 =~ /^deparse(,.+)?$/) {
44 chdir 't' if -f 't/TEST';
46 die "You need to run \"make test\" first to set things up.\n"
47 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
49 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
50 unless (-x 'perl.third') {
51 unless (-x '../perl.third') {
52 die "You need to run \"make perl.third first.\n";
55 print "Symlinking ../perl.third as perl.third...\n";
56 die "Failed to symlink: $!\n"
57 unless symlink("../perl.third", "perl.third");
58 die "Symlinked but no executable perl.third: $!\n"
59 unless -x 'perl.third';
64 # check leakage for embedders
65 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
67 $ENV{EMXSHELL} = 'sh'; # For OS/2
69 # Roll your own File::Find!
71 if ($show_elapsed_time) { require Time::HiRes }
84 opendir DIR, $dir or die "Trouble opening $dir: $!";
85 foreach my $f (sort { $a cmp $b } readdir DIR) {
88 my $fullpath = "$dir/$f";
91 _find_tests($fullpath);
92 } elsif ($f =~ /\.t$/) {
93 push @ARGV, $fullpath;
102 foreach (split(/\s+/,$args)) {
103 # In VMS protect with doublequotes because otherwise
104 # DCL will lowercase -- unless already doublequoted.
105 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
106 $argstring .= ' ' . $_;
112 return map {$_, 1} split /\s+/, $_[0];
116 # base first, as TEST bails out if that can't run
117 # then comp, to validate that require works
118 # then run, to validate that -M works
119 # then we know we can -MTestInit for everything else, making life simpler
120 foreach my $dir (qw(base comp run cmd io op uni mro)) {
123 _find_tests("lib") unless $::core;
124 # Config.pm may be broken for make minitest. And this is only a refinement
125 # for skipping tests on non-default builds, so it is allowed to fail.
126 # What we want to to is make a list of extensions which we did not build.
127 my $configsh = '../config.sh';
130 my (%extensions, %known_extensions);
131 open FH, $configsh or die "Can't open $configsh: $!";
133 if (/^extensions=['"](.*)['"]$/) {
134 # Deliberate string interpolation to avoid triggering possible
136 %extensions = _populate_hash ("$1");
138 elsif (/^known_extensions=['"](.*)['"]$/) {
139 %known_extensions = _populate_hash ($1);
143 if (%known_extensions) {
144 foreach (keys %known_extensions) {
145 $skip{$_}++ unless $extensions{$_};
148 warn "No known_extensions line found in $configsh";
151 warn "No extensions line found in $configsh";
154 my $mani = '../MANIFEST';
155 if (open(MANI, $mani)) {
156 while (<MANI>) { # similar code in t/harness
157 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
160 if (!$::core || $t =~ m!^lib/[a-z]!)
162 if (defined $extension) {
163 $extension =~ s!/t$!!;
164 # XXX Do I want to warn that I'm skipping these?
165 next if $skip{$extension};
166 my $flat_extension = $extension;
167 $flat_extension =~ s!-!/!g;
168 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
172 $::path_to_name{$path} = $t;
178 warn "$0: cannot open $mani: $!\n";
183 _find_tests('porting');
184 _find_tests('japh') if $::torture;
185 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
190 _testprogs('deparse', '', @ARGV);
192 elsif ($::with_utf16) {
195 print STDERR "# ENDIAN $e BOM $b\n";
198 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
199 my $f = $e ? "v" : "n";
203 if (open(U, ">$u")) {
204 print U pack("$f", 0xFEFF) if $b;
206 print U pack("$f*", unpack("C*", $_));
213 _testprogs('perl', '', @UARGV);
219 _testprogs('perl', '', @ARGV);
223 my ($type, $args, @tests) = @_;
225 print <<'EOT' if ($type eq 'deparse');
226 ------------------------------------------------------------------------------
228 ------------------------------------------------------------------------------
233 foreach my $t (@tests) {
234 unless (exists $::path_to_name{$t}) {
236 $::path_to_name{$t} = $tname;
240 foreach (@::path_to_name{@tests}) {
243 $maxlen = $len if $len > $maxlen;
245 # + 3 : we want three dots between the test name and the "ok"
246 my $dotdotdot = $maxlen + 3 ;
248 my $valgrind_log = 'current.valgrind';
249 my $total_files = @tests;
251 my $tested_files = 0;
255 while (my $test = shift @tests) {
256 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
261 if ($type eq 'deparse') {
262 if ($test eq "comp/redef.t") {
263 # Redefinition happens at compile time
266 elsif ($test =~ m{lib/Switch/t/}) {
267 # B::Deparse doesn't support source filtering
271 my $te = $::path_to_name{$test} . '.'
272 x ($dotdotdot - length($::path_to_name{$test}));
274 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
279 # XXX DAPM %OVER not defined anywhere
280 # $test = $OVER{$test} if exists $OVER{$test};
282 open(SCRIPT,"<",$test) or die "Can't run $test.\n";
284 close(SCRIPT) unless ($type eq 'deparse');
289 if (/#!.*\bperl.*\s-\w*([tT])/) {
294 # not all tests are expected to pass with this option
303 if ($type eq 'deparse') {
304 # Look for #line directives which change the filename
306 $file_opts .= ",-f$3$4"
307 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
312 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
313 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
314 if ($type eq 'deparse') {
316 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
317 "-l$::deparse_opts$file_opts ".
319 "&& ./perl $testswitch $switch -I../lib $test.dp |";
320 open(RESULTS, $deparse_cmd)
321 or print "can't deparse '$deparse_cmd': $!.\n";
323 elsif ($type eq 'perl') {
324 my $perl = $ENV{PERL} || './perl';
325 my $redir = $^O eq 'VMS' ? '2>&1' : '';
326 if ($ENV{PERL_VALGRIND}) {
327 my $valgrind = $ENV{VALGRIND} // 'valgrind';
328 my $vg_opts = $ENV{VG_OPTS}
329 // "--suppressions=perl.supp --leak-check=yes "
330 . "--leak-resolution=high --show-reachable=yes "
331 . "--num-callers=50";
332 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
333 $redir = "3>$valgrind_log";
335 my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
337 open(RESULTS,$run) or print "can't run '$run': $!.\n";
339 # Our environment may force us to use UTF-8, but we can't be sure that
340 # anything we're reading from will be generating (well formed) UTF-8
341 # This may not be the best way - possibly we should unset ${^OPEN} up
349 my $trailing_leader = 0;
353 next if /^\s*$/; # skip blank lines
354 if (/^1..$/ && ($^O eq 'VMS')) {
355 # VMS pipe bug inserts blank lines.
357 if ($l2 =~ /^\s*$/) {
366 if ($trailing_leader) {
367 # shouldn't be anything following a postfix 1..n
368 $failure = 'FAILED--extra output after trailing 1..n';
371 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
373 $failure = 'FAILED--seen duplicate leader';
377 %todo = map { $_ => 1 } split / /, $3 if $3;
381 # 1..n appears at end of file
382 $trailing_leader = 1;
384 $failure = "FAILED--expected $max tests, saw $next";
394 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
395 unless ($seen_leader) {
402 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
403 $num = $next unless $num;
407 # SKIP is essentially the same as TODO for t/TEST
408 # this still conforms to TAP:
409 # http://search.cpan.org/dist/TAP/TAP.pod
410 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
411 $istodo = 1 if $todo{$num};
413 if( $not && !$istodo ) {
414 $failure = "FAILED at test $num";
419 $failure ="FAILED--expected test $next, saw test $num";
423 elsif (/^Bail out!\s*(.*)/i) { # magic words
424 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
427 # module tests are allowed extra output,
428 # because Test::Harness allows it
429 next if $test =~ /^\W*(ext|lib)\b/;
430 $failure = "FAILED--unexpected output at test $next";
438 if (not defined $failure) {
439 $failure = 'FAILED--no leader found' unless $seen_leader;
442 if ($ENV{PERL_VALGRIND}) {
444 if (-e $valgrind_log) {
445 if (open(V, $valgrind_log)) {
449 warn "$0: Failed to open '$valgrind_log': $!\n";
452 if ($ENV{VG_OPTS} =~ /cachegrind/) {
453 if (rename $valgrind_log, "$test.valgrind") {
456 warn "$0: Failed to create '$test.valgrind': $!\n";
462 for my $i (0..$#valgrind) {
463 local $_ = $valgrind[$i];
464 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
465 $errors += $1; # there may be multiple error summaries
466 } elsif (/^==\d+== LEAK SUMMARY:/) {
467 for my $off (1 .. 4) {
468 if ($valgrind[$i+$off] =~
469 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
475 if ($errors or $leaks) {
476 if (rename $valgrind_log, "$test.valgrind") {
479 warn "$0: Failed to create '$test.valgrind': $!\n";
483 warn "No valgrind output?\n";
485 if (-e $valgrind_log) {
487 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
490 if ($type eq 'deparse') {
493 if ($ENV{PERL_3LOG}) {
497 $tpp =~ s:\.t$:.3log:;
498 rename("perl.3log", $tpp) ||
499 die "rename: perl3.log to $tpp: $!\n";
501 if (not defined $failure and $next != $max) {
502 $failure="FAILED--expected $max tests, saw $next";
505 if( !defined $failure # don't mask a test failure
508 $failure = "FAILED--non-zero wait status: $?";
511 if (defined $failure) {
512 print "${te}$failure\n";
514 if ($test =~ /^base/) {
515 die "Failed a basic test ($test) -- cannot continue.\n";
517 ++$failed_tests{$test};
522 if ( $show_elapsed_time ) {
523 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
528 print "${te}ok$elapsed\n";
532 print "${te}skipped\n";
538 if ($::bad_files == 0) {
540 print "All tests successful.\n";
541 # XXX add mention of 'perlbug -ok' ?
544 die "FAILED--no tests were run for some reason.\n";
548 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
549 my $s = $::bad_files == 1 ? "" : "s";
550 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
551 for my $test ( sort keys %failed_tests ) {
555 ### Since not all tests were successful, you may want to run some of
556 ### them individually and examine any diagnostic messages they produce.
557 ### See the INSTALL document's section on "make test".
559 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
560 ### You have a good chance to get more information by running
562 ### in the 't' directory since most (>=80%) of the tests succeeded.
564 if (eval {require Config; import Config; 1}) {
565 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
567 ### You may have to set your dynamic library search path,
568 ### $p, to point to the build directory:
570 if (exists $ENV{$p} && $ENV{$p} ne '') {
572 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
573 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
574 ### export $p=`pwd`:\$$p; cd t; ./perl harness
578 ### setenv $p `pwd`; cd t; ./perl harness
579 ### $p=`pwd`; export $p; cd t; ./perl harness
580 ### export $p=`pwd`; cd t; ./perl harness
584 ### for csh-style shells, like tcsh; or for traditional/modern
585 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
590 my ($user,$sys,$cuser,$csys) = times;
591 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
592 $user,$sys,$cuser,$csys,$tested_files,$totmax);
593 if ($ENV{PERL_VALGRIND}) {
594 my $s = $valgrind == 1 ? '' : 's';
595 print "$valgrind valgrind report$s created.\n", ;
598 exit ($::bad_files != 0);
600 # ex: set ts=8 sts=4 sw=4 noet: