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
CommitLineData
8d063cd8
LW
1#!./perl
2
8d063cd8
LW
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
a687059c
LW
6$| = 1;
7
5d9a6404
MS
8# Cheesy version of Getopt::Std. Maybe we should replace it with that.
9if ($#ARGV >= 0) {
10 foreach my $idx (0..$#ARGV) {
485988ae 11 next unless $ARGV[$idx] =~ /^-(\S+)$/;
5a6e071d 12 $core = 1 if $1 eq 'core';
5d9a6404
MS
13 $verbose = 1 if $1 eq 'v';
14 $with_utf= 1 if $1 eq 'utf8';
485988ae
RH
15 if ($1 =~ /^deparse(,.+)?$/) {
16 $deparse = 1;
17 $deparse_opts = $1;
18 }
5d9a6404
MS
19 splice(@ARGV, $idx, 1);
20 }
8d063cd8
LW
21}
22
378cc40b
LW
23chdir 't' if -f 't/TEST';
24
3e6e8be7 25die "You need to run \"make test\" first to set things up.\n"
4633a7c4
LW
26 unless -e 'perl' or -e 'perl.exe';
27
7a315204 28if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
09187cb1
JH
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
3fb91a5e
GS
43# check leakage for embedders
44$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
45
4633a7c4 46$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 47
24c841ba
MS
48# Roll your own File::Find!
49use TestInit;
50use File::Spec;
51my $curdir = File::Spec->curdir;
52my $updir = File::Spec->updir;
53
54sub _find_tests {
55 my($dir) = @_;
56 opendir DIR, $dir || die "Trouble opening $dir: $!";
a1886d87 57 foreach my $f (sort { $a cmp $b } readdir DIR) {
24c841ba
MS
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
67unless (@ARGV) {
5a6e071d 68 foreach my $dir (qw(base comp cmd run io op)) {
24c841ba
MS
69 _find_tests($dir);
70 }
5a6e071d 71 _find_tests("lib") unless $core;
7a315204
JH
72 my $mani = File::Spec->catdir($updir, "MANIFEST");
73 if (open(MANI, $mani)) {
80ffb5f9 74 while (<MANI>) { # similar code in t/harness
b695f709 75 if (m!^(ext/\S+/([^/]+\.t|test\.pl)|lib/\S+?(\.t|test\.pl))\s!) {
5a6e071d
PJ
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 }
7a315204
JH
82 }
83 }
84 } else {
85 warn "$0: cannot open $mani: $!\n";
86 }
b695f709 87 _find_tests('pod');
8d063cd8
LW
88}
89
7a315204 90# Tests known to cause infinite loops for the perlcc tests.
595ae481 91# %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
24c841ba 92%infinite = ();
6ee623d5 93
485988ae
RH
94if ($deparse) {
95 _testprogs('deparse', @ARGV);
96} else {
97 _testprogs('perl', @ARGV);
98 _testprogs('compile', @ARGV) if (-e "../testcompile");
99}
6ee623d5 100
bb365837
GS
101sub _testprogs {
102 $type = shift @_;
103 @tests = @_;
6ee623d5 104
bb365837 105 print <<'EOT' if ($type eq 'compile');
7a315204 106------------------------------------------------------------------------------
6ee623d5 107TESTING COMPILER
7a315204 108------------------------------------------------------------------------------
bb365837
GS
109EOT
110
485988ae 111 print <<'EOT' if ($type eq 'deparse');
7a315204 112------------------------------------------------------------------------------
485988ae 113TESTING DEPARSER
7a315204 114------------------------------------------------------------------------------
485988ae
RH
115EOT
116
595ae481 117 $ENV{PERLCC_TIMEOUT} = 120
9636a016 118 if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
ef712cf7 119
bb365837
GS
120 $bad = 0;
121 $good = 0;
122 $total = @tests;
123 $files = 0;
124 $totmax = 0;
908801fe
JH
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;
088b5126 133 }
908801fe
JH
134 # + 3 : we want three dots between the test name and the "ok"
135 $dotdotdot = $maxlen + 3 - $maxsuflen;
bb365837
GS
136 while ($test = shift @tests) {
137
138 if ( $infinite{$test} && $type eq 'compile' ) {
595ae481 139 print STDERR "$test creates infinite loop! Skipping.\n";
bb365837 140 next;
6ee623d5 141 }
bb365837
GS
142 if ($test =~ /^$/) {
143 next;
6ee623d5 144 }
485988ae
RH
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 }
bb365837 155 $te = $test;
b695f709 156 $te =~ s/\.\w+$/./;
088b5126 157 print "$te" . '.' x ($dotdotdot - length($te));
bb365837 158
7a315204
JH
159 $test = $OVER{$test} if exists $OVER{$test};
160
d638aca2
GS
161 open(SCRIPT,"<$test") or die "Can't run $test.\n";
162 $_ = <SCRIPT>;
485988ae 163 close(SCRIPT) unless ($type eq 'deparse');
d638aca2
GS
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;
55497cff 169 }
135863df 170 }
bb365837 171 else {
d638aca2
GS
172 $switch = '';
173 }
6ee623d5 174
485988ae
RH
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 }
7a315204
JH
184
185 $test = $OVER{$test} if exists $OVER{$test};
186
187 my $utf = $with_utf ? '-I../lib -Mutf8' : '';
4343e7c3 188 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae
RH
189 if ($type eq 'deparse') {
190 my $deparse =
191 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,".
192 "-l$deparse_opts$file_opts ".
7a315204
JH
193 "$test > $test.dp ".
194 "&& ./perl $testswitch $switch -I../lib $test.dp |";
485988ae
RH
195 open(RESULTS, $deparse)
196 or print "can't deparse '$deparse': $!.\n";
197 }
198 elsif ($type eq 'perl') {
a7da9a42
JH
199 my $perl = $ENV{PERL} || './perl';
200 my $run = "$perl $testswitch $switch $utf $test |";
be24517c 201 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2
GS
202 }
203 else {
be24517c 204 my $compile =
4343e7c3 205 "./perl $testswitch -I../lib ../utils/perlcc -o ".
7a315204
JH
206 "$test.plc $utf $test ".
207 " && $test.plc |";
be24517c
JH
208 open(RESULTS, $compile)
209 or print "can't compile '$compile': $!.\n";
7a315204 210 unlink "$test.plc";
6ee623d5 211 }
d638aca2 212
bb365837
GS
213 $ok = 0;
214 $next = 0;
215 while (<RESULTS>) {
216 if ($verbose) {
217 print $_;
218 }
219 unless (/^#/) {
809908f7 220 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
bb365837 221 $max = $1;
809908f7 222 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837
GS
223 $totmax += $max;
224 $files += 1;
225 $next = 1;
226 $ok = 1;
227 }
228 else {
37ce32a7 229 if (/^(not )?ok (\d+)(\s*#.*)?/ &&
595ae481 230 $2 == $next)
37ce32a7
MS
231 {
232 my($not, $num, $extra) = ($1, $2, $3);
233 my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
809908f7 234 $istodo = 1 if $todo{$num};
37ce32a7
MS
235
236 if( $not && !$istodo ) {
237 $ok = 0;
238 $next = $num;
239 last;
240 }
241 else {
242 $next = $next + 1;
243 }
d667a7e6
A
244 }
245 elsif (/^Bail out!\s*(.*)/i) { # magic words
246 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837
GS
247 }
248 else {
249 $ok = 0;
250 }
8d063cd8
LW
251 }
252 }
253 }
bb365837 254 close RESULTS;
485988ae
RH
255 if ($type eq 'deparse') {
256 unlink "./$test.dp";
257 }
211f317f
JH
258 if ($ENV{PERL_3LOG}) {
259 my $tpp = $test;
a7da9a42 260 $tpp =~ s:^../::;
211f317f
JH
261 $tpp =~ s:/:_:g;
262 $tpp =~ s:\.t$::;
a7da9a42
JH
263 rename("perl.3log", "perl.3log.$tpp") ||
264 die "rename: perl3.log to perl.3log.$tpp: $!\n";
211f317f 265 }
bb365837
GS
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 }
bcce72a7 276 }
bb365837
GS
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 }
8d063cd8
LW
285 }
286 }
8d063cd8 287
bb365837
GS
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 }
8d063cd8 296 }
bb365837 297 else {
ba1398cf 298 $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
bb365837 299 if ($bad == 1) {
e824fb2c 300 warn "Failed 1 test script out of $files, $pct% okay.\n";
bb365837
GS
301 }
302 else {
e824fb2c 303 warn "Failed $bad test scripts out of $files, $pct% okay.\n";
bb365837
GS
304 }
305 warn <<'SHRDLU';
f46c10df
CS
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".
595ae481
NIS
309 ### If you are testing the compiler, then ignore this message
310 ### and run
6ee623d5
GS
311 ### ./perl harness
312 ### in the directory ./t.
f46c10df 313SHRDLU
bb365837 314 warn <<'SHRDLU' if $good / $total > 0.8;
3e6e8be7
MB
315 ###
316 ### Since most tests were successful, you have a good chance to
317 ### get information with better granularity by running
595ae481 318 ### ./perl harness
3e6e8be7
MB
319 ### in directory ./t.
320SHRDLU
bb365837
GS
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);
6ee623d5 325}
3e6e8be7 326exit ($bad != 0);