Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/childprocess/process_spawn_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def launch_process

options = {}

options[:out] = io.stdout ? io.stdout.fileno : File::NULL
options[:err] = io.stderr ? io.stderr.fileno : File::NULL
options[:out] = io.stdout ? fileno_for(io.stdout) : File::NULL
options[:err] = io.stderr ? fileno_for(io.stderr) : File::NULL

if duplex?
reader, writer = ::IO.pipe
Expand Down Expand Up @@ -100,6 +100,10 @@ def set_exit_code(status)
@exit_code = status.exitstatus || status.termsig
end

def fileno_for(io)
io.respond_to?(:fileno) ? io.fileno : io.to_io.fileno
end

def send_term
send_signal 'TERM'
end
Expand Down
17 changes: 17 additions & 0 deletions spec/io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@
end
end

it "can redirect stdout through a to_io wrapper" do
process = echo
out = Tempfile.new("to-io-wrapper-spec")
wrapper = Object.new
wrapper.define_singleton_method(:to_io) { out.to_io }

begin
process.io.stdout = wrapper
process.start
process.wait

expect(rewind_and_read(out)).to eq "hello\n"
ensure
out.close
end
end

it "pumps all output" do
process = echo

Expand Down