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