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