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