This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
filecopy.t #3 fails on dos-djgpp
[perl5.git] / t / lib / open3.t
CommitLineData
7e1af8bc 1#!./perl -w
7e1af8bc 2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
774d564b 6 require Config; import Config;
7 unless ($Config{'d_fork'}) {
8 print "1..0\n";
9 exit 0;
10 }
7e1af8bc 11 # make warnings fatal
12 $SIG{__WARN__} = sub { die @_ };
13}
14
71be2cbc 15use strict;
7e1af8bc 16use IO::Handle;
17use IPC::Open3;
18#require 'open3.pl'; use subs 'open3';
19
774d564b 20my $perl = './perl';
21
7e1af8bc 22sub ok {
23 my ($n, $result, $info) = @_;
24 if ($result) {
25 print "ok $n\n";
26 }
27 else {
28 print "not ok $n\n";
29 print "# $info\n" if $info;
30 }
31}
32
33my ($pid, $reaped_pid);
34STDOUT->autoflush;
35STDERR->autoflush;
36
37print "1..21\n";
38
39# basic
774d564b 40ok 1, $pid = open3 'WRITE', 'READ', 'ERROR', $perl, '-e', <<'EOF';
7e1af8bc 41 $| = 1;
42 print scalar <STDIN>;
43 print STDERR "hi error\n";
44EOF
45ok 2, print WRITE "hi kid\n";
46ok 3, <READ> eq "hi kid\n";
47ok 4, <ERROR> eq "hi error\n";
48ok 5, close(WRITE), $!;
49ok 6, close(READ), $!;
50ok 7, close(ERROR), $!;
51$reaped_pid = waitpid $pid, 0;
52ok 8, $reaped_pid == $pid, $reaped_pid;
53ok 9, $? == 0, $?;
54
55# read and error together, both named
774d564b 56$pid = open3 'WRITE', 'READ', 'READ', $perl, '-e', <<'EOF';
7e1af8bc 57 $| = 1;
58 print scalar <STDIN>;
59 print STDERR scalar <STDIN>;
60EOF
61print WRITE "ok 10\n";
62print scalar <READ>;
63print WRITE "ok 11\n";
64print scalar <READ>;
65waitpid $pid, 0;
66
67# read and error together, error empty
774d564b 68$pid = open3 'WRITE', 'READ', '', $perl, '-e', <<'EOF';
7e1af8bc 69 $| = 1;
70 print scalar <STDIN>;
71 print STDERR scalar <STDIN>;
72EOF
73print WRITE "ok 12\n";
74print scalar <READ>;
75print WRITE "ok 13\n";
76print scalar <READ>;
77waitpid $pid, 0;
78
79# dup writer
80ok 14, pipe PIPE_READ, PIPE_WRITE;
81$pid = open3 '<&PIPE_READ', 'READ', '',
774d564b 82 $perl, '-e', 'print scalar <STDIN>';
7e1af8bc 83close PIPE_READ;
84print PIPE_WRITE "ok 15\n";
85close PIPE_WRITE;
86print scalar <READ>;
87waitpid $pid, 0;
88
89# dup reader
90$pid = open3 'WRITE', '>&STDOUT', 'ERROR',
774d564b 91 $perl, '-e', 'print scalar <STDIN>';
7e1af8bc 92print WRITE "ok 16\n";
93waitpid $pid, 0;
94
95# dup error: This particular case, duping stderr onto the existing
96# stdout but putting stdout somewhere else, is a good case because it
97# used not to work.
98$pid = open3 'WRITE', 'READ', '>&STDOUT',
774d564b 99 $perl, '-e', 'print STDERR scalar <STDIN>';
7e1af8bc 100print WRITE "ok 17\n";
101waitpid $pid, 0;
102
103# dup reader and error together, both named
774d564b 104$pid = open3 'WRITE', '>&STDOUT', '>&STDOUT', $perl, '-e', <<'EOF';
7e1af8bc 105 $| = 1;
106 print STDOUT scalar <STDIN>;
107 print STDERR scalar <STDIN>;
108EOF
109print WRITE "ok 18\n";
110print WRITE "ok 19\n";
111waitpid $pid, 0;
112
113# dup reader and error together, error empty
774d564b 114$pid = open3 'WRITE', '>&STDOUT', '', $perl, '-e', <<'EOF';
7e1af8bc 115 $| = 1;
116 print STDOUT scalar <STDIN>;
117 print STDERR scalar <STDIN>;
118EOF
119print WRITE "ok 20\n";
120print WRITE "ok 21\n";
121waitpid $pid, 0;