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