Merge branch 'vincent/rvalue_stmt_given' into blead
[p5sagit/p5-mst-13.2.git] / ext / IPC-Open3 / t / IPC-Open3.t
CommitLineData
7e1af8bc 1#!./perl -w
7e1af8bc 2
3BEGIN {
774d564b 4 require Config; import Config;
f55ee38a 5 if (!$Config{'d_fork'}
6 # open2/3 supported on win32 (but not Borland due to CRT bugs)
2986a63f 7 && (($^O ne 'MSWin32' && $^O ne 'NetWare') || $Config{'cc'} =~ /^bcc/i))
f55ee38a 8 {
774d564b 9 print "1..0\n";
10 exit 0;
11 }
7e1af8bc 12 # make warnings fatal
13 $SIG{__WARN__} = sub { die @_ };
14}
15
71be2cbc 16use strict;
7e1af8bc 17use IO::Handle;
18use IPC::Open3;
19#require 'open3.pl'; use subs 'open3';
20
945d6bad 21my $perl = $^X;
774d564b 22
7e1af8bc 23sub ok {
24 my ($n, $result, $info) = @_;
25 if ($result) {
26 print "ok $n\n";
27 }
28 else {
f55ee38a 29 print "not ok $n\n";
7e1af8bc 30 print "# $info\n" if $info;
31 }
32}
33
f55ee38a 34sub cmd_line {
2986a63f 35 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
f55ee38a 36 my $cmd = shift;
37 $cmd =~ tr/\r\n//d;
38 $cmd =~ s/"/\\"/g;
39 return qq/"$cmd"/;
40 }
41 else {
42 return $_[0];
43 }
44}
45
7e1af8bc 46my ($pid, $reaped_pid);
47STDOUT->autoflush;
48STDERR->autoflush;
49
bb5bc496 50print "1..23\n";
7e1af8bc 51
52# basic
f55ee38a 53ok 1, $pid = open3 'WRITE', 'READ', 'ERROR', $perl, '-e', cmd_line(<<'EOF');
7e1af8bc 54 $| = 1;
55 print scalar <STDIN>;
56 print STDERR "hi error\n";
57EOF
58ok 2, print WRITE "hi kid\n";
f55ee38a 59ok 3, <READ> =~ /^hi kid\r?\n$/;
60ok 4, <ERROR> =~ /^hi error\r?\n$/;
7e1af8bc 61ok 5, close(WRITE), $!;
62ok 6, close(READ), $!;
63ok 7, close(ERROR), $!;
64$reaped_pid = waitpid $pid, 0;
65ok 8, $reaped_pid == $pid, $reaped_pid;
66ok 9, $? == 0, $?;
67
68# read and error together, both named
f55ee38a 69$pid = open3 'WRITE', 'READ', 'READ', $perl, '-e', cmd_line(<<'EOF');
7e1af8bc 70 $| = 1;
71 print scalar <STDIN>;
72 print STDERR scalar <STDIN>;
73EOF
74print WRITE "ok 10\n";
75print scalar <READ>;
76print WRITE "ok 11\n";
77print scalar <READ>;
78waitpid $pid, 0;
79
80# read and error together, error empty
f55ee38a 81$pid = open3 'WRITE', 'READ', '', $perl, '-e', cmd_line(<<'EOF');
7e1af8bc 82 $| = 1;
83 print scalar <STDIN>;
84 print STDERR scalar <STDIN>;
85EOF
86print WRITE "ok 12\n";
87print scalar <READ>;
88print WRITE "ok 13\n";
89print scalar <READ>;
90waitpid $pid, 0;
91
92# dup writer
93ok 14, pipe PIPE_READ, PIPE_WRITE;
94$pid = open3 '<&PIPE_READ', 'READ', '',
f55ee38a 95 $perl, '-e', cmd_line('print scalar <STDIN>');
7e1af8bc 96close PIPE_READ;
97print PIPE_WRITE "ok 15\n";
98close PIPE_WRITE;
99print scalar <READ>;
100waitpid $pid, 0;
101
102# dup reader
103$pid = open3 'WRITE', '>&STDOUT', 'ERROR',
f55ee38a 104 $perl, '-e', cmd_line('print scalar <STDIN>');
7e1af8bc 105print WRITE "ok 16\n";
106waitpid $pid, 0;
107
108# dup error: This particular case, duping stderr onto the existing
109# stdout but putting stdout somewhere else, is a good case because it
110# used not to work.
111$pid = open3 'WRITE', 'READ', '>&STDOUT',
f55ee38a 112 $perl, '-e', cmd_line('print STDERR scalar <STDIN>');
7e1af8bc 113print WRITE "ok 17\n";
114waitpid $pid, 0;
115
116# dup reader and error together, both named
f55ee38a 117$pid = open3 'WRITE', '>&STDOUT', '>&STDOUT', $perl, '-e', cmd_line(<<'EOF');
7e1af8bc 118 $| = 1;
119 print STDOUT scalar <STDIN>;
120 print STDERR scalar <STDIN>;
121EOF
122print WRITE "ok 18\n";
123print WRITE "ok 19\n";
124waitpid $pid, 0;
125
126# dup reader and error together, error empty
f55ee38a 127$pid = open3 'WRITE', '>&STDOUT', '', $perl, '-e', cmd_line(<<'EOF');
7e1af8bc 128 $| = 1;
129 print STDOUT scalar <STDIN>;
130 print STDERR scalar <STDIN>;
131EOF
132print WRITE "ok 20\n";
133print WRITE "ok 21\n";
134waitpid $pid, 0;
7083d4d1 135
136# command line in single parameter variant of open3
137# for understanding of Config{'sh'} test see exec description in camel book
138my $cmd = 'print(scalar(<STDIN>))';
139$cmd = $Config{'sh'} =~ /sh/ ? "'$cmd'" : cmd_line($cmd);
140eval{$pid = open3 'WRITE', '>&STDOUT', 'ERROR', "$perl -e " . $cmd; };
141if ($@) {
142 print "error $@\n";
143 print "not ok 22\n";
144}
145else {
146 print WRITE "ok 22\n";
147 waitpid $pid, 0;
85e4d853 148}
bb5bc496 149
150# RT 72016
151eval{$pid = open3 'WRITE', 'READ', 'ERROR', '/non/existant/program'; };
152if (IPC::Open3::DO_SPAWN) {
153 if ($@ || waitpid($pid, 0) > 0) {
154 print "ok 23\n";
155 } else {
156 print "not ok 23\n";
157 }
158} else {
159 if ($@) {
8960aa87 160 print "ok 23\n";
bb5bc496 161 } else {
8960aa87 162 waitpid($pid, 0);
163 print "not ok 23\n";
bb5bc496 164 }
165}