Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / perl5 / 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 # Check if can fork with dynamic extensions (bug in CRT):
45 if ($^O eq 'os2' and
46     system "$^X -I../lib -MOpcode -e 'defined fork or die'  > /dev/null 2>&1") {
47     print "ok $_ # skipped: broken fork\n" for 5..10;
48     exit 0;
49 }
50
51 $pipe = new IO::Pipe;
52
53 $pid = fork();
54
55 if($pid)
56  {
57   $pipe->writer;
58   print $pipe "Xk 5\n";
59   print $pipe "oY 6\n";
60   $pipe->close;
61   wait;
62  }
63 elsif(defined $pid)
64  {
65   $pipe->reader;
66   $stdin = bless \*STDIN, "IO::Handle";
67   $stdin->fdopen($pipe,"r");
68   exec 'tr', 'YX', 'ko';
69  }
70 else
71  {
72   die "# error = $!";
73  }
74
75 $pipe = new IO::Pipe;
76 $pid = fork();
77
78 if($pid)
79  {
80   $pipe->reader;
81   while(<$pipe>) {
82       s/^not //;
83       print;
84   }
85   $pipe->close;
86   wait;
87  }
88 elsif(defined $pid)
89  {
90   $pipe->writer;
91
92   $stdout = bless \*STDOUT, "IO::Handle";
93   $stdout->fdopen($pipe,"w");
94   print STDOUT "not ok 7\n";
95   exec 'echo', 'not ok 8';
96  }
97 else
98  {
99   die;
100  }
101
102 $pipe = new IO::Pipe;
103 $pipe->writer;
104
105 $SIG{'PIPE'} = 'broken_pipe';
106
107 sub broken_pipe {
108     print "ok 9\n";
109 }
110
111 print $pipe "not ok 9\n";
112 $pipe->close;
113
114 sleep 1;
115
116 print "ok 10\n";
117