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;
99 # Scan the text of the test program to find switches and special options
100 # we might need to apply.
102 my($test, $type) = @_;
104 open(my $script, "<", $test) or die "Can't read $test.\n";
105 my $first_line = <$script>;
107 $first_line =~ tr/\0//d if $::with_utf16;
110 if ($first_line =~ /#!.*\bperl.*\s-\w*([tT])/) {
114 # not all tests are expected to pass with this option
122 if ($type eq 'deparse') {
123 # Look for #line directives which change the filename
125 $file_opts .= ",-f$3$4"
126 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
130 return { file => $file_opts, switch => $switch };
137 foreach (split(/\s+/,$args)) {
138 # In VMS protect with doublequotes because otherwise
139 # DCL will lowercase -- unless already doublequoted.
140 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
141 $argstring .= ' ' . $_;
147 return map {$_, 1} split /\s+/, $_[0];
151 # base first, as TEST bails out if that can't run
152 # then comp, to validate that require works
153 # then run, to validate that -M works
154 # then we know we can -MTestInit for everything else, making life simpler
155 foreach my $dir (qw(base comp run cmd io op uni mro)) {
158 _find_tests("lib") unless $::core;
159 # Config.pm may be broken for make minitest. And this is only a refinement
160 # for skipping tests on non-default builds, so it is allowed to fail.
161 # What we want to to is make a list of extensions which we did not build.
162 my $configsh = '../config.sh';
165 my (%extensions, %known_extensions);
166 open FH, $configsh or die "Can't open $configsh: $!";
168 if (/^extensions=['"](.*)['"]$/) {
169 # Deliberate string interpolation to avoid triggering possible
171 %extensions = _populate_hash ("$1");
173 elsif (/^known_extensions=['"](.*)['"]$/) {
174 %known_extensions = _populate_hash ($1);
178 if (%known_extensions) {
179 foreach (keys %known_extensions) {
180 $skip{$_}++ unless $extensions{$_};
183 warn "No known_extensions line found in $configsh";
186 warn "No extensions line found in $configsh";
189 my $mani = '../MANIFEST';
190 if (open(MANI, $mani)) {
191 while (<MANI>) { # similar code in t/harness
192 if (m!^(ext/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
195 if (!$::core || $t =~ m!^lib/[a-z]!)
197 if (defined $extension) {
198 $extension =~ s!/t$!!;
199 # XXX Do I want to warn that I'm skipping these?
200 next if $skip{$extension};
201 my $flat_extension = $extension;
202 $flat_extension =~ s!-!/!g;
203 next if $skip{$flat_extension}; # Foo/Bar may live in Foo-Bar
207 $::path_to_name{$path} = $t;
213 warn "$0: cannot open $mani: $!\n";
218 _find_tests('porting');
219 _find_tests('japh') if $::torture;
220 _find_tests('t/benchmark') if $::benchmark or $ENV{PERL_BENCHMARK};
225 _testprogs('deparse', '', @ARGV);
227 elsif ($::with_utf16) {
230 print STDERR "# ENDIAN $e BOM $b\n";
233 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
234 my $f = $e ? "v" : "n";
238 if (open(U, ">$u")) {
239 print U pack("$f", 0xFEFF) if $b;
241 print U pack("$f*", unpack("C*", $_));
248 _testprogs('perl', '', @UARGV);
254 _testprogs('perl', '', @ARGV);
258 my ($type, $args, @tests) = @_;
260 print <<'EOT' if ($type eq 'deparse');
261 ------------------------------------------------------------------------------
263 ------------------------------------------------------------------------------
268 foreach my $t (@tests) {
269 unless (exists $::path_to_name{$t}) {
271 $::path_to_name{$t} = $tname;
275 foreach (@::path_to_name{@tests}) {
278 $maxlen = $len if $len > $maxlen;
280 # + 3 : we want three dots between the test name and the "ok"
281 my $dotdotdot = $maxlen + 3 ;
283 my $valgrind_log = 'current.valgrind';
284 my $total_files = @tests;
286 my $tested_files = 0;
290 while (my $test = shift @tests) {
291 my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
296 if ($type eq 'deparse') {
297 if ($test eq "comp/redef.t") {
298 # Redefinition happens at compile time
301 elsif ($test =~ m{lib/Switch/t/}) {
302 # B::Deparse doesn't support source filtering
306 my $te = $::path_to_name{$test} . '.'
307 x ($dotdotdot - length($::path_to_name{$test}));
309 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
314 # XXX DAPM %OVER not defined anywhere
315 # $test = $OVER{$test} if exists $OVER{$test};
317 my $options = _scan_test($test, $type);
319 my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
320 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
321 if ($type eq 'deparse') {
323 "./perl $testswitch $options->{switch} -I../lib -MO=-qq,Deparse,-sv1.,".
324 "-l$::deparse_opts$options->{file} ".
326 "&& ./perl $testswitch $options->{switch} -I../lib $test.dp |";
327 open(RESULTS, $deparse_cmd)
328 or print "can't deparse '$deparse_cmd': $!.\n";
330 elsif ($type eq 'perl') {
331 my $perl = $ENV{PERL} || './perl';
332 my $redir = $^O eq 'VMS' ? '2>&1' : '';
333 if ($ENV{PERL_VALGRIND}) {
334 my $valgrind = $ENV{VALGRIND} // 'valgrind';
335 my $vg_opts = $ENV{VG_OPTS}
336 // "--suppressions=perl.supp --leak-check=yes "
337 . "--leak-resolution=high --show-reachable=yes "
338 . "--num-callers=50";
339 $perl = "$valgrind --log-fd=3 $vg_opts $perl";
340 $redir = "3>$valgrind_log";
342 my $run = "$perl" . _quote_args("$testswitch $options->{switch} $utf8")
344 open(RESULTS,$run) or print "can't run '$run': $!.\n";
346 # Our environment may force us to use UTF-8, but we can't be sure that
347 # anything we're reading from will be generating (well formed) UTF-8
348 # This may not be the best way - possibly we should unset ${^OPEN} up
356 my $trailing_leader = 0;
360 next if /^\s*$/; # skip blank lines
361 if (/^1..$/ && ($^O eq 'VMS')) {
362 # VMS pipe bug inserts blank lines.
364 if ($l2 =~ /^\s*$/) {
373 if ($trailing_leader) {
374 # shouldn't be anything following a postfix 1..n
375 $failure = 'FAILED--extra output after trailing 1..n';
378 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
380 $failure = 'FAILED--seen duplicate leader';
384 %todo = map { $_ => 1 } split / /, $3 if $3;
388 # 1..n appears at end of file
389 $trailing_leader = 1;
391 $failure = "FAILED--expected $max tests, saw $next";
401 if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
402 unless ($seen_leader) {
409 my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
410 $num = $next unless $num;
414 # SKIP is essentially the same as TODO for t/TEST
415 # this still conforms to TAP:
416 # http://search.cpan.org/dist/TAP/TAP.pod
417 $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
418 $istodo = 1 if $todo{$num};
420 if( $not && !$istodo ) {
421 $failure = "FAILED at test $num";
426 $failure ="FAILED--expected test $next, saw test $num";
430 elsif (/^Bail out!\s*(.*)/i) { # magic words
431 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
434 # module tests are allowed extra output,
435 # because Test::Harness allows it
436 next if $test =~ /^\W*(ext|lib)\b/;
437 $failure = "FAILED--unexpected output at test $next";
445 if (not defined $failure) {
446 $failure = 'FAILED--no leader found' unless $seen_leader;
449 if ($ENV{PERL_VALGRIND}) {
451 if (-e $valgrind_log) {
452 if (open(V, $valgrind_log)) {
456 warn "$0: Failed to open '$valgrind_log': $!\n";
459 if ($ENV{VG_OPTS} =~ /cachegrind/) {
460 if (rename $valgrind_log, "$test.valgrind") {
463 warn "$0: Failed to create '$test.valgrind': $!\n";
469 for my $i (0..$#valgrind) {
470 local $_ = $valgrind[$i];
471 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
472 $errors += $1; # there may be multiple error summaries
473 } elsif (/^==\d+== LEAK SUMMARY:/) {
474 for my $off (1 .. 4) {
475 if ($valgrind[$i+$off] =~
476 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
482 if ($errors or $leaks) {
483 if (rename $valgrind_log, "$test.valgrind") {
486 warn "$0: Failed to create '$test.valgrind': $!\n";
490 warn "No valgrind output?\n";
492 if (-e $valgrind_log) {
494 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
497 if ($type eq 'deparse') {
500 if ($ENV{PERL_3LOG}) {
504 $tpp =~ s:\.t$:.3log:;
505 rename("perl.3log", $tpp) ||
506 die "rename: perl3.log to $tpp: $!\n";
508 if (not defined $failure and $next != $max) {
509 $failure="FAILED--expected $max tests, saw $next";
512 if( !defined $failure # don't mask a test failure
515 $failure = "FAILED--non-zero wait status: $?";
518 if (defined $failure) {
519 print "${te}$failure\n";
521 if ($test =~ /^base/) {
522 die "Failed a basic test ($test) -- cannot continue.\n";
524 ++$failed_tests{$test};
529 if ( $show_elapsed_time ) {
530 $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
535 print "${te}ok$elapsed\n";
539 print "${te}skipped\n";
545 if ($::bad_files == 0) {
547 print "All tests successful.\n";
548 # XXX add mention of 'perlbug -ok' ?
551 die "FAILED--no tests were run for some reason.\n";
555 my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
556 my $s = $::bad_files == 1 ? "" : "s";
557 warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
558 for my $test ( sort keys %failed_tests ) {
562 ### Since not all tests were successful, you may want to run some of
563 ### them individually and examine any diagnostic messages they produce.
564 ### See the INSTALL document's section on "make test".
566 warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
567 ### You have a good chance to get more information by running
569 ### in the 't' directory since most (>=80%) of the tests succeeded.
571 if (eval {require Config; import Config; 1}) {
572 if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
574 ### You may have to set your dynamic library search path,
575 ### $p, to point to the build directory:
577 if (exists $ENV{$p} && $ENV{$p} ne '') {
579 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
580 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
581 ### export $p=`pwd`:\$$p; cd t; ./perl harness
585 ### setenv $p `pwd`; cd t; ./perl harness
586 ### $p=`pwd`; export $p; cd t; ./perl harness
587 ### export $p=`pwd`; cd t; ./perl harness
591 ### for csh-style shells, like tcsh; or for traditional/modern
592 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
597 my ($user,$sys,$cuser,$csys) = times;
598 print sprintf("u=%.2f s=%.2f cu=%.2f cs=%.2f scripts=%d tests=%d\n",
599 $user,$sys,$cuser,$csys,$tested_files,$totmax);
600 if ($ENV{PERL_VALGRIND}) {
601 my $s = $valgrind == 1 ? '' : 's';
602 print "$valgrind valgrind report$s created.\n", ;
605 exit ($::bad_files != 0);
607 # ex: set ts=8 sts=4 sw=4 noet: