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