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