1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
|
#!/usr/bin/perl
=pod
=head1 NAME
run.t - Test suite for IPC::Run::run, etc.
=cut
use strict;
BEGIN {
$| = 1;
$^W = 1;
if( $ENV{PERL_CORE} ) {
chdir '../lib/IPC/Run' if -d '../lib/IPC/Run';
unshift @INC, 'lib', '../..';
$^X = '../../../t/' . $^X;
}
}
my @WARNING_MESSAGES;
$SIG{__WARN__} = sub{
push @WARNING_MESSAGES, @_;
diag("WARN: $_") foreach(@_);
};
sub get_warnings {
my @warnings = @WARNING_MESSAGES;
@WARNING_MESSAGES = ();
return @warnings
}
## Handy to have when our output is intermingled with debugging output sent
## to the debugging fd.
select STDERR;
select STDOUT;
use Test::More tests => 268;
use IPC::Run::Debug qw( _map_fds );
use IPC::Run qw( :filters :filter_imp start );
use t::lib::Test;
# Must do this this late as plan uses localtime, and localtime on darwin opens
# a file descriptor. Quite probably other operating systems do file descriptor
# things during the test setup.
my $fd_map = _map_fds;
sub run {
IPC::Run::run( ref $_[0] ? ( noinherit => 1 ) : (), @_ );
}
## Test at least some of the win32 PATHEXT logic
my $perl = $^X;
$perl =~ s/\.\w+\z// if IPC::Run::Win32_MODE();
sub _unlink {
my ( $f ) = @_;
my $tries;
while () {
return if unlink $f;
if ( $^O =~ /Win32/ && ++$tries <= 10 ) {
print STDOUT "# Waiting for Win32 to allow $f to be unlinked ($!)\n";
select undef, undef, undef, 0.1;
next;
}
die "$! unlinking $f at ", join( ", line ", (caller)[1,2] ), "\n";
}
}
my $text = "Hello World\n";
my @perl = ( $perl );
my $emitter_script =
qq{print '$text'; print STDERR uc( '$text' ) unless \@ARGV };
my @emitter = ( @perl, '-e', $emitter_script );
my $in;
my $out;
my $err;
my $in_file = 'run.t.in';
my $out_file = 'run.t.out';
my $err_file = 'run.t.err';
my $h;
sub slurp($) {
my ( $f ) = @_;
open( S, "<$f" ) or return "$! $f";
my $r = join( '', <S> );
close S or warn "$!: $f";
select 0.1 if $^O =~ /Win32/;
return $r;
}
sub spit($$) {
my ( $f, $s ) = @_;
open( S, ">$f" ) or die "$! $f";
print S $s or die "$! $f";
close S or die "$! $f";
}
##
## A grossly inefficient filter to test filter
## chains. It's inefficient because we want to make sure that the
## filter chain flushing logic works. The inefficiency is that it
## doesn't process as much input as it could each call, so lots of calls
## are required.
##
sub alt_casing_filter {
my ( $in_ref, $out_ref ) = @_;
return input_avail && do {
$$out_ref .= lc( substr( $$in_ref, 0, 1, '' ) );
1;
} && (
! input_avail || do {
$$out_ref .= uc( substr( $$in_ref, 0, 1, '' ) );
1;
}
);
}
sub case_inverting_filter {
my ( $in_ref, $out_ref ) = @_;
return input_avail && do {
$$in_ref =~ tr/a-zA-Z/A-Za-z/;
$$out_ref .= $$in_ref;
$$in_ref = '';
1;
};
}
sub eok {
my ( $got, $exp, $name ) = @_;
$got =~ s/([\000-\037])/sprintf "\\0x%02x", ord $1/ge if defined $exp;
$exp =~ s/([\000-\037])/sprintf "\\0x%02x", ord $1/ge if defined $exp;
my($pack, $file, $line) = caller();
$name ||= qq[eok at $file line $line];
local $Test::Builder::Level = $Test::Builder::Level + 1;
return is( $got, $exp, $name );
}
my $r;
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## Internal testing
##
filter_tests(
"alt_casing_filter",
"Hello World",
["hElLo wOrLd" =~ m/(..?)/g],
\&alt_casing_filter
),
is( _map_fds, $fd_map ); $fd_map = _map_fds;
filter_tests(
"case_inverting_filter",
"Hello World",
"hELLO wORLD",
\&case_inverting_filter
),
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## Calling the local system shell
##
ok( run qq{$perl -e exit} );
is( $?, 0 );
is( _map_fds, $fd_map ); $fd_map = _map_fds;
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "$^O's shell returns 0 even if last command doesn't", 3 );
}
ok( ! run(qq{$perl -e 'exit(42)'}) );
ok( $? );
is( $? >> 8, 42 );
}
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## Simple commands, not executed via shell
##
ok( run $perl, qw{-e exit} );
is( $?, 0 );
is( _map_fds, $fd_map ); $fd_map = _map_fds;
ok( ! run $perl, qw{-e exit(42)} );
ok( $? );
is $? >> 8, 42;
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## A function
##
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "Can't spawn subroutines on $^O", 5 );
}
ok run sub{} ;
is $?, 0 ;
ok !run sub{ exit 42 };
ok $? ;
is $? >> 8, 42 ;
}
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## A function, and an init function
##
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "Can't spawn subroutines on $^O", 2 );
}
my $e = 0;
ok(
! run(
sub{ exit($e) },
init => sub { $e = 42 }
)
);
ok( $? );
}
is( _map_fds, $fd_map ); $fd_map = _map_fds;
##
## scalar ref I & O redirection using op tokens
##
$out = 'REPLACE ME';
$fd_map = _map_fds;
$r = run [ @emitter, "nostderr" ], '>', \$out;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
$out = 'REPLACE ME';
$fd_map = _map_fds;
$r = run [ @emitter, "nostderr" ], '<', \undef, '>', \$out;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
$in = $emitter_script;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run \@perl, '<', \$in, '>', \$out, '2>', \$err,;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $emitter_script );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## scalar ref I & O redirection, succinct mode.
##
$in = $emitter_script;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run \@perl, \$in, \$out, \$err;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $emitter_script );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## Long output, to test for blocking read.
##
## Assume pipe buffer length <= 10000, need to double that to assure enough
## chars to fill a buffer so. This test adapted from a test submitted by
## Borislav Deianov <borislav@ensim.com>.
$in = "-" x 20000 . "end\n";
$out = 'REPLACE ME';
$fd_map = _map_fds;
$r = run [ $perl, qw{-e print"-"x20000;<STDIN>;} ], \$in, \$out;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
is( length $out, 20000 );
unlike( $out, qr/[^-]/ );
##
## Long output run through twice
##
## Adapted from a stress test by Aaron Elkiss <aelkiss@wam.umd.edu>
##
$h = start [$perl, qw( -pe BEGIN{$|=1}1 )], \$in, \$out;
$in = "\n";
$out = "";
pump $h until length $out;
is $out, "\n";
my $long_string = "x" x 20000 . "DOC2\n";
$in = $long_string;
$out = "";
my $ok_1 = eval {
pump $h until $out =~ /DOC2/;
1;
};
my $x = $@;
my $ok_2 = eval {
finish $h;
1;
};
$x = $@ if $ok_1 && ! $ok_2;
if ( $ok_1 && $ok_2 ) {
is $long_string, $out;
}
else {
$x =~ s/(x+)/sprintf "...%d \"x\" chars...", length $1/e;
is $x, "";
}
##
## child function, scalar ref I & O redirection, succinct mode.
##
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "Can't spawn subroutines on $^O", 6 );
}
$in = $text;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run(
sub { while (<>) { print; print STDERR uc( $_ ) } },
\$in, \$out, \$err
);
ok( $r );
ok ! $?;
is( _map_fds, $fd_map );
eok( $in, $text );
eok( $out, $text );
eok( $err, uc( $text ) );
}
##
## here document as input
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run \@perl, \<<TOHERE, \$out, \$err;
$emitter_script
TOHERE
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## undef as input
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run \@perl, \undef, \$out, \$err;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, '' );
eok( $err, '' );
##
## filehandle input redirection
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
spit( $in_file, $emitter_script );
open( F, "<$in_file" ) or die "$! $in_file";
$r = run \@perl, \*F, \$out, \$err;
close F;
unlink $in_file or warn "$! $in_file";
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## input redirection via caller writing directly to a pipe
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$h = start \@perl, '<pipe', \*IN, '>', \$out, '2>', \$err;
## Assume this won't block...
print IN $emitter_script;
close IN or warn $!;
$r = $h->finish;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## filehandle input redirection, passed via *F{IO}
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
spit( $in_file, $emitter_script );
open( F, "<$in_file" ) or die "$! $in_file";
$r = run \@perl, *F{IO}, \$out, \$err;
close F;
_unlink $in_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## filehandle output redirection
##
$fd_map = _map_fds;
open( OUT, ">$out_file" ) or die "$! $out_file";
open( ERR, ">$err_file" ) or die "$! $err_file";
print OUT "out: ";
print ERR uc( "err: " );
$r = run \@emitter, \undef, \*OUT, \*ERR;
print OUT " more out data";
print ERR uc( " more err data" );
close OUT;
close ERR;
$out = slurp( $out_file );
$err = slurp( $err_file );
_unlink $out_file;
_unlink $err_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, "out: $text more out data" );
eok( $err, uc( "err: $text more err data" ) );
##
## filehandle output redirection via a pipe that is returned to the caller
##
$fd_map = _map_fds;
$r = run \@emitter, \undef, '>pipe', \*OUT, '2>pipe', \*ERR;
$out = '';
$err = '';
read OUT, $out, 10000 or warn $!;
read ERR, $err, 10000 or warn $!;
close OUT or warn $!;
close ERR or warn $!;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## sub I & O redirection
##
$in = $emitter_script;
$out = undef;
$err = undef;
$fd_map = _map_fds;
$r = run(
\@perl,
'<', sub { my $f = $in; $in = undef; return $f },
'>', sub { $out .= shift },
'2>', sub { $err .= shift },
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## input redirection from a file
##
$out = undef;
$err = undef;
$fd_map = _map_fds;
spit( $in_file, $emitter_script );
$r = run(
\@perl,
"<$in_file",
'>', sub { $out .= shift },
'2>', sub { $err .= shift },
);
_unlink $in_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## reading input from a non standard fd
##
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "$^O does not allow redirection of file descriptors > 2", 11 );
}
$out = undef;
$err = undef;
$fd_map = _map_fds;
$r = run(
## FreeBSD doesn't guarantee that fd 3 or 4 are available, so
## don't assume, go for 5.
[ @perl, '-le', 'open( STDIN, "<&5" ) or die $!; print <STDIN>' ],
"5<", \"Hello World",
'>', \$out,
'2>', \$err,
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, '' );
##
## duping input descriptors and an input descriptor > 0
##
$in = $emitter_script;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run(
\@perl,
'>', \$out,
'2>', \$err,
'3<', \$in,
'0<&3',
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $emitter_script );
eok( $out, $text );
eok( $err, uc( $text ) );
}
##
## closing input descriptors
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
spit( $in_file, $emitter_script );
$r = run(
[ @perl, '-e', '$l = readline *STDIN or die $!; print $l' ],
'>', \$out,
'2>', \$err,
'<', $in_file,
'0<&-',
);
_unlink $in_file;
ok( ! $r );
ok( $? );
is( _map_fds, $fd_map );
eok( $out, '' );
#ok( $err =~ /file descriptor/i ? "Bad file descriptor error" : $err, "Bad file descriptor error" );
# XXX This should be use Errno; if $!{EBADF}. --rs
is( length $err ? "Bad file descriptor error" : $err, "Bad file descriptor error" );
##
## input redirection from a non-existent file
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
my $bad_file = "$in_file.nonexistant";
_unlink $bad_file if -e $bad_file;
eval {
$r = run \@perl, ">$out_file", "<$bad_file";
};
like $@, qr/\Q$bad_file\E/;
is( _map_fds, $fd_map );
##
## output redirection to a file w/ creation or truncation
##
$fd_map = _map_fds;
_unlink $out_file if -x $out_file;
_unlink $err_file if -x $err_file;
$r = run(
\@emitter,
">$out_file",
"2>$err_file",
);
$out = slurp( $out_file );
$err = slurp( $err_file );
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## output file redirection, w/ truncation
##
$fd_map = _map_fds;
spit( $out_file, 'out: ' );
spit( $err_file, 'ERR: ' );
$r = run(
\@emitter,
">$out_file",
"2>$err_file",
);
$out = slurp( $out_file ); _unlink $out_file;
$err = slurp( $err_file ); _unlink $err_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
##
## output file redirection w/ append
##
spit( $out_file, 'out: ' );
spit( $err_file, 'ERR: ' );
$fd_map = _map_fds;
$r = run(
\@emitter,
">>$out_file",
"2>>$err_file",
);
$out = slurp( $out_file );
_unlink $out_file;
$err = slurp( $err_file );
_unlink $err_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, "out: $text" );
eok( $err, uc( "err: $text" ) );
##
## dup()ing output descriptors
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run \@emitter, '>', \$out, '2>', \$err, '2>&1';
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
like $out, qr/(?:$text){2}/i;
eok( $err, '' );
##
## stderr & stdout redirection to the same file via >&word
##
$fd_map = _map_fds;
_unlink $out_file if -x $out_file;
$r = run \@emitter, ">&$out_file";
$out = slurp( $out_file );
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
like $out, qr/(?:$text){2}/i;
##
## Non-zero exit value, command with args, no redirects.
##
$fd_map = _map_fds;
$r = run [ @perl, '-e', 'exit(42)' ];
ok( !$r );
is( $?, 42 << 8 );
is( _map_fds, $fd_map );
##
## Zero exit value, command with args, no redirects.
##
$fd_map = _map_fds;
$r = run [ @perl, qw{ -e exit }];
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
##
## dup()ing output descriptors that collide.
##
## This test assumes that our caller doesn't leave a lot of fds opened,
## and assumes that $out_file will be opened on fd 3, 4 or 5.
##
SKIP: {
if ( IPC::Run::Win32_MODE() ) {
skip( "$^O does not allow redirection of file descriptors > 2", 5 );
}
$out = 'REPLACE ME';
$err = 'REPLACE ME';
_unlink $out_file if -x $out_file;
$fd_map = _map_fds;
$r = run(
\@emitter,
"<", \"",
"3>&1", "4>&1", "5>&1",
">$out_file",
'2>', \$err,
);
$out = slurp( $out_file );
_unlink $out_file;
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
}
##
## Pipelining
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run(
[ @perl, '-lane', 'print STDERR "1:$_"; print uc($F[0])," ",$F[1]'],
\"Hello World",
'|',[ @perl, '-lane', 'print STDERR "2:$_"; print $F[0]," ",lc($F[1])'],
\$out,
\$err,
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, "HELLO world\n" );
eok( $err, "1:Hello World\n2:HELLO World\n" );
##
## Parallel (unpiplined) processes
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run(
[ @perl, '-lane', 'print STDERR "1:$_"; print uc($F[0])," ",$F[1]' ],
\"Hello World",
'&', [ @perl, '-lane', 'print STDERR "2:$_"; print $F[0]," ",lc( $F[1] )' ],
\"Hello World",
\$out,
\$err,
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
like $out, qr/^(?:HELLO World\n|Hello world\n){2}$/s;
like $err, qr/^(?:[12]:Hello World.*){2}$/s;
##
## A few error cases...
##
eval { $r = run \@perl, '<', [], [] };
like( $@, qr/not allowed/ );
eval { $r = run \@perl, '>', [], [] };
like( $@, qr/not allowed/ );
foreach my $foo ( qw( | & < > >& 1>&2 >file <file 2<&1 <&- 3<&- ) ) {
eval { $r = run $foo, [] };
like( $@, qr/command/ );
}
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
eval {
$r = run( \@emitter, '>', \$out, '2>', \$err,
_simulate_fork_failure => 1
);
};
ok( $@ );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, '' );
eok( $err, '' );
$fd_map = _map_fds;
eval {
$r = run \@perl, '<file', _simulate_open_failure => 1;
};
ok( $@ );
ok( ! $? );
is( _map_fds, $fd_map );
$fd_map = _map_fds;
eval {
$r = run \@perl, '>file', _simulate_open_failure => 1;
};
ok( $@ );
ok( ! $? );
is( _map_fds, $fd_map );
##
## harness, pump, run
##
$in = 'SHOULD BE UNCHANGED';
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$? = 99;
$fd_map = _map_fds;
$h = start(
[ @perl, '-pe', 'BEGIN { $| = 1 } print STDERR uc($_)' ],
\$in, \$out, \$err,
);
isa_ok( $h, 'IPC::Run' );
is( $?, 99 );
eok( $in, 'SHOULD BE UNCHANGED' );
eok( $out, '' );
eok( $err, '' );
ok( $h->pumpable );
$in = '';
$? = 0;
pump_nb $h for ( 1..100 );
pass( "after pump_nb" );
eok( $in, '' );
eok( $out, '' );
eok( $err, '' );
ok( $h->pumpable );
$in = $text;
$? = 0;
pump $h until $out =~ /Hello World/;
pass("after pump");
ok( ! $? );
eok( $in, '' );
eok( $out, $text );
ok( $h->pumpable );
ok( $h->finish );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, $text );
eok( $err, uc( $text ) );
ok( ! $h->pumpable );
##
## start, run, run, run. See Tom run. A do-run-run, a-do-run-run.
##
$in = 'SHOULD BE UNCHANGED';
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$h = start(
[ @perl, '-pe', 'binmode STDOUT; binmode STDERR; BEGIN { $| = 1 } print STDERR uc($_)' ],
\$in, \$out, \$err,
);
ok( $h->isa('IPC::Run') );
eok( $in, 'SHOULD BE UNCHANGED' );
eok( $out, '' );
eok( $err, '' );
ok( $h->pumpable );
$in = $text;
ok( $h->finish );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, '' );
eok( $out, $text );
eok( $err, uc( $text ) );
ok( ! $h->pumpable );
$in = $text;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
ok( $h->run );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $text );
eok( $out, $text );
eok( $err, uc( $text ) );
ok( ! $h->pumpable );
$in = $text;
$out = 'REPLACE ME';
$err = 'REPLACE ME';
ok( $h->run );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $text );
eok( $out, $text );
eok( $err, uc( $text ) );
ok( ! $h->pumpable );
##
## Output filters
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$r = run(
\@emitter,
'>',
\&alt_casing_filter,
\&case_inverting_filter,
\$out,
'2>', \$err,
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $out, "HeLlO WoRlD\n" );
eok( $err, uc( $text ) );
##
## Input filters
##
$out = 'REPLACE ME';
$err = 'REPLACE ME';
$fd_map = _map_fds;
$in = $text;
$r = run(
[ @perl, '-pe', 'binmode STDOUT; binmode STDERR; print STDERR uc $_' ],
'0<',
\&case_inverting_filter,
\&alt_casing_filter,
\$in,
'1>', \$out,
'2>', \$err,
);
ok( $r );
ok( ! $? );
is( _map_fds, $fd_map );
eok( $in, $text );
eok( $out, "HeLlO WoRlD\n" );
eok( $err, uc( $text ) );
{ # no warnings for an empty path but it does die.
# Some other OSes might not support find. Windows and UNIX do...
my @simple_command = ('bogusprogram');
local $ENV{PATH};
delete $ENV{PATH};
eval {$h = start \@simple_command, \$in, \$out;};
ok($@, "Error running bogus program when path is empty");
my ($message) = get_warnings();
is($message, undef, "No warnings found during program call with empty path");
finish $h; # Close out the program call
}
|