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