New try at the Class::DBI core dump at global cleanup.
[p5sagit/p5-mst-13.2.git] / t / TEST
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 = ();
17 if ($#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         $with_utf= 1 if $1 eq 'utf8';
23         $byte_compile = 1 if $1 eq 'bytecompile';
24         $compile = 1 if $1 eq 'compile';
25         if ($1 =~ /^deparse(,.+)?$/) {
26             $deparse = 1;
27             $deparse_opts = $1;
28         }
29     }
30 }
31 @ARGV = @argv;
32
33 chdir 't' if -f 't/TEST';
34
35 die "You need to run \"make test\" first to set things up.\n"
36   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
37
38 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
39     unless (-x 'perl.third') {
40         unless (-x '../perl.third') {
41             die "You need to run \"make perl.third first.\n";
42         }
43         else {
44             print "Symlinking ../perl.third as perl.third...\n";
45             die "Failed to symlink: $!\n"
46                 unless symlink("../perl.third", "perl.third");
47             die "Symlinked but no executable perl.third: $!\n"
48                 unless -x 'perl.third';
49         }
50     }
51 }
52
53 # check leakage for embedders
54 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
55
56 $ENV{EMXSHELL} = 'sh';        # For OS/2
57
58 # Roll your own File::Find!
59 use TestInit;
60 use File::Spec;
61 my $curdir = File::Spec->curdir;
62 my $updir  = File::Spec->updir;
63
64 sub _find_tests {
65     my($dir) = @_;
66     opendir DIR, $dir || die "Trouble opening $dir: $!";
67     foreach my $f (sort { $a cmp $b } readdir DIR) {
68         next if $f eq $curdir or $f eq $updir;
69
70         my $fullpath = File::Spec->catfile($dir, $f);
71
72         _find_tests($fullpath) if -d $fullpath;
73         $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
74         push @ARGV, $fullpath if $f =~ /\.t$/;
75     }
76 }
77
78 sub _quote_args {
79     my ($args) = @_;
80     my $argstring = '';
81
82     foreach (split(/\s+/,$args)) {
83        # In VMS protect with doublequotes because otherwise
84        # DCL will lowercase -- unless already doublequoted.
85        $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
86        $argstring .= ' ' . $_;
87     }
88     return $argstring;
89 }
90
91 unless (@ARGV) {
92     foreach my $dir (qw(base comp cmd run io op uni)) {
93         _find_tests($dir);
94     }
95     _find_tests("lib") unless $core;
96     my $mani = File::Spec->catfile($updir, "MANIFEST");
97     if (open(MANI, $mani)) {
98         while (<MANI>) { # similar code in t/harness
99             if (m!^(ext/\S+/?([^/]+\.t|test\.pl)|lib/\S+?(\.t|test\.pl))\s!) {
100                 $t = $1;
101                 if (!$core || $t =~ m!^lib/[a-z]!)
102                 {
103                     $path = File::Spec->catfile($updir, $t);
104                     push @ARGV, $path;
105                     $name{$path} = $t;
106                 }
107             }
108         }
109         close MANI;
110     } else {
111         warn "$0: cannot open $mani: $!\n";
112     }
113     _find_tests('pod')  unless $core;
114     _find_tests('x2p')  unless $core;
115     _find_tests('japh') unless $core;
116 }
117
118 # Tests known to cause infinite loops for the perlcc tests.
119 # %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
120 %infinite = ();
121
122 if ($deparse) {
123     _testprogs('deparse', '',   @ARGV);
124 }
125 elsif( $compile || $byte_compile ) { 
126     _testprogs('compile', '',   @ARGV) if $compile;
127     _testprogs('compile', '-B', @ARGV) if $byte_compile;
128 }
129 else {
130     _testprogs('compile', '',   @ARGV) if -e "../testcompile";
131     _testprogs('perl',    '',   @ARGV);
132 }
133
134 sub _testprogs {
135     $type = shift @_;
136     $args = shift;
137     @tests = @_;
138
139     print <<'EOT' if ($type eq 'compile');
140 ------------------------------------------------------------------------------
141 TESTING COMPILER
142 ------------------------------------------------------------------------------
143 EOT
144
145     print <<'EOT' if ($type eq 'deparse');
146 ------------------------------------------------------------------------------
147 TESTING DEPARSER
148 ------------------------------------------------------------------------------
149 EOT
150
151     $ENV{PERLCC_TIMEOUT} = 120
152           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
153
154     $bad = 0;
155     $good = 0;
156     $total = @tests;
157     $files  = 0;
158     $totmax = 0;
159
160     foreach my $t (@tests) {
161       unless (exists $name{$t}) {
162         my $tname = File::Spec->catfile('t',$t);
163         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
164         $name{$t} = $tname;
165       }
166     }
167     my $maxlen = 0;
168     foreach (@name{@tests}) {
169         s/\.\w+\z/./;
170         my $len = length ;
171         $maxlen = $len if $len > $maxlen;
172     }
173     # + 3 : we want three dots between the test name and the "ok"
174     $dotdotdot = $maxlen + 3 ;
175     while ($test = shift @tests) {
176
177         if ( $infinite{$test} && $type eq 'compile' ) {
178             print STDERR "$test creates infinite loop! Skipping.\n";
179             next;
180         }
181         if ($test =~ /^$/) {
182             next;
183         }
184         if ($type eq 'deparse') {
185             if ($test eq "comp/redef.t") {
186                 # Redefinition happens at compile time
187                 next;
188             }
189             elsif ($test eq "lib/switch.t") {
190                 # B::Deparse doesn't support source filtering
191                 next;
192             }
193         }
194         $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
195
196         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
197             print $te;
198             $te = '';
199         }
200
201         $test = $OVER{$test} if exists $OVER{$test};
202
203         open(SCRIPT,"<$test") or die "Can't run $test.\n";
204         $_ = <SCRIPT>;
205         close(SCRIPT) unless ($type eq 'deparse');
206         if (/#!.*\bperl.*-\w*([tT])/) {
207             $switch = qq{"-$1"};
208         }
209         else {
210             $switch = '';
211         }
212
213         my $test_executable; # for 'compile' tests
214         my $file_opts = "";
215         if ($type eq 'deparse') {
216             # Look for #line directives which change the filename
217             while (<SCRIPT>) {
218                 $file_opts .= ",-f$3$4"
219                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
220             }
221             close(SCRIPT);
222         }
223
224         my $utf = $with_utf ? '-I../lib -Mutf8' : '';
225         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
226         if ($type eq 'deparse') {
227             my $deparse =
228                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,".
229                 "-l$deparse_opts$file_opts ".
230                 "$test > $test.dp ".
231                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
232             open(RESULTS, $deparse)
233                 or print "can't deparse '$deparse': $!.\n";
234         }
235         elsif ($type eq 'perl') {
236             my $perl = $ENV{PERL} || './perl';
237             my $redir = ($^O eq 'VMS' ? '2>&1' : '');
238             my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
239             open(RESULTS,$run) or print "can't run '$run': $!.\n";
240         }
241         else {
242             my $compile;
243             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
244               # -O9 for good measure, -fcog is broken ATM
245                        "$switch -Wb=-O9,-fno-cog -L .. " .
246                        "-I \".. ../lib/CORE\" $args $utf $test -o ";
247
248             if( $^O eq 'MSWin32' ) {
249                 $test_executable = "$test.exe";
250                 # hopefully unused name...
251                 open HACK, "> xweghyz.pl";
252                 print HACK <<EOT;
253 #!./perl
254
255 open HACK, '.\\perl $pl2c $test_executable |';
256 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
257 while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
258 open HACK, '$test_executable |';
259 while(<HACK>) {print}
260 EOT
261                 close HACK;
262                 $compile = 'xweghyz.pl |';
263             }
264             else {
265                 $test_executable = "$test.plc";
266                 $compile = "./perl $pl2c $test_executable && $test_executable |";
267             }
268             unlink $test_executable if -f $test_executable;
269             open(RESULTS, $compile)
270                 or print "can't compile '$compile': $!.\n";
271         }
272
273         $ok = 0;
274         $next = 0;
275         while (<RESULTS>) {
276             next if /^\s*$/; # skip blank lines
277             if ($verbose) {
278                 print $_;
279             }
280             unless (/^#/) {
281                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
282                     $max = $1;
283                     %todo = map { $_ => 1 } split / /, $3 if $3;
284                     $totmax += $max;
285                     $files += 1;
286                     $next = 1;
287                     $ok = 1;
288                 }
289                 else {
290                     if (/^(not )?ok (\d+)[^#]*(\s*#.*)?/ &&
291                         $2 == $next)
292                     {
293                         my($not, $num, $extra) = ($1, $2, $3);
294                         my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
295                         $istodo = 1 if $todo{$num};
296
297                         if( $not && !$istodo ) {
298                             $ok = 0;
299                             $next = $num;
300                             last;
301                         }
302                         else {
303                             $next = $next + 1;
304                         }
305                     }
306                     elsif (/^Bail out!\s*(.*)/i) { # magic words
307                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
308                     }
309                     else {
310                         $ok = 0;
311                     }
312                 }
313             }
314         }
315         close RESULTS;
316         if ($type eq 'deparse') {
317             unlink "./$test.dp";
318         }
319         if ($ENV{PERL_3LOG}) {
320             my $tpp = $test;
321             $tpp =~ s:^../::;
322             $tpp =~ s:/:_:g;
323             $tpp =~ s:\.t$::;
324             rename("perl.3log", "perl.3log.$tpp") ||
325                 die "rename: perl3.log to perl.3log.$tpp: $!\n";
326         }
327         $next = $next - 1;
328         # test if the compiler compiled something
329         if( $type eq 'compile' && !-e "$test_executable" ) {
330             $ok = 0;
331             print "Test did not compile\n";
332         }
333         if ($ok && $next == $max ) {
334             if ($max) {
335                 print "${te}ok\n";
336                 $good = $good + 1;
337             }
338             else {
339                 print "${te}skipping test on this platform\n";
340                 $files -= 1;
341             }
342         }
343         else {
344             $next += 1;
345             print "${te}FAILED at test $next\n";
346             $bad = $bad + 1;
347             $_ = $test;
348             if (/^base/) {
349                 die "Failed a basic test--cannot continue.\n";
350             }
351         }
352     }
353
354     if ($bad == 0) {
355         if ($ok) {
356             print "All tests successful.\n";
357             # XXX add mention of 'perlbug -ok' ?
358         }
359         else {
360             die "FAILED--no tests were run for some reason.\n";
361         }
362     }
363     else {
364         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
365         if ($bad == 1) {
366             warn "Failed 1 test script out of $files, $pct% okay.\n";
367         }
368         else {
369             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
370         }
371         warn <<'SHRDLU_1';
372 ### Since not all tests were successful, you may want to run some of
373 ### them individually and examine any diagnostic messages they produce.
374 ### See the INSTALL document's section on "make test".
375 SHRDLU_1
376         warn <<'SHRDLU_2' if $good / $total > 0.8;
377 ### You have a good chance to get more information by running
378 ###   ./perl harness
379 ### in the 't' directory since most (>=80%) of the tests succeeded.
380 SHRDLU_2
381         if (eval {require Config; import Config; 1}) {
382             if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
383                 warn <<SHRDLU_3;
384 ### You may have to set your dynamic library search path,
385 ### $p, to point to the build directory:
386 SHRDLU_3
387                 if (exists $ENV{$p} && $ENV{$p} ne '') {
388                     warn <<SHRDLU_4a;
389 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
390 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
391 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
392 SHRDLU_4a
393                 } else {
394                     warn <<SHRDLU_4b;
395 ###   setenv $p `pwd`; cd t; ./perl harness
396 ###   $p=`pwd`; export $p; cd t; ./perl harness
397 ###   export $p=`pwd`; cd t; ./perl harness
398 SHRDLU_4b
399                 }    
400                 warn <<SHRDLU_5;
401 ### for csh-style shells, like tcsh; or for traditional/modern
402 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
403 SHRDLU_5
404             }
405         }
406     }
407     ($user,$sys,$cuser,$csys) = times;
408     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
409         $user,$sys,$cuser,$csys,$files,$totmax);
410 }
411 exit ($bad != 0);