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