PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/t/io/pipe.t

http://github.com/ggoossen/kurila
Perl | 228 lines | 196 code | 22 blank | 10 comment | 8 complexity | 1ab137cfca8ddefa258aa48fc55a7542 MD5 | raw file
Possible License(s): AGPL-1.0
  1. #!./perl
  2. use Config
  3. use signals
  4. BEGIN
  5. require './test.pl'
  6. if (!(config_value: 'd_fork'))
  7. skip_all: "fork required to pipe"
  8. else
  9. plan: tests => 24
  10. my $Perl = (which_perl: )
  11. $^OUTPUT_AUTOFLUSH = 1
  12. (open: my $pipe, "|-", "-") || exec: $Perl, '-e', 'while (my $_ = ~< $^STDIN) { s/Y/k/g; s/X/o/g; print: $^STDOUT, $_ }'
  13. printf: $pipe, "Xk \%d - open |- || exec\n", (curr_test: )
  14. (next_test: )
  15. printf: $pipe, "oY \%d - again\n", (curr_test: )
  16. (next_test: )
  17. close $pipe
  18. :SKIP do
  19. # Technically this should be TODO. Someone try it if you happen to
  20. # have a vmesa machine.
  21. skip: "Doesn't work here yet", 6 if $^OS_NAME eq 'vmesa'
  22. if ((open: $pipe, "-|", "-"))
  23. while( ~< $pipe)
  24. s/^not //
  25. print: $^STDOUT, $_
  26. close $pipe # avoid zombies
  27. else
  28. printf: $^STDOUT, "not ok \%d - open -|\n", (curr_test: )
  29. (next_test: )
  30. my $tnum = (curr_test: )
  31. (next_test: )
  32. exec: $Perl, '-e', "print: \$^STDOUT, q\{not ok $tnum - again\n\}"
  33. # This has to be *outside* the fork
  34. for (1..2)
  35. (next_test: )
  36. my $raw = "abc\nrst\rxyz\r\nfoo\n"
  37. if ((open: $pipe, "-|", "-"))
  38. $_ = join: '', @: ~< $pipe
  39. (my $raw1 = $_) =~ s/not ok \d+ - //
  40. my @r = map: { ord }, split: m//, $raw
  41. my @r1 = map: { ord }, split: m//, $raw1
  42. if ($raw1 eq $raw)
  43. s/^not (ok \d+ -) .*/$1 '$((join: ' ',@r1))' passes through '-|'\n/s
  44. else
  45. s/^(not ok \d+ -) .*/$1 expect '$((join: ' ',@r))', got '$((join: ' ',@r1))'\n/s
  46. print: $^STDOUT, $_
  47. close $pipe # avoid zombies
  48. else
  49. printf: $^STDOUT, "not ok \%d - $raw", (curr_test: )
  50. exec: $Perl, '-e0' # Do not run END()...
  51. # This has to be *outside* the fork
  52. (next_test: )
  53. if ((open: $pipe, "|-", "-"))
  54. printf: $pipe, "not ok \%d - $raw", (curr_test: )
  55. close $pipe # avoid zombies
  56. else
  57. $_ = join: '', @: ~< $^STDIN
  58. (my $raw1 = $_) =~ s/not ok \d+ - //
  59. my @r = map: { ord }, split: m//, $raw
  60. my @r1 = map: { ord }, split: m//, $raw1
  61. if ($raw1 eq $raw)
  62. s/^not (ok \d+ -) .*/$1 '$((join: ' ',@r1))' passes through '|-'\n/s
  63. else
  64. s/^(not ok \d+ -) .*/$1 expect '$((join: ' ',@r))', got '$((join: ' ',@r1))'\n/s
  65. print: $^STDOUT, $_
  66. exec: $Perl, '-e0' # Do not run END()...
  67. # This has to be *outside* the fork
  68. (next_test: )
  69. :SKIP do
  70. skip: "fork required", 2 unless config_value: 'd_fork'
  71. (pipe: my $reader, my $writer) || die: "Can't open pipe"
  72. if (my $pid = fork)
  73. close $writer
  74. while( ~< $reader)
  75. s/^not //
  76. s/([A-Z])/$((lc: $1))/g
  77. print: $^STDOUT, $_
  78. close $reader # avoid zombies
  79. else
  80. die: "Couldn't fork" unless defined $pid
  81. close $reader
  82. printf: $writer, "not ok \%d - pipe & fork\n", (curr_test: )
  83. (next_test: )
  84. (open: $^STDOUT, ">&", $writer) || die: "Can't dup WRITER to STDOUT"
  85. close $writer
  86. my $tnum = (curr_test: )
  87. (next_test: )
  88. exec: $Perl, '-e', "print: \$^STDOUT, q\{not ok $tnum - with fh dup \n\}"
  89. # This has to be done *outside* the fork.
  90. for (1..2)
  91. (next_test: )
  92. wait # Collect from $pid
  93. (pipe: my $reader, my $writer) || die: "Can't open pipe"
  94. close $reader
  95. (signals::handler: 'PIPE') = &broken_pipe
  96. sub broken_pipe(...)
  97. (signals::handler: 'PIPE') = 'IGNORE' # loop preventer
  98. (printf: $^STDOUT, "ok \%d - SIGPIPE\n", (curr_test: ))
  99. (printf: $writer, "not ok \%d - SIGPIPE\n", (curr_test: ))
  100. close $writer
  101. sleep 1
  102. (next_test: )
  103. (pass: )
  104. # VMS doesn't like spawning subprocesses that are still connected to
  105. # STDOUT. Someone should modify these tests to work with VMS.
  106. :SKIP do
  107. skip: "doesn't like spawning subprocesses that are still connected", 10
  108. if $^OS_NAME eq 'VMS'
  109. :SKIP do
  110. # Sfio doesn't report failure when closing a broken pipe
  111. # that has pending output. Go figure.
  112. # BeOS will not write to broken pipes, either.
  113. # Nor does POSIX-BC.
  114. skip: "Won't report failure on broken pipe", 1
  115. if (config_value: 'd_sfio') || $^OS_NAME eq 'beos' ||
  116. $^OS_NAME eq 'posix-bc'
  117. local (signals::handler: "PIPE") = 'IGNORE'
  118. open: my $nil, '|-', qq{$Perl -e "exit 0"} or die: "open failed: $^OS_ERROR"
  119. sleep 5
  120. if (print: $nil, 'foo')
  121. # If print was allowed we had better get an error on close
  122. ok: !close $nil, 'close error on broken pipe'
  123. else
  124. ok: close $nil, 'print failed on broken pipe'
  125. :SKIP do
  126. skip: "Don't work yet", 9 if $^OS_NAME eq 'vmesa'
  127. # check that errno gets forced to 0 if the piped program exited
  128. # non-zero
  129. open: my $nil, '|-', qq{$Perl -e "exit 23";} or die: "fork failed: $^OS_ERROR"
  130. $^OS_ERROR = 1
  131. ok: !close $nil, 'close failure on non-zero piped exit'
  132. is: $^OS_ERROR, '', ' errno'
  133. isnt: $^CHILD_ERROR, 0, ' status'
  134. :SKIP do
  135. skip: "Don't work yet", 6 if $^OS_NAME eq 'mpeix'
  136. # check that status for the correct process is collected
  137. my $zombie
  138. unless( $zombie = fork )
  139. our $NO_ENDING = 1
  140. exit 37
  141. my $pipe = (open: my $fh, "-|", "sleep 2;exit 13") or die: "Open: $^OS_ERROR\n"
  142. (signals::handler: "ALRM") = sub (@< @_) { return }
  143. alarm: 1
  144. is: close $fh, '', 'close failure for... umm, something'
  145. is: $^CHILD_ERROR, 13*256, ' status'
  146. is: $^OS_ERROR, '', ' errno'
  147. my $wait = wait
  148. is: $^CHILD_ERROR, 37*256, 'status correct after wait'
  149. is: $wait, $zombie, ' wait pid'
  150. is: $^OS_ERROR, '', ' errno'
  151. # Test new semantics for missing command in piped open
  152. # 19990114 M-J. Dominus mjd@plover.com
  153. do
  154. no warnings 'pipe'
  155. my $p
  156. ok: !(open: $p, "|-", ""), 'missing command in piped open input'
  157. ok: !(open: $p, "-|", ""), ' output'
  158. # check that status is unaffected by implicit close
  159. do
  160. open: my $nil, '|-', qq{$Perl -e "exit 23"} or die: "fork failed: $^OS_ERROR"
  161. $^CHILD_ERROR = 42
  162. # NIL implicitly closed here
  163. is: $^CHILD_ERROR, 42, 'status unaffected by implicit close'
  164. $^CHILD_ERROR = 0
  165. # check that child is reaped if the piped program can't be executed
  166. :SKIP do
  167. skip: "/no_such_process exists", 1 if -e "/no_such_process"
  168. open: my $nil, "-|", '/no_such_process'
  169. close $nil
  170. my $child = 0
  171. try {
  172. local (signals::handler: "ALRM") = sub () { (die: ); };
  173. alarm 2;
  174. $child = wait;
  175. alarm 0;
  176. }
  177. is: $child, -1, 'child reaped if piped program cannot be executed'