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 # Location to put the Valgrind log.
9 my $Valgrind_Log = 'current.valgrind';
13 # for testing TEST only
14 #BEGIN { require '../lib/strict.pm'; "strict"->import() };
15 #BEGIN { require '../lib/warnings.pm'; "warnings"->import() };
17 # Let tests know they're running in the perl core. Useful for modules
18 # which live dual lives on CPAN.
20 delete $ENV{PERL5LIB};
22 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
23 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
24 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
26 # Cheesy version of Getopt::Std. Maybe we should replace it with that.
29 foreach my $idx (0..$#ARGV) {
30 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
31 $::benchmark = 1 if $1 eq 'benchmark';
32 $::core = 1 if $1 eq 'core';
33 $::verbose = 1 if $1 eq 'v';
34 $::torture = 1 if $1 eq 'torture';
35 $::with_utf8 = 1 if $1 eq 'utf8';
36 $::with_utf16 = 1 if $1 eq 'utf16';
37 $::taintwarn = 1 if $1 eq 'taintwarn';
38 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
39 if ($1 =~ /^deparse(,.+)?$/) {
47 chdir 't' if -f 't/TEST';
49 die "You need to run \"make test\" first to set things up.\n"
50 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
52 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
53 unless (-x 'perl.third') {
54 unless (-x '../perl.third') {
55 die "You need to run \"make perl.third first.\n";
58 print "Symlinking ../perl.third as perl.third...\n";
59 die "Failed to symlink: $!\n"
60 unless symlink("../perl.third", "perl.third");
61 die "Symlinked but no executable perl.third: $!\n"
62 unless -x 'perl.third';
67 # check leakage for embedders
68 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
70 $ENV{EMXSHELL} = 'sh'; # For OS/2
72 # Roll your own File::Find!
73 if ($show_elapsed_time) { require Time::HiRes }
86 opendir DIR, $dir or die "Trouble opening $dir: $!";
87 foreach my $f (sort { $a cmp $b } readdir DIR) {
90 my $fullpath = "$dir/$f";
93 _find_tests($fullpath);
94 } elsif ($f =~ /\.t$/) {
95 push @ARGV, $fullpath;
101 # Scan the text of the test program to find switches and special options
102 # we might need to apply.
104 my($test, $type) = @_;
106 open(my $script, "<", $test) or die "Can't read $test.\n";
107 my $first_line = <$script>;
109 $first_line =~ tr/\0//d if $::with_utf16;
112 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
116 # not all tests are expected to pass with this option
124 if ($type eq 'deparse') {
125 # Look for #line directives which change the filename
127 $file_opts .= ",-f$3$4"
128 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
132 return { file => $file_opts, switch => $switch };
137 my($test, $type) = @_;
139 my $options = _scan_test($test, $type);
141 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
142 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
145 if ($type eq 'deparse') {
147 "./perl $testswitch $options->{switch} -I../lib -MO=-qq,Deparse,-sv1.,".
148 "-l$::deparse_opts$options->{file} ".
150 "&& ./perl $testswitch $options->{switch} -I../lib $test.dp |";
151 open($results, $deparse_cmd)
152 or print "can't deparse '$deparse_cmd': $!.\n";
154 elsif ($type eq 'perl') {
155 my $perl = $ENV{PERL} || './perl';
156 my $redir = $^O eq 'VMS' ? '2>&1' : '';
158 if ($ENV{PERL_VALGRIND}) {
159 my $valgrind = $ENV{VALGRIND} // 'valgrind';
160 my $vg_opts = $ENV{VG_OPTS}
161 // "--suppressions=perl.supp --leak-check=yes "
162 . "--leak-resolution=high --show-reachable=yes "
163 . "--num-callers=50";
164 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
165 $redir = "3>$Valgrind_Log";
168 my $run = "$perl" . _quote_args("$testswitch $options->{switch} $utf8")
170 open($results, $run) or print "can't run '$run': $!.\n";
173 # Our environment may force us to use UTF-8, but we can't be sure that
174 # anything we're reading from will be generating (well formed) UTF-8
175 # This may not be the best way - possibly we should unset ${^OPEN} up
186 foreach (split(/\s+/,$args)) {
187 # In VMS protect with doublequotes because otherwise
188 # DCL will lowercase -- unless already doublequoted.
189 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
190 $argstring .= ' ' . $_;
196 return map {$_, 1} split /\s+/, $_[0];
200 # base first, as TEST bails out if that can't run
201 # then comp, to validate that require works
202 # then run, to validate that -M works
203 # then we know we can -MTestInit for everything else, making life simpler
204 foreach my $dir (qw(base comp run cmd io op uni mro)) {
207 _find_tests("lib") unless $::core;
208 # Config.pm may be broken for make minitest. And this is only a refinement
209 # for skipping tests on non-default builds, so it is allowed to fail.
210 # What we want to to is make a list of extensions which we did not build.
211 my $configsh = '../config.sh';
214 my (%extensions, %known_extensions);
215 open FH, $configsh or die "Can't open $configsh: $!";
217 if (/^extensions=['"](.*)['"]$/) {
218 # Deliberate string interpolation to avoid triggering possible
220 %extensions = _populate_hash ("$1");
222 elsif (/^known_extensions=['"](.*)['"]$/) {
223 %known_extensions = _populate_hash ($1);
227 if (%known_extensions) {
228 foreach (keys %known_extensions) {
229 $skip{$_}++ unless $extensions{$_};
232 warn "No known_extensions line found in $configsh";
235 warn "No extensions line found in $configsh";
238 my $mani = '../MANIFEST';
239 if (open(MANI, $mani)) {
240 while (<MANI>) { # similar code in t/harness
241 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
244 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
256 $::path_to_name{$path} = $t;
262 warn "$0: cannot open $mani: $!\n";
267 _find_tests('porting');
268 _find_tests('japh') if $::torture;
269 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
274 _testprogs('deparse', '', @ARGV);
276 elsif ($::with_utf16) {
279 print STDERR "# ENDIAN $e BOM $b\n";
282 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
283 my $f = $e ? "v" : "n";
287 if (open(U, ">$u")) {
288 print U pack("$f", 0xFEFF) if $b;
290 print U pack("$f*", unpack("C*", $_));
297 _testprogs('perl', '', @UARGV);
303 _testprogs('perl', '', @ARGV);
307 my ($type, $args, @tests) = @_;
309 print <<'EOT' if ($type eq 'deparse');
310 ------------------------------------------------------------------------------
312 ------------------------------------------------------------------------------
317 foreach my $t (@tests) {
318 unless (exists $::path_to_name{$t}) {
320 $::path_to_name{$t} = $tname;
324 foreach (@::path_to_name{@tests}) {
327 $maxlen = $len if $len > $maxlen;
329 # + 3 : we want three dots between the test name and the "ok"
330 my $dotdotdot = $maxlen + 3 ;
332 my $total_files = @tests;
334 my $tested_files = 0;
338 while (my $test = shift @tests) {
339 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
344 if ($type eq 'deparse') {
345 if ($test eq "comp/redef.t") {
346 # Redefinition happens at compile time
349 elsif ($test =~ m{lib/Switch/t/}) {
350 # B::Deparse doesn't support source filtering
354 my $te = $::path_to_name{$test} . '.'
355 x ($dotdotdot - length($::path_to_name{$test}));
357 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
362 # XXX DAPM %OVER not defined anywhere
363 # $test = $OVER{$test} if exists $OVER{$test};
365 my $results = _run_test($test, $type);
371 my $trailing_leader = 0;
375 next if /^\s*$/; # skip blank lines
376 if (/^1..$/ && ($^O eq 'VMS')) {
377 # VMS pipe bug inserts blank lines.
379 if ($l2 =~ /^\s*$/) {
388 if ($trailing_leader) {
389 # shouldn't be anything following a postfix 1..n
390 $failure = 'FAILED--extra output after trailing 1..n';
393 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
395 $failure = 'FAILED--seen duplicate leader';
399 %todo = map { $_ => 1 } split / /, $3 if $3;
403 # 1..n appears at end of file
404 $trailing_leader = 1;
406 $failure = "FAILED--expected $max tests, saw $next";
416 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
417 unless ($seen_leader) {
424 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
425 $num = $next unless $num;
429 # SKIP is essentially the same as TODO for t/TEST
430 # this still conforms to TAP:
431 # http://search.cpan.org/dist/TAP/TAP.pod
432 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
433 $istodo = 1 if $todo{$num};
435 if( $not && !$istodo ) {
436 $failure = "FAILED at test $num";
441 $failure ="FAILED--expected test $next, saw test $num";
445 elsif (/^Bail out!\s*(.*)/i) { # magic words
446 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
449 # module tests are allowed extra output,
450 # because Test::Harness allows it
451 next if $test =~ /^\W*(ext|lib)\b/;
452 $failure = "FAILED--unexpected output at test $next";
460 if (not defined $failure) {
461 $failure = 'FAILED--no leader found' unless $seen_leader;
464 if ($ENV{PERL_VALGRIND}) {
466 if (-e $Valgrind_Log) {
467 if (open(V, $Valgrind_Log)) {
471 warn "$0: Failed to open '$Valgrind_Log': $!\n";
474 if ($ENV{VG_OPTS} =~ /cachegrind/) {
475 if (rename $Valgrind_Log, "$test.valgrind") {
478 warn "$0: Failed to create '$test.valgrind': $!\n";
484 for my $i (0..$#valgrind) {
485 local $_ = $valgrind[$i];
486 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
487 $errors += $1; # there may be multiple error summaries
488 } elsif (/^==\d+== LEAK SUMMARY:/) {
489 for my $off (1 .. 4) {
490 if ($valgrind[$i+$off] =~
491 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
497 if ($errors or $leaks) {
498 if (rename $Valgrind_Log, "$test.valgrind") {
501 warn "$0: Failed to create '$test.valgrind': $!\n";
505 warn "No valgrind output?\n";
507 if (-e $Valgrind_Log) {
509 or warn "$0: Failed to unlink '$Valgrind_Log': $!\n";
512 if ($type eq 'deparse') {
515 if ($ENV{PERL_3LOG}) {
519 $tpp =~ s:\.t$:.3log:;
520 rename("perl.3log", $tpp) ||
521 die "rename: perl3.log to $tpp: $!\n";
523 if (not defined $failure and $next != $max) {
524 $failure="FAILED--expected $max tests, saw $next";
527 if( !defined $failure # don't mask a test failure
530 $failure = "FAILED--non-zero wait status: $?";
533 if (defined $failure) {
534 print "${te}$failure\n";
536 if ($test =~ /^base/) {
537 die "Failed a basic test ($test) -- cannot continue.\n";
539 ++$failed_tests{$test};
544 if ( $show_elapsed_time ) {
545 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
550 print "${te}ok$elapsed\n";
554 print "${te}skipped\n";
560 if ($::bad_files == 0) {
562 print "All tests successful.\n";
563 # XXX add mention of 'perlbug -ok' ?
566 die "FAILED--no tests were run for some reason.\n";
570 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
571 my $s = $::bad_files == 1 ? "" : "s";
572 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
573 for my $test ( sort keys %failed_tests ) {
577 ### Since not all tests were successful, you may want to run some of
578 ### them individually and examine any diagnostic messages they produce.
579 ### See the INSTALL document's section on "make test".
581 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
582 ### You have a good chance to get more information by running
584 ### in the 't' directory since most (>=80%) of the tests succeeded.
586 if (eval {require Config; import Config; 1}) {
587 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
589 ### You may have to set your dynamic library search path,
590 ### $p, to point to the build directory:
592 if (exists $ENV{$p} && $ENV{$p} ne '') {
594 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
595 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
596 ### export $p=`pwd`:\$$p; cd t; ./perl harness
600 ### setenv $p `pwd`; cd t; ./perl harness
601 ### $p=`pwd`; export $p; cd t; ./perl harness
602 ### export $p=`pwd`; cd t; ./perl harness
606 ### for csh-style shells, like tcsh; or for traditional/modern
607 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
612 my ($user,$sys,$cuser,$csys) = times;
613 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
614 $user,$sys,$cuser,$csys,$tested_files,$totmax);
615 if ($ENV{PERL_VALGRIND}) {
616 my $s = $valgrind == 1 ? '' : 's';
617 print "$valgrind valgrind report$s created.\n", ;
620 exit ($::bad_files != 0);
622 # ex: set ts=8 sts=4 sw=4 noet: