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!
72 if ($show_elapsed_time) { require Time::HiRes }
73 my $curdir = File::Spec->curdir;
74 my $updir = File::Spec->updir;
78 opendir DIR, $dir or die "Trouble opening $dir: $!";
79 foreach my $f (sort { $a cmp $b } readdir DIR) {
80 next if $f eq $curdir or $f eq $updir or
81 $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
83 my $fullpath = File::Spec->catfile($dir, $f);
85 _find_tests($fullpath) if -d $fullpath;
86 $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
87 push @ARGV, $fullpath if $f =~ /\.t$/;
95 foreach (split(/\s+/,$args)) {
96 # In VMS protect with doublequotes because otherwise
97 # DCL will lowercase -- unless already doublequoted.
98 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
99 $argstring .= ' ' . $_;
105 return map {$_, 1} split /\s+/, $_[0];
109 foreach my $dir (qw(base comp cmd run io op uni mro)) {
112 _find_tests("lib") unless $::core;
113 # Config.pm may be broken for make minitest. And this is only a refinement
114 # for skipping tests on non-default builds, so it is allowed to fail.
115 # What we want to to is make a list of extensions which we did not build.
116 my $configsh = File::Spec->catfile($updir, "config.sh");
119 my (%extensions, %known_extensions);
120 open FH, $configsh or die "Can't open $configsh: $!";
122 if (/^extensions=['"](.*)['"]$/) {
123 # Deliberate string interpolation to avoid triggering possible
125 %extensions = _populate_hash ("$1");
127 elsif (/^known_extensions=['"](.*)['"]$/) {
128 %known_extensions = _populate_hash ($1);
132 if (%known_extensions) {
133 foreach (keys %known_extensions) {
134 $skip{$_}++ unless $extensions{$_};
137 warn "No known_extensions line found in $configsh";
140 warn "No extensions line found in $configsh";
143 my $mani = File::Spec->catfile($updir, "MANIFEST");
144 if (open(MANI, $mani)) {
145 while (<MANI>) { # similar code in t/harness
146 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
149 if (!$::core || $t =~ m!^lib/[a-z]!)
151 if (defined $extension) {
152 $extension =~ s!/t$!!;
153 # XXX Do I want to warn that I'm skipping these?
154 next if $skip{$extension};
155 my $flat_extension = $extension;
156 $flat_extension =~ s!-!/!g;
157 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
159 my $path = File::Spec->catfile($updir, $t);
161 $::path_to_name{$path} = $t;
167 warn "$0: cannot open $mani: $!\n";
172 _find_tests('porting');
173 _find_tests('japh') if $::torture;
174 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
179 _testprogs('deparse', '', @ARGV);
181 elsif ($::with_utf16) {
184 print STDERR "# ENDIAN $e BOM $b\n";
187 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
188 my $f = $e ? "v" : "n";
192 if (open(U, ">$u")) {
193 print U pack("$f", 0xFEFF) if $b;
195 print U pack("$f*", unpack("C*", $_));
202 _testprogs('perl', '', @UARGV);
208 _testprogs('perl', '', @ARGV);
212 my ($type, $args, @tests) = @_;
214 print <<'EOT' if ($type eq 'deparse');
215 ------------------------------------------------------------------------------
217 ------------------------------------------------------------------------------
222 foreach my $t (@tests) {
223 unless (exists $::path_to_name{$t}) {
224 my $tname = File::Spec->catfile('t',$t);
225 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
226 $::path_to_name{$t} = $tname;
230 foreach (@::path_to_name{@tests}) {
233 $maxlen = $len if $len > $maxlen;
235 # + 3 : we want three dots between the test name and the "ok"
236 my $dotdotdot = $maxlen + 3 ;
238 my $valgrind_log = 'current.valgrind';
239 my $total_files = @tests;
241 my $tested_files = 0;
245 while (my $test = shift @tests) {
246 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
251 if ($type eq 'deparse') {
252 if ($test eq "comp/redef.t") {
253 # Redefinition happens at compile time
256 elsif ($test =~ m{lib/Switch/t/}) {
257 # B::Deparse doesn't support source filtering
261 my $te = $::path_to_name{$test} . '.'
262 x ($dotdotdot - length($::path_to_name{$test}));
264 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
269 # XXX DAPM %OVER not defined anywhere
270 # $test = $OVER{$test} if exists $OVER{$test};
272 open(SCRIPT,"<",$test) or die "Can't run $test.\n";
274 close(SCRIPT) unless ($type eq 'deparse');
279 if (/#!.*\bperl.*\s-\w*([tT])/) {
284 # not all tests are expected to pass with this option
293 if ($type eq 'deparse') {
294 # Look for #line directives which change the filename
296 $file_opts .= ",-f$3$4"
297 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
302 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
303 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
304 if ($type eq 'deparse') {
306 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
307 "-l$::deparse_opts$file_opts ".
309 "&& ./perl $testswitch $switch -I../lib $test.dp |";
310 open(RESULTS, $deparse_cmd)
311 or print "can't deparse '$deparse_cmd': $!.\n";
313 elsif ($type eq 'perl') {
314 my $perl = $ENV{PERL} || './perl';
315 my $redir = $^O eq 'VMS' ? '2>&1' : '';
316 if ($ENV{PERL_VALGRIND}) {
317 my $valgrind = $ENV{VALGRIND} // 'valgrind';
318 my $vg_opts = $ENV{VG_OPTS}
319 // "--suppressions=perl.supp --leak-check=yes "
320 . "--leak-resolution=high --show-reachable=yes "
321 . "--num-callers=50";
322 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
323 $redir = "3>$valgrind_log";
325 my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
327 open(RESULTS,$run) or print "can't run '$run': $!.\n";
329 # Our environment may force us to use UTF-8, but we can't be sure that
330 # anything we're reading from will be generating (well formed) UTF-8
331 # This may not be the best way - possibly we should unset ${^OPEN} up
339 my $trailing_leader = 0;
343 next if /^\s*$/; # skip blank lines
344 if (/^1..$/ && ($^O eq 'VMS')) {
345 # VMS pipe bug inserts blank lines.
347 if ($l2 =~ /^\s*$/) {
356 if ($trailing_leader) {
357 # shouldn't be anything following a postfix 1..n
358 $failure = 'FAILED--extra output after trailing 1..n';
361 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
363 $failure = 'FAILED--seen duplicate leader';
367 %todo = map { $_ => 1 } split / /, $3 if $3;
371 # 1..n appears at end of file
372 $trailing_leader = 1;
374 $failure = "FAILED--expected $max tests, saw $next";
384 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
385 unless ($seen_leader) {
392 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
393 $num = $next unless $num;
397 # SKIP is essentially the same as TODO for t/TEST
398 # this still conforms to TAP:
399 # http://search.cpan.org/dist/TAP/TAP.pod
400 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
401 $istodo = 1 if $todo{$num};
403 if( $not && !$istodo ) {
404 $failure = "FAILED at test $num";
409 $failure ="FAILED--expected test $next, saw test $num";
413 elsif (/^Bail out!\s*(.*)/i) { # magic words
414 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
417 # module tests are allowed extra output,
418 # because Test::Harness allows it
419 next if $test =~ /^\W*(ext|lib)\b/;
420 $failure = "FAILED--unexpected output at test $next";
428 if (not defined $failure) {
429 $failure = 'FAILED--no leader found' unless $seen_leader;
432 if ($ENV{PERL_VALGRIND}) {
434 if (-e $valgrind_log) {
435 if (open(V, $valgrind_log)) {
439 warn "$0: Failed to open '$valgrind_log': $!\n";
442 if ($ENV{VG_OPTS} =~ /cachegrind/) {
443 if (rename $valgrind_log, "$test.valgrind") {
446 warn "$0: Failed to create '$test.valgrind': $!\n";
452 for my $i (0..$#valgrind) {
453 local $_ = $valgrind[$i];
454 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
455 $errors += $1; # there may be multiple error summaries
456 } elsif (/^==\d+== LEAK SUMMARY:/) {
457 for my $off (1 .. 4) {
458 if ($valgrind[$i+$off] =~
459 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
465 if ($errors or $leaks) {
466 if (rename $valgrind_log, "$test.valgrind") {
469 warn "$0: Failed to create '$test.valgrind': $!\n";
473 warn "No valgrind output?\n";
475 if (-e $valgrind_log) {
477 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
480 if ($type eq 'deparse') {
483 if ($ENV{PERL_3LOG}) {
487 $tpp =~ s:\.t$:.3log:;
488 rename("perl.3log", $tpp) ||
489 die "rename: perl3.log to $tpp: $!\n";
491 if (not defined $failure and $next != $max) {
492 $failure="FAILED--expected $max tests, saw $next";
495 if( !defined $failure # don't mask a test failure
498 $failure = "FAILED--non-zero wait status: $?";
501 if (defined $failure) {
502 print "${te}$failure\n";
504 if ($test =~ /^base/) {
505 die "Failed a basic test ($test) -- cannot continue.\n";
507 ++$failed_tests{$test};
512 if ( $show_elapsed_time ) {
513 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
518 print "${te}ok$elapsed\n";
522 print "${te}skipped\n";
528 if ($::bad_files == 0) {
530 print "All tests successful.\n";
531 # XXX add mention of 'perlbug -ok' ?
534 die "FAILED--no tests were run for some reason.\n";
538 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
539 my $s = $::bad_files == 1 ? "" : "s";
540 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
541 for my $test ( sort keys %failed_tests ) {
545 ### Since not all tests were successful, you may want to run some of
546 ### them individually and examine any diagnostic messages they produce.
547 ### See the INSTALL document's section on "make test".
549 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
550 ### You have a good chance to get more information by running
552 ### in the 't' directory since most (>=80%) of the tests succeeded.
554 if (eval {require Config; import Config; 1}) {
555 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
557 ### You may have to set your dynamic library search path,
558 ### $p, to point to the build directory:
560 if (exists $ENV{$p} && $ENV{$p} ne '') {
562 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
563 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
564 ### export $p=`pwd`:\$$p; cd t; ./perl harness
568 ### setenv $p `pwd`; cd t; ./perl harness
569 ### $p=`pwd`; export $p; cd t; ./perl harness
570 ### export $p=`pwd`; cd t; ./perl harness
574 ### for csh-style shells, like tcsh; or for traditional/modern
575 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
580 my ($user,$sys,$cuser,$csys) = times;
581 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
582 $user,$sys,$cuser,$csys,$tested_files,$totmax);
583 if ($ENV{PERL_VALGRIND}) {
584 my $s = $valgrind == 1 ? '' : 's';
585 print "$valgrind valgrind report$s created.\n", ;
588 exit ($::bad_files != 0);
590 # ex: set ts=8 sts=4 sw=4 noet: