xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/TEST (revision 0:68f95e015346)
1#!./perl
2
3# This is written in a peculiar style, since we're trying to avoid
4# most of the constructs we'll be testing for.
5
6$| = 1;
7
8# Let tests know they're running in the perl core.  Useful for modules
9# which live dual lives on CPAN.
10$ENV{PERL_CORE} = 1;
11
12# remove empty elements due to insertion of empty symbols via "''p1'" syntax
13@ARGV = grep($_,@ARGV) if $^O eq 'VMS';
14
15# Cheesy version of Getopt::Std.  Maybe we should replace it with that.
16@argv = ();
17if ($#ARGV >= 0) {
18    foreach my $idx (0..$#ARGV) {
19	push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
20	$core    = 1 if $1 eq 'core';
21	$verbose = 1 if $1 eq 'v';
22	$torture = 1 if $1 eq 'torture';
23	$with_utf= 1 if $1 eq 'utf8';
24	$bytecompile = 1 if $1 eq 'bytecompile';
25	$compile = 1 if $1 eq 'compile';
26	$taintwarn = 1 if $1 eq 'taintwarn';
27	$ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
28	if ($1 =~ /^deparse(,.+)?$/) {
29	    $deparse = 1;
30	    $deparse_opts = $1;
31	}
32    }
33}
34@ARGV = @argv;
35
36chdir 't' if -f 't/TEST';
37
38die "You need to run \"make test\" first to set things up.\n"
39  unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
40
41if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
42    unless (-x 'perl.third') {
43	unless (-x '../perl.third') {
44	    die "You need to run \"make perl.third first.\n";
45	}
46	else {
47	    print "Symlinking ../perl.third as perl.third...\n";
48	    die "Failed to symlink: $!\n"
49		unless symlink("../perl.third", "perl.third");
50	    die "Symlinked but no executable perl.third: $!\n"
51		unless -x 'perl.third';
52	}
53    }
54}
55
56# check leakage for embedders
57$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
58
59$ENV{EMXSHELL} = 'sh';        # For OS/2
60
61# Roll your own File::Find!
62use TestInit;
63use File::Spec;
64my $curdir = File::Spec->curdir;
65my $updir  = File::Spec->updir;
66
67sub _find_tests {
68    my($dir) = @_;
69    opendir DIR, $dir or die "Trouble opening $dir: $!";
70    foreach my $f (sort { $a cmp $b } readdir DIR) {
71        next if $f eq $curdir or $f eq $updir or
72	    $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
73
74        my $fullpath = File::Spec->catfile($dir, $f);
75
76        _find_tests($fullpath) if -d $fullpath;
77        $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
78        push @ARGV, $fullpath if $f =~ /\.t$/;
79    }
80}
81
82sub _quote_args {
83    my ($args) = @_;
84    my $argstring = '';
85
86    foreach (split(/\s+/,$args)) {
87       # In VMS protect with doublequotes because otherwise
88       # DCL will lowercase -- unless already doublequoted.
89       $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
90       $argstring .= ' ' . $_;
91    }
92    return $argstring;
93}
94
95unless (@ARGV) {
96    foreach my $dir (qw(base comp cmd run io op uni)) {
97        _find_tests($dir);
98    }
99    _find_tests("lib") unless $core;
100    my $mani = File::Spec->catfile($updir, "MANIFEST");
101    if (open(MANI, $mani)) {
102        while (<MANI>) { # similar code in t/harness
103	    if (m!^(ext/\S+/?(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
104		$t = $1;
105		if (!$core || $t =~ m!^lib/[a-z]!)
106		{
107		    $path = File::Spec->catfile($updir, $t);
108		    push @ARGV, $path;
109		    $name{$path} = $t;
110		}
111	    }
112	}
113	close MANI;
114    } else {
115        warn "$0: cannot open $mani: $!\n";
116    }
117    unless ($core) {
118	_find_tests('pod');
119	_find_tests('x2p');
120	_find_tests('japh') if $torture;
121    }
122}
123
124# Tests known to cause infinite loops for the perlcc tests.
125# %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
126%infinite = ();
127
128if ($deparse) {
129    _testprogs('deparse', '',   @ARGV);
130}
131elsif( $compile ) {
132    _testprogs('compile', '',   @ARGV);
133}
134elsif( $bytecompile ) {
135    _testprogs('bytecompile', '', @ARGV);
136}
137else {
138    _testprogs('compile', '',   @ARGV) if -e "../testcompile";
139    _testprogs('perl',    '',   @ARGV);
140}
141
142sub _testprogs {
143    $type = shift @_;
144    $args = shift;
145    @tests = @_;
146
147    print <<'EOT' if ($type eq 'compile');
148------------------------------------------------------------------------------
149TESTING COMPILER
150------------------------------------------------------------------------------
151EOT
152
153    print <<'EOT' if ($type eq 'deparse');
154------------------------------------------------------------------------------
155TESTING DEPARSER
156------------------------------------------------------------------------------
157EOT
158
159    print <<EOT if ($type eq 'bytecompile');
160------------------------------------------------------------------------------
161TESTING BYTECODE COMPILER
162------------------------------------------------------------------------------
163EOT
164
165    $ENV{PERLCC_TIMEOUT} = 120
166          if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
167
168    $bad = 0;
169    $good = 0;
170    $total = @tests;
171    $files  = 0;
172    $totmax = 0;
173
174    foreach my $t (@tests) {
175      unless (exists $name{$t}) {
176        my $tname = File::Spec->catfile('t',$t);
177        $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
178        $name{$t} = $tname;
179      }
180    }
181    my $maxlen = 0;
182    foreach (@name{@tests}) {
183	s/\.\w+\z/./;
184	my $len = length ;
185	$maxlen = $len if $len > $maxlen;
186    }
187    # + 3 : we want three dots between the test name and the "ok"
188    $dotdotdot = $maxlen + 3 ;
189    my $valgrind = 0;
190    my $valgrind_log = 'current.valgrind';
191    while ($test = shift @tests) {
192
193	if ( $infinite{$test} && $type eq 'compile' ) {
194	    print STDERR "$test creates infinite loop! Skipping.\n";
195            next;
196	}
197	if ($test =~ /^$/) {
198	    next;
199	}
200	if ($type eq 'deparse') {
201	    if ($test eq "comp/redef.t") {
202		# Redefinition happens at compile time
203		next;
204	    }
205	    elsif ($test =~ m{lib/Switch/t/}) {
206		# B::Deparse doesn't support source filtering
207		next;
208	    }
209	}
210	$te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
211
212	if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
213	    print $te;
214	    $te = '';
215	}
216
217	$test = $OVER{$test} if exists $OVER{$test};
218
219 	open(SCRIPT,"<$test") or die "Can't run $test.\n";
220 	$_ = <SCRIPT>;
221 	close(SCRIPT) unless ($type eq 'deparse');
222 	if (/#!.*\bperl.*\s-\w*([tT])/) {
223 	    $switch = qq{"-$1"};
224 	}
225 	else {
226	    if ($taintwarn) {
227		# not all tests are expected to pass with this option
228		$switch = '"-t"';
229	    }
230	    else {
231		$switch = '';
232	    }
233 	}
234
235        my $test_executable; # for 'compile' tests
236	my $file_opts = "";
237	if ($type eq 'deparse') {
238	    # Look for #line directives which change the filename
239	    while (<SCRIPT>) {
240		$file_opts .= ",-f$3$4"
241			if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
242	    }
243	    close(SCRIPT);
244	}
245
246	my $utf = $with_utf ? '-I../lib -Mutf8' : '';
247	my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
248	if ($type eq 'deparse') {
249	    my $deparse =
250		"./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
251		"-l$deparse_opts$file_opts ".
252		"$test > $test.dp ".
253		"&& ./perl $testswitch $switch -I../lib $test.dp |";
254	    open(RESULTS, $deparse)
255		or print "can't deparse '$deparse': $!.\n";
256	}
257	elsif ($type eq 'bytecompile') {
258	    my ($pwd, $null);
259            if( $^O eq 'MSWin32') {
260		$pwd = `cd`;
261		$null = 'nul';
262	    } else {
263		$pwd = `pwd`;
264		$null = '/dev/null';
265	    }
266	    chomp $pwd;
267	    my $perl = $ENV{PERL} || "$pwd/perl";
268	    my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
269	    $bswitch .= "-TF$test.plc,"
270		if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
271	    $bswitch .= "-k,"
272		if $test =~ m(deparse|terse|ext/Storable/t/code);
273	    $bswitch .= "-b,"
274		if $test =~ m(op/getpid);
275	    my $bytecompile =
276		"$perl $testswitch $switch -I../lib $bswitch".
277		"-o$test.plc $test 2>$null &&".
278		"$perl $testswitch $switch -I../lib $utf $test.plc |";
279	    open(RESULTS,$bytecompile)
280		or print "can't byte-compile '$bytecompile': $!.\n";
281	}
282	elsif ($type eq 'perl') {
283	    my $perl = $ENV{PERL} || './perl';
284	    my $redir = $^O eq 'VMS' ? '2>&1' : '';
285	    if ($ENV{PERL_VALGRIND}) {
286		$perl = "valgrind --suppressions=perl.supp --leak-check=yes "
287                               . "--leak-resolution=high --show-reachable=yes "
288                               . "--num-callers=50 --logfile-fd=3 $perl";
289		$redir = "3>$valgrind_log";
290	    }
291	    my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
292	    open(RESULTS,$run) or print "can't run '$run': $!.\n";
293	}
294	else {
295	    my $compile;
296            my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
297              # -O9 for good measure, -fcog is broken ATM
298                       "$switch -Wb=-O9,-fno-cog -L .. " .
299                       "-I \".. ../lib/CORE\" $args $utf $test -o ";
300
301            if( $^O eq 'MSWin32' ) {
302                $test_executable = "$test.exe";
303                # hopefully unused name...
304                open HACK, "> xweghyz.pl";
305                print HACK <<EOT;
306#!./perl
307
308open HACK, '.\\perl $pl2c $test_executable |';
309# cl.exe prints the name of the .c file on stdout (\%^\$^#)
310while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
311open HACK, '$test_executable |';
312while(<HACK>) {print}
313EOT
314                close HACK;
315                $compile = 'xweghyz.pl |';
316            }
317            else {
318                $test_executable = "$test.plc";
319                $compile = "./perl $pl2c $test_executable && $test_executable |";
320            }
321            unlink $test_executable if -f $test_executable;
322	    open(RESULTS, $compile)
323		or print "can't compile '$compile': $!.\n";
324	}
325
326        $ok = 0;
327        $next = 0;
328        my $seen_leader = 0;
329        my $seen_ok = 0;
330	while (<RESULTS>) {
331	    next if /^\s*$/; # skip blank lines
332	    if ($verbose) {
333		print $_;
334	    }
335	    unless (/^\#/) {
336		if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
337		    $max = $1;
338                    %todo = map { $_ => 1 } split / /, $3 if $3;
339		    $totmax += $max;
340		    $files += 1;
341                    unless ($seen_ok) {
342                      $next = 1;
343                      $ok = 1;
344                    }
345                    $seen_leader = 1;
346		}
347		else {
348		    if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
349			unless ($seen_leader) {
350			    unless ($seen_ok) {
351				$next = 1;
352				$ok = 1;
353			    }
354			}
355			$seen_ok = 1;
356			if ($2 == $next) {
357			    my($not, $num, $extra) = ($1, $2, $3);
358			    my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
359			    $istodo = 1 if $todo{$num};
360
361			    if( $not && !$istodo ) {
362				$ok = 0;
363				$next = $num;
364				last;
365			    }
366			    else {
367				$next = $next + 1;
368			    }
369			}
370                    }
371                    elsif (/^Bail out!\s*(.*)/i) { # magic words
372                        die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
373		    }
374		    else {
375			$ok = 0;
376		    }
377		}
378	    }
379	}
380	close RESULTS;
381	if ($ENV{PERL_VALGRIND}) {
382	    my @valgrind;
383	    if (-e $valgrind_log) {
384		if (open(V, $valgrind_log)) {
385		    @valgrind = <V>;
386		    close V;
387		} else {
388		    warn "$0: Failed to open '$valgrind_log': $!\n";
389		}
390	    }
391	    if (@valgrind) {
392		my $leaks = 0;
393		my $errors = 0;
394		for my $i (0..$#valgrind) {
395		    local $_ = $valgrind[$i];
396		    if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
397			$errors += $1;   # there may be multiple error summaries
398		    } elsif (/^==\d+== LEAK SUMMARY:/) {
399			for my $off (1 .. 4) {
400			    if ($valgrind[$i+$off] =~
401				/(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
402				$leaks += $1;
403			    }
404			}
405		    }
406		}
407		if ($errors or $leaks) {
408		    if (rename $valgrind_log, "$test.valgrind") {
409			$valgrind++;
410		    } else {
411			warn "$0: Failed to create '$test.valgrind': $!\n";
412		    }
413		}
414	    } else {
415		warn "No valgrind output?\n";
416	    }
417	    if (-e $valgrind_log) {
418		unlink $valgrind_log
419		    or warn "$0: Failed to unlink '$valgrind_log': $!\n";
420	    }
421	}
422	if ($type eq 'deparse') {
423	    unlink "./$test.dp";
424	}
425	if ($ENV{PERL_3LOG}) {
426	    my $tpp = $test;
427	    $tpp =~ s:^\.\./::;
428	    $tpp =~ s:/:_:g;
429	    $tpp =~ s:\.t$:.3log:;
430	    rename("perl.3log", $tpp) ||
431		die "rename: perl3.log to $tpp: $!\n";
432	}
433	$next = $next - 1;
434        # test if the compiler compiled something
435        if( $type eq 'compile' && !-e "$test_executable" ) {
436            $ok = 0;
437            print "Test did not compile\n";
438        }
439	if ($ok && $next == $max ) {
440	    if ($max) {
441		print "${te}ok\n";
442		$good = $good + 1;
443	    }
444	    else {
445		print "${te}skipping test on this platform\n";
446		$files -= 1;
447	    }
448	}
449	else {
450	    $next += 1;
451	    print "${te}FAILED at test $next\n";
452	    $bad = $bad + 1;
453	    $_ = $test;
454	    if (/^base/) {
455		die "Failed a basic test--cannot continue.\n";
456	    }
457	}
458    }
459
460    if ($bad == 0) {
461	if ($ok) {
462	    print "All tests successful.\n";
463	    # XXX add mention of 'perlbug -ok' ?
464	}
465	else {
466	    die "FAILED--no tests were run for some reason.\n";
467	}
468    }
469    else {
470	$pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
471	if ($bad == 1) {
472	    warn "Failed 1 test script out of $files, $pct% okay.\n";
473	}
474	else {
475	    warn "Failed $bad test scripts out of $files, $pct% okay.\n";
476	}
477	warn <<'SHRDLU_1';
478### Since not all tests were successful, you may want to run some of
479### them individually and examine any diagnostic messages they produce.
480### See the INSTALL document's section on "make test".
481SHRDLU_1
482	warn <<'SHRDLU_2' if $good / $total > 0.8;
483### You have a good chance to get more information by running
484###   ./perl harness
485### in the 't' directory since most (>=80%) of the tests succeeded.
486SHRDLU_2
487        if (eval {require Config; import Config; 1}) {
488	    if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
489		warn <<SHRDLU_3;
490### You may have to set your dynamic library search path,
491### $p, to point to the build directory:
492SHRDLU_3
493                if (exists $ENV{$p} && $ENV{$p} ne '') {
494		    warn <<SHRDLU_4a;
495###   setenv $p `pwd`:\$$p; cd t; ./perl harness
496###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
497###   export $p=`pwd`:\$$p; cd t; ./perl harness
498SHRDLU_4a
499                } else {
500		    warn <<SHRDLU_4b;
501###   setenv $p `pwd`; cd t; ./perl harness
502###   $p=`pwd`; export $p; cd t; ./perl harness
503###   export $p=`pwd`; cd t; ./perl harness
504SHRDLU_4b
505                }
506		warn <<SHRDLU_5;
507### for csh-style shells, like tcsh; or for traditional/modern
508### Bourne-style shells, like bash, ksh, and zsh, respectively.
509SHRDLU_5
510            }
511	}
512    }
513    ($user,$sys,$cuser,$csys) = times;
514    print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
515	$user,$sys,$cuser,$csys,$files,$totmax);
516    if ($ENV{PERL_VALGRIND}) {
517	my $s = $valgrind == 1 ? '' : 's';
518	print "$valgrind valgrind report$s created.\n", ;
519    }
520}
521exit ($bad != 0);
522