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