Fix for ID 20010519.003: sysopen() wasn't tainting :-(
[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}) {
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 if ($#ARGV == -1) {
48     @ARGV = split(/[ \n]/,
49       `echo base/*.t comp/*.t cmd/*.t run/*.t io/*.t; echo op/*.t pragma/*.t lib/*.t pod/*.t`);
50 }
51
52 # %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
53
54 if ($deparse) {
55     _testprogs('deparse', @ARGV);
56 } else {
57     _testprogs('perl', @ARGV);
58     _testprogs('compile', @ARGV) if (-e "../testcompile");
59 }
60
61 sub _testprogs {
62     $type = shift @_;
63     @tests = @_;
64
65
66     print <<'EOT' if ($type eq 'compile');
67 --------------------------------------------------------------------------------
68 TESTING COMPILER
69 --------------------------------------------------------------------------------
70 EOT
71
72     print <<'EOT' if ($type eq 'deparse');
73 --------------------------------------------------------------------------------
74 TESTING DEPARSER
75 --------------------------------------------------------------------------------
76 EOT
77
78     $ENV{PERLCC_TIMEOUT} = 120
79           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
80
81     $bad = 0;
82     $good = 0;
83     $total = @tests;
84     $files  = 0;
85     $totmax = 0;
86     $maxlen = 0;
87     foreach (@tests) {
88         $len = length;
89         $maxlen = $len if $len > $maxlen;
90     }
91     # +3 : we want three dots between the test name and the "ok"
92     # -2 : the .t suffix
93     $dotdotdot = $maxlen + 3 - 2;
94     while ($test = shift @tests) {
95
96         if ( $infinite{$test} && $type eq 'compile' ) {
97             print STDERR "$test creates infinite loop! Skipping.\n";
98             next;
99         }
100         if ($test =~ /^$/) {
101             next;
102         }
103         if ($type eq 'deparse') {
104             if ($test eq "comp/redef.t") {
105                 # Redefinition happens at compile time
106                 next;
107             }
108             elsif ($test eq "lib/switch.t") {
109                 # B::Deparse doesn't support source filtering
110                 next;
111             }
112         }
113         $te = $test;
114         chop($te);
115         print "$te" . '.' x ($dotdotdot - length($te));
116
117         open(SCRIPT,"<$test") or die "Can't run $test.\n";
118         $_ = <SCRIPT>;
119         close(SCRIPT) unless ($type eq 'deparse');
120         if (/#!.*perl(.*)$/) {
121             $switch = $1;
122             if ($^O eq 'VMS') {
123                 # Must protect uppercase switches with "" on command line
124                 $switch =~ s/-([A-Z]\S*)/"-$1"/g;
125             }
126         }
127         else {
128             $switch = '';
129         }
130
131         my $file_opts = "";
132         if ($type eq 'deparse') {
133             # Look for #line directives which change the filename
134             while (<SCRIPT>) {
135                 $file_opts .= ",-f$3$4"
136                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
137             }
138             close(SCRIPT);
139         }
140         my $utf = $with_utf ? '-I../lib -Mutf8'
141                             : '';
142         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
143         if ($type eq 'deparse') {
144             my $deparse =
145                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,".
146                 "-l$deparse_opts$file_opts ".
147                 "./$test > ./$test.dp ".
148                 "&& ./perl $testswitch $switch -I../lib ./$test.dp |";
149             open(RESULTS, $deparse)
150                 or print "can't deparse '$deparse': $!.\n";
151         }
152         elsif ($type eq 'perl') {
153             my $run = "./perl $testswitch $switch $utf $test |";
154             open(RESULTS,$run) or print "can't run '$run': $!.\n";
155         }
156         else {
157             my $compile =
158                 "./perl $testswitch -I../lib ../utils/perlcc -o ".
159                 "./$test.plc $utf ./$test ".
160                 " && ./$test.plc |";
161             open(RESULTS, $compile)
162                 or print "can't compile '$compile': $!.\n";
163             unlink "./$test.plc";
164         }
165
166         $ok = 0;
167         $next = 0;
168         while (<RESULTS>) {
169             if ($verbose) {
170                 print $_;
171             }
172             unless (/^#/) {
173                 if (/^1\.\.([0-9]+)/) {
174                     $max = $1;
175                     $totmax += $max;
176                     $files += 1;
177                     $next = 1;
178                     $ok = 1;
179                 }
180                 else {
181                     if (/^(not )?ok (\d+)(\s*#.*)?/ &&
182                         $2 == $next)
183                     {
184                         my($not, $num, $extra) = ($1, $2, $3);
185                         my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
186
187                         if( $not && !$istodo ) {
188                             $ok = 0;
189                             $next = $num;
190                             last;
191                         }
192                         else {
193                             $next = $next + 1;
194                         }
195                     }
196                     elsif (/^Bail out!\s*(.*)/i) { # magic words
197                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
198                     }
199                     else {
200                         $ok = 0;
201                     }
202                 }
203             }
204         }
205         close RESULTS;
206         if ($type eq 'deparse') {
207             unlink "./$test.dp";
208         }
209         if ($ENV{PERL_3LOG}) {
210             my $tpp = $test;
211             $tpp =~ s:/:_:g;
212             $tpp =~ s:\.t$::;
213             rename("perl.3log", "perl.3log.$tpp");
214         }
215         $next = $next - 1;
216         if ($ok && $next == $max) {
217             if ($max) {
218                 print "ok\n";
219                 $good = $good + 1;
220             }
221             else {
222                 print "skipping test on this platform\n";
223                 $files -= 1;
224             }
225         }
226         else {
227             $next += 1;
228             print "FAILED at test $next\n";
229             $bad = $bad + 1;
230             $_ = $test;
231             if (/^base/) {
232                 die "Failed a basic test--cannot continue.\n";
233             }
234         }
235     }
236
237     if ($bad == 0) {
238         if ($ok) {
239             print "All tests successful.\n";
240             # XXX add mention of 'perlbug -ok' ?
241         }
242         else {
243             die "FAILED--no tests were run for some reason.\n";
244         }
245     }
246     else {
247         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
248         if ($bad == 1) {
249             warn "Failed 1 test script out of $files, $pct% okay.\n";
250         }
251         else {
252             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
253         }
254         warn <<'SHRDLU';
255    ### Since not all tests were successful, you may want to run some
256    ### of them individually and examine any diagnostic messages they
257    ### produce.  See the INSTALL document's section on "make test".
258    ### If you are testing the compiler, then ignore this message
259    ### and run
260    ###      ./perl harness
261    ### in the directory ./t.
262 SHRDLU
263         warn <<'SHRDLU' if $good / $total > 0.8;
264    ###
265    ### Since most tests were successful, you have a good chance to
266    ### get information with better granularity by running
267    ###     ./perl harness
268    ### in directory ./t.
269 SHRDLU
270     }
271     ($user,$sys,$cuser,$csys) = times;
272     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
273         $user,$sys,$cuser,$csys,$files,$totmax);
274 }
275 exit ($bad != 0);