This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add valgrind testing target.
[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';
1df34986 24 $bytecompile = 1 if $1 eq 'bytecompile';
f193aa2f 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}
1df34986
AE
128elsif( $compile ) {
129 _testprogs('compile', '', @ARGV);
130}
131elsif( $bytecompile ) {
132 _testprogs('bytecompile', '', @ARGV);
f193aa2f
MS
133}
134else {
135 _testprogs('compile', '', @ARGV) if -e "../testcompile";
136 _testprogs('perl', '', @ARGV);
485988ae 137}
6ee623d5 138
bb365837
GS
139sub _testprogs {
140 $type = shift @_;
f193aa2f 141 $args = shift;
bb365837 142 @tests = @_;
6ee623d5 143
bb365837 144 print <<'EOT' if ($type eq 'compile');
7a315204 145------------------------------------------------------------------------------
6ee623d5 146TESTING COMPILER
7a315204 147------------------------------------------------------------------------------
bb365837
GS
148EOT
149
485988ae 150 print <<'EOT' if ($type eq 'deparse');
7a315204 151------------------------------------------------------------------------------
485988ae 152TESTING DEPARSER
7a315204 153------------------------------------------------------------------------------
485988ae
RH
154EOT
155
566ece03 156 print <<EOT if ($type eq 'bytecompile');
1df34986
AE
157------------------------------------------------------------------------------
158TESTING BYTECODE COMPILER
159------------------------------------------------------------------------------
160EOT
161
595ae481 162 $ENV{PERLCC_TIMEOUT} = 120
9636a016 163 if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
ef712cf7 164
bb365837
GS
165 $bad = 0;
166 $good = 0;
167 $total = @tests;
168 $files = 0;
169 $totmax = 0;
73ddec28 170
cc6ae9e5
CB
171 foreach my $t (@tests) {
172 unless (exists $name{$t}) {
173 my $tname = File::Spec->catfile('t',$t);
174 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
175 $name{$t} = $tname;
176 }
73ddec28 177 }
908801fe 178 my $maxlen = 0;
73ddec28
RB
179 foreach (@name{@tests}) {
180 s/\.\w+\z/./;
181 my $len = length ;
182 $maxlen = $len if $len > $maxlen;
088b5126 183 }
908801fe 184 # + 3 : we want three dots between the test name and the "ok"
73ddec28 185 $dotdotdot = $maxlen + 3 ;
7a834142 186 my $valgrind = 0;
bb365837
GS
187 while ($test = shift @tests) {
188
189 if ( $infinite{$test} && $type eq 'compile' ) {
595ae481 190 print STDERR "$test creates infinite loop! Skipping.\n";
bb365837 191 next;
6ee623d5 192 }
bb365837
GS
193 if ($test =~ /^$/) {
194 next;
6ee623d5 195 }
485988ae
RH
196 if ($type eq 'deparse') {
197 if ($test eq "comp/redef.t") {
198 # Redefinition happens at compile time
199 next;
200 }
7a834142 201 elsif ($test =~ m{lib/Switch/t/}) {
485988ae
RH
202 # B::Deparse doesn't support source filtering
203 next;
204 }
205 }
cc6ae9e5
CB
206 $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
207
208 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
209 print $te;
210 $te = '';
211 }
bb365837 212
7a315204
JH
213 $test = $OVER{$test} if exists $OVER{$test};
214
2f6bec1d
MS
215 open(SCRIPT,"<$test") or die "Can't run $test.\n";
216 $_ = <SCRIPT>;
217 close(SCRIPT) unless ($type eq 'deparse');
5dc83c40 218 if (/#!.*\bperl.*\s-\w*([tT])/) {
6537fe72 219 $switch = qq{"-$1"};
2f6bec1d
MS
220 }
221 else {
222 $switch = '';
223 }
6ee623d5 224
b326da91 225 my $test_executable; # for 'compile' tests
485988ae
RH
226 my $file_opts = "";
227 if ($type eq 'deparse') {
228 # Look for #line directives which change the filename
229 while (<SCRIPT>) {
230 $file_opts .= ",-f$3$4"
231 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
232 }
233 close(SCRIPT);
234 }
7a315204 235
7a315204 236 my $utf = $with_utf ? '-I../lib -Mutf8' : '';
4343e7c3 237 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae
RH
238 if ($type eq 'deparse') {
239 my $deparse =
127212b2 240 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
485988ae 241 "-l$deparse_opts$file_opts ".
7a315204
JH
242 "$test > $test.dp ".
243 "&& ./perl $testswitch $switch -I../lib $test.dp |";
485988ae
RH
244 open(RESULTS, $deparse)
245 or print "can't deparse '$deparse': $!.\n";
246 }
1df34986
AE
247 elsif ($type eq 'bytecompile') {
248 my $perl = $ENV{PERL} || './perl';
249 my $redir = ($^O eq 'VMS' ? '2>&1' : '');
566ece03
JH
250 my $bswitch = "-MO=Bytecode,-H,-TI,-s`pwd`/$test,";
251 $bswitch .= "-TF$test.plc,"
1df34986
AE
252 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
253 $bswitch .= "-k,"
254 if $test =~ m(deparse|terse|ext/Storable/t/code);
1df34986
AE
255 $bswitch .= "-b,"
256 if $test =~ m(op/getpid);
257 my $bytecompile =
258 "$perl $testswitch $switch -I../lib $bswitch".
259 "-o$test.plc $test 2>/dev/null &&".
a243a48e 260 "$perl $testswitch $switch -I../lib $utf $test.plc $redir|";
1df34986
AE
261 open(RESULTS,$bytecompile)
262 or print "can't byte-compile '$bytecompile': $!.\n";
263 }
485988ae 264 elsif ($type eq 'perl') {
a7da9a42 265 my $perl = $ENV{PERL} || './perl';
7a834142
JH
266 my $redir = ($^O eq 'VMS' || $ENV{PERL_VALGRIND} ? '2>&1' : '');
267 if ($ENV{PERL_VALGRIND}) {
268 $perl = "valgrind --num-callers=50 --leak-check=yes $perl";
269 }
cc6ae9e5 270 my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
be24517c 271 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2
GS
272 }
273 else {
b326da91
MB
274 my $compile;
275 my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
9d2bbe64
MB
276 # -O9 for good measure, -fcog is broken ATM
277 "$switch -Wb=-O9,-fno-cog -L .. " .
b326da91
MB
278 "-I \".. ../lib/CORE\" $args $utf $test -o ";
279
280 if( $^O eq 'MSWin32' ) {
281 $test_executable = "$test.exe";
282 # hopefully unused name...
283 open HACK, "> xweghyz.pl";
284 print HACK <<EOT;
285#!./perl
286
287open HACK, '.\\perl $pl2c $test_executable |';
288# cl.exe prints the name of the .c file on stdout (\%^\$^#)
6d73d07f 289while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
b326da91
MB
290open HACK, '$test_executable |';
291while(<HACK>) {print}
292EOT
293 close HACK;
294 $compile = 'xweghyz.pl |';
295 }
296 else {
297 $test_executable = "$test.plc";
298 $compile = "./perl $pl2c $test_executable && $test_executable |";
299 }
300 unlink $test_executable if -f $test_executable;
be24517c
JH
301 open(RESULTS, $compile)
302 or print "can't compile '$compile': $!.\n";
6ee623d5 303 }
d638aca2 304
b326da91
MB
305 $ok = 0;
306 $next = 0;
21c74f43
A
307 my $seen_leader = 0;
308 my $seen_ok = 0;
7a834142 309 my @valgrind;
bb365837 310 while (<RESULTS>) {
cc6ae9e5 311 next if /^\s*$/; # skip blank lines
bb365837
GS
312 if ($verbose) {
313 print $_;
314 }
7a834142
JH
315 if ($ENV{PERL_VALGRIND} && /^==\d+== /) {
316 push @valgrind, $_;
317 next;
318 }
21c74f43 319 unless (/^\#/) {
809908f7 320 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
bb365837 321 $max = $1;
809908f7 322 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837
GS
323 $totmax += $max;
324 $files += 1;
21c74f43
A
325 unless ($seen_ok) {
326 $next = 1;
327 $ok = 1;
328 }
329 $seen_leader = 1;
bb365837
GS
330 }
331 else {
21c74f43
A
332 if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
333 unless ($seen_leader) {
334 unless ($seen_ok) {
335 $next = 1;
336 $ok = 1;
337 }
37ce32a7 338 }
21c74f43
A
339 $seen_ok = 1;
340 if ($2 == $next) {
341 my($not, $num, $extra) = ($1, $2, $3);
342 my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
343 $istodo = 1 if $todo{$num};
344
345 if( $not && !$istodo ) {
346 $ok = 0;
347 $next = $num;
348 last;
349 }
350 else {
351 $next = $next + 1;
352 }
37ce32a7 353 }
d667a7e6
A
354 }
355 elsif (/^Bail out!\s*(.*)/i) { # magic words
356 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837
GS
357 }
358 else {
359 $ok = 0;
360 }
8d063cd8
LW
361 }
362 }
363 }
bb365837 364 close RESULTS;
7a834142
JH
365 if ($ENV{PERL_VALGRIND}) {
366 if (@valgrind) {
367 my $skip_pthread_mutex_unlock;
368 for my $i (0..$#valgrind) {
369 local $_ = $valgrind[$i];
370 my $pid;
371 if (/^==(\d+)== pthread_mutex_unlock: mutex is not locked/ &&
372 ($pid = $1) &&
373 $valgrind[$i+2] =~ m{\(in .+/libc.+\.so\)}) {
374 $skip_pthread_mutex_unlock++;
375 } elsif (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
376 $errors = $1;
377 }
378 }
379 if (defined $errors) {
380 $errors -= $skip_pthread_mutex_unlock;
381 if ($errors) {
382 if (open(V, ">$test.valgrind")) {
383 for (@valgrind) {
384 print V $_;
385 }
386 close V;
387 $valgrind++;
388 } else {
389 warn "$0: Failed to create '$test.valgrind': $!\n";
390 }
391 }
392 }
393 } else {
394 warn "No valgrind output?\n";
395 }
396 }
485988ae
RH
397 if ($type eq 'deparse') {
398 unlink "./$test.dp";
399 }
211f317f
JH
400 if ($ENV{PERL_3LOG}) {
401 my $tpp = $test;
3716a21d 402 $tpp =~ s:^\.\./::;
9c54ecba 403 $tpp =~ s:/:_:g;
3716a21d
JH
404 $tpp =~ s:\.t$:.3log:;
405 rename("perl.3log", $tpp) ||
406 die "rename: perl3.log to $tpp: $!\n";
211f317f 407 }
bb365837 408 $next = $next - 1;
b326da91
MB
409 # test if the compiler compiled something
410 if( $type eq 'compile' && !-e "$test_executable" ) {
411 $ok = 0;
412 print "Test did not compile\n";
413 }
414 if ($ok && $next == $max ) {
bb365837 415 if ($max) {
cc6ae9e5 416 print "${te}ok\n";
bb365837
GS
417 $good = $good + 1;
418 }
419 else {
cc6ae9e5 420 print "${te}skipping test on this platform\n";
bb365837
GS
421 $files -= 1;
422 }
bcce72a7 423 }
bb365837
GS
424 else {
425 $next += 1;
cc6ae9e5 426 print "${te}FAILED at test $next\n";
bb365837
GS
427 $bad = $bad + 1;
428 $_ = $test;
429 if (/^base/) {
430 die "Failed a basic test--cannot continue.\n";
431 }
8d063cd8
LW
432 }
433 }
8d063cd8 434
bb365837
GS
435 if ($bad == 0) {
436 if ($ok) {
437 print "All tests successful.\n";
438 # XXX add mention of 'perlbug -ok' ?
439 }
440 else {
441 die "FAILED--no tests were run for some reason.\n";
442 }
8d063cd8 443 }
bb365837 444 else {
ba1398cf 445 $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
bb365837 446 if ($bad == 1) {
e824fb2c 447 warn "Failed 1 test script out of $files, $pct% okay.\n";
bb365837
GS
448 }
449 else {
e824fb2c 450 warn "Failed $bad test scripts out of $files, $pct% okay.\n";
bb365837 451 }
4e4732c1 452 warn <<'SHRDLU_1';
f7d228c6
JH
453### Since not all tests were successful, you may want to run some of
454### them individually and examine any diagnostic messages they produce.
455### See the INSTALL document's section on "make test".
4e4732c1
NC
456SHRDLU_1
457 warn <<'SHRDLU_2' if $good / $total > 0.8;
f7d228c6
JH
458### You have a good chance to get more information by running
459### ./perl harness
460### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1
NC
461SHRDLU_2
462 if (eval {require Config; import Config; 1}) {
e6af294e 463 if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
4e4732c1 464 warn <<SHRDLU_3;
f7d228c6
JH
465### You may have to set your dynamic library search path,
466### $p, to point to the build directory:
4e4732c1
NC
467SHRDLU_3
468 if (exists $ENV{$p} && $ENV{$p} ne '') {
469 warn <<SHRDLU_4a;
f7d228c6
JH
470### setenv $p `pwd`:\$$p; cd t; ./perl harness
471### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
472### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1
NC
473SHRDLU_4a
474 } else {
475 warn <<SHRDLU_4b;
f7d228c6
JH
476### setenv $p `pwd`; cd t; ./perl harness
477### $p=`pwd`; export $p; cd t; ./perl harness
478### export $p=`pwd`; cd t; ./perl harness
4e4732c1
NC
479SHRDLU_4b
480 }
481 warn <<SHRDLU_5;
f7d228c6
JH
482### for csh-style shells, like tcsh; or for traditional/modern
483### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1
NC
484SHRDLU_5
485 }
afd33fa9 486 }
bb365837
GS
487 }
488 ($user,$sys,$cuser,$csys) = times;
489 print sprintf("u=%g s=%g cu=%g cs=%g scripts=%d tests=%d\n",
490 $user,$sys,$cuser,$csys,$files,$totmax);
7a834142
JH
491 if ($ENV{PERL_VALGRIND}) {
492 my $s = $valgrind == 1 ? '' : 's';
493 print "$valgrind valgrind report$s created.\n", ;
494 }
6ee623d5 495}
3e6e8be7 496exit ($bad != 0);