Commit | Line | Data |
---|---|---|
69026470 JH |
1 | # |
2 | # t/test.pl - most of Test::More functionality without the fuss | |
485f531e DL |
3 | |
4 | ||
5 | # NOTE: | |
6 | # | |
7 | # Increment ($x++) has a certain amount of cleverness for things like | |
8 | # | |
9 | # $x = 'zz'; | |
10 | # $x++; # $x eq 'aaa'; | |
69026470 | 11 | # |
485f531e DL |
12 | # stands more chance of breaking than just a simple |
13 | # | |
14 | # $x = $x + 1 | |
15 | # | |
16 | # In this file, we use the latter "Baby Perl" approach, and increment | |
17 | # will be worked over by t/op/inc.t | |
69026470 | 18 | |
dcc7f481 | 19 | $Level = 1; |
69026470 JH |
20 | my $test = 1; |
21 | my $planned; | |
6137113d | 22 | my $noplan; |
5fe9b82b | 23 | my $Perl; # Safer version of $^X set by which_perl() |
69026470 | 24 | |
7d932aad | 25 | $TODO = 0; |
b6345914 | 26 | $NO_ENDING = 0; |
7d932aad | 27 | |
3d66076a MS |
28 | # Use this instead of print to avoid interference while testing globals. |
29 | sub _print { | |
30 | local($\, $", $,) = (undef, ' ', ''); | |
31 | print STDOUT @_; | |
32 | } | |
33 | ||
34 | sub _print_stderr { | |
35 | local($\, $", $,) = (undef, ' ', ''); | |
36 | print STDERR @_; | |
37 | } | |
38 | ||
69026470 JH |
39 | sub plan { |
40 | my $n; | |
41 | if (@_ == 1) { | |
42 | $n = shift; | |
6137113d NC |
43 | if ($n eq 'no_plan') { |
44 | undef $n; | |
45 | $noplan = 1; | |
46 | } | |
69026470 JH |
47 | } else { |
48 | my %plan = @_; | |
8210c8d3 | 49 | $n = $plan{tests}; |
69026470 | 50 | } |
3d66076a | 51 | _print "1..$n\n" unless $noplan; |
69026470 JH |
52 | $planned = $n; |
53 | } | |
54 | ||
55 | END { | |
56 | my $ran = $test - 1; | |
6137113d NC |
57 | if (!$NO_ENDING) { |
58 | if (defined $planned && $planned != $ran) { | |
3d66076a | 59 | _print_stderr |
6137113d NC |
60 | "# Looks like you planned $planned tests but ran $ran.\n"; |
61 | } elsif ($noplan) { | |
3d66076a | 62 | _print "1..$ran\n"; |
6137113d | 63 | } |
69026470 JH |
64 | } |
65 | } | |
66 | ||
8210c8d3 | 67 | # Use this instead of "print STDERR" when outputing failure diagnostic |
de522f7a MS |
68 | # messages |
69 | sub _diag { | |
cf8feb78 | 70 | return unless @_; |
8210c8d3 | 71 | my @mess = map { /^#/ ? "$_\n" : "# $_\n" } |
cf8feb78 | 72 | map { split /\n/ } @_; |
44826442 | 73 | $TODO ? _print(@mess) : _print_stderr(@mess); |
de522f7a MS |
74 | } |
75 | ||
485f531e DL |
76 | sub diag { |
77 | _diag(@_); | |
78 | } | |
79 | ||
69026470 JH |
80 | sub skip_all { |
81 | if (@_) { | |
7bb7fa38 | 82 | _print "1..0 # Skip @_\n"; |
69026470 | 83 | } else { |
3d66076a | 84 | _print "1..0\n"; |
69026470 JH |
85 | } |
86 | exit(0); | |
87 | } | |
88 | ||
89 | sub _ok { | |
7d932aad | 90 | my ($pass, $where, $name, @mess) = @_; |
69026470 JH |
91 | # Do not try to microoptimize by factoring out the "not ". |
92 | # VMS will avenge. | |
7d932aad MS |
93 | my $out; |
94 | if ($name) { | |
b734d6c9 MS |
95 | # escape out '#' or it will interfere with '# skip' and such |
96 | $name =~ s/#/\\#/g; | |
7d932aad | 97 | $out = $pass ? "ok $test - $name" : "not ok $test - $name"; |
69026470 | 98 | } else { |
7d932aad | 99 | $out = $pass ? "ok $test" : "not ok $test"; |
69026470 | 100 | } |
7d932aad MS |
101 | |
102 | $out .= " # TODO $TODO" if $TODO; | |
3d66076a | 103 | _print "$out\n"; |
7d932aad | 104 | |
69026470 | 105 | unless ($pass) { |
de522f7a | 106 | _diag "# Failed $where\n"; |
69026470 | 107 | } |
7d932aad MS |
108 | |
109 | # Ensure that the message is properly escaped. | |
cf8feb78 | 110 | _diag @mess; |
7d932aad | 111 | |
485f531e | 112 | $test = $test + 1; # don't use ++ |
1577bb16 MS |
113 | |
114 | return $pass; | |
69026470 JH |
115 | } |
116 | ||
117 | sub _where { | |
dcc7f481 | 118 | my @caller = caller($Level); |
69026470 JH |
119 | return "at $caller[1] line $caller[2]"; |
120 | } | |
121 | ||
1d662fb6 | 122 | # DON'T use this for matches. Use like() instead. |
c3029c66 | 123 | sub ok ($@) { |
7d932aad MS |
124 | my ($pass, $name, @mess) = @_; |
125 | _ok($pass, _where(), $name, @mess); | |
69026470 JH |
126 | } |
127 | ||
b3c72391 JH |
128 | sub _q { |
129 | my $x = shift; | |
130 | return 'undef' unless defined $x; | |
131 | my $q = $x; | |
d279d8f8 NC |
132 | $q =~ s/\\/\\\\/g; |
133 | $q =~ s/'/\\'/g; | |
b3c72391 JH |
134 | return "'$q'"; |
135 | } | |
136 | ||
677fb045 NC |
137 | sub _qq { |
138 | my $x = shift; | |
139 | return defined $x ? '"' . display ($x) . '"' : 'undef'; | |
140 | }; | |
141 | ||
142 | # keys are the codes \n etc map to, values are 2 char strings such as \n | |
143 | my %backslash_escape; | |
144 | foreach my $x (split //, 'nrtfa\\\'"') { | |
145 | $backslash_escape{ord eval "\"\\$x\""} = "\\$x"; | |
146 | } | |
147 | # A way to display scalars containing control characters and Unicode. | |
148 | # Trying to avoid setting $_, or relying on local $_ to work. | |
149 | sub display { | |
150 | my @result; | |
151 | foreach my $x (@_) { | |
152 | if (defined $x and not ref $x) { | |
153 | my $y = ''; | |
154 | foreach my $c (unpack("U*", $x)) { | |
155 | if ($c > 255) { | |
156 | $y .= sprintf "\\x{%x}", $c; | |
157 | } elsif ($backslash_escape{$c}) { | |
158 | $y .= $backslash_escape{$c}; | |
159 | } else { | |
160 | my $z = chr $c; # Maybe we can get away with a literal... | |
161 | $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/; | |
162 | $y .= $z; | |
163 | } | |
164 | } | |
165 | $x = $y; | |
166 | } | |
167 | return $x unless wantarray; | |
168 | push @result, $x; | |
169 | } | |
170 | return @result; | |
171 | } | |
172 | ||
c3029c66 | 173 | sub is ($$@) { |
7d932aad | 174 | my ($got, $expected, $name, @mess) = @_; |
c831d34f MS |
175 | |
176 | my $pass; | |
177 | if( !defined $got || !defined $expected ) { | |
178 | # undef only matches undef | |
179 | $pass = !defined $got && !defined $expected; | |
180 | } | |
181 | else { | |
182 | $pass = $got eq $expected; | |
183 | } | |
184 | ||
69026470 | 185 | unless ($pass) { |
d5f8084a KW |
186 | unshift(@mess, "# got "._qq($got)."\n", |
187 | "# expected "._qq($expected)."\n"); | |
69026470 | 188 | } |
7d932aad | 189 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
190 | } |
191 | ||
c3029c66 | 192 | sub isnt ($$@) { |
3e90d5a3 | 193 | my ($got, $isnt, $name, @mess) = @_; |
c831d34f MS |
194 | |
195 | my $pass; | |
196 | if( !defined $got || !defined $isnt ) { | |
197 | # undef only matches undef | |
198 | $pass = defined $got || defined $isnt; | |
199 | } | |
200 | else { | |
201 | $pass = $got ne $isnt; | |
202 | } | |
203 | ||
3e90d5a3 | 204 | unless( $pass ) { |
d5f8084a | 205 | unshift(@mess, "# it should not be "._qq($got)."\n", |
3e90d5a3 MS |
206 | "# but it is.\n"); |
207 | } | |
208 | _ok($pass, _where(), $name, @mess); | |
209 | } | |
210 | ||
c3029c66 | 211 | sub cmp_ok ($$$@) { |
58d76dfd JH |
212 | my($got, $type, $expected, $name, @mess) = @_; |
213 | ||
214 | my $pass; | |
215 | { | |
216 | local $^W = 0; | |
217 | local($@,$!); # don't interfere with $@ | |
218 | # eval() sometimes resets $! | |
219 | $pass = eval "\$got $type \$expected"; | |
220 | } | |
221 | unless ($pass) { | |
222 | # It seems Irix long doubles can have 2147483648 and 2147483648 | |
223 | # that stringify to the same thing but are acutally numerically | |
224 | # different. Display the numbers if $type isn't a string operator, | |
225 | # and the numbers are stringwise the same. | |
226 | # (all string operators have alphabetic names, so tr/a-z// is true) | |
227 | # This will also show numbers for some uneeded cases, but will | |
228 | # definately be helpful for things such as == and <= that fail | |
229 | if ($got eq $expected and $type !~ tr/a-z//) { | |
230 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
231 | } | |
d5f8084a KW |
232 | unshift(@mess, "# got "._qq($got)."\n", |
233 | "# expected $type "._qq($expected)."\n"); | |
58d76dfd JH |
234 | } |
235 | _ok($pass, _where(), $name, @mess); | |
236 | } | |
237 | ||
238 | # Check that $got is within $range of $expected | |
239 | # if $range is 0, then check it's exact | |
240 | # else if $expected is 0, then $range is an absolute value | |
241 | # otherwise $range is a fractional error. | |
242 | # Here $range must be numeric, >= 0 | |
243 | # Non numeric ranges might be a useful future extension. (eg %) | |
c3029c66 | 244 | sub within ($$$@) { |
58d76dfd JH |
245 | my ($got, $expected, $range, $name, @mess) = @_; |
246 | my $pass; | |
247 | if (!defined $got or !defined $expected or !defined $range) { | |
248 | # This is a fail, but doesn't need extra diagnostics | |
249 | } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) { | |
250 | # This is a fail | |
251 | unshift @mess, "# got, expected and range must be numeric\n"; | |
252 | } elsif ($range < 0) { | |
253 | # This is also a fail | |
254 | unshift @mess, "# range must not be negative\n"; | |
255 | } elsif ($range == 0) { | |
256 | # Within 0 is == | |
257 | $pass = $got == $expected; | |
258 | } elsif ($expected == 0) { | |
259 | # If expected is 0, treat range as absolute | |
260 | $pass = ($got <= $range) && ($got >= - $range); | |
261 | } else { | |
262 | my $diff = $got - $expected; | |
263 | $pass = abs ($diff / $expected) < $range; | |
264 | } | |
265 | unless ($pass) { | |
266 | if ($got eq $expected) { | |
267 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
268 | } | |
d5f8084a KW |
269 | unshift@mess, "# got "._qq($got)."\n", |
270 | "# expected "._qq($expected)." (within "._qq($range).")\n"; | |
58d76dfd JH |
271 | } |
272 | _ok($pass, _where(), $name, @mess); | |
273 | } | |
274 | ||
69026470 | 275 | # Note: this isn't quite as fancy as Test::More::like(). |
724aa791 JC |
276 | |
277 | sub like ($$@) { like_yn (0,@_) }; # 0 for - | |
278 | sub unlike ($$@) { like_yn (1,@_) }; # 1 for un- | |
279 | ||
280 | sub like_yn ($$$@) { | |
281 | my ($flip, $got, $expected, $name, @mess) = @_; | |
69026470 | 282 | my $pass; |
724aa791 JC |
283 | $pass = $got =~ /$expected/ if !$flip; |
284 | $pass = $got !~ /$expected/ if $flip; | |
285 | unless ($pass) { | |
286 | unshift(@mess, "# got '$got'\n", | |
5a4a8c8b NC |
287 | $flip |
288 | ? "# expected !~ /$expected/\n" : "# expected /$expected/\n"); | |
69026470 | 289 | } |
5693d826 | 290 | local $Level = $Level + 1; |
7d932aad | 291 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
292 | } |
293 | ||
294 | sub pass { | |
295 | _ok(1, '', @_); | |
296 | } | |
297 | ||
298 | sub fail { | |
299 | _ok(0, _where(), @_); | |
300 | } | |
301 | ||
ad20d923 | 302 | sub curr_test { |
cf8feb78 | 303 | $test = shift if @_; |
ad20d923 MS |
304 | return $test; |
305 | } | |
306 | ||
3e90d5a3 | 307 | sub next_test { |
178eff92 | 308 | my $retval = $test; |
485f531e | 309 | $test = $test + 1; # don't use ++ |
178eff92 | 310 | $retval; |
3e90d5a3 MS |
311 | } |
312 | ||
69026470 JH |
313 | # Note: can't pass multipart messages since we try to |
314 | # be compatible with Test::More::skip(). | |
315 | sub skip { | |
7d932aad | 316 | my $why = shift; |
982b7cb7 | 317 | my $n = @_ ? shift : 1; |
69026470 | 318 | for (1..$n) { |
7bb7fa38 | 319 | _print "ok $test # skip $why\n"; |
485f531e | 320 | $test = $test + 1; |
69026470 JH |
321 | } |
322 | local $^W = 0; | |
323 | last SKIP; | |
324 | } | |
325 | ||
09f04786 MS |
326 | sub todo_skip { |
327 | my $why = shift; | |
328 | my $n = @_ ? shift : 1; | |
329 | ||
330 | for (1..$n) { | |
7bb7fa38 | 331 | _print "not ok $test # TODO & SKIP $why\n"; |
485f531e | 332 | $test = $test + 1; |
09f04786 MS |
333 | } |
334 | local $^W = 0; | |
335 | last TODO; | |
336 | } | |
337 | ||
69026470 JH |
338 | sub eq_array { |
339 | my ($ra, $rb) = @_; | |
340 | return 0 unless $#$ra == $#$rb; | |
341 | for my $i (0..$#$ra) { | |
8210c8d3 | 342 | next if !defined $ra->[$i] && !defined $rb->[$i]; |
135d199b DM |
343 | return 0 if !defined $ra->[$i]; |
344 | return 0 if !defined $rb->[$i]; | |
69026470 JH |
345 | return 0 unless $ra->[$i] eq $rb->[$i]; |
346 | } | |
347 | return 1; | |
348 | } | |
349 | ||
677fb045 NC |
350 | sub eq_hash { |
351 | my ($orig, $suspect) = @_; | |
352 | my $fail; | |
353 | while (my ($key, $value) = each %$suspect) { | |
354 | # Force a hash recompute if this perl's internals can cache the hash key. | |
355 | $key = "" . $key; | |
356 | if (exists $orig->{$key}) { | |
357 | if ($orig->{$key} ne $value) { | |
3d66076a | 358 | _print "# key ", _qq($key), " was ", _qq($orig->{$key}), |
de522f7a | 359 | " now ", _qq($value), "\n"; |
677fb045 NC |
360 | $fail = 1; |
361 | } | |
362 | } else { | |
3d66076a | 363 | _print "# key ", _qq($key), " is ", _qq($value), |
75385f53 | 364 | ", not in original.\n"; |
677fb045 NC |
365 | $fail = 1; |
366 | } | |
367 | } | |
368 | foreach (keys %$orig) { | |
369 | # Force a hash recompute if this perl's internals can cache the hash key. | |
370 | $_ = "" . $_; | |
371 | next if (exists $suspect->{$_}); | |
3d66076a | 372 | _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 NC |
373 | $fail = 1; |
374 | } | |
375 | !$fail; | |
376 | } | |
377 | ||
c3029c66 | 378 | sub require_ok ($) { |
69026470 JH |
379 | my ($require) = @_; |
380 | eval <<REQUIRE_OK; | |
381 | require $require; | |
382 | REQUIRE_OK | |
1577bb16 | 383 | _ok(!$@, _where(), "require $require"); |
69026470 JH |
384 | } |
385 | ||
c3029c66 | 386 | sub use_ok ($) { |
69026470 JH |
387 | my ($use) = @_; |
388 | eval <<USE_OK; | |
389 | use $use; | |
390 | USE_OK | |
1577bb16 | 391 | _ok(!$@, _where(), "use $use"); |
69026470 JH |
392 | } |
393 | ||
137352a2 RGS |
394 | # runperl - Runs a separate perl interpreter. |
395 | # Arguments : | |
396 | # switches => [ command-line switches ] | |
397 | # nolib => 1 # don't use -I../lib (included by default) | |
398 | # prog => one-liner (avoid quotes) | |
d83945bc | 399 | # progs => [ multi-liner (avoid quotes) ] |
137352a2 RGS |
400 | # progfile => perl script |
401 | # stdin => string to feed the stdin | |
402 | # stderr => redirect stderr to stdout | |
403 | # args => [ command-line arguments to the perl program ] | |
cb9c5e20 | 404 | # verbose => print the command line |
137352a2 RGS |
405 | |
406 | my $is_mswin = $^O eq 'MSWin32'; | |
407 | my $is_netware = $^O eq 'NetWare'; | |
137352a2 | 408 | my $is_vms = $^O eq 'VMS'; |
e67ed694 | 409 | my $is_cygwin = $^O eq 'cygwin'; |
137352a2 | 410 | |
cb9c5e20 JH |
411 | sub _quote_args { |
412 | my ($runperl, $args) = @_; | |
413 | ||
414 | foreach (@$args) { | |
415 | # In VMS protect with doublequotes because otherwise | |
416 | # DCL will lowercase -- unless already doublequoted. | |
ea9ac5ad | 417 | $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; |
cb9c5e20 JH |
418 | $$runperl .= ' ' . $_; |
419 | } | |
420 | } | |
421 | ||
4cd2bd1f | 422 | sub _create_runperl { # Create the string to qx in runperl(). |
137352a2 | 423 | my %args = @_; |
5fe9b82b JH |
424 | my $runperl = which_perl(); |
425 | if ($runperl =~ m/\s/) { | |
426 | $runperl = qq{"$runperl"}; | |
427 | } | |
6cf707aa RGS |
428 | #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind |
429 | if ($ENV{PERL_RUNPERL_DEBUG}) { | |
430 | $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl"; | |
431 | } | |
f93a5f07 | 432 | unless ($args{nolib}) { |
7b903762 | 433 | $runperl .= ' "-I../lib"'; # doublequotes because of VMS |
137352a2 | 434 | } |
d83945bc | 435 | if ($args{switches}) { |
343d4a7b JH |
436 | local $Level = 2; |
437 | die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where() | |
438 | unless ref $args{switches} eq "ARRAY"; | |
d83945bc A |
439 | _quote_args(\$runperl, $args{switches}); |
440 | } | |
137352a2 | 441 | if (defined $args{prog}) { |
21820af6 JH |
442 | die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where() |
443 | if defined $args{progs}; | |
d83945bc A |
444 | $args{progs} = [$args{prog}] |
445 | } | |
446 | if (defined $args{progs}) { | |
21820af6 JH |
447 | die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where() |
448 | unless ref $args{progs} eq "ARRAY"; | |
d83945bc A |
449 | foreach my $prog (@{$args{progs}}) { |
450 | if ($is_mswin || $is_netware || $is_vms) { | |
451 | $runperl .= qq ( -e "$prog" ); | |
452 | } | |
453 | else { | |
454 | $runperl .= qq ( -e '$prog' ); | |
455 | } | |
456 | } | |
137352a2 RGS |
457 | } elsif (defined $args{progfile}) { |
458 | $runperl .= qq( "$args{progfile}"); | |
9a731dbd NC |
459 | } else { |
460 | # You probaby didn't want to be sucking in from the upstream stdin | |
461 | die "test.pl:runperl(): none of prog, progs, progfile, args, " | |
462 | . " switches or stdin specified" | |
463 | unless defined $args{args} or defined $args{switches} | |
464 | or defined $args{stdin}; | |
137352a2 RGS |
465 | } |
466 | if (defined $args{stdin}) { | |
dc459aad JH |
467 | # so we don't try to put literal newlines and crs onto the |
468 | # command line. | |
469 | $args{stdin} =~ s/\n/\\n/g; | |
470 | $args{stdin} =~ s/\r/\\r/g; | |
5ae09a77 | 471 | |
137352a2 | 472 | if ($is_mswin || $is_netware || $is_vms) { |
5fe9b82b | 473 | $runperl = qq{$Perl -e "print qq(} . |
137352a2 RGS |
474 | $args{stdin} . q{)" | } . $runperl; |
475 | } | |
476 | else { | |
5fe9b82b | 477 | $runperl = qq{$Perl -e 'print qq(} . |
137352a2 RGS |
478 | $args{stdin} . q{)' | } . $runperl; |
479 | } | |
480 | } | |
481 | if (defined $args{args}) { | |
cb9c5e20 JH |
482 | _quote_args(\$runperl, $args{args}); |
483 | } | |
7b903762 | 484 | $runperl .= ' 2>&1' if $args{stderr}; |
cb9c5e20 JH |
485 | if ($args{verbose}) { |
486 | my $runperldisplay = $runperl; | |
487 | $runperldisplay =~ s/\n/\n\#/g; | |
3d66076a | 488 | _print_stderr "# $runperldisplay\n"; |
137352a2 | 489 | } |
4cd2bd1f JH |
490 | return $runperl; |
491 | } | |
492 | ||
493 | sub runperl { | |
9a731dbd NC |
494 | die "test.pl:runperl() does not take a hashref" |
495 | if ref $_[0] and ref $_[0] eq 'HASH'; | |
4cd2bd1f | 496 | my $runperl = &_create_runperl; |
613de57f NC |
497 | my $result; |
498 | ||
8210c8d3 MB |
499 | my $tainted = ${^TAINT}; |
500 | my %args = @_; | |
485f531e | 501 | exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1; |
8210c8d3 MB |
502 | |
503 | if ($tainted) { | |
613de57f NC |
504 | # We will assume that if you're running under -T, you really mean to |
505 | # run a fresh perl, so we'll brute force launder everything for you | |
506 | my $sep; | |
507 | ||
afe79e7b | 508 | if (! eval 'require Config; 1') { |
613de57f NC |
509 | warn "test.pl had problems loading Config: $@"; |
510 | $sep = ':'; | |
511 | } else { | |
afe79e7b | 512 | $sep = $Config::Config{path_sep}; |
a70a1627 | 513 | } |
613de57f NC |
514 | |
515 | my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV); | |
516 | local @ENV{@keys} = (); | |
517 | # Untaint, plus take out . and empty string: | |
326b5008 | 518 | local $ENV{'DCL$PATH'} = $1 if $is_vms && ($ENV{'DCL$PATH'} =~ /(.*)/s); |
613de57f | 519 | $ENV{PATH} =~ /(.*)/s; |
8210c8d3 | 520 | local $ENV{PATH} = |
3b6d8381 | 521 | join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and |
326b5008 | 522 | ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) } |
8210c8d3 | 523 | split quotemeta ($sep), $1; |
e67ed694 | 524 | $ENV{PATH} .= "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin |
613de57f NC |
525 | |
526 | $runperl =~ /(.*)/s; | |
527 | $runperl = $1; | |
528 | ||
529 | $result = `$runperl`; | |
530 | } else { | |
531 | $result = `$runperl`; | |
a70a1627 | 532 | } |
137352a2 RGS |
533 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these |
534 | return $result; | |
535 | } | |
536 | ||
d2c6a9c9 | 537 | *run_perl = \&runperl; # Nice alias. |
8799135f | 538 | |
c4fbe247 | 539 | sub DIE { |
3d66076a | 540 | _print_stderr "# @_\n"; |
c4fbe247 | 541 | exit 1; |
8799135f MS |
542 | } |
543 | ||
b5fe401b | 544 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 JH |
545 | sub which_perl { |
546 | unless (defined $Perl) { | |
547 | $Perl = $^X; | |
8210c8d3 | 548 | |
73421c4a CB |
549 | # VMS should have 'perl' aliased properly |
550 | return $Perl if $^O eq 'VMS'; | |
551 | ||
17a740d5 | 552 | my $exe; |
afe79e7b | 553 | if (! eval 'require Config; 1') { |
17a740d5 JH |
554 | warn "test.pl had problems loading Config: $@"; |
555 | $exe = ''; | |
85363d30 | 556 | } else { |
afe79e7b | 557 | $exe = $Config::Config{_exe}; |
85363d30 | 558 | } |
da405c16 | 559 | $exe = '' unless defined $exe; |
8210c8d3 | 560 | |
17a740d5 JH |
561 | # This doesn't absolutize the path: beware of future chdirs(). |
562 | # We could do File::Spec->abs2rel() but that does getcwd()s, | |
563 | # which is a bit heavyweight to do here. | |
8210c8d3 | 564 | |
17a740d5 | 565 | if ($Perl =~ /^perl\Q$exe\E$/i) { |
8db06b02 | 566 | my $perl = "perl$exe"; |
afe79e7b | 567 | if (! eval 'require File::Spec; 1') { |
17a740d5 | 568 | warn "test.pl had problems loading File::Spec: $@"; |
8db06b02 | 569 | $Perl = "./$perl"; |
17a740d5 | 570 | } else { |
8db06b02 | 571 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 JH |
572 | } |
573 | } | |
196918b0 PG |
574 | |
575 | # Build up the name of the executable file from the name of | |
576 | # the command. | |
577 | ||
578 | if ($Perl !~ /\Q$exe\E$/i) { | |
579 | $Perl .= $exe; | |
580 | } | |
c880be78 | 581 | |
8db06b02 | 582 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
8210c8d3 | 583 | |
17a740d5 JH |
584 | # For subcommands to use. |
585 | $ENV{PERLEXE} = $Perl; | |
85363d30 | 586 | } |
17a740d5 | 587 | return $Perl; |
b5fe401b MS |
588 | } |
589 | ||
435e7af6 NC |
590 | sub unlink_all { |
591 | foreach my $file (@_) { | |
592 | 1 while unlink $file; | |
3d66076a | 593 | _print_stderr "# Couldn't unlink '$file': $!\n" if -f $file; |
435e7af6 NC |
594 | } |
595 | } | |
eeabcb2d | 596 | |
748a4b20 NC |
597 | my %tmpfiles; |
598 | END { unlink_all keys %tmpfiles } | |
599 | ||
600 | # A regexp that matches the tempfile names | |
601 | $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?'; | |
c1ddc35c | 602 | |
7a7e4936 NC |
603 | # Avoid ++, avoid ranges, avoid split // |
604 | my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z); | |
605 | sub tempfile { | |
606 | my $count = 0; | |
607 | do { | |
608 | my $temp = $count; | |
609 | my $try = "tmp$$"; | |
610 | do { | |
611 | $try .= $letters[$temp % 26]; | |
748a4b20 | 612 | $temp = int ($temp / 26); |
7a7e4936 | 613 | } while $temp; |
748a4b20 NC |
614 | # Need to note all the file names we allocated, as a second request may |
615 | # come before the first is created. | |
616 | if (!-e $try && !$tmpfiles{$try}) { | |
c1ddc35c | 617 | # We have a winner |
748a4b20 | 618 | $tmpfiles{$try}++; |
c1ddc35c NC |
619 | return $try; |
620 | } | |
7a7e4936 NC |
621 | $count = $count + 1; |
622 | } while $count < 26 * 26; | |
623 | die "Can't find temporary file name starting 'tmp$$'"; | |
624 | } | |
625 | ||
c1ddc35c | 626 | # This is the temporary file for _fresh_perl |
7a7e4936 | 627 | my $tmpfile = tempfile(); |
eeabcb2d | 628 | |
f5cda331 JH |
629 | # |
630 | # _fresh_perl | |
631 | # | |
632 | # The $resolve must be a subref that tests the first argument | |
633 | # for success, or returns the definition of success (e.g. the | |
634 | # expected scalar) if given no arguments. | |
635 | # | |
636 | ||
637 | sub _fresh_perl { | |
638 | my($prog, $resolve, $runperl_args, $name) = @_; | |
eeabcb2d MS |
639 | |
640 | $runperl_args ||= {}; | |
641 | $runperl_args->{progfile} = $tmpfile; | |
642 | $runperl_args->{stderr} = 1; | |
643 | ||
644 | open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; | |
645 | ||
646 | # VMS adjustments | |
647 | if( $^O eq 'VMS' ) { | |
648 | $prog =~ s#/dev/null#NL:#; | |
649 | ||
8210c8d3 | 650 | # VMS file locking |
eeabcb2d MS |
651 | $prog =~ s{if \(-e _ and -f _ and -r _\)} |
652 | {if (-e _ and -f _)} | |
653 | } | |
654 | ||
0d65d7d5 | 655 | print TEST $prog; |
eeabcb2d MS |
656 | close TEST or die "Cannot close $tmpfile: $!"; |
657 | ||
658 | my $results = runperl(%$runperl_args); | |
659 | my $status = $?; | |
660 | ||
661 | # Clean up the results into something a bit more predictable. | |
50f17f89 | 662 | $results =~ s/\n+$//; |
748a4b20 NC |
663 | $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g; |
664 | $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g; | |
eeabcb2d MS |
665 | |
666 | # bison says 'parse error' instead of 'syntax error', | |
667 | # various yaccs may or may not capitalize 'syntax'. | |
668 | $results =~ s/^(syntax|parse) error/syntax error/mig; | |
669 | ||
670 | if ($^O eq 'VMS') { | |
671 | # some tests will trigger VMS messages that won't be expected | |
672 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; | |
673 | ||
674 | # pipes double these sometimes | |
675 | $results =~ s/\n\n/\n/g; | |
676 | } | |
677 | ||
f5cda331 | 678 | my $pass = $resolve->($results); |
eeabcb2d | 679 | unless ($pass) { |
cf8feb78 MJD |
680 | _diag "# PROG: \n$prog\n"; |
681 | _diag "# EXPECTED:\n", $resolve->(), "\n"; | |
682 | _diag "# GOT:\n$results\n"; | |
683 | _diag "# STATUS: $status\n"; | |
eeabcb2d MS |
684 | } |
685 | ||
e2c38acd JH |
686 | # Use the first line of the program as a name if none was given |
687 | unless( $name ) { | |
688 | ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; | |
689 | $name .= '...' if length $first_line > length $name; | |
690 | } | |
eeabcb2d | 691 | |
f5cda331 JH |
692 | _ok($pass, _where(), "fresh_perl - $name"); |
693 | } | |
694 | ||
695 | # | |
141f445b | 696 | # fresh_perl_is |
f5cda331 JH |
697 | # |
698 | # Combination of run_perl() and is(). | |
699 | # | |
700 | ||
701 | sub fresh_perl_is { | |
702 | my($prog, $expected, $runperl_args, $name) = @_; | |
50f17f89 MS |
703 | |
704 | # _fresh_perl() is going to clip the trailing newlines off the result. | |
705 | # This will make it so the test author doesn't have to know that. | |
706 | $expected =~ s/\n+$//; | |
707 | ||
dcc7f481 | 708 | local $Level = 2; |
f5cda331 JH |
709 | _fresh_perl($prog, |
710 | sub { @_ ? $_[0] eq $expected : $expected }, | |
711 | $runperl_args, $name); | |
712 | } | |
713 | ||
714 | # | |
141f445b | 715 | # fresh_perl_like |
f5cda331 JH |
716 | # |
717 | # Combination of run_perl() and like(). | |
718 | # | |
719 | ||
720 | sub fresh_perl_like { | |
721 | my($prog, $expected, $runperl_args, $name) = @_; | |
dcc7f481 | 722 | local $Level = 2; |
f5cda331 JH |
723 | _fresh_perl($prog, |
724 | sub { @_ ? | |
725 | $_[0] =~ (ref $expected ? $expected : /$expected/) : | |
726 | $expected }, | |
727 | $runperl_args, $name); | |
eeabcb2d MS |
728 | } |
729 | ||
35a60386 RGS |
730 | sub can_ok ($@) { |
731 | my($proto, @methods) = @_; | |
732 | my $class = ref $proto || $proto; | |
733 | ||
734 | unless( @methods ) { | |
735 | return _ok( 0, _where(), "$class->can(...)" ); | |
736 | } | |
737 | ||
738 | my @nok = (); | |
739 | foreach my $method (@methods) { | |
740 | local($!, $@); # don't interfere with caller's $@ | |
741 | # eval sometimes resets $! | |
742 | eval { $proto->can($method) } || push @nok, $method; | |
743 | } | |
744 | ||
745 | my $name; | |
8210c8d3 | 746 | $name = @methods == 1 ? "$class->can('$methods[0]')" |
35a60386 | 747 | : "$class->can(...)"; |
8210c8d3 | 748 | |
35a60386 RGS |
749 | _ok( !@nok, _where(), $name ); |
750 | } | |
751 | ||
752 | sub isa_ok ($$;$) { | |
753 | my($object, $class, $obj_name) = @_; | |
754 | ||
755 | my $diag; | |
756 | $obj_name = 'The object' unless defined $obj_name; | |
757 | my $name = "$obj_name isa $class"; | |
758 | if( !defined $object ) { | |
759 | $diag = "$obj_name isn't defined"; | |
760 | } | |
761 | elsif( !ref $object ) { | |
762 | $diag = "$obj_name isn't a reference"; | |
763 | } | |
764 | else { | |
765 | # We can't use UNIVERSAL::isa because we want to honor isa() overrides | |
766 | local($@, $!); # eval sometimes resets $! | |
767 | my $rslt = eval { $object->isa($class) }; | |
768 | if( $@ ) { | |
769 | if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) { | |
770 | if( !UNIVERSAL::isa($object, $class) ) { | |
771 | my $ref = ref $object; | |
772 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
773 | } | |
774 | } else { | |
775 | die <<WHOA; | |
776 | WHOA! I tried to call ->isa on your object and got some weird error. | |
777 | This should never happen. Please contact the author immediately. | |
778 | Here's the error. | |
779 | $@ | |
780 | WHOA | |
781 | } | |
782 | } | |
783 | elsif( !$rslt ) { | |
784 | my $ref = ref $object; | |
785 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
786 | } | |
787 | } | |
788 | ||
789 | _ok( !$diag, _where(), $name ); | |
790 | } | |
791 | ||
087986a7 | 792 | # Set a watchdog to timeout the entire test file |
5fe9b82b JH |
793 | # NOTE: If the test file uses 'threads', then call the watchdog() function |
794 | # _AFTER_ the 'threads' module is loaded. | |
087986a7 JH |
795 | sub watchdog ($) |
796 | { | |
797 | my $timeout = shift; | |
798 | my $timeout_msg = 'Test process timed out - terminating'; | |
799 | ||
800 | my $pid_to_kill = $$; # PID for this process | |
801 | ||
5fe9b82b JH |
802 | # Don't use a watchdog process if 'threads' is loaded - |
803 | # use a watchdog thread instead | |
804 | if (! $threads::threads) { | |
805 | ||
806 | # On Windows and VMS, try launching a watchdog process | |
807 | # using system(1, ...) (see perlport.pod) | |
808 | if (($^O eq 'MSWin32') || ($^O eq 'VMS')) { | |
809 | # On Windows, try to get the 'real' PID | |
810 | if ($^O eq 'MSWin32') { | |
811 | eval { require Win32; }; | |
812 | if (defined(&Win32::GetCurrentProcessId)) { | |
813 | $pid_to_kill = Win32::GetCurrentProcessId(); | |
814 | } | |
087986a7 | 815 | } |
087986a7 | 816 | |
5fe9b82b JH |
817 | # If we still have a fake PID, we can't use this method at all |
818 | return if ($pid_to_kill <= 0); | |
819 | ||
820 | # Launch watchdog process | |
821 | my $watchdog; | |
822 | eval { | |
823 | local $SIG{'__WARN__'} = sub { | |
824 | _diag("Watchdog warning: $_[0]"); | |
825 | }; | |
c1c45e36 | 826 | my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL'; |
5fe9b82b JH |
827 | $watchdog = system(1, which_perl(), '-e', |
828 | "sleep($timeout);" . | |
2d4a14fe | 829 | "warn('# $timeout_msg\n');" . |
c1c45e36 | 830 | "kill($sig, $pid_to_kill);"); |
5fe9b82b JH |
831 | }; |
832 | if ($@ || ($watchdog <= 0)) { | |
833 | _diag('Failed to start watchdog'); | |
834 | _diag($@) if $@; | |
835 | undef($watchdog); | |
836 | return; | |
837 | } | |
087986a7 | 838 | |
5fe9b82b JH |
839 | # Add END block to parent to terminate and |
840 | # clean up watchdog process | |
7e1027b9 JH |
841 | eval "END { local \$! = 0; local \$? = 0; |
842 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b | 843 | return; |
087986a7 | 844 | } |
087986a7 | 845 | |
5fe9b82b JH |
846 | # Try using fork() to generate a watchdog process |
847 | my $watchdog; | |
848 | eval { $watchdog = fork() }; | |
849 | if (defined($watchdog)) { | |
850 | if ($watchdog) { # Parent process | |
851 | # Add END block to parent to terminate and | |
852 | # clean up watchdog process | |
7e1027b9 JH |
853 | eval "END { local \$! = 0; local \$? = 0; |
854 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b JH |
855 | return; |
856 | } | |
857 | ||
858 | ### Watchdog process code | |
087986a7 | 859 | |
5fe9b82b JH |
860 | # Load POSIX if available |
861 | eval { require POSIX; }; | |
087986a7 | 862 | |
5fe9b82b JH |
863 | # Execute the timeout |
864 | sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073 | |
865 | sleep(2); | |
087986a7 | 866 | |
5fe9b82b JH |
867 | # Kill test process if still running |
868 | if (kill(0, $pid_to_kill)) { | |
869 | _diag($timeout_msg); | |
870 | kill('KILL', $pid_to_kill); | |
871 | } | |
087986a7 | 872 | |
5fe9b82b JH |
873 | # Don't execute END block (added at beginning of this file) |
874 | $NO_ENDING = 1; | |
087986a7 | 875 | |
5fe9b82b JH |
876 | # Terminate ourself (i.e., the watchdog) |
877 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
878 | exit(1); | |
087986a7 JH |
879 | } |
880 | ||
5fe9b82b | 881 | # fork() failed - fall through and try using a thread |
087986a7 JH |
882 | } |
883 | ||
5fe9b82b JH |
884 | # Use a watchdog thread because either 'threads' is loaded, |
885 | # or fork() failed | |
afe79e7b | 886 | if (eval 'require threads; 1') { |
087986a7 JH |
887 | threads->create(sub { |
888 | # Load POSIX if available | |
889 | eval { require POSIX; }; | |
890 | ||
891 | # Execute the timeout | |
c1c45e36 | 892 | my $time_left = $timeout; |
a6c9a815 JH |
893 | do { |
894 | $time_left -= sleep($time_left); | |
895 | } while ($time_left > 0); | |
087986a7 JH |
896 | |
897 | # Kill the parent (and ourself) | |
5fe9b82b | 898 | select(STDERR); $| = 1; |
087986a7 JH |
899 | _diag($timeout_msg); |
900 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
c1c45e36 CB |
901 | my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL'; |
902 | kill($sig, $pid_to_kill); | |
087986a7 JH |
903 | })->detach(); |
904 | return; | |
905 | } | |
906 | ||
5fe9b82b | 907 | # If everything above fails, then just use an alarm timeout |
087986a7 JH |
908 | if (eval { alarm($timeout); 1; }) { |
909 | # Load POSIX if available | |
910 | eval { require POSIX; }; | |
911 | ||
912 | # Alarm handler will do the actual 'killing' | |
913 | $SIG{'ALRM'} = sub { | |
5fe9b82b | 914 | select(STDERR); $| = 1; |
087986a7 JH |
915 | _diag($timeout_msg); |
916 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
c1c45e36 CB |
917 | my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL'; |
918 | kill($sig, $pid_to_kill); | |
087986a7 JH |
919 | }; |
920 | } | |
921 | } | |
922 | ||
69026470 | 923 | 1; |