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