Commit | Line | Data |
---|---|---|
69026470 | 1 | # |
f69d9fdf KW |
2 | # t/test.pl - most of Test::More functionality without the fuss, plus |
3 | # has mappings native_to_latin1 and latin1_to_native so that fewer tests | |
4 | # on non ASCII-ish platforms need to be skipped | |
485f531e DL |
5 | |
6 | ||
7 | # NOTE: | |
8 | # | |
9 | # Increment ($x++) has a certain amount of cleverness for things like | |
10 | # | |
11 | # $x = 'zz'; | |
12 | # $x++; # $x eq 'aaa'; | |
69026470 | 13 | # |
485f531e DL |
14 | # stands more chance of breaking than just a simple |
15 | # | |
16 | # $x = $x + 1 | |
17 | # | |
18 | # In this file, we use the latter "Baby Perl" approach, and increment | |
19 | # will be worked over by t/op/inc.t | |
69026470 | 20 | |
dcc7f481 | 21 | $Level = 1; |
69026470 JH |
22 | my $test = 1; |
23 | my $planned; | |
6137113d | 24 | my $noplan; |
5fe9b82b | 25 | my $Perl; # Safer version of $^X set by which_perl() |
69026470 | 26 | |
7d932aad | 27 | $TODO = 0; |
b6345914 | 28 | $NO_ENDING = 0; |
02455492 | 29 | $Tests_Are_Passing = 1; |
7d932aad | 30 | |
3d66076a MS |
31 | # Use this instead of print to avoid interference while testing globals. |
32 | sub _print { | |
33 | local($\, $", $,) = (undef, ' ', ''); | |
34 | print STDOUT @_; | |
35 | } | |
36 | ||
37 | sub _print_stderr { | |
38 | local($\, $", $,) = (undef, ' ', ''); | |
39 | print STDERR @_; | |
40 | } | |
41 | ||
69026470 JH |
42 | sub plan { |
43 | my $n; | |
44 | if (@_ == 1) { | |
45 | $n = shift; | |
6137113d NC |
46 | if ($n eq 'no_plan') { |
47 | undef $n; | |
48 | $noplan = 1; | |
49 | } | |
69026470 JH |
50 | } else { |
51 | my %plan = @_; | |
8210c8d3 | 52 | $n = $plan{tests}; |
69026470 | 53 | } |
3d66076a | 54 | _print "1..$n\n" unless $noplan; |
69026470 JH |
55 | $planned = $n; |
56 | } | |
57 | ||
c4ef7183 MS |
58 | |
59 | # Set the plan at the end. See Test::More::done_testing. | |
60 | sub done_testing { | |
61 | my $n = $test - 1; | |
62 | $n = shift if @_; | |
63 | ||
64 | _print "1..$n\n"; | |
65 | $planned = $n; | |
66 | } | |
67 | ||
68 | ||
69026470 JH |
69 | END { |
70 | my $ran = $test - 1; | |
6137113d NC |
71 | if (!$NO_ENDING) { |
72 | if (defined $planned && $planned != $ran) { | |
3d66076a | 73 | _print_stderr |
6137113d NC |
74 | "# Looks like you planned $planned tests but ran $ran.\n"; |
75 | } elsif ($noplan) { | |
3d66076a | 76 | _print "1..$ran\n"; |
6137113d | 77 | } |
69026470 JH |
78 | } |
79 | } | |
80 | ||
de522f7a | 81 | sub _diag { |
cf8feb78 | 82 | return unless @_; |
92c9394b | 83 | my @mess = _comment(@_); |
44826442 | 84 | $TODO ? _print(@mess) : _print_stderr(@mess); |
de522f7a MS |
85 | } |
86 | ||
93f09d7b | 87 | # Use this instead of "print STDERR" when outputting failure diagnostic |
92c9394b | 88 | # messages |
485f531e DL |
89 | sub diag { |
90 | _diag(@_); | |
91 | } | |
92 | ||
93f09d7b | 93 | # Use this instead of "print" when outputting informational messages |
92c9394b MS |
94 | sub note { |
95 | return unless @_; | |
96 | _print( _comment(@_) ); | |
97 | } | |
98 | ||
445876fa KW |
99 | sub is_miniperl { |
100 | return !defined &DynaLoader::boot_DynaLoader; | |
101 | } | |
102 | ||
92c9394b MS |
103 | sub _comment { |
104 | return map { /^#/ ? "$_\n" : "# $_\n" } | |
105 | map { split /\n/ } @_; | |
106 | } | |
107 | ||
69026470 JH |
108 | sub skip_all { |
109 | if (@_) { | |
7bb7fa38 | 110 | _print "1..0 # Skip @_\n"; |
69026470 | 111 | } else { |
3d66076a | 112 | _print "1..0\n"; |
69026470 JH |
113 | } |
114 | exit(0); | |
115 | } | |
116 | ||
c82d0e1e | 117 | sub skip_all_if_miniperl { |
445876fa | 118 | skip_all(@_) if is_miniperl(); |
c82d0e1e NC |
119 | } |
120 | ||
69026470 | 121 | sub _ok { |
7d932aad | 122 | my ($pass, $where, $name, @mess) = @_; |
69026470 JH |
123 | # Do not try to microoptimize by factoring out the "not ". |
124 | # VMS will avenge. | |
7d932aad MS |
125 | my $out; |
126 | if ($name) { | |
b734d6c9 MS |
127 | # escape out '#' or it will interfere with '# skip' and such |
128 | $name =~ s/#/\\#/g; | |
7d932aad | 129 | $out = $pass ? "ok $test - $name" : "not ok $test - $name"; |
69026470 | 130 | } else { |
7d932aad | 131 | $out = $pass ? "ok $test" : "not ok $test"; |
69026470 | 132 | } |
7d932aad | 133 | |
02455492 NC |
134 | if ($TODO) { |
135 | $out = $out . " # TODO $TODO"; | |
136 | } else { | |
137 | $Tests_Are_Passing = 0 unless $pass; | |
138 | } | |
139 | ||
3d66076a | 140 | _print "$out\n"; |
7d932aad | 141 | |
9b9ae264 DM |
142 | if ($pass) { |
143 | note @mess; # Ensure that the message is properly escaped. | |
144 | } | |
145 | else { | |
de522f7a | 146 | _diag "# Failed $where\n"; |
9b9ae264 | 147 | _diag @mess; |
69026470 | 148 | } |
7d932aad | 149 | |
485f531e | 150 | $test = $test + 1; # don't use ++ |
1577bb16 MS |
151 | |
152 | return $pass; | |
69026470 JH |
153 | } |
154 | ||
155 | sub _where { | |
dcc7f481 | 156 | my @caller = caller($Level); |
69026470 JH |
157 | return "at $caller[1] line $caller[2]"; |
158 | } | |
159 | ||
1d662fb6 | 160 | # DON'T use this for matches. Use like() instead. |
c3029c66 | 161 | sub ok ($@) { |
7d932aad MS |
162 | my ($pass, $name, @mess) = @_; |
163 | _ok($pass, _where(), $name, @mess); | |
69026470 JH |
164 | } |
165 | ||
b3c72391 JH |
166 | sub _q { |
167 | my $x = shift; | |
168 | return 'undef' unless defined $x; | |
169 | my $q = $x; | |
d279d8f8 NC |
170 | $q =~ s/\\/\\\\/g; |
171 | $q =~ s/'/\\'/g; | |
b3c72391 JH |
172 | return "'$q'"; |
173 | } | |
174 | ||
677fb045 NC |
175 | sub _qq { |
176 | my $x = shift; | |
177 | return defined $x ? '"' . display ($x) . '"' : 'undef'; | |
178 | }; | |
179 | ||
180 | # keys are the codes \n etc map to, values are 2 char strings such as \n | |
181 | my %backslash_escape; | |
182 | foreach my $x (split //, 'nrtfa\\\'"') { | |
183 | $backslash_escape{ord eval "\"\\$x\""} = "\\$x"; | |
184 | } | |
185 | # A way to display scalars containing control characters and Unicode. | |
186 | # Trying to avoid setting $_, or relying on local $_ to work. | |
187 | sub display { | |
188 | my @result; | |
189 | foreach my $x (@_) { | |
190 | if (defined $x and not ref $x) { | |
191 | my $y = ''; | |
192 | foreach my $c (unpack("U*", $x)) { | |
193 | if ($c > 255) { | |
11ea18f2 | 194 | $y = $y . sprintf "\\x{%x}", $c; |
677fb045 | 195 | } elsif ($backslash_escape{$c}) { |
11ea18f2 | 196 | $y = $y . $backslash_escape{$c}; |
677fb045 NC |
197 | } else { |
198 | my $z = chr $c; # Maybe we can get away with a literal... | |
1cfccccd KW |
199 | if ($z =~ /[[:^print:]]/) { |
200 | ||
201 | # Use octal for characters traditionally expressed as | |
202 | # such: the low controls | |
203 | if ($c <= 037) { | |
204 | $z = sprintf "\\%03o", $c; | |
205 | } else { | |
206 | $z = sprintf "\\x{%x}", $c; | |
207 | } | |
208 | } | |
11ea18f2 | 209 | $y = $y . $z; |
677fb045 NC |
210 | } |
211 | } | |
212 | $x = $y; | |
213 | } | |
214 | return $x unless wantarray; | |
215 | push @result, $x; | |
216 | } | |
217 | return @result; | |
218 | } | |
219 | ||
c3029c66 | 220 | sub is ($$@) { |
7d932aad | 221 | my ($got, $expected, $name, @mess) = @_; |
c831d34f MS |
222 | |
223 | my $pass; | |
224 | if( !defined $got || !defined $expected ) { | |
225 | # undef only matches undef | |
226 | $pass = !defined $got && !defined $expected; | |
227 | } | |
228 | else { | |
229 | $pass = $got eq $expected; | |
230 | } | |
231 | ||
69026470 | 232 | unless ($pass) { |
d5f8084a KW |
233 | unshift(@mess, "# got "._qq($got)."\n", |
234 | "# expected "._qq($expected)."\n"); | |
69026470 | 235 | } |
7d932aad | 236 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
237 | } |
238 | ||
c3029c66 | 239 | sub isnt ($$@) { |
3e90d5a3 | 240 | my ($got, $isnt, $name, @mess) = @_; |
c831d34f MS |
241 | |
242 | my $pass; | |
243 | if( !defined $got || !defined $isnt ) { | |
244 | # undef only matches undef | |
245 | $pass = defined $got || defined $isnt; | |
246 | } | |
247 | else { | |
248 | $pass = $got ne $isnt; | |
249 | } | |
250 | ||
3e90d5a3 | 251 | unless( $pass ) { |
d5f8084a | 252 | unshift(@mess, "# it should not be "._qq($got)."\n", |
3e90d5a3 MS |
253 | "# but it is.\n"); |
254 | } | |
255 | _ok($pass, _where(), $name, @mess); | |
256 | } | |
257 | ||
c3029c66 | 258 | sub cmp_ok ($$$@) { |
58d76dfd JH |
259 | my($got, $type, $expected, $name, @mess) = @_; |
260 | ||
261 | my $pass; | |
262 | { | |
263 | local $^W = 0; | |
264 | local($@,$!); # don't interfere with $@ | |
265 | # eval() sometimes resets $! | |
266 | $pass = eval "\$got $type \$expected"; | |
267 | } | |
268 | unless ($pass) { | |
269 | # It seems Irix long doubles can have 2147483648 and 2147483648 | |
93f09d7b | 270 | # that stringify to the same thing but are actually numerically |
58d76dfd JH |
271 | # different. Display the numbers if $type isn't a string operator, |
272 | # and the numbers are stringwise the same. | |
273 | # (all string operators have alphabetic names, so tr/a-z// is true) | |
93f09d7b PA |
274 | # This will also show numbers for some unneeded cases, but will |
275 | # definitely be helpful for things such as == and <= that fail | |
58d76dfd JH |
276 | if ($got eq $expected and $type !~ tr/a-z//) { |
277 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
278 | } | |
d5f8084a KW |
279 | unshift(@mess, "# got "._qq($got)."\n", |
280 | "# expected $type "._qq($expected)."\n"); | |
58d76dfd JH |
281 | } |
282 | _ok($pass, _where(), $name, @mess); | |
283 | } | |
284 | ||
285 | # Check that $got is within $range of $expected | |
286 | # if $range is 0, then check it's exact | |
287 | # else if $expected is 0, then $range is an absolute value | |
288 | # otherwise $range is a fractional error. | |
289 | # Here $range must be numeric, >= 0 | |
290 | # Non numeric ranges might be a useful future extension. (eg %) | |
c3029c66 | 291 | sub within ($$$@) { |
58d76dfd JH |
292 | my ($got, $expected, $range, $name, @mess) = @_; |
293 | my $pass; | |
294 | if (!defined $got or !defined $expected or !defined $range) { | |
295 | # This is a fail, but doesn't need extra diagnostics | |
296 | } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) { | |
297 | # This is a fail | |
298 | unshift @mess, "# got, expected and range must be numeric\n"; | |
299 | } elsif ($range < 0) { | |
300 | # This is also a fail | |
301 | unshift @mess, "# range must not be negative\n"; | |
302 | } elsif ($range == 0) { | |
303 | # Within 0 is == | |
304 | $pass = $got == $expected; | |
305 | } elsif ($expected == 0) { | |
306 | # If expected is 0, treat range as absolute | |
307 | $pass = ($got <= $range) && ($got >= - $range); | |
308 | } else { | |
309 | my $diff = $got - $expected; | |
310 | $pass = abs ($diff / $expected) < $range; | |
311 | } | |
312 | unless ($pass) { | |
313 | if ($got eq $expected) { | |
314 | unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; | |
315 | } | |
d5f8084a KW |
316 | unshift@mess, "# got "._qq($got)."\n", |
317 | "# expected "._qq($expected)." (within "._qq($range).")\n"; | |
58d76dfd JH |
318 | } |
319 | _ok($pass, _where(), $name, @mess); | |
320 | } | |
321 | ||
69026470 | 322 | # Note: this isn't quite as fancy as Test::More::like(). |
724aa791 JC |
323 | |
324 | sub like ($$@) { like_yn (0,@_) }; # 0 for - | |
325 | sub unlike ($$@) { like_yn (1,@_) }; # 1 for un- | |
326 | ||
327 | sub like_yn ($$$@) { | |
328 | my ($flip, $got, $expected, $name, @mess) = @_; | |
69026470 | 329 | my $pass; |
724aa791 JC |
330 | $pass = $got =~ /$expected/ if !$flip; |
331 | $pass = $got !~ /$expected/ if $flip; | |
332 | unless ($pass) { | |
333 | unshift(@mess, "# got '$got'\n", | |
5a4a8c8b NC |
334 | $flip |
335 | ? "# expected !~ /$expected/\n" : "# expected /$expected/\n"); | |
69026470 | 336 | } |
5693d826 | 337 | local $Level = $Level + 1; |
7d932aad | 338 | _ok($pass, _where(), $name, @mess); |
69026470 JH |
339 | } |
340 | ||
341 | sub pass { | |
342 | _ok(1, '', @_); | |
343 | } | |
344 | ||
345 | sub fail { | |
346 | _ok(0, _where(), @_); | |
347 | } | |
348 | ||
ad20d923 | 349 | sub curr_test { |
cf8feb78 | 350 | $test = shift if @_; |
ad20d923 MS |
351 | return $test; |
352 | } | |
353 | ||
3e90d5a3 | 354 | sub next_test { |
178eff92 | 355 | my $retval = $test; |
485f531e | 356 | $test = $test + 1; # don't use ++ |
178eff92 | 357 | $retval; |
3e90d5a3 MS |
358 | } |
359 | ||
69026470 JH |
360 | # Note: can't pass multipart messages since we try to |
361 | # be compatible with Test::More::skip(). | |
362 | sub skip { | |
7d932aad | 363 | my $why = shift; |
982b7cb7 | 364 | my $n = @_ ? shift : 1; |
69026470 | 365 | for (1..$n) { |
7bb7fa38 | 366 | _print "ok $test # skip $why\n"; |
485f531e | 367 | $test = $test + 1; |
69026470 JH |
368 | } |
369 | local $^W = 0; | |
370 | last SKIP; | |
371 | } | |
372 | ||
8c49cd2e | 373 | sub skip_if_miniperl { |
445876fa | 374 | skip(@_) if is_miniperl(); |
8c49cd2e NC |
375 | } |
376 | ||
09f04786 MS |
377 | sub todo_skip { |
378 | my $why = shift; | |
379 | my $n = @_ ? shift : 1; | |
380 | ||
381 | for (1..$n) { | |
7bb7fa38 | 382 | _print "not ok $test # TODO & SKIP $why\n"; |
485f531e | 383 | $test = $test + 1; |
09f04786 MS |
384 | } |
385 | local $^W = 0; | |
386 | last TODO; | |
387 | } | |
388 | ||
69026470 JH |
389 | sub eq_array { |
390 | my ($ra, $rb) = @_; | |
391 | return 0 unless $#$ra == $#$rb; | |
392 | for my $i (0..$#$ra) { | |
8210c8d3 | 393 | next if !defined $ra->[$i] && !defined $rb->[$i]; |
135d199b DM |
394 | return 0 if !defined $ra->[$i]; |
395 | return 0 if !defined $rb->[$i]; | |
69026470 JH |
396 | return 0 unless $ra->[$i] eq $rb->[$i]; |
397 | } | |
398 | return 1; | |
399 | } | |
400 | ||
677fb045 NC |
401 | sub eq_hash { |
402 | my ($orig, $suspect) = @_; | |
403 | my $fail; | |
404 | while (my ($key, $value) = each %$suspect) { | |
405 | # Force a hash recompute if this perl's internals can cache the hash key. | |
406 | $key = "" . $key; | |
407 | if (exists $orig->{$key}) { | |
408 | if ($orig->{$key} ne $value) { | |
3d66076a | 409 | _print "# key ", _qq($key), " was ", _qq($orig->{$key}), |
de522f7a | 410 | " now ", _qq($value), "\n"; |
677fb045 NC |
411 | $fail = 1; |
412 | } | |
413 | } else { | |
3d66076a | 414 | _print "# key ", _qq($key), " is ", _qq($value), |
75385f53 | 415 | ", not in original.\n"; |
677fb045 NC |
416 | $fail = 1; |
417 | } | |
418 | } | |
419 | foreach (keys %$orig) { | |
420 | # Force a hash recompute if this perl's internals can cache the hash key. | |
421 | $_ = "" . $_; | |
422 | next if (exists $suspect->{$_}); | |
3d66076a | 423 | _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 NC |
424 | $fail = 1; |
425 | } | |
426 | !$fail; | |
427 | } | |
428 | ||
d47bdea7 | 429 | # We only provide a subset of the Test::More functionality. |
c3029c66 | 430 | sub require_ok ($) { |
69026470 | 431 | my ($require) = @_; |
d47bdea7 NC |
432 | if ($require =~ tr/[A-Za-z0-9:.]//c) { |
433 | fail("Invalid character in \"$require\", passed to require_ok"); | |
434 | } else { | |
435 | eval <<REQUIRE_OK; | |
69026470 JH |
436 | require $require; |
437 | REQUIRE_OK | |
d47bdea7 NC |
438 | is($@, '', _where(), "require $require"); |
439 | } | |
69026470 JH |
440 | } |
441 | ||
c3029c66 | 442 | sub use_ok ($) { |
69026470 | 443 | my ($use) = @_; |
d47bdea7 NC |
444 | if ($use =~ tr/[A-Za-z0-9:.]//c) { |
445 | fail("Invalid character in \"$use\", passed to use"); | |
446 | } else { | |
447 | eval <<USE_OK; | |
69026470 JH |
448 | use $use; |
449 | USE_OK | |
d47bdea7 NC |
450 | is($@, '', _where(), "use $use"); |
451 | } | |
69026470 JH |
452 | } |
453 | ||
137352a2 RGS |
454 | # runperl - Runs a separate perl interpreter. |
455 | # Arguments : | |
456 | # switches => [ command-line switches ] | |
457 | # nolib => 1 # don't use -I../lib (included by default) | |
3d7a9343 | 458 | # non_portable => Don't warn if a one liner contains quotes |
137352a2 | 459 | # prog => one-liner (avoid quotes) |
d83945bc | 460 | # progs => [ multi-liner (avoid quotes) ] |
137352a2 RGS |
461 | # progfile => perl script |
462 | # stdin => string to feed the stdin | |
463 | # stderr => redirect stderr to stdout | |
464 | # args => [ command-line arguments to the perl program ] | |
cb9c5e20 | 465 | # verbose => print the command line |
137352a2 RGS |
466 | |
467 | my $is_mswin = $^O eq 'MSWin32'; | |
468 | my $is_netware = $^O eq 'NetWare'; | |
137352a2 | 469 | my $is_vms = $^O eq 'VMS'; |
e67ed694 | 470 | my $is_cygwin = $^O eq 'cygwin'; |
137352a2 | 471 | |
cb9c5e20 JH |
472 | sub _quote_args { |
473 | my ($runperl, $args) = @_; | |
474 | ||
475 | foreach (@$args) { | |
476 | # In VMS protect with doublequotes because otherwise | |
477 | # DCL will lowercase -- unless already doublequoted. | |
ea9ac5ad | 478 | $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; |
1cce9906 | 479 | $runperl = $runperl . ' ' . $_; |
cb9c5e20 | 480 | } |
1cce9906 | 481 | return $runperl; |
cb9c5e20 JH |
482 | } |
483 | ||
4cd2bd1f | 484 | sub _create_runperl { # Create the string to qx in runperl(). |
137352a2 | 485 | my %args = @_; |
5fe9b82b JH |
486 | my $runperl = which_perl(); |
487 | if ($runperl =~ m/\s/) { | |
488 | $runperl = qq{"$runperl"}; | |
489 | } | |
6cf707aa RGS |
490 | #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind |
491 | if ($ENV{PERL_RUNPERL_DEBUG}) { | |
492 | $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl"; | |
493 | } | |
f93a5f07 | 494 | unless ($args{nolib}) { |
11ea18f2 | 495 | $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS |
137352a2 | 496 | } |
d83945bc | 497 | if ($args{switches}) { |
343d4a7b JH |
498 | local $Level = 2; |
499 | die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where() | |
500 | unless ref $args{switches} eq "ARRAY"; | |
1cce9906 | 501 | $runperl = _quote_args($runperl, $args{switches}); |
d83945bc | 502 | } |
137352a2 | 503 | if (defined $args{prog}) { |
21820af6 JH |
504 | die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where() |
505 | if defined $args{progs}; | |
d83945bc A |
506 | $args{progs} = [$args{prog}] |
507 | } | |
508 | if (defined $args{progs}) { | |
21820af6 JH |
509 | die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where() |
510 | unless ref $args{progs} eq "ARRAY"; | |
d83945bc | 511 | foreach my $prog (@{$args{progs}}) { |
3d7a9343 NC |
512 | if ($prog =~ tr/'"// && !$args{non_portable}) { |
513 | warn "quotes in prog >>$prog<< are not portable"; | |
514 | } | |
d83945bc | 515 | if ($is_mswin || $is_netware || $is_vms) { |
11ea18f2 | 516 | $runperl = $runperl . qq ( -e "$prog" ); |
d83945bc A |
517 | } |
518 | else { | |
11ea18f2 | 519 | $runperl = $runperl . qq ( -e '$prog' ); |
d83945bc A |
520 | } |
521 | } | |
137352a2 | 522 | } elsif (defined $args{progfile}) { |
11ea18f2 | 523 | $runperl = $runperl . qq( "$args{progfile}"); |
9a731dbd | 524 | } else { |
93f09d7b | 525 | # You probably didn't want to be sucking in from the upstream stdin |
9a731dbd NC |
526 | die "test.pl:runperl(): none of prog, progs, progfile, args, " |
527 | . " switches or stdin specified" | |
528 | unless defined $args{args} or defined $args{switches} | |
529 | or defined $args{stdin}; | |
137352a2 RGS |
530 | } |
531 | if (defined $args{stdin}) { | |
dc459aad JH |
532 | # so we don't try to put literal newlines and crs onto the |
533 | # command line. | |
534 | $args{stdin} =~ s/\n/\\n/g; | |
535 | $args{stdin} =~ s/\r/\\r/g; | |
5ae09a77 | 536 | |
137352a2 | 537 | if ($is_mswin || $is_netware || $is_vms) { |
5fe9b82b | 538 | $runperl = qq{$Perl -e "print qq(} . |
137352a2 RGS |
539 | $args{stdin} . q{)" | } . $runperl; |
540 | } | |
541 | else { | |
5fe9b82b | 542 | $runperl = qq{$Perl -e 'print qq(} . |
137352a2 RGS |
543 | $args{stdin} . q{)' | } . $runperl; |
544 | } | |
545 | } | |
546 | if (defined $args{args}) { | |
1cce9906 | 547 | $runperl = _quote_args($runperl, $args{args}); |
cb9c5e20 | 548 | } |
11ea18f2 | 549 | $runperl = $runperl . ' 2>&1' if $args{stderr}; |
cb9c5e20 JH |
550 | if ($args{verbose}) { |
551 | my $runperldisplay = $runperl; | |
552 | $runperldisplay =~ s/\n/\n\#/g; | |
3d66076a | 553 | _print_stderr "# $runperldisplay\n"; |
137352a2 | 554 | } |
4cd2bd1f JH |
555 | return $runperl; |
556 | } | |
557 | ||
558 | sub runperl { | |
9a731dbd NC |
559 | die "test.pl:runperl() does not take a hashref" |
560 | if ref $_[0] and ref $_[0] eq 'HASH'; | |
4cd2bd1f | 561 | my $runperl = &_create_runperl; |
613de57f NC |
562 | my $result; |
563 | ||
8210c8d3 MB |
564 | my $tainted = ${^TAINT}; |
565 | my %args = @_; | |
485f531e | 566 | exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1; |
8210c8d3 MB |
567 | |
568 | if ($tainted) { | |
613de57f NC |
569 | # We will assume that if you're running under -T, you really mean to |
570 | # run a fresh perl, so we'll brute force launder everything for you | |
571 | my $sep; | |
572 | ||
afe79e7b | 573 | if (! eval 'require Config; 1') { |
613de57f NC |
574 | warn "test.pl had problems loading Config: $@"; |
575 | $sep = ':'; | |
576 | } else { | |
afe79e7b | 577 | $sep = $Config::Config{path_sep}; |
a70a1627 | 578 | } |
613de57f NC |
579 | |
580 | my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV); | |
581 | local @ENV{@keys} = (); | |
582 | # Untaint, plus take out . and empty string: | |
02bb3106 | 583 | local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s); |
613de57f | 584 | $ENV{PATH} =~ /(.*)/s; |
8210c8d3 | 585 | local $ENV{PATH} = |
3b6d8381 | 586 | join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and |
326b5008 | 587 | ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) } |
8210c8d3 | 588 | split quotemeta ($sep), $1; |
59aae9bd JH |
589 | if ($is_cygwin) { # Must have /bin under Cygwin |
590 | if (length $ENV{PATH}) { | |
591 | $ENV{PATH} = $ENV{PATH} . $sep; | |
592 | } | |
593 | $ENV{PATH} = $ENV{PATH} . '/bin'; | |
594 | } | |
613de57f NC |
595 | $runperl =~ /(.*)/s; |
596 | $runperl = $1; | |
597 | ||
598 | $result = `$runperl`; | |
599 | } else { | |
600 | $result = `$runperl`; | |
a70a1627 | 601 | } |
137352a2 RGS |
602 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these |
603 | return $result; | |
604 | } | |
605 | ||
140f5369 MS |
606 | # Nice alias |
607 | *run_perl = *run_perl = \&runperl; # shut up "used only once" warning | |
8799135f | 608 | |
c4fbe247 | 609 | sub DIE { |
3d66076a | 610 | _print_stderr "# @_\n"; |
c4fbe247 | 611 | exit 1; |
8799135f MS |
612 | } |
613 | ||
b5fe401b | 614 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 JH |
615 | sub which_perl { |
616 | unless (defined $Perl) { | |
617 | $Perl = $^X; | |
8210c8d3 | 618 | |
73421c4a | 619 | # VMS should have 'perl' aliased properly |
4b0f0df6 | 620 | return $Perl if $is_vms; |
73421c4a | 621 | |
17a740d5 | 622 | my $exe; |
afe79e7b | 623 | if (! eval 'require Config; 1') { |
17a740d5 JH |
624 | warn "test.pl had problems loading Config: $@"; |
625 | $exe = ''; | |
85363d30 | 626 | } else { |
afe79e7b | 627 | $exe = $Config::Config{_exe}; |
85363d30 | 628 | } |
da405c16 | 629 | $exe = '' unless defined $exe; |
8210c8d3 | 630 | |
17a740d5 JH |
631 | # This doesn't absolutize the path: beware of future chdirs(). |
632 | # We could do File::Spec->abs2rel() but that does getcwd()s, | |
633 | # which is a bit heavyweight to do here. | |
8210c8d3 | 634 | |
17a740d5 | 635 | if ($Perl =~ /^perl\Q$exe\E$/i) { |
8db06b02 | 636 | my $perl = "perl$exe"; |
afe79e7b | 637 | if (! eval 'require File::Spec; 1') { |
17a740d5 | 638 | warn "test.pl had problems loading File::Spec: $@"; |
8db06b02 | 639 | $Perl = "./$perl"; |
17a740d5 | 640 | } else { |
8db06b02 | 641 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 JH |
642 | } |
643 | } | |
196918b0 PG |
644 | |
645 | # Build up the name of the executable file from the name of | |
646 | # the command. | |
647 | ||
648 | if ($Perl !~ /\Q$exe\E$/i) { | |
11ea18f2 | 649 | $Perl = $Perl . $exe; |
196918b0 | 650 | } |
c880be78 | 651 | |
8db06b02 | 652 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
8210c8d3 | 653 | |
17a740d5 JH |
654 | # For subcommands to use. |
655 | $ENV{PERLEXE} = $Perl; | |
85363d30 | 656 | } |
17a740d5 | 657 | return $Perl; |
b5fe401b MS |
658 | } |
659 | ||
435e7af6 | 660 | sub unlink_all { |
55b0687d | 661 | my $count = 0; |
435e7af6 NC |
662 | foreach my $file (@_) { |
663 | 1 while unlink $file; | |
55b0687d BG |
664 | if( -f $file ){ |
665 | _print_stderr "# Couldn't unlink '$file': $!\n"; | |
666 | }else{ | |
667 | ++$count; | |
668 | } | |
435e7af6 | 669 | } |
55b0687d | 670 | $count; |
435e7af6 | 671 | } |
eeabcb2d | 672 | |
748a4b20 NC |
673 | my %tmpfiles; |
674 | END { unlink_all keys %tmpfiles } | |
675 | ||
676 | # A regexp that matches the tempfile names | |
677 | $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?'; | |
c1ddc35c | 678 | |
7a7e4936 NC |
679 | # Avoid ++, avoid ranges, avoid split // |
680 | 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); | |
681 | sub tempfile { | |
682 | my $count = 0; | |
683 | do { | |
684 | my $temp = $count; | |
685 | my $try = "tmp$$"; | |
686 | do { | |
11ea18f2 | 687 | $try = $try . $letters[$temp % 26]; |
748a4b20 | 688 | $temp = int ($temp / 26); |
7a7e4936 | 689 | } while $temp; |
748a4b20 NC |
690 | # Need to note all the file names we allocated, as a second request may |
691 | # come before the first is created. | |
692 | if (!-e $try && !$tmpfiles{$try}) { | |
c1ddc35c | 693 | # We have a winner |
11ea18f2 | 694 | $tmpfiles{$try} = 1; |
c1ddc35c NC |
695 | return $try; |
696 | } | |
7a7e4936 NC |
697 | $count = $count + 1; |
698 | } while $count < 26 * 26; | |
699 | die "Can't find temporary file name starting 'tmp$$'"; | |
700 | } | |
701 | ||
c1ddc35c | 702 | # This is the temporary file for _fresh_perl |
7a7e4936 | 703 | my $tmpfile = tempfile(); |
eeabcb2d | 704 | |
f5cda331 | 705 | sub _fresh_perl { |
55280a0d | 706 | my($prog, $action, $expect, $runperl_args, $name) = @_; |
eeabcb2d | 707 | |
11ea18f2 NC |
708 | # Given the choice of the mis-parsable {} |
709 | # (we want an anon hash, but a borked lexer might think that it's a block) | |
710 | # or relying on taking a reference to a lexical | |
711 | # (\ might be mis-parsed, and the reference counting on the pad may go | |
712 | # awry) | |
713 | # it feels like the least-worse thing is to assume that auto-vivification | |
714 | # works. At least, this is only going to be a run-time failure, so won't | |
715 | # affect tests using this file but not this function. | |
eeabcb2d MS |
716 | $runperl_args->{progfile} = $tmpfile; |
717 | $runperl_args->{stderr} = 1; | |
718 | ||
719 | open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; | |
720 | ||
721 | # VMS adjustments | |
4b0f0df6 | 722 | if( $is_vms ) { |
eeabcb2d MS |
723 | $prog =~ s#/dev/null#NL:#; |
724 | ||
8210c8d3 | 725 | # VMS file locking |
eeabcb2d MS |
726 | $prog =~ s{if \(-e _ and -f _ and -r _\)} |
727 | {if (-e _ and -f _)} | |
728 | } | |
729 | ||
0d65d7d5 | 730 | print TEST $prog; |
eeabcb2d MS |
731 | close TEST or die "Cannot close $tmpfile: $!"; |
732 | ||
733 | my $results = runperl(%$runperl_args); | |
734 | my $status = $?; | |
735 | ||
736 | # Clean up the results into something a bit more predictable. | |
50f17f89 | 737 | $results =~ s/\n+$//; |
748a4b20 NC |
738 | $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g; |
739 | $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g; | |
eeabcb2d MS |
740 | |
741 | # bison says 'parse error' instead of 'syntax error', | |
742 | # various yaccs may or may not capitalize 'syntax'. | |
743 | $results =~ s/^(syntax|parse) error/syntax error/mig; | |
744 | ||
4b0f0df6 | 745 | if ($is_vms) { |
eeabcb2d MS |
746 | # some tests will trigger VMS messages that won't be expected |
747 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; | |
748 | ||
749 | # pipes double these sometimes | |
750 | $results =~ s/\n\n/\n/g; | |
751 | } | |
752 | ||
e2c38acd JH |
753 | # Use the first line of the program as a name if none was given |
754 | unless( $name ) { | |
755 | ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; | |
11ea18f2 | 756 | $name = $name . '...' if length $first_line > length $name; |
e2c38acd | 757 | } |
eeabcb2d | 758 | |
55280a0d NC |
759 | # Historically this was implemented using a closure, but then that means |
760 | # that the tests for closures avoid using this code. Given that there | |
761 | # are exactly two callers, doing exactly two things, the simpler approach | |
762 | # feels like a better trade off. | |
763 | my $pass; | |
764 | if ($action eq 'eq') { | |
765 | $pass = is($results, $expect, $name); | |
766 | } elsif ($action eq '=~') { | |
767 | $pass = like($results, $expect, $name); | |
768 | } else { | |
769 | die "_fresh_perl can't process action '$action'"; | |
770 | } | |
771 | ||
772 | unless ($pass) { | |
773 | _diag "# PROG: \n$prog\n"; | |
774 | _diag "# STATUS: $status\n"; | |
775 | } | |
776 | ||
777 | return $pass; | |
f5cda331 JH |
778 | } |
779 | ||
780 | # | |
141f445b | 781 | # fresh_perl_is |
f5cda331 JH |
782 | # |
783 | # Combination of run_perl() and is(). | |
784 | # | |
785 | ||
786 | sub fresh_perl_is { | |
787 | my($prog, $expected, $runperl_args, $name) = @_; | |
50f17f89 MS |
788 | |
789 | # _fresh_perl() is going to clip the trailing newlines off the result. | |
790 | # This will make it so the test author doesn't have to know that. | |
791 | $expected =~ s/\n+$//; | |
792 | ||
dcc7f481 | 793 | local $Level = 2; |
55280a0d | 794 | _fresh_perl($prog, 'eq', $expected, $runperl_args, $name); |
f5cda331 JH |
795 | } |
796 | ||
797 | # | |
141f445b | 798 | # fresh_perl_like |
f5cda331 JH |
799 | # |
800 | # Combination of run_perl() and like(). | |
801 | # | |
802 | ||
803 | sub fresh_perl_like { | |
804 | my($prog, $expected, $runperl_args, $name) = @_; | |
dcc7f481 | 805 | local $Level = 2; |
55280a0d | 806 | _fresh_perl($prog, '=~', $expected, $runperl_args, $name); |
eeabcb2d MS |
807 | } |
808 | ||
ebf2da99 NC |
809 | # Many tests use the same format in __DATA__ or external files to specify a |
810 | # sequence of (fresh) tests to run, extra files they may temporarily need, and | |
811 | # what the expected output is. So have excatly one copy of the code to run that | |
812 | ||
813 | sub run_multiple_progs { | |
5f7e0818 NC |
814 | my $up = shift; |
815 | my @prgs; | |
816 | if ($up) { | |
817 | # The tests in lib run in a temporary subdirectory of t, and always | |
818 | # pass in a list of "programs" to run | |
819 | @prgs = @_; | |
820 | } else { | |
821 | # The tests below t run in t and pass in a file handle. | |
822 | my $fh = shift; | |
823 | local $/; | |
824 | @prgs = split "\n########\n", <$fh>; | |
825 | } | |
826 | ||
ebf2da99 NC |
827 | my $tmpfile = tempfile(); |
828 | ||
829 | for (@prgs){ | |
830 | unless (/\n/) { | |
831 | print "# From $_\n"; | |
832 | next; | |
833 | } | |
834 | my $switch = ""; | |
835 | my @temps ; | |
836 | my @temp_path; | |
837 | if (s/^(\s*-\w+)//) { | |
838 | $switch = $1; | |
839 | } | |
840 | my ($prog, $expected) = split(/\nEXPECT(?:\n|$)/, $_, 2); | |
841 | ||
842 | my %reason; | |
843 | foreach my $what (qw(skip todo)) { | |
844 | $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1; | |
845 | # If the SKIP reason starts ? then it's taken as a code snippet to | |
846 | # evaluate. This provides the flexibility to have conditional SKIPs | |
847 | if ($reason{$what} && $reason{$what} =~ s/^\?//) { | |
848 | my $temp = eval $reason{$what}; | |
849 | if ($@) { | |
850 | die "# In \U$what\E code reason:\n# $reason{$what}\n$@"; | |
851 | } | |
852 | $reason{$what} = $temp; | |
853 | } | |
854 | } | |
855 | ||
856 | if ($prog =~ /--FILE--/) { | |
857 | my @files = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ; | |
858 | shift @files ; | |
859 | die "Internal error: test $_ didn't split into pairs, got " . | |
860 | scalar(@files) . "[" . join("%%%%", @files) ."]\n" | |
861 | if @files % 2; | |
862 | while (@files > 2) { | |
863 | my $filename = shift @files; | |
864 | my $code = shift @files; | |
865 | push @temps, $filename; | |
866 | if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) { | |
867 | require File::Path; | |
868 | File::Path::mkpath($1); | |
869 | push(@temp_path, $1); | |
870 | } | |
871 | open my $fh, '>', $filename or die "Cannot open $filename: $!\n"; | |
872 | print $fh $code; | |
873 | close $fh or die "Cannot close $filename: $!\n"; | |
874 | } | |
875 | shift @files; | |
876 | $prog = shift @files; | |
877 | } | |
878 | ||
879 | open my $fh, '>', $tmpfile or die "Cannot open >$tmpfile: $!"; | |
880 | print $fh q{ | |
881 | BEGIN { | |
882 | open STDERR, '>&', STDOUT | |
883 | or die "Can't dup STDOUT->STDERR: $!;"; | |
884 | } | |
885 | }; | |
886 | print $fh "\n#line 1\n"; # So the line numbers don't get messed up. | |
887 | print $fh $prog,"\n"; | |
888 | close $fh or die "Cannot close $tmpfile: $!"; | |
5f7e0818 NC |
889 | my $results = runperl( stderr => 1, progfile => $tmpfile, $up |
890 | ? (switches => ["-I$up/lib", $switch], nolib => 1) | |
891 | : (switches => [$switch]) | |
892 | ); | |
ebf2da99 NC |
893 | my $status = $?; |
894 | $results =~ s/\n+$//; | |
895 | # allow expected output to be written as if $prog is on STDIN | |
896 | $results =~ s/$::tempfile_regexp/-/g; | |
897 | if ($^O eq 'VMS') { | |
898 | # some tests will trigger VMS messages that won't be expected | |
899 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; | |
900 | ||
901 | # pipes double these sometimes | |
902 | $results =~ s/\n\n/\n/g; | |
903 | } | |
904 | # bison says 'parse error' instead of 'syntax error', | |
905 | # various yaccs may or may not capitalize 'syntax'. | |
906 | $results =~ s/^(syntax|parse) error/syntax error/mig; | |
907 | # allow all tests to run when there are leaks | |
908 | $results =~ s/Scalars leaked: \d+\n//g; | |
909 | ||
910 | $expected =~ s/\n+$//; | |
911 | my $prefix = ($results =~ s#^PREFIX(\n|$)##) ; | |
912 | # any special options? (OPTIONS foo bar zap) | |
913 | my $option_regex = 0; | |
914 | my $option_random = 0; | |
915 | if ($expected =~ s/^OPTIONS? (.+)\n//) { | |
916 | foreach my $option (split(' ', $1)) { | |
917 | if ($option eq 'regex') { # allow regular expressions | |
918 | $option_regex = 1; | |
919 | } | |
920 | elsif ($option eq 'random') { # all lines match, but in any order | |
921 | $option_random = 1; | |
922 | } | |
923 | else { | |
924 | die "$0: Unknown OPTION '$option'\n"; | |
925 | } | |
926 | } | |
927 | } | |
928 | die "$0: can't have OPTION regex and random\n" | |
929 | if $option_regex + $option_random > 1; | |
930 | my $ok = 0; | |
931 | if ($results =~ s/^SKIPPED\n//) { | |
932 | print "$results\n" ; | |
933 | $ok = 1; | |
934 | } | |
935 | elsif ($option_random) { | |
936 | my @got = sort split "\n", $results; | |
937 | my @expected = sort split "\n", $expected; | |
938 | ||
939 | $ok = "@got" eq "@expected"; | |
940 | } | |
941 | elsif ($option_regex) { | |
942 | $ok = $results =~ /^$expected/; | |
943 | } | |
944 | elsif ($prefix) { | |
945 | $ok = $results =~ /^\Q$expected/; | |
946 | } | |
947 | else { | |
948 | $ok = $results eq $expected; | |
949 | } | |
950 | ||
951 | local $::TODO = $reason{todo}; | |
952 | ||
953 | unless ($ok) { | |
954 | my $err_line = "PROG: $switch\n$prog\n" . | |
955 | "EXPECTED:\n$expected\n" . | |
956 | "GOT:\n$results\n"; | |
957 | if ($::TODO) { | |
958 | $err_line =~ s/^/# /mg; | |
959 | print $err_line; # Harness can't filter it out from STDERR. | |
960 | } | |
961 | else { | |
962 | print STDERR $err_line; | |
963 | } | |
964 | } | |
965 | ||
966 | ok($ok); | |
967 | ||
968 | foreach (@temps) { | |
969 | unlink $_ if $_; | |
970 | } | |
971 | foreach (@temp_path) { | |
972 | File::Path::rmtree $_ if -d $_; | |
973 | } | |
974 | } | |
975 | } | |
976 | ||
35a60386 RGS |
977 | sub can_ok ($@) { |
978 | my($proto, @methods) = @_; | |
979 | my $class = ref $proto || $proto; | |
980 | ||
981 | unless( @methods ) { | |
982 | return _ok( 0, _where(), "$class->can(...)" ); | |
983 | } | |
984 | ||
985 | my @nok = (); | |
986 | foreach my $method (@methods) { | |
987 | local($!, $@); # don't interfere with caller's $@ | |
988 | # eval sometimes resets $! | |
989 | eval { $proto->can($method) } || push @nok, $method; | |
990 | } | |
991 | ||
992 | my $name; | |
8210c8d3 | 993 | $name = @methods == 1 ? "$class->can('$methods[0]')" |
35a60386 | 994 | : "$class->can(...)"; |
8210c8d3 | 995 | |
35a60386 RGS |
996 | _ok( !@nok, _where(), $name ); |
997 | } | |
998 | ||
ad4e703e MS |
999 | |
1000 | # Call $class->new( @$args ); and run the result through isa_ok. | |
1001 | # See Test::More::new_ok | |
1002 | sub new_ok { | |
1003 | my($class, $args, $obj_name) = @_; | |
1004 | $args ||= []; | |
1005 | $object_name = "The object" unless defined $obj_name; | |
1006 | ||
1007 | local $Level = $Level + 1; | |
1008 | ||
1009 | my $obj; | |
1010 | my $ok = eval { $obj = $class->new(@$args); 1 }; | |
1011 | my $error = $@; | |
1012 | ||
1013 | if($ok) { | |
1014 | isa_ok($obj, $class, $object_name); | |
1015 | } | |
1016 | else { | |
1017 | ok( 0, "new() died" ); | |
1018 | diag("Error was: $@"); | |
1019 | } | |
1020 | ||
1021 | return $obj; | |
1022 | ||
1023 | } | |
1024 | ||
1025 | ||
35a60386 RGS |
1026 | sub isa_ok ($$;$) { |
1027 | my($object, $class, $obj_name) = @_; | |
1028 | ||
1029 | my $diag; | |
1030 | $obj_name = 'The object' unless defined $obj_name; | |
1031 | my $name = "$obj_name isa $class"; | |
1032 | if( !defined $object ) { | |
1033 | $diag = "$obj_name isn't defined"; | |
1034 | } | |
1035 | elsif( !ref $object ) { | |
1036 | $diag = "$obj_name isn't a reference"; | |
1037 | } | |
1038 | else { | |
1039 | # We can't use UNIVERSAL::isa because we want to honor isa() overrides | |
1040 | local($@, $!); # eval sometimes resets $! | |
1041 | my $rslt = eval { $object->isa($class) }; | |
1042 | if( $@ ) { | |
1043 | if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) { | |
1044 | if( !UNIVERSAL::isa($object, $class) ) { | |
1045 | my $ref = ref $object; | |
1046 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
1047 | } | |
1048 | } else { | |
1049 | die <<WHOA; | |
1050 | WHOA! I tried to call ->isa on your object and got some weird error. | |
1051 | This should never happen. Please contact the author immediately. | |
1052 | Here's the error. | |
1053 | $@ | |
1054 | WHOA | |
1055 | } | |
1056 | } | |
1057 | elsif( !$rslt ) { | |
1058 | my $ref = ref $object; | |
1059 | $diag = "$obj_name isn't a '$class' it's a '$ref'"; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | _ok( !$diag, _where(), $name ); | |
1064 | } | |
1065 | ||
087986a7 | 1066 | # Set a watchdog to timeout the entire test file |
5fe9b82b JH |
1067 | # NOTE: If the test file uses 'threads', then call the watchdog() function |
1068 | # _AFTER_ the 'threads' module is loaded. | |
5732108f | 1069 | sub watchdog ($;$) |
087986a7 JH |
1070 | { |
1071 | my $timeout = shift; | |
36436324 | 1072 | my $method = shift || ""; |
087986a7 JH |
1073 | my $timeout_msg = 'Test process timed out - terminating'; |
1074 | ||
e07ce2e4 GG |
1075 | # Valgrind slows perl way down so give it more time before dying. |
1076 | $timeout *= 10 if $ENV{PERL_VALGRIND}; | |
1077 | ||
087986a7 JH |
1078 | my $pid_to_kill = $$; # PID for this process |
1079 | ||
5732108f GG |
1080 | if ($method eq "alarm") { |
1081 | goto WATCHDOG_VIA_ALARM; | |
1082 | } | |
1083 | ||
140f5369 MS |
1084 | # shut up use only once warning |
1085 | my $threads_on = $threads::threads && $threads::threads; | |
1086 | ||
5fe9b82b JH |
1087 | # Don't use a watchdog process if 'threads' is loaded - |
1088 | # use a watchdog thread instead | |
140f5369 | 1089 | if (!$threads_on) { |
5fe9b82b JH |
1090 | |
1091 | # On Windows and VMS, try launching a watchdog process | |
1092 | # using system(1, ...) (see perlport.pod) | |
4b0f0df6 | 1093 | if ($is_mswin || $is_vms) { |
5fe9b82b | 1094 | # On Windows, try to get the 'real' PID |
4b0f0df6 | 1095 | if ($is_mswin) { |
5fe9b82b JH |
1096 | eval { require Win32; }; |
1097 | if (defined(&Win32::GetCurrentProcessId)) { | |
1098 | $pid_to_kill = Win32::GetCurrentProcessId(); | |
1099 | } | |
087986a7 | 1100 | } |
087986a7 | 1101 | |
5fe9b82b JH |
1102 | # If we still have a fake PID, we can't use this method at all |
1103 | return if ($pid_to_kill <= 0); | |
1104 | ||
1105 | # Launch watchdog process | |
1106 | my $watchdog; | |
1107 | eval { | |
1108 | local $SIG{'__WARN__'} = sub { | |
1109 | _diag("Watchdog warning: $_[0]"); | |
1110 | }; | |
4b0f0df6 | 1111 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
9b7a5066 CB |
1112 | my $cmd = _create_runperl( prog => "sleep($timeout);" . |
1113 | "warn qq/# $timeout_msg" . '\n/;' . | |
c1c45e36 | 1114 | "kill($sig, $pid_to_kill);"); |
9b7a5066 | 1115 | $watchdog = system(1, $cmd); |
5fe9b82b JH |
1116 | }; |
1117 | if ($@ || ($watchdog <= 0)) { | |
1118 | _diag('Failed to start watchdog'); | |
1119 | _diag($@) if $@; | |
1120 | undef($watchdog); | |
1121 | return; | |
1122 | } | |
087986a7 | 1123 | |
5fe9b82b JH |
1124 | # Add END block to parent to terminate and |
1125 | # clean up watchdog process | |
7e1027b9 JH |
1126 | eval "END { local \$! = 0; local \$? = 0; |
1127 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b | 1128 | return; |
087986a7 | 1129 | } |
087986a7 | 1130 | |
5fe9b82b JH |
1131 | # Try using fork() to generate a watchdog process |
1132 | my $watchdog; | |
1133 | eval { $watchdog = fork() }; | |
1134 | if (defined($watchdog)) { | |
1135 | if ($watchdog) { # Parent process | |
1136 | # Add END block to parent to terminate and | |
1137 | # clean up watchdog process | |
7e1027b9 JH |
1138 | eval "END { local \$! = 0; local \$? = 0; |
1139 | wait() if kill('KILL', $watchdog); };"; | |
5fe9b82b JH |
1140 | return; |
1141 | } | |
1142 | ||
1143 | ### Watchdog process code | |
087986a7 | 1144 | |
5fe9b82b JH |
1145 | # Load POSIX if available |
1146 | eval { require POSIX; }; | |
087986a7 | 1147 | |
5fe9b82b JH |
1148 | # Execute the timeout |
1149 | sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073 | |
1150 | sleep(2); | |
087986a7 | 1151 | |
5fe9b82b JH |
1152 | # Kill test process if still running |
1153 | if (kill(0, $pid_to_kill)) { | |
1154 | _diag($timeout_msg); | |
1155 | kill('KILL', $pid_to_kill); | |
1156 | } | |
087986a7 | 1157 | |
5fe9b82b JH |
1158 | # Don't execute END block (added at beginning of this file) |
1159 | $NO_ENDING = 1; | |
087986a7 | 1160 | |
5fe9b82b JH |
1161 | # Terminate ourself (i.e., the watchdog) |
1162 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
1163 | exit(1); | |
087986a7 JH |
1164 | } |
1165 | ||
5fe9b82b | 1166 | # fork() failed - fall through and try using a thread |
087986a7 JH |
1167 | } |
1168 | ||
5fe9b82b JH |
1169 | # Use a watchdog thread because either 'threads' is loaded, |
1170 | # or fork() failed | |
afe79e7b | 1171 | if (eval 'require threads; 1') { |
b296285b | 1172 | 'threads'->create(sub { |
087986a7 JH |
1173 | # Load POSIX if available |
1174 | eval { require POSIX; }; | |
1175 | ||
1176 | # Execute the timeout | |
c1c45e36 | 1177 | my $time_left = $timeout; |
a6c9a815 | 1178 | do { |
11ea18f2 | 1179 | $time_left = $time_left - sleep($time_left); |
a6c9a815 | 1180 | } while ($time_left > 0); |
087986a7 JH |
1181 | |
1182 | # Kill the parent (and ourself) | |
5fe9b82b | 1183 | select(STDERR); $| = 1; |
087986a7 JH |
1184 | _diag($timeout_msg); |
1185 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
4b0f0df6 | 1186 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
c1c45e36 | 1187 | kill($sig, $pid_to_kill); |
087986a7 JH |
1188 | })->detach(); |
1189 | return; | |
1190 | } | |
1191 | ||
5fe9b82b | 1192 | # If everything above fails, then just use an alarm timeout |
5732108f | 1193 | WATCHDOG_VIA_ALARM: |
087986a7 JH |
1194 | if (eval { alarm($timeout); 1; }) { |
1195 | # Load POSIX if available | |
1196 | eval { require POSIX; }; | |
1197 | ||
1198 | # Alarm handler will do the actual 'killing' | |
1199 | $SIG{'ALRM'} = sub { | |
5fe9b82b | 1200 | select(STDERR); $| = 1; |
087986a7 JH |
1201 | _diag($timeout_msg); |
1202 | POSIX::_exit(1) if (defined(&POSIX::_exit)); | |
4b0f0df6 | 1203 | my $sig = $is_vms ? 'TERM' : 'KILL'; |
c1c45e36 | 1204 | kill($sig, $pid_to_kill); |
087986a7 JH |
1205 | }; |
1206 | } | |
1207 | } | |
1208 | ||
f69d9fdf KW |
1209 | my $cp_0037 = # EBCDIC code page 0037 |
1210 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' . | |
1211 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1212 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1213 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1214 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1215 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' . | |
1216 | '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1217 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' . | |
1218 | '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1219 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' . | |
1220 | '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' . | |
1221 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1222 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1223 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' . | |
1224 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1225 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1226 | ||
1227 | my $cp_1047 = # EBCDIC code page 1047 | |
1228 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' . | |
1229 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1230 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1231 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1232 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1233 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' . | |
1234 | '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1235 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' . | |
1236 | '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1237 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' . | |
1238 | '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' . | |
1239 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1240 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1241 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' . | |
1242 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1243 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1244 | ||
1245 | my $cp_bc = # EBCDIC code page POSiX-BC | |
1246 | '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' . | |
1247 | '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . | |
1248 | '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . | |
1249 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . | |
1250 | '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . | |
1251 | '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' . | |
1252 | '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . | |
1253 | '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' . | |
1254 | '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . | |
1255 | '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' . | |
1256 | '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' . | |
1257 | '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . | |
1258 | '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . | |
1259 | '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' . | |
1260 | '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . | |
1261 | '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF'; | |
1262 | ||
1263 | my $straight = # Avoid ranges | |
1264 | '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' . | |
1265 | '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' . | |
1266 | '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' . | |
1267 | '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' . | |
1268 | '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' . | |
1269 | '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' . | |
1270 | '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' . | |
1271 | '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' . | |
1272 | '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' . | |
1273 | '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' . | |
1274 | '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' . | |
1275 | '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' . | |
1276 | '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' . | |
1277 | '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' . | |
1278 | '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' . | |
1279 | '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF'; | |
1280 | ||
1281 | # The following 2 functions allow tests to work on both EBCDIC and | |
1282 | # ASCII-ish platforms. They convert string scalars between the native | |
1283 | # character set and the set of 256 characters which is usually called | |
1284 | # Latin1. | |
1285 | # | |
1286 | # These routines don't work on UTF-EBCDIC and UTF-8. | |
1287 | ||
1288 | sub native_to_latin1($) { | |
1289 | my $string = shift; | |
1290 | ||
1291 | return $string if ord('^') == 94; # ASCII, Latin1 | |
1292 | my $cp; | |
1293 | if (ord('^') == 95) { # EBCDIC 1047 | |
1294 | $cp = \$cp_1047; | |
1295 | } | |
1296 | elsif (ord('^') == 106) { # EBCDIC POSIX-BC | |
1297 | $cp = \$cp_bc; | |
1298 | } | |
1299 | elsif (ord('^') == 176) { # EBCDIC 037 */ | |
1300 | $cp = \$cp_0037; | |
1301 | } | |
1302 | else { | |
1303 | die "Unknown native character set"; | |
1304 | } | |
1305 | ||
1306 | eval '$string =~ tr/' . $$cp . '/' . $straight . '/'; | |
1307 | return $string; | |
1308 | } | |
1309 | ||
1310 | sub latin1_to_native($) { | |
1311 | my $string = shift; | |
1312 | ||
1313 | return $string if ord('^') == 94; # ASCII, Latin1 | |
1314 | my $cp; | |
1315 | if (ord('^') == 95) { # EBCDIC 1047 | |
1316 | $cp = \$cp_1047; | |
1317 | } | |
1318 | elsif (ord('^') == 106) { # EBCDIC POSIX-BC | |
1319 | $cp = \$cp_bc; | |
1320 | } | |
1321 | elsif (ord('^') == 176) { # EBCDIC 037 */ | |
1322 | $cp = \$cp_0037; | |
1323 | } | |
1324 | else { | |
1325 | die "Unknown native character set"; | |
1326 | } | |
1327 | ||
1328 | eval '$string =~ tr/' . $straight . '/' . $$cp . '/'; | |
1329 | return $string; | |
1330 | } | |
1331 | ||
883cce4a | 1332 | sub ord_latin1_to_native { |
ec0363d9 KW |
1333 | # given an input code point, return the platform's native |
1334 | # equivalent value. Anything above latin1 is itself. | |
883cce4a | 1335 | |
ec0363d9 KW |
1336 | my $ord = shift; |
1337 | return $ord if $ord > 255; | |
1338 | return ord latin1_to_native(chr $ord); | |
883cce4a KW |
1339 | } |
1340 | ||
1341 | sub ord_native_to_latin1 { | |
ec0363d9 KW |
1342 | # given an input platform code point, return the latin1 equivalent value. |
1343 | # Anything above latin1 is itself. | |
883cce4a | 1344 | |
ec0363d9 KW |
1345 | my $ord = shift; |
1346 | return $ord if $ord > 255; | |
1347 | return ord native_to_latin1(chr $ord); | |
883cce4a KW |
1348 | } |
1349 | ||
69026470 | 1350 | 1; |