This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tests for #14098.
[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
60e23f2f
MS
8# Let tests know they're running in the perl core. Useful for modules
9# which live dual lives on CPAN.
10$ENV{PERL_CORE} = 1;
11
5d9a6404
MS
12# Cheesy version of Getopt::Std. Maybe we should replace it with that.
13if ($#ARGV >= 0) {
14 foreach my $idx (0..$#ARGV) {
485988ae 15 next unless $ARGV[$idx] =~ /^-(\S+)$/;
5a6e071d 16 $core = 1 if $1 eq 'core';
5d9a6404
MS
17 $verbose = 1 if $1 eq 'v';
18 $with_utf= 1 if $1 eq 'utf8';
f193aa2f
MS
19 $byte_compile = 1 if $1 eq 'bytecompile';
20 $compile = 1 if $1 eq 'compile';
485988ae
RH
21 if ($1 =~ /^deparse(,.+)?$/) {
22 $deparse = 1;
23 $deparse_opts = $1;
24 }
5d9a6404
MS
25 splice(@ARGV, $idx, 1);
26 }
8d063cd8
LW
27}
28
378cc40b
LW
29chdir 't' if -f 't/TEST';
30
3e6e8be7 31die "You need to run \"make test\" first to set things up.\n"
4633a7c4
LW
32 unless -e 'perl' or -e 'perl.exe';
33
7a315204 34if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
09187cb1
JH
35 unless (-x 'perl.third') {
36 unless (-x '../perl.third') {
37 die "You need to run \"make perl.third first.\n";
38 }
39 else {
40 print "Symlinking ../perl.third as perl.third...\n";
41 die "Failed to symlink: $!\n"
42 unless symlink("../perl.third", "perl.third");
43 die "Symlinked but no executable perl.third: $!\n"
44 unless -x 'perl.third';
45 }
46 }
47}
48
3fb91a5e
GS
49# check leakage for embedders
50$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
51
4633a7c4 52$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 53
24c841ba
MS
54# Roll your own File::Find!
55use TestInit;
56use File::Spec;
57my $curdir = File::Spec->curdir;
58my $updir = File::Spec->updir;
59
60sub _find_tests {
61 my($dir) = @_;
62 opendir DIR, $dir || die "Trouble opening $dir: $!";
a1886d87 63 foreach my $f (sort { $a cmp $b } readdir DIR) {
24c841ba
MS
64 next if $f eq $curdir or $f eq $updir;
65
66 my $fullpath = File::Spec->catdir($dir, $f);
67
68 _find_tests($fullpath) if -d $fullpath;
69 push @ARGV, $fullpath if $f =~ /\.t$/;
70 }
71}
72
73unless (@ARGV) {
5a6e071d 74 foreach my $dir (qw(base comp cmd run io op)) {
24c841ba
MS
75 _find_tests($dir);
76 }
5a6e071d 77 _find_tests("lib") unless $core;
7a315204
JH
78 my $mani = File::Spec->catdir($updir, "MANIFEST");
79 if (open(MANI, $mani)) {
80ffb5f9 80 while (<MANI>) { # similar code in t/harness
b695f709 81 if (m!^(ext/\S+/([^/]+\.t|test\.pl)|lib/\S+?(\.t|test\.pl))\s!) {
5a6e071d
PJ
82 $t = $1;
83 if (!$core || $t =~ m!^lib/[a-z]!)
84 {
73ddec28
RB
85 $path = File::Spec->catdir($updir, $t);
86 push @ARGV, $path;
87 $name{$path} = $t;
5a6e071d 88 }
7a315204
JH
89 }
90 }
91 } else {
92 warn "$0: cannot open $mani: $!\n";
93 }
b695f709 94 _find_tests('pod');
8d063cd8
LW
95}
96
7a315204 97# Tests known to cause infinite loops for the perlcc tests.
595ae481 98# %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
24c841ba 99%infinite = ();
6ee623d5 100
485988ae 101if ($deparse) {
f193aa2f
MS
102 _testprogs('deparse', '', @ARGV);
103}
104elsif( $compile || $byte_compile ) {
105 _testprogs('compile', '', @ARGV) if $compile;
106 _testprogs('compile', '-B', @ARGV) if $byte_compile;
107}
108else {
109 _testprogs('compile', '', @ARGV) if -e "../testcompile";
110 _testprogs('perl', '', @ARGV);
485988ae 111}
6ee623d5 112
bb365837
GS
113sub _testprogs {
114 $type = shift @_;
f193aa2f 115 $args = shift;
bb365837 116 @tests = @_;
6ee623d5 117
bb365837 118 print <<'EOT' if ($type eq 'compile');
7a315204 119------------------------------------------------------------------------------
6ee623d5 120TESTING COMPILER
7a315204 121------------------------------------------------------------------------------
bb365837
GS
122EOT
123
485988ae 124 print <<'EOT' if ($type eq 'deparse');
7a315204 125------------------------------------------------------------------------------
485988ae 126TESTING DEPARSER
7a315204 127------------------------------------------------------------------------------
485988ae
RH
128EOT
129
595ae481 130 $ENV{PERLCC_TIMEOUT} = 120
9636a016 131 if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
ef712cf7 132
bb365837
GS
133 $bad = 0;
134 $good = 0;
135 $total = @tests;
136 $files = 0;
137 $totmax = 0;
73ddec28
RB
138
139 foreach (@tests) {
140 $name{$_} = File::Spec->catdir('t',$_) unless exists $name{$_};
141 }
908801fe 142 my $maxlen = 0;
73ddec28
RB
143 foreach (@name{@tests}) {
144 s/\.\w+\z/./;
145 my $len = length ;
146 $maxlen = $len if $len > $maxlen;
088b5126 147 }
908801fe 148 # + 3 : we want three dots between the test name and the "ok"
73ddec28 149 $dotdotdot = $maxlen + 3 ;
bb365837
GS
150 while ($test = shift @tests) {
151
152 if ( $infinite{$test} && $type eq 'compile' ) {
595ae481 153 print STDERR "$test creates infinite loop! Skipping.\n";
bb365837 154 next;
6ee623d5 155 }
bb365837
GS
156 if ($test =~ /^$/) {
157 next;
6ee623d5 158 }
485988ae
RH
159 if ($type eq 'deparse') {
160 if ($test eq "comp/redef.t") {
161 # Redefinition happens at compile time
162 next;
163 }
164 elsif ($test eq "lib/switch.t") {
165 # B::Deparse doesn't support source filtering
166 next;
167 }
168 }
73ddec28 169 $te = $name{$test};
088b5126 170 print "$te" . '.' x ($dotdotdot - length($te));
bb365837 171
7a315204
JH
172 $test = $OVER{$test} if exists $OVER{$test};
173
2f6bec1d
MS
174 open(SCRIPT,"<$test") or die "Can't run $test.\n";
175 $_ = <SCRIPT>;
176 close(SCRIPT) unless ($type eq 'deparse');
6537fe72
MS
177 if (/#!.*\bperl.*-\w*([tT])/) {
178 $switch = qq{"-$1"};
2f6bec1d
MS
179 }
180 else {
181 $switch = '';
182 }
6ee623d5 183
485988ae
RH
184 my $file_opts = "";
185 if ($type eq 'deparse') {
186 # Look for #line directives which change the filename
187 while (<SCRIPT>) {
188 $file_opts .= ",-f$3$4"
189 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
190 }
191 close(SCRIPT);
192 }
7a315204 193
7a315204 194 my $utf = $with_utf ? '-I../lib -Mutf8' : '';
4343e7c3 195 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae
RH
196 if ($type eq 'deparse') {
197 my $deparse =
198 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,".
199 "-l$deparse_opts$file_opts ".
7a315204
JH
200 "$test > $test.dp ".
201 "&& ./perl $testswitch $switch -I../lib $test.dp |";
485988ae
RH
202 open(RESULTS, $deparse)
203 or print "can't deparse '$deparse': $!.\n";
204 }
205 elsif ($type eq 'perl') {
a7da9a42
JH
206 my $perl = $ENV{PERL} || './perl';
207 my $run = "$perl $testswitch $switch $utf $test |";
be24517c 208 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2
GS
209 }
210 else {
be24517c 211 my $compile =
f193aa2f 212 "./perl $testswitch -I../lib ../utils/perlcc -I .. $args -o ".
7a315204
JH
213 "$test.plc $utf $test ".
214 " && $test.plc |";
be24517c
JH
215 open(RESULTS, $compile)
216 or print "can't compile '$compile': $!.\n";
7a315204 217 unlink "$test.plc";
6ee623d5 218 }
d638aca2 219
bb365837
GS
220 $ok = 0;
221 $next = 0;
222 while (<RESULTS>) {
223 if ($verbose) {
224 print $_;
225 }
226 unless (/^#/) {
809908f7 227 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
bb365837 228 $max = $1;
809908f7 229 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837
GS
230 $totmax += $max;
231 $files += 1;
232 $next = 1;
233 $ok = 1;
234 }
235 else {
2fe373ce 236 if (/^(not )?ok (\d+)[^#]*(\s*#.*)?/ &&
595ae481 237 $2 == $next)
37ce32a7
MS
238 {
239 my($not, $num, $extra) = ($1, $2, $3);
240 my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
809908f7 241 $istodo = 1 if $todo{$num};
37ce32a7
MS
242
243 if( $not && !$istodo ) {
244 $ok = 0;
245 $next = $num;
246 last;
247 }
248 else {
249 $next = $next + 1;
250 }
d667a7e6
A
251 }
252 elsif (/^Bail out!\s*(.*)/i) { # magic words
253 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837
GS
254 }
255 else {
256 $ok = 0;
257 }
8d063cd8
LW
258 }
259 }
260 }
bb365837 261 close RESULTS;
485988ae
RH
262 if ($type eq 'deparse') {
263 unlink "./$test.dp";
264 }
211f317f
JH
265 if ($ENV{PERL_3LOG}) {
266 my $tpp = $test;
a7da9a42 267 $tpp =~ s:^../::;
211f317f
JH
268 $tpp =~ s:/:_:g;
269 $tpp =~ s:\.t$::;
a7da9a42
JH
270 rename("perl.3log", "perl.3log.$tpp") ||
271 die "rename: perl3.log to perl.3log.$tpp: $!\n";
211f317f 272 }
bb365837
GS
273 $next = $next - 1;
274 if ($ok && $next == $max) {
275 if ($max) {
276 print "ok\n";
277 $good = $good + 1;
278 }
279 else {
280 print "skipping test on this platform\n";
281 $files -= 1;
282 }
bcce72a7 283 }
bb365837
GS
284 else {
285 $next += 1;
286 print "FAILED at test $next\n";
287 $bad = $bad + 1;
288 $_ = $test;
289 if (/^base/) {
290 die "Failed a basic test--cannot continue.\n";
291 }
8d063cd8
LW
292 }
293 }
8d063cd8 294
bb365837
GS
295 if ($bad == 0) {
296 if ($ok) {
297 print "All tests successful.\n";
298 # XXX add mention of 'perlbug -ok' ?
299 }
300 else {
301 die "FAILED--no tests were run for some reason.\n";
302 }
8d063cd8 303 }
bb365837 304 else {
ba1398cf 305 $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
bb365837 306 if ($bad == 1) {
e824fb2c 307 warn "Failed 1 test script out of $files, $pct% okay.\n";
bb365837
GS
308 }
309 else {
e824fb2c 310 warn "Failed $bad test scripts out of $files, $pct% okay.\n";
bb365837 311 }
4e4732c1 312 warn <<'SHRDLU_1';
e089c33f
JH
313 ### Since not all tests were successful, you may want to run some of
314 ### them individually and examine any diagnostic messages they produce.
315 ### See the INSTALL document's section on "make test".
4e4732c1
NC
316SHRDLU_1
317 warn <<'SHRDLU_2' if $good / $total > 0.8;
e089c33f 318 ### You have a good chance to get more information by running
595ae481 319 ### ./perl harness
e089c33f 320 ### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1
NC
321SHRDLU_2
322 if (eval {require Config; import Config; 1}) {
e6af294e 323 if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
4e4732c1 324 warn <<SHRDLU_3;
e089c33f 325 ### You may have to set your dynamic library search path,
b3ec3848 326 ### $p, to point to the build directory:
4e4732c1
NC
327SHRDLU_3
328 if (exists $ENV{$p} && $ENV{$p} ne '') {
329 warn <<SHRDLU_4a;
b3ec3848
JH
330 ### setenv $p `pwd`:\$$p; cd t; ./perl harness
331 ### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
332 ### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1
NC
333SHRDLU_4a
334 } else {
335 warn <<SHRDLU_4b;
b3ec3848
JH
336 ### setenv $p `pwd`; cd t; ./perl harness
337 ### $p=`pwd`; export $p; cd t; ./perl harness
338 ### export $p=`pwd`; cd t; ./perl harness
4e4732c1
NC
339SHRDLU_4b
340 }
341 warn <<SHRDLU_5;
61d44243
JH
342 ### for csh-style shells, like tcsh; or for traditional/modern
343 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1
NC
344SHRDLU_5
345 }
afd33fa9 346 }
bb365837
GS
347 }
348 ($user,$sys,$cuser,$csys) = times;
349 print sprintf("u=%g s=%g cu=%g cs=%g scripts=%d tests=%d\n",
350 $user,$sys,$cuser,$csys,$files,$totmax);
6ee623d5 351}
3e6e8be7 352exit ($bad != 0);