This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] Integrate mainline
[perl5.git] / t / lib / io_pipe.t
1 #!./perl
2
3 BEGIN {
4     unless(grep /blib/, @INC) {
5         chdir 't' if -d 't';
6         @INC = '../lib' if -d '../lib';
7     }
8 }
9
10 use Config;
11
12 BEGIN {
13     if(-d "lib" && -f "TEST") {
14         if (! $Config{'d_fork'} ||
15             ($Config{'extensions'} !~ /\bIO\b/ && $^O ne 'VMS'))
16         {
17             print "1..0\n";
18             exit 0;
19         }
20     }
21 }
22
23 use IO::Pipe;
24
25 my $perl = './perl';
26
27 $| = 1;
28 print "1..10\n";
29
30 $pipe = new IO::Pipe->reader($perl, '-e', 'print "not ok 1\n"');
31 while (<$pipe>) {
32   s/^not //;
33   print;
34 }
35 $pipe->close or print "# \$!=$!\nnot ";
36 print "ok 2\n";
37
38 $cmd = 'BEGIN{$SIG{ALRM} = sub {print "not ok 4\n"; exit}; alarm 10} s/not //';
39 $pipe = new IO::Pipe->writer($perl, '-pe', $cmd);
40 print $pipe "not ok 3\n" ;
41 $pipe->close or print "# \$!=$!\nnot ";
42 print "ok 4\n";
43
44 $pipe = new IO::Pipe;
45
46 $pid = fork();
47
48 if($pid)
49  {
50   $pipe->writer;
51   print $pipe "Xk 5\n";
52   print $pipe "oY 6\n";
53   $pipe->close;
54   wait;
55  }
56 elsif(defined $pid)
57  {
58   $pipe->reader;
59   $stdin = bless \*STDIN, "IO::Handle";
60   $stdin->fdopen($pipe,"r");
61   exec 'tr', 'YX', 'ko';
62  }
63 else
64  {
65   die "# error = $!";
66  }
67
68 $pipe = new IO::Pipe;
69 $pid = fork();
70
71 if($pid)
72  {
73   $pipe->reader;
74   while(<$pipe>) {
75       s/^not //;
76       print;
77   }
78   $pipe->close;
79   wait;
80  }
81 elsif(defined $pid)
82  {
83   $pipe->writer;
84
85   $stdout = bless \*STDOUT, "IO::Handle";
86   $stdout->fdopen($pipe,"w");
87   print STDOUT "not ok 7\n";
88   exec 'echo', 'not ok 8';
89  }
90 else
91  {
92   die;
93  }
94
95 $pipe = new IO::Pipe;
96 $pipe->writer;
97
98 $SIG{'PIPE'} = 'broken_pipe';
99
100 sub broken_pipe {
101     print "ok 9\n";
102 }
103
104 print $pipe "not ok 9\n";
105 $pipe->close;
106
107 sleep 1;
108
109 print "ok 10\n";
110