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