This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #31843] warnings::warn($obj,...) fails when $obj overloads ""
[perl5.git] / t / TEST
1 #!./perl
2
3 # This is written in a peculiar style, since we're trying to avoid
4 # most of the constructs we'll be testing for.  (This comment is
5 # probably obsolete on the avoidance side, though still currrent
6 # on the peculiarity side.)
7
8 $| = 1;
9
10 # Let tests know they're running in the perl core.  Useful for modules
11 # which live dual lives on CPAN.
12 $ENV{PERL_CORE} = 1;
13
14 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
15 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
16
17 # Cheesy version of Getopt::Std.  Maybe we should replace it with that.
18 @argv = ();
19 if ($#ARGV >= 0) {
20     foreach my $idx (0..$#ARGV) {
21         push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
22         $core    = 1 if $1 eq 'core';
23         $verbose = 1 if $1 eq 'v';
24         $torture = 1 if $1 eq 'torture';
25         $with_utf8 = 1 if $1 eq 'utf8';
26         $with_utf16 = 1 if $1 eq 'utf16';
27         $bytecompile = 1 if $1 eq 'bytecompile';
28         $compile = 1 if $1 eq 'compile';
29         $taintwarn = 1 if $1 eq 'taintwarn';
30         $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
31         if ($1 =~ /^deparse(,.+)?$/) {
32             $deparse = 1;
33             $deparse_opts = $1;
34         }
35     }
36 }
37 @ARGV = @argv;
38
39 chdir 't' if -f 't/TEST';
40
41 die "You need to run \"make test\" first to set things up.\n"
42   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
43
44 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
45     unless (-x 'perl.third') {
46         unless (-x '../perl.third') {
47             die "You need to run \"make perl.third first.\n";
48         }
49         else {
50             print "Symlinking ../perl.third as perl.third...\n";
51             die "Failed to symlink: $!\n"
52                 unless symlink("../perl.third", "perl.third");
53             die "Symlinked but no executable perl.third: $!\n"
54                 unless -x 'perl.third';
55         }
56     }
57 }
58
59 # check leakage for embedders
60 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
61
62 $ENV{EMXSHELL} = 'sh';        # For OS/2
63
64 # Roll your own File::Find!
65 use TestInit;
66 use File::Spec;
67 my $curdir = File::Spec->curdir;
68 my $updir  = File::Spec->updir;
69
70 sub _find_tests {
71     my($dir) = @_;
72     opendir DIR, $dir or die "Trouble opening $dir: $!";
73     foreach my $f (sort { $a cmp $b } readdir DIR) {
74         next if $f eq $curdir or $f eq $updir or
75             $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
76
77         my $fullpath = File::Spec->catfile($dir, $f);
78
79         _find_tests($fullpath) if -d $fullpath;
80         $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
81         push @ARGV, $fullpath if $f =~ /\.t$/;
82     }
83 }
84
85 sub _quote_args {
86     my ($args) = @_;
87     my $argstring = '';
88
89     foreach (split(/\s+/,$args)) {
90        # In VMS protect with doublequotes because otherwise
91        # DCL will lowercase -- unless already doublequoted.
92        $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
93        $argstring .= ' ' . $_;
94     }
95     return $argstring;
96 }
97
98 unless (@ARGV) {
99     foreach my $dir (qw(base comp cmd run io op uni)) {
100         _find_tests($dir);
101     }
102     _find_tests("lib") unless $core;
103     my $mani = File::Spec->catfile($updir, "MANIFEST");
104     if (open(MANI, $mani)) {
105         while (<MANI>) { # similar code in t/harness
106             if (m!^(ext/\S+/?(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
107                 $t = $1;
108                 if (!$core || $t =~ m!^lib/[a-z]!)
109                 {
110                     $path = File::Spec->catfile($updir, $t);
111                     push @ARGV, $path;
112                     $name{$path} = $t;
113                 }
114             }
115         }
116         close MANI;
117     } else {
118         warn "$0: cannot open $mani: $!\n";
119     }
120     unless ($core) {
121         _find_tests('pod');
122         _find_tests('x2p');
123         _find_tests('japh') if $torture;
124     }
125 }
126
127 # Tests known to cause infinite loops for the perlcc tests.
128 # %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
129 %infinite = ();
130
131 if ($deparse) {
132     _testprogs('deparse', '',   @ARGV);
133 }
134 elsif( $compile ) { 
135     _testprogs('compile', '',   @ARGV);
136 }
137 elsif( $bytecompile ) {
138     _testprogs('bytecompile', '', @ARGV);
139 }
140 elsif ($with_utf16) {
141     for my $e (0, 1) {
142         for my $b (0, 1) {
143             print STDERR "# ENDIAN $e BOM $b\n";
144             my @UARGV;
145             for my $a (@ARGV) {
146                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
147                 my $f = $e ? "v" : "n";
148                 push @UARGV, $u;
149                 unlink($u);
150                 if (open(A, $a)) {
151                     if (open(U, ">$u")) {
152                         print U pack("$f", 0xFEFF);
153                         while (<A>) {
154                             print U pack("$f*", unpack("C*", $_));
155                         }
156                         close(A);
157                     }
158                     close(B);
159                 }
160             }
161             _testprogs('perl', '', @UARGV);
162             unlink(@UARGV);
163         }
164     }
165 }
166 else {
167     _testprogs('compile', '',   @ARGV) if -e "../testcompile";
168     _testprogs('perl',    '',   @ARGV);
169 }
170
171 sub _testprogs {
172     $type = shift @_;
173     $args = shift;
174     @tests = @_;
175
176     print <<'EOT' if ($type eq 'compile');
177 ------------------------------------------------------------------------------
178 TESTING COMPILER
179 ------------------------------------------------------------------------------
180 EOT
181
182     print <<'EOT' if ($type eq 'deparse');
183 ------------------------------------------------------------------------------
184 TESTING DEPARSER
185 ------------------------------------------------------------------------------
186 EOT
187
188     print <<EOT if ($type eq 'bytecompile');
189 ------------------------------------------------------------------------------
190 TESTING BYTECODE COMPILER
191 ------------------------------------------------------------------------------
192 EOT
193
194     $ENV{PERLCC_TIMEOUT} = 120
195           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
196
197     $bad = 0;
198     $good = 0;
199     $total = @tests;
200     $files  = 0;
201     $totmax = 0;
202
203     foreach my $t (@tests) {
204       unless (exists $name{$t}) {
205         my $tname = File::Spec->catfile('t',$t);
206         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
207         $name{$t} = $tname;
208       }
209     }
210     my $maxlen = 0;
211     foreach (@name{@tests}) {
212         s/\.\w+\z/./;
213         my $len = length ;
214         $maxlen = $len if $len > $maxlen;
215     }
216     # + 3 : we want three dots between the test name and the "ok"
217     $dotdotdot = $maxlen + 3 ;
218     my $valgrind = 0;
219     my $valgrind_log = 'current.valgrind';
220     while ($test = shift @tests) {
221
222         if ( $infinite{$test} && $type eq 'compile' ) {
223             print STDERR "$test creates infinite loop! Skipping.\n";
224             next;
225         }
226         if ($test =~ /^$/) {
227             next;
228         }
229         if ($type eq 'deparse') {
230             if ($test eq "comp/redef.t") {
231                 # Redefinition happens at compile time
232                 next;
233             }
234             elsif ($test =~ m{lib/Switch/t/}) {
235                 # B::Deparse doesn't support source filtering
236                 next;
237             }
238         }
239         $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
240
241         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
242             print $te;
243             $te = '';
244         }
245
246         $test = $OVER{$test} if exists $OVER{$test};
247
248         open(SCRIPT,"<$test") or die "Can't run $test.\n";
249         $_ = <SCRIPT>;
250         close(SCRIPT) unless ($type eq 'deparse');
251         if (/#!.*\bperl.*\s-\w*([tT])/) {
252             $switch = qq{"-$1"};
253         }
254         else {
255             if ($taintwarn) {
256                 # not all tests are expected to pass with this option
257                 $switch = '"-t"';
258             }
259             else {
260                 $switch = '';
261             }
262         }
263
264         my $test_executable; # for 'compile' tests
265         my $file_opts = "";
266         if ($type eq 'deparse') {
267             # Look for #line directives which change the filename
268             while (<SCRIPT>) {
269                 $file_opts .= ",-f$3$4"
270                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
271             }
272             close(SCRIPT);
273         }
274
275         my $utf8 = $with_utf8 ? '-I../lib -Mutf8' : '';
276         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
277         if ($type eq 'deparse') {
278             my $deparse =
279                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
280                 "-l$deparse_opts$file_opts ".
281                 "$test > $test.dp ".
282                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
283             open(RESULTS, $deparse)
284                 or print "can't deparse '$deparse': $!.\n";
285         }
286         elsif ($type eq 'bytecompile') {
287             my ($pwd, $null);
288             if( $^O eq 'MSWin32') {
289                 $pwd = `cd`;
290                 $null = 'nul';
291             } else {
292                 $pwd = `pwd`;
293                 $null = '/dev/null';
294             }
295             chomp $pwd;
296             my $perl = $ENV{PERL} || "$pwd/perl";
297             my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
298             $bswitch .= "-TF$test.plc,"
299                 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
300             $bswitch .= "-k,"
301                 if $test =~ m(deparse|terse|ext/Storable/t/code);
302             $bswitch .= "-b,"
303                 if $test =~ m(op/getpid);
304             my $bytecompile =
305                 "$perl $testswitch $switch -I../lib $bswitch". 
306                 "-o$test.plc $test 2>$null &&".
307                 "$perl $testswitch $switch -I../lib $utf8 $test.plc |";
308             open(RESULTS,$bytecompile)
309                 or print "can't byte-compile '$bytecompile': $!.\n";
310         }
311         elsif ($type eq 'perl') {
312             my $perl = $ENV{PERL} || './perl';
313             my $redir = $^O eq 'VMS' ? '2>&1' : '';
314             if ($ENV{PERL_VALGRIND}) {
315                 $perl = "valgrind --suppressions=perl.supp --leak-check=yes "
316                                . "--leak-resolution=high --show-reachable=yes "
317                                . "--num-callers=50 --logfile-fd=3 $perl";
318                 $redir = "3>$valgrind_log";
319             }
320             my $run = "$perl" . _quote_args("$testswitch $switch $utf8") . " $test $redir|";
321             open(RESULTS,$run) or print "can't run '$run': $!.\n";
322         }
323         else {
324             my $compile;
325             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
326               # -O9 for good measure, -fcog is broken ATM
327                        "$switch -Wb=-O9,-fno-cog -L .. " .
328                        "-I \".. ../lib/CORE\" $args $utf8 $test -o ";
329
330             if( $^O eq 'MSWin32' ) {
331                 $test_executable = "$test.exe";
332                 # hopefully unused name...
333                 open HACK, "> xweghyz.pl";
334                 print HACK <<EOT;
335 #!./perl
336
337 open HACK, '.\\perl $pl2c $test_executable |';
338 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
339 while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
340 open HACK, '$test_executable |';
341 while(<HACK>) {print}
342 EOT
343                 close HACK;
344                 $compile = 'xweghyz.pl |';
345             }
346             else {
347                 $test_executable = "$test.plc";
348                 $compile = "./perl $pl2c $test_executable && $test_executable |";
349             }
350             unlink $test_executable if -f $test_executable;
351             open(RESULTS, $compile)
352                 or print "can't compile '$compile': $!.\n";
353         }
354
355         $ok = 0;
356         $next = 0;
357         my $seen_leader = 0;
358         my $seen_ok = 0;
359         while (<RESULTS>) {
360             next if /^\s*$/; # skip blank lines
361             if ($verbose) {
362                 print $_;
363             }
364             unless (/^\#/) {
365                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
366                     $max = $1;
367                     %todo = map { $_ => 1 } split / /, $3 if $3;
368                     $totmax += $max;
369                     $files += 1;
370                     unless ($seen_ok) {
371                       $next = 1;
372                       $ok = 1;
373                     }
374                     $seen_leader = 1;
375                 }
376                 else {
377                     if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
378                         unless ($seen_leader) {
379                             unless ($seen_ok) {
380                                 $next = 1;
381                                 $ok = 1;
382                             }
383                         }
384                         $seen_ok = 1;
385                         if ($2 == $next) {
386                             my($not, $num, $extra) = ($1, $2, $3);
387                             my($istodo) = $extra =~ /#\s*TODO/ if $extra;
388                             $istodo = 1 if $todo{$num};
389
390                             if( $not && !$istodo ) {
391                                 $ok = 0;
392                                 $next = $num;
393                                 last;
394                             }
395                             else {
396                                 $next = $next + 1;
397                             }
398                         }
399                     }
400                     elsif (/^Bail out!\s*(.*)/i) { # magic words
401                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
402                     }
403                     else {
404                         $ok = 0;
405                     }
406                 }
407             }
408         }
409         close RESULTS;
410         if ($ENV{PERL_VALGRIND}) {
411             my @valgrind;
412             if (-e $valgrind_log) {
413                 if (open(V, $valgrind_log)) {
414                     @valgrind = <V>;
415                     close V;
416                 } else {
417                     warn "$0: Failed to open '$valgrind_log': $!\n";
418                 }
419             }
420             if (@valgrind) {
421                 my $leaks = 0;
422                 my $errors = 0;
423                 for my $i (0..$#valgrind) {
424                     local $_ = $valgrind[$i];
425                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
426                         $errors += $1;   # there may be multiple error summaries
427                     } elsif (/^==\d+== LEAK SUMMARY:/) {
428                         for my $off (1 .. 4) {
429                             if ($valgrind[$i+$off] =~
430                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
431                                 $leaks += $1;
432                             }
433                         }
434                     }
435                 }
436                 if ($errors or $leaks) {
437                     if (rename $valgrind_log, "$test.valgrind") {
438                         $valgrind++;
439                     } else {
440                         warn "$0: Failed to create '$test.valgrind': $!\n";
441                     }
442                 }
443             } else {
444                 warn "No valgrind output?\n";
445             }
446             if (-e $valgrind_log) {
447                 unlink $valgrind_log
448                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
449             }
450         }
451         if ($type eq 'deparse') {
452             unlink "./$test.dp";
453         }
454         if ($ENV{PERL_3LOG}) {
455             my $tpp = $test;
456             $tpp =~ s:^\.\./::;
457             $tpp =~ s:/:_:g;
458             $tpp =~ s:\.t$:.3log:;
459             rename("perl.3log", $tpp) ||
460                 die "rename: perl3.log to $tpp: $!\n";
461         }
462         $next = $next - 1;
463         # test if the compiler compiled something
464         if( $type eq 'compile' && !-e "$test_executable" ) {
465             $ok = 0;
466             print "Test did not compile\n";
467         }
468         if ($ok && $next == $max ) {
469             if ($max) {
470                 print "${te}ok\n";
471                 $good = $good + 1;
472             }
473             else {
474                 print "${te}skipping test on this platform\n";
475                 $files -= 1;
476             }
477         }
478         else {
479             $next += 1;
480             if ($next > $max) {
481                 print "${te}FAILED at test $next\tpossibly due to extra output\n";
482             }
483             else {
484                 print "${te}FAILED at test $next\n";
485             }
486             $bad = $bad + 1;
487             $_ = $test;
488             if (/^base/) {
489                 die "Failed a basic test--cannot continue.\n";
490             }
491         }
492     }
493
494     if ($bad == 0) {
495         if ($ok) {
496             print "All tests successful.\n";
497             # XXX add mention of 'perlbug -ok' ?
498         }
499         else {
500             die "FAILED--no tests were run for some reason.\n";
501         }
502     }
503     else {
504         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
505         if ($bad == 1) {
506             warn "Failed 1 test script out of $files, $pct% okay.\n";
507         }
508         else {
509             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
510         }
511         warn <<'SHRDLU_1';
512 ### Since not all tests were successful, you may want to run some of
513 ### them individually and examine any diagnostic messages they produce.
514 ### See the INSTALL document's section on "make test".
515 SHRDLU_1
516         warn <<'SHRDLU_2' if $good / $total > 0.8;
517 ### You have a good chance to get more information by running
518 ###   ./perl harness
519 ### in the 't' directory since most (>=80%) of the tests succeeded.
520 SHRDLU_2
521         if (eval {require Config; import Config; 1}) {
522             if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
523                 warn <<SHRDLU_3;
524 ### You may have to set your dynamic library search path,
525 ### $p, to point to the build directory:
526 SHRDLU_3
527                 if (exists $ENV{$p} && $ENV{$p} ne '') {
528                     warn <<SHRDLU_4a;
529 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
530 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
531 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
532 SHRDLU_4a
533                 } else {
534                     warn <<SHRDLU_4b;
535 ###   setenv $p `pwd`; cd t; ./perl harness
536 ###   $p=`pwd`; export $p; cd t; ./perl harness
537 ###   export $p=`pwd`; cd t; ./perl harness
538 SHRDLU_4b
539                 }    
540                 warn <<SHRDLU_5;
541 ### for csh-style shells, like tcsh; or for traditional/modern
542 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
543 SHRDLU_5
544             }
545         }
546     }
547     ($user,$sys,$cuser,$csys) = times;
548     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
549         $user,$sys,$cuser,$csys,$files,$totmax);
550     if ($ENV{PERL_VALGRIND}) {
551         my $s = $valgrind == 1 ? '' : 's';
552         print "$valgrind valgrind report$s created.\n", ;
553     }
554 }
555 exit ($bad != 0);