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