1*0Sstevel@tonic-gate# 2*0Sstevel@tonic-gate# t/test.pl - most of Test::More functionality without the fuss 3*0Sstevel@tonic-gate# 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate$Level = 1; 6*0Sstevel@tonic-gatemy $test = 1; 7*0Sstevel@tonic-gatemy $planned; 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate$TODO = 0; 10*0Sstevel@tonic-gate$NO_ENDING = 0; 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gatesub plan { 13*0Sstevel@tonic-gate my $n; 14*0Sstevel@tonic-gate if (@_ == 1) { 15*0Sstevel@tonic-gate $n = shift; 16*0Sstevel@tonic-gate } else { 17*0Sstevel@tonic-gate my %plan = @_; 18*0Sstevel@tonic-gate $n = $plan{tests}; 19*0Sstevel@tonic-gate } 20*0Sstevel@tonic-gate print STDOUT "1..$n\n"; 21*0Sstevel@tonic-gate $planned = $n; 22*0Sstevel@tonic-gate} 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gateEND { 25*0Sstevel@tonic-gate my $ran = $test - 1; 26*0Sstevel@tonic-gate if (!$NO_ENDING && defined $planned && $planned != $ran) { 27*0Sstevel@tonic-gate print STDERR "# Looks like you planned $planned tests but ran $ran.\n"; 28*0Sstevel@tonic-gate } 29*0Sstevel@tonic-gate} 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate# Use this instead of "print STDERR" when outputing failure diagnostic 32*0Sstevel@tonic-gate# messages 33*0Sstevel@tonic-gatesub _diag { 34*0Sstevel@tonic-gate return unless @_; 35*0Sstevel@tonic-gate my @mess = map { /^#/ ? "$_\n" : "# $_\n" } 36*0Sstevel@tonic-gate map { split /\n/ } @_; 37*0Sstevel@tonic-gate my $fh = $TODO ? *STDOUT : *STDERR; 38*0Sstevel@tonic-gate print $fh @mess; 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate} 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gatesub skip_all { 43*0Sstevel@tonic-gate if (@_) { 44*0Sstevel@tonic-gate print STDOUT "1..0 # Skipped: @_\n"; 45*0Sstevel@tonic-gate } else { 46*0Sstevel@tonic-gate print STDOUT "1..0\n"; 47*0Sstevel@tonic-gate } 48*0Sstevel@tonic-gate exit(0); 49*0Sstevel@tonic-gate} 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gatesub _ok { 52*0Sstevel@tonic-gate my ($pass, $where, $name, @mess) = @_; 53*0Sstevel@tonic-gate # Do not try to microoptimize by factoring out the "not ". 54*0Sstevel@tonic-gate # VMS will avenge. 55*0Sstevel@tonic-gate my $out; 56*0Sstevel@tonic-gate if ($name) { 57*0Sstevel@tonic-gate # escape out '#' or it will interfere with '# skip' and such 58*0Sstevel@tonic-gate $name =~ s/#/\\#/g; 59*0Sstevel@tonic-gate $out = $pass ? "ok $test - $name" : "not ok $test - $name"; 60*0Sstevel@tonic-gate } else { 61*0Sstevel@tonic-gate $out = $pass ? "ok $test" : "not ok $test"; 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate $out .= " # TODO $TODO" if $TODO; 65*0Sstevel@tonic-gate print STDOUT "$out\n"; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate unless ($pass) { 68*0Sstevel@tonic-gate _diag "# Failed $where\n"; 69*0Sstevel@tonic-gate } 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate # Ensure that the message is properly escaped. 72*0Sstevel@tonic-gate _diag @mess; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate $test++; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate return $pass; 77*0Sstevel@tonic-gate} 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gatesub _where { 80*0Sstevel@tonic-gate my @caller = caller($Level); 81*0Sstevel@tonic-gate return "at $caller[1] line $caller[2]"; 82*0Sstevel@tonic-gate} 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate# DON'T use this for matches. Use like() instead. 85*0Sstevel@tonic-gatesub ok ($@) { 86*0Sstevel@tonic-gate my ($pass, $name, @mess) = @_; 87*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 88*0Sstevel@tonic-gate} 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gatesub _q { 91*0Sstevel@tonic-gate my $x = shift; 92*0Sstevel@tonic-gate return 'undef' unless defined $x; 93*0Sstevel@tonic-gate my $q = $x; 94*0Sstevel@tonic-gate $q =~ s/\\/\\\\/; 95*0Sstevel@tonic-gate $q =~ s/'/\\'/; 96*0Sstevel@tonic-gate return "'$q'"; 97*0Sstevel@tonic-gate} 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gatesub _qq { 100*0Sstevel@tonic-gate my $x = shift; 101*0Sstevel@tonic-gate return defined $x ? '"' . display ($x) . '"' : 'undef'; 102*0Sstevel@tonic-gate}; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate# keys are the codes \n etc map to, values are 2 char strings such as \n 105*0Sstevel@tonic-gatemy %backslash_escape; 106*0Sstevel@tonic-gateforeach my $x (split //, 'nrtfa\\\'"') { 107*0Sstevel@tonic-gate $backslash_escape{ord eval "\"\\$x\""} = "\\$x"; 108*0Sstevel@tonic-gate} 109*0Sstevel@tonic-gate# A way to display scalars containing control characters and Unicode. 110*0Sstevel@tonic-gate# Trying to avoid setting $_, or relying on local $_ to work. 111*0Sstevel@tonic-gatesub display { 112*0Sstevel@tonic-gate my @result; 113*0Sstevel@tonic-gate foreach my $x (@_) { 114*0Sstevel@tonic-gate if (defined $x and not ref $x) { 115*0Sstevel@tonic-gate my $y = ''; 116*0Sstevel@tonic-gate foreach my $c (unpack("U*", $x)) { 117*0Sstevel@tonic-gate if ($c > 255) { 118*0Sstevel@tonic-gate $y .= sprintf "\\x{%x}", $c; 119*0Sstevel@tonic-gate } elsif ($backslash_escape{$c}) { 120*0Sstevel@tonic-gate $y .= $backslash_escape{$c}; 121*0Sstevel@tonic-gate } else { 122*0Sstevel@tonic-gate my $z = chr $c; # Maybe we can get away with a literal... 123*0Sstevel@tonic-gate $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/; 124*0Sstevel@tonic-gate $y .= $z; 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate $x = $y; 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate return $x unless wantarray; 130*0Sstevel@tonic-gate push @result, $x; 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate return @result; 133*0Sstevel@tonic-gate} 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gatesub is ($$@) { 136*0Sstevel@tonic-gate my ($got, $expected, $name, @mess) = @_; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate my $pass; 139*0Sstevel@tonic-gate if( !defined $got || !defined $expected ) { 140*0Sstevel@tonic-gate # undef only matches undef 141*0Sstevel@tonic-gate $pass = !defined $got && !defined $expected; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate else { 144*0Sstevel@tonic-gate $pass = $got eq $expected; 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate unless ($pass) { 148*0Sstevel@tonic-gate unshift(@mess, "# got "._q($got)."\n", 149*0Sstevel@tonic-gate "# expected "._q($expected)."\n"); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 152*0Sstevel@tonic-gate} 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gatesub isnt ($$@) { 155*0Sstevel@tonic-gate my ($got, $isnt, $name, @mess) = @_; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate my $pass; 158*0Sstevel@tonic-gate if( !defined $got || !defined $isnt ) { 159*0Sstevel@tonic-gate # undef only matches undef 160*0Sstevel@tonic-gate $pass = defined $got || defined $isnt; 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate else { 163*0Sstevel@tonic-gate $pass = $got ne $isnt; 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate unless( $pass ) { 167*0Sstevel@tonic-gate unshift(@mess, "# it should not be "._q($got)."\n", 168*0Sstevel@tonic-gate "# but it is.\n"); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 171*0Sstevel@tonic-gate} 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gatesub cmp_ok ($$$@) { 174*0Sstevel@tonic-gate my($got, $type, $expected, $name, @mess) = @_; 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate my $pass; 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate local $^W = 0; 179*0Sstevel@tonic-gate local($@,$!); # don't interfere with $@ 180*0Sstevel@tonic-gate # eval() sometimes resets $! 181*0Sstevel@tonic-gate $pass = eval "\$got $type \$expected"; 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate unless ($pass) { 184*0Sstevel@tonic-gate # It seems Irix long doubles can have 2147483648 and 2147483648 185*0Sstevel@tonic-gate # that stringify to the same thing but are acutally numerically 186*0Sstevel@tonic-gate # different. Display the numbers if $type isn't a string operator, 187*0Sstevel@tonic-gate # and the numbers are stringwise the same. 188*0Sstevel@tonic-gate # (all string operators have alphabetic names, so tr/a-z// is true) 189*0Sstevel@tonic-gate # This will also show numbers for some uneeded cases, but will 190*0Sstevel@tonic-gate # definately be helpful for things such as == and <= that fail 191*0Sstevel@tonic-gate if ($got eq $expected and $type !~ tr/a-z//) { 192*0Sstevel@tonic-gate unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate unshift(@mess, "# got "._q($got)."\n", 195*0Sstevel@tonic-gate "# expected $type "._q($expected)."\n"); 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 198*0Sstevel@tonic-gate} 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate# Check that $got is within $range of $expected 201*0Sstevel@tonic-gate# if $range is 0, then check it's exact 202*0Sstevel@tonic-gate# else if $expected is 0, then $range is an absolute value 203*0Sstevel@tonic-gate# otherwise $range is a fractional error. 204*0Sstevel@tonic-gate# Here $range must be numeric, >= 0 205*0Sstevel@tonic-gate# Non numeric ranges might be a useful future extension. (eg %) 206*0Sstevel@tonic-gatesub within ($$$@) { 207*0Sstevel@tonic-gate my ($got, $expected, $range, $name, @mess) = @_; 208*0Sstevel@tonic-gate my $pass; 209*0Sstevel@tonic-gate if (!defined $got or !defined $expected or !defined $range) { 210*0Sstevel@tonic-gate # This is a fail, but doesn't need extra diagnostics 211*0Sstevel@tonic-gate } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) { 212*0Sstevel@tonic-gate # This is a fail 213*0Sstevel@tonic-gate unshift @mess, "# got, expected and range must be numeric\n"; 214*0Sstevel@tonic-gate } elsif ($range < 0) { 215*0Sstevel@tonic-gate # This is also a fail 216*0Sstevel@tonic-gate unshift @mess, "# range must not be negative\n"; 217*0Sstevel@tonic-gate } elsif ($range == 0) { 218*0Sstevel@tonic-gate # Within 0 is == 219*0Sstevel@tonic-gate $pass = $got == $expected; 220*0Sstevel@tonic-gate } elsif ($expected == 0) { 221*0Sstevel@tonic-gate # If expected is 0, treat range as absolute 222*0Sstevel@tonic-gate $pass = ($got <= $range) && ($got >= - $range); 223*0Sstevel@tonic-gate } else { 224*0Sstevel@tonic-gate my $diff = $got - $expected; 225*0Sstevel@tonic-gate $pass = abs ($diff / $expected) < $range; 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate unless ($pass) { 228*0Sstevel@tonic-gate if ($got eq $expected) { 229*0Sstevel@tonic-gate unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n"; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate unshift@mess, "# got "._q($got)."\n", 232*0Sstevel@tonic-gate "# expected "._q($expected)." (within "._q($range).")\n"; 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 235*0Sstevel@tonic-gate} 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate# Note: this isn't quite as fancy as Test::More::like(). 238*0Sstevel@tonic-gatesub like ($$@) { 239*0Sstevel@tonic-gate my ($got, $expected, $name, @mess) = @_; 240*0Sstevel@tonic-gate my $pass; 241*0Sstevel@tonic-gate if (ref $expected eq 'Regexp') { 242*0Sstevel@tonic-gate $pass = $got =~ $expected; 243*0Sstevel@tonic-gate unless ($pass) { 244*0Sstevel@tonic-gate unshift(@mess, "# got '$got'\n", 245*0Sstevel@tonic-gate "# expected /$expected/\n"); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate } else { 248*0Sstevel@tonic-gate $pass = $got =~ /$expected/; 249*0Sstevel@tonic-gate unless ($pass) { 250*0Sstevel@tonic-gate unshift(@mess, "# got '$got'\n", 251*0Sstevel@tonic-gate "# expected /$expected/\n"); 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate _ok($pass, _where(), $name, @mess); 255*0Sstevel@tonic-gate} 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gatesub pass { 258*0Sstevel@tonic-gate _ok(1, '', @_); 259*0Sstevel@tonic-gate} 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gatesub fail { 262*0Sstevel@tonic-gate _ok(0, _where(), @_); 263*0Sstevel@tonic-gate} 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gatesub curr_test { 266*0Sstevel@tonic-gate $test = shift if @_; 267*0Sstevel@tonic-gate return $test; 268*0Sstevel@tonic-gate} 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gatesub next_test { 271*0Sstevel@tonic-gate $test++; 272*0Sstevel@tonic-gate} 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate# Note: can't pass multipart messages since we try to 275*0Sstevel@tonic-gate# be compatible with Test::More::skip(). 276*0Sstevel@tonic-gatesub skip { 277*0Sstevel@tonic-gate my $why = shift; 278*0Sstevel@tonic-gate my $n = @_ ? shift : 1; 279*0Sstevel@tonic-gate for (1..$n) { 280*0Sstevel@tonic-gate print STDOUT "ok $test # skip: $why\n"; 281*0Sstevel@tonic-gate $test++; 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate local $^W = 0; 284*0Sstevel@tonic-gate last SKIP; 285*0Sstevel@tonic-gate} 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gatesub eq_array { 288*0Sstevel@tonic-gate my ($ra, $rb) = @_; 289*0Sstevel@tonic-gate return 0 unless $#$ra == $#$rb; 290*0Sstevel@tonic-gate for my $i (0..$#$ra) { 291*0Sstevel@tonic-gate return 0 unless $ra->[$i] eq $rb->[$i]; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate return 1; 294*0Sstevel@tonic-gate} 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gatesub eq_hash { 297*0Sstevel@tonic-gate my ($orig, $suspect) = @_; 298*0Sstevel@tonic-gate my $fail; 299*0Sstevel@tonic-gate while (my ($key, $value) = each %$suspect) { 300*0Sstevel@tonic-gate # Force a hash recompute if this perl's internals can cache the hash key. 301*0Sstevel@tonic-gate $key = "" . $key; 302*0Sstevel@tonic-gate if (exists $orig->{$key}) { 303*0Sstevel@tonic-gate if ($orig->{$key} ne $value) { 304*0Sstevel@tonic-gate print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}), 305*0Sstevel@tonic-gate " now ", _qq($value), "\n"; 306*0Sstevel@tonic-gate $fail = 1; 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate } else { 309*0Sstevel@tonic-gate print STDOUT "# key ", _qq($key), " is ", _qq($value), 310*0Sstevel@tonic-gate ", not in original.\n"; 311*0Sstevel@tonic-gate $fail = 1; 312*0Sstevel@tonic-gate } 313*0Sstevel@tonic-gate } 314*0Sstevel@tonic-gate foreach (keys %$orig) { 315*0Sstevel@tonic-gate # Force a hash recompute if this perl's internals can cache the hash key. 316*0Sstevel@tonic-gate $_ = "" . $_; 317*0Sstevel@tonic-gate next if (exists $suspect->{$_}); 318*0Sstevel@tonic-gate print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; 319*0Sstevel@tonic-gate $fail = 1; 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate !$fail; 322*0Sstevel@tonic-gate} 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gatesub require_ok ($) { 325*0Sstevel@tonic-gate my ($require) = @_; 326*0Sstevel@tonic-gate eval <<REQUIRE_OK; 327*0Sstevel@tonic-gaterequire $require; 328*0Sstevel@tonic-gateREQUIRE_OK 329*0Sstevel@tonic-gate _ok(!$@, _where(), "require $require"); 330*0Sstevel@tonic-gate} 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gatesub use_ok ($) { 333*0Sstevel@tonic-gate my ($use) = @_; 334*0Sstevel@tonic-gate eval <<USE_OK; 335*0Sstevel@tonic-gateuse $use; 336*0Sstevel@tonic-gateUSE_OK 337*0Sstevel@tonic-gate _ok(!$@, _where(), "use $use"); 338*0Sstevel@tonic-gate} 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate# runperl - Runs a separate perl interpreter. 341*0Sstevel@tonic-gate# Arguments : 342*0Sstevel@tonic-gate# switches => [ command-line switches ] 343*0Sstevel@tonic-gate# nolib => 1 # don't use -I../lib (included by default) 344*0Sstevel@tonic-gate# prog => one-liner (avoid quotes) 345*0Sstevel@tonic-gate# progs => [ multi-liner (avoid quotes) ] 346*0Sstevel@tonic-gate# progfile => perl script 347*0Sstevel@tonic-gate# stdin => string to feed the stdin 348*0Sstevel@tonic-gate# stderr => redirect stderr to stdout 349*0Sstevel@tonic-gate# args => [ command-line arguments to the perl program ] 350*0Sstevel@tonic-gate# verbose => print the command line 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gatemy $is_mswin = $^O eq 'MSWin32'; 353*0Sstevel@tonic-gatemy $is_netware = $^O eq 'NetWare'; 354*0Sstevel@tonic-gatemy $is_macos = $^O eq 'MacOS'; 355*0Sstevel@tonic-gatemy $is_vms = $^O eq 'VMS'; 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gatesub _quote_args { 358*0Sstevel@tonic-gate my ($runperl, $args) = @_; 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate foreach (@$args) { 361*0Sstevel@tonic-gate # In VMS protect with doublequotes because otherwise 362*0Sstevel@tonic-gate # DCL will lowercase -- unless already doublequoted. 363*0Sstevel@tonic-gate $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; 364*0Sstevel@tonic-gate $$runperl .= ' ' . $_; 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate} 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gatesub _create_runperl { # Create the string to qx in runperl(). 369*0Sstevel@tonic-gate my %args = @_; 370*0Sstevel@tonic-gate my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X; 371*0Sstevel@tonic-gate unless ($args{nolib}) { 372*0Sstevel@tonic-gate if ($is_macos) { 373*0Sstevel@tonic-gate $runperl .= ' -I::lib'; 374*0Sstevel@tonic-gate # Use UNIX style error messages instead of MPW style. 375*0Sstevel@tonic-gate $runperl .= ' -MMac::err=unix' if $args{stderr}; 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate else { 378*0Sstevel@tonic-gate $runperl .= ' "-I../lib"'; # doublequotes because of VMS 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate } 381*0Sstevel@tonic-gate if ($args{switches}) { 382*0Sstevel@tonic-gate local $Level = 2; 383*0Sstevel@tonic-gate die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where() 384*0Sstevel@tonic-gate unless ref $args{switches} eq "ARRAY"; 385*0Sstevel@tonic-gate _quote_args(\$runperl, $args{switches}); 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate if (defined $args{prog}) { 388*0Sstevel@tonic-gate die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where() 389*0Sstevel@tonic-gate if defined $args{progs}; 390*0Sstevel@tonic-gate $args{progs} = [$args{prog}] 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate if (defined $args{progs}) { 393*0Sstevel@tonic-gate die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where() 394*0Sstevel@tonic-gate unless ref $args{progs} eq "ARRAY"; 395*0Sstevel@tonic-gate foreach my $prog (@{$args{progs}}) { 396*0Sstevel@tonic-gate if ($is_mswin || $is_netware || $is_vms) { 397*0Sstevel@tonic-gate $runperl .= qq ( -e "$prog" ); 398*0Sstevel@tonic-gate } 399*0Sstevel@tonic-gate else { 400*0Sstevel@tonic-gate $runperl .= qq ( -e '$prog' ); 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate } elsif (defined $args{progfile}) { 404*0Sstevel@tonic-gate $runperl .= qq( "$args{progfile}"); 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate if (defined $args{stdin}) { 407*0Sstevel@tonic-gate # so we don't try to put literal newlines and crs onto the 408*0Sstevel@tonic-gate # command line. 409*0Sstevel@tonic-gate $args{stdin} =~ s/\n/\\n/g; 410*0Sstevel@tonic-gate $args{stdin} =~ s/\r/\\r/g; 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate if ($is_mswin || $is_netware || $is_vms) { 413*0Sstevel@tonic-gate $runperl = qq{$^X -e "print qq(} . 414*0Sstevel@tonic-gate $args{stdin} . q{)" | } . $runperl; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate elsif ($is_macos) { 417*0Sstevel@tonic-gate # MacOS can only do two processes under MPW at once; 418*0Sstevel@tonic-gate # the test itself is one; we can't do two more, so 419*0Sstevel@tonic-gate # write to temp file 420*0Sstevel@tonic-gate my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; }; 421*0Sstevel@tonic-gate if ($args{verbose}) { 422*0Sstevel@tonic-gate my $stdindisplay = $stdin; 423*0Sstevel@tonic-gate $stdindisplay =~ s/\n/\n\#/g; 424*0Sstevel@tonic-gate print STDERR "# $stdindisplay\n"; 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate `$stdin`; 427*0Sstevel@tonic-gate $runperl .= q{ < teststdin }; 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate else { 430*0Sstevel@tonic-gate $runperl = qq{$^X -e 'print qq(} . 431*0Sstevel@tonic-gate $args{stdin} . q{)' | } . $runperl; 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate } 434*0Sstevel@tonic-gate if (defined $args{args}) { 435*0Sstevel@tonic-gate _quote_args(\$runperl, $args{args}); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate $runperl .= ' 2>&1' if $args{stderr} && !$is_macos; 438*0Sstevel@tonic-gate $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos; 439*0Sstevel@tonic-gate if ($args{verbose}) { 440*0Sstevel@tonic-gate my $runperldisplay = $runperl; 441*0Sstevel@tonic-gate $runperldisplay =~ s/\n/\n\#/g; 442*0Sstevel@tonic-gate print STDERR "# $runperldisplay\n"; 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate return $runperl; 445*0Sstevel@tonic-gate} 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gatesub runperl { 448*0Sstevel@tonic-gate my $runperl = &_create_runperl; 449*0Sstevel@tonic-gate my $result = `$runperl`; 450*0Sstevel@tonic-gate $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these 451*0Sstevel@tonic-gate return $result; 452*0Sstevel@tonic-gate} 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate*run_perl = \&runperl; # Nice alias. 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gatesub DIE { 457*0Sstevel@tonic-gate print STDERR "# @_\n"; 458*0Sstevel@tonic-gate exit 1; 459*0Sstevel@tonic-gate} 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate# A somewhat safer version of the sometimes wrong $^X. 462*0Sstevel@tonic-gatemy $Perl; 463*0Sstevel@tonic-gatesub which_perl { 464*0Sstevel@tonic-gate unless (defined $Perl) { 465*0Sstevel@tonic-gate $Perl = $^X; 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate # VMS should have 'perl' aliased properly 468*0Sstevel@tonic-gate return $Perl if $^O eq 'VMS'; 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gate my $exe; 471*0Sstevel@tonic-gate eval "require Config; Config->import"; 472*0Sstevel@tonic-gate if ($@) { 473*0Sstevel@tonic-gate warn "test.pl had problems loading Config: $@"; 474*0Sstevel@tonic-gate $exe = ''; 475*0Sstevel@tonic-gate } else { 476*0Sstevel@tonic-gate $exe = $Config{_exe}; 477*0Sstevel@tonic-gate } 478*0Sstevel@tonic-gate $exe = '' unless defined $exe; 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate # This doesn't absolutize the path: beware of future chdirs(). 481*0Sstevel@tonic-gate # We could do File::Spec->abs2rel() but that does getcwd()s, 482*0Sstevel@tonic-gate # which is a bit heavyweight to do here. 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate if ($Perl =~ /^perl\Q$exe\E$/i) { 485*0Sstevel@tonic-gate my $perl = "perl$exe"; 486*0Sstevel@tonic-gate eval "require File::Spec"; 487*0Sstevel@tonic-gate if ($@) { 488*0Sstevel@tonic-gate warn "test.pl had problems loading File::Spec: $@"; 489*0Sstevel@tonic-gate $Perl = "./$perl"; 490*0Sstevel@tonic-gate } else { 491*0Sstevel@tonic-gate $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); 492*0Sstevel@tonic-gate } 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate # Build up the name of the executable file from the name of 496*0Sstevel@tonic-gate # the command. 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate if ($Perl !~ /\Q$exe\E$/i) { 499*0Sstevel@tonic-gate $Perl .= $exe; 500*0Sstevel@tonic-gate } 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gate warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate # For subcommands to use. 505*0Sstevel@tonic-gate $ENV{PERLEXE} = $Perl; 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate return $Perl; 508*0Sstevel@tonic-gate} 509*0Sstevel@tonic-gate 510*0Sstevel@tonic-gatesub unlink_all { 511*0Sstevel@tonic-gate foreach my $file (@_) { 512*0Sstevel@tonic-gate 1 while unlink $file; 513*0Sstevel@tonic-gate print STDERR "# Couldn't unlink '$file': $!\n" if -f $file; 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate} 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gatemy $tmpfile = "misctmp000"; 519*0Sstevel@tonic-gate1 while -f ++$tmpfile; 520*0Sstevel@tonic-gateEND { unlink_all $tmpfile } 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate# 523*0Sstevel@tonic-gate# _fresh_perl 524*0Sstevel@tonic-gate# 525*0Sstevel@tonic-gate# The $resolve must be a subref that tests the first argument 526*0Sstevel@tonic-gate# for success, or returns the definition of success (e.g. the 527*0Sstevel@tonic-gate# expected scalar) if given no arguments. 528*0Sstevel@tonic-gate# 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gatesub _fresh_perl { 531*0Sstevel@tonic-gate my($prog, $resolve, $runperl_args, $name) = @_; 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gate $runperl_args ||= {}; 534*0Sstevel@tonic-gate $runperl_args->{progfile} = $tmpfile; 535*0Sstevel@tonic-gate $runperl_args->{stderr} = 1; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate # VMS adjustments 540*0Sstevel@tonic-gate if( $^O eq 'VMS' ) { 541*0Sstevel@tonic-gate $prog =~ s#/dev/null#NL:#; 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate # VMS file locking 544*0Sstevel@tonic-gate $prog =~ s{if \(-e _ and -f _ and -r _\)} 545*0Sstevel@tonic-gate {if (-e _ and -f _)} 546*0Sstevel@tonic-gate } 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate print TEST $prog, "\n"; 549*0Sstevel@tonic-gate close TEST or die "Cannot close $tmpfile: $!"; 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate my $results = runperl(%$runperl_args); 552*0Sstevel@tonic-gate my $status = $?; 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate # Clean up the results into something a bit more predictable. 555*0Sstevel@tonic-gate $results =~ s/\n+$//; 556*0Sstevel@tonic-gate $results =~ s/at\s+misctmp\d+\s+line/at - line/g; 557*0Sstevel@tonic-gate $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g; 558*0Sstevel@tonic-gate 559*0Sstevel@tonic-gate # bison says 'parse error' instead of 'syntax error', 560*0Sstevel@tonic-gate # various yaccs may or may not capitalize 'syntax'. 561*0Sstevel@tonic-gate $results =~ s/^(syntax|parse) error/syntax error/mig; 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate if ($^O eq 'VMS') { 564*0Sstevel@tonic-gate # some tests will trigger VMS messages that won't be expected 565*0Sstevel@tonic-gate $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; 566*0Sstevel@tonic-gate 567*0Sstevel@tonic-gate # pipes double these sometimes 568*0Sstevel@tonic-gate $results =~ s/\n\n/\n/g; 569*0Sstevel@tonic-gate } 570*0Sstevel@tonic-gate 571*0Sstevel@tonic-gate my $pass = $resolve->($results); 572*0Sstevel@tonic-gate unless ($pass) { 573*0Sstevel@tonic-gate _diag "# PROG: \n$prog\n"; 574*0Sstevel@tonic-gate _diag "# EXPECTED:\n", $resolve->(), "\n"; 575*0Sstevel@tonic-gate _diag "# GOT:\n$results\n"; 576*0Sstevel@tonic-gate _diag "# STATUS: $status\n"; 577*0Sstevel@tonic-gate } 578*0Sstevel@tonic-gate 579*0Sstevel@tonic-gate # Use the first line of the program as a name if none was given 580*0Sstevel@tonic-gate unless( $name ) { 581*0Sstevel@tonic-gate ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; 582*0Sstevel@tonic-gate $name .= '...' if length $first_line > length $name; 583*0Sstevel@tonic-gate } 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate _ok($pass, _where(), "fresh_perl - $name"); 586*0Sstevel@tonic-gate} 587*0Sstevel@tonic-gate 588*0Sstevel@tonic-gate# 589*0Sstevel@tonic-gate# fresh_perl_is 590*0Sstevel@tonic-gate# 591*0Sstevel@tonic-gate# Combination of run_perl() and is(). 592*0Sstevel@tonic-gate# 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gatesub fresh_perl_is { 595*0Sstevel@tonic-gate my($prog, $expected, $runperl_args, $name) = @_; 596*0Sstevel@tonic-gate local $Level = 2; 597*0Sstevel@tonic-gate _fresh_perl($prog, 598*0Sstevel@tonic-gate sub { @_ ? $_[0] eq $expected : $expected }, 599*0Sstevel@tonic-gate $runperl_args, $name); 600*0Sstevel@tonic-gate} 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate# 603*0Sstevel@tonic-gate# fresh_perl_like 604*0Sstevel@tonic-gate# 605*0Sstevel@tonic-gate# Combination of run_perl() and like(). 606*0Sstevel@tonic-gate# 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gatesub fresh_perl_like { 609*0Sstevel@tonic-gate my($prog, $expected, $runperl_args, $name) = @_; 610*0Sstevel@tonic-gate local $Level = 2; 611*0Sstevel@tonic-gate _fresh_perl($prog, 612*0Sstevel@tonic-gate sub { @_ ? 613*0Sstevel@tonic-gate $_[0] =~ (ref $expected ? $expected : /$expected/) : 614*0Sstevel@tonic-gate $expected }, 615*0Sstevel@tonic-gate $runperl_args, $name); 616*0Sstevel@tonic-gate} 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate1; 619