User-defined character properties were unintentionally
[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 uni)) {
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')  unless $core;
96     _find_tests('x2p')  unless $core;
97     _find_tests('japh') unless $core;
98 }
99
100 # Tests known to cause infinite loops for the perlcc tests.
101 # %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
102 %infinite = ();
103
104 if ($deparse) {
105     _testprogs('deparse', '',   @ARGV);
106 }
107 elsif( $compile || $byte_compile ) { 
108     _testprogs('compile', '',   @ARGV) if $compile;
109     _testprogs('compile', '-B', @ARGV) if $byte_compile;
110 }
111 else {
112     _testprogs('compile', '',   @ARGV) if -e "../testcompile";
113     _testprogs('perl',    '',   @ARGV);
114 }
115
116 sub _testprogs {
117     $type = shift @_;
118     $args = shift;
119     @tests = @_;
120
121     print <<'EOT' if ($type eq 'compile');
122 ------------------------------------------------------------------------------
123 TESTING COMPILER
124 ------------------------------------------------------------------------------
125 EOT
126
127     print <<'EOT' if ($type eq 'deparse');
128 ------------------------------------------------------------------------------
129 TESTING DEPARSER
130 ------------------------------------------------------------------------------
131 EOT
132
133     $ENV{PERLCC_TIMEOUT} = 120
134           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
135
136     $bad = 0;
137     $good = 0;
138     $total = @tests;
139     $files  = 0;
140     $totmax = 0;
141
142     foreach (@tests) {
143         $name{$_} = File::Spec->catdir('t',$_) unless exists $name{$_};
144     }
145     my $maxlen = 0;
146     foreach (@name{@tests}) {
147         s/\.\w+\z/./;
148         my $len = length ;
149         $maxlen = $len if $len > $maxlen;
150     }
151     # + 3 : we want three dots between the test name and the "ok"
152     $dotdotdot = $maxlen + 3 ;
153     while ($test = shift @tests) {
154
155         if ( $infinite{$test} && $type eq 'compile' ) {
156             print STDERR "$test creates infinite loop! Skipping.\n";
157             next;
158         }
159         if ($test =~ /^$/) {
160             next;
161         }
162         if ($type eq 'deparse') {
163             if ($test eq "comp/redef.t") {
164                 # Redefinition happens at compile time
165                 next;
166             }
167             elsif ($test eq "lib/switch.t") {
168                 # B::Deparse doesn't support source filtering
169                 next;
170             }
171         }
172         $te = $name{$test};
173         print "$te" . '.' x ($dotdotdot - length($te));
174
175         $test = $OVER{$test} if exists $OVER{$test};
176
177         open(SCRIPT,"<$test") or die "Can't run $test.\n";
178         $_ = <SCRIPT>;
179         close(SCRIPT) unless ($type eq 'deparse');
180         if (/#!.*\bperl.*-\w*([tT])/) {
181             $switch = qq{"-$1"};
182         }
183         else {
184             $switch = '';
185         }
186
187         my $test_executable; # for 'compile' tests
188         my $file_opts = "";
189         if ($type eq 'deparse') {
190             # Look for #line directives which change the filename
191             while (<SCRIPT>) {
192                 $file_opts .= ",-f$3$4"
193                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
194             }
195             close(SCRIPT);
196         }
197
198         my $utf = $with_utf ? '-I../lib -Mutf8' : '';
199         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
200         if ($type eq 'deparse') {
201             my $deparse =
202                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,".
203                 "-l$deparse_opts$file_opts ".
204                 "$test > $test.dp ".
205                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
206             open(RESULTS, $deparse)
207                 or print "can't deparse '$deparse': $!.\n";
208         }
209         elsif ($type eq 'perl') {
210             my $perl = $ENV{PERL} || './perl';
211             my $run = "$perl $testswitch $switch $utf $test |";
212             open(RESULTS,$run) or print "can't run '$run': $!.\n";
213         }
214         else {
215             my $compile;
216             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
217               # -O9 for good measure, -fcog is broken ATM
218                        "$switch -Wb=-O9,-fno-cog -L .. " .
219                        "-I \".. ../lib/CORE\" $args $utf $test -o ";
220
221             if( $^O eq 'MSWin32' ) {
222                 $test_executable = "$test.exe";
223                 # hopefully unused name...
224                 open HACK, "> xweghyz.pl";
225                 print HACK <<EOT;
226 #!./perl
227
228 open HACK, '.\\perl $pl2c $test_executable |';
229 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
230 while(<HACK>) {m/^\w+\.[cC]\$/ && next;print}
231 open HACK, '$test_executable |';
232 while(<HACK>) {print}
233 EOT
234                 close HACK;
235                 $compile = 'xweghyz.pl |';
236             }
237             else {
238                 $test_executable = "$test.plc";
239                 $compile = "./perl $pl2c $test_executable && $test_executable |";
240             }
241             unlink $test_executable if -f $test_executable;
242             open(RESULTS, $compile)
243                 or print "can't compile '$compile': $!.\n";
244         }
245
246         $ok = 0;
247         $next = 0;
248         while (<RESULTS>) {
249             if ($verbose) {
250                 print $_;
251             }
252             unless (/^#/) {
253                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
254                     $max = $1;
255                     %todo = map { $_ => 1 } split / /, $3 if $3;
256                     $totmax += $max;
257                     $files += 1;
258                     $next = 1;
259                     $ok = 1;
260                 }
261                 else {
262                     if (/^(not )?ok (\d+)[^#]*(\s*#.*)?/ &&
263                         $2 == $next)
264                     {
265                         my($not, $num, $extra) = ($1, $2, $3);
266                         my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
267                         $istodo = 1 if $todo{$num};
268
269                         if( $not && !$istodo ) {
270                             $ok = 0;
271                             $next = $num;
272                             last;
273                         }
274                         else {
275                             $next = $next + 1;
276                         }
277                     }
278                     elsif (/^Bail out!\s*(.*)/i) { # magic words
279                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
280                     }
281                     else {
282                         $ok = 0;
283                     }
284                 }
285             }
286         }
287         close RESULTS;
288         if ($type eq 'deparse') {
289             unlink "./$test.dp";
290         }
291         if ($ENV{PERL_3LOG}) {
292             my $tpp = $test;
293             $tpp =~ s:^../::;
294             $tpp =~ s:/:_:g;
295             $tpp =~ s:\.t$::;
296             rename("perl.3log", "perl.3log.$tpp") ||
297                 die "rename: perl3.log to perl.3log.$tpp: $!\n";
298         }
299         $next = $next - 1;
300         # test if the compiler compiled something
301         if( $type eq 'compile' && !-e "$test_executable" ) {
302             $ok = 0;
303             print "Test did not compile\n";
304         }
305         if ($ok && $next == $max ) {
306             if ($max) {
307                 print "ok\n";
308                 $good = $good + 1;
309             }
310             else {
311                 print "skipping test on this platform\n";
312                 $files -= 1;
313             }
314         }
315         else {
316             $next += 1;
317             print "FAILED at test $next\n";
318             $bad = $bad + 1;
319             $_ = $test;
320             if (/^base/) {
321                 die "Failed a basic test--cannot continue.\n";
322             }
323         }
324     }
325
326     if ($bad == 0) {
327         if ($ok) {
328             print "All tests successful.\n";
329             # XXX add mention of 'perlbug -ok' ?
330         }
331         else {
332             die "FAILED--no tests were run for some reason.\n";
333         }
334     }
335     else {
336         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
337         if ($bad == 1) {
338             warn "Failed 1 test script out of $files, $pct% okay.\n";
339         }
340         else {
341             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
342         }
343         warn <<'SHRDLU_1';
344 ### Since not all tests were successful, you may want to run some of
345 ### them individually and examine any diagnostic messages they produce.
346 ### See the INSTALL document's section on "make test".
347 SHRDLU_1
348         warn <<'SHRDLU_2' if $good / $total > 0.8;
349 ### You have a good chance to get more information by running
350 ###   ./perl harness
351 ### in the 't' directory since most (>=80%) of the tests succeeded.
352 SHRDLU_2
353         if (eval {require Config; import Config; 1}) {
354             if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
355                 warn <<SHRDLU_3;
356 ### You may have to set your dynamic library search path,
357 ### $p, to point to the build directory:
358 SHRDLU_3
359                 if (exists $ENV{$p} && $ENV{$p} ne '') {
360                     warn <<SHRDLU_4a;
361 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
362 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
363 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
364 SHRDLU_4a
365                 } else {
366                     warn <<SHRDLU_4b;
367 ###   setenv $p `pwd`; cd t; ./perl harness
368 ###   $p=`pwd`; export $p; cd t; ./perl harness
369 ###   export $p=`pwd`; cd t; ./perl harness
370 SHRDLU_4b
371                 }    
372                 warn <<SHRDLU_5;
373 ### for csh-style shells, like tcsh; or for traditional/modern
374 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
375 SHRDLU_5
376             }
377         }
378     }
379     ($user,$sys,$cuser,$csys) = times;
380     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
381         $user,$sys,$cuser,$csys,$files,$totmax);
382 }
383 exit ($bad != 0);