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