diff --git a/lib/childprocess/process_spawn_process.rb b/lib/childprocess/process_spawn_process.rb index 11bd3a6..14f1231 100644 --- a/lib/childprocess/process_spawn_process.rb +++ b/lib/childprocess/process_spawn_process.rb @@ -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 @@ -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 diff --git a/spec/io_spec.rb b/spec/io_spec.rb index 0c16ee9..ad80fb7 100644 --- a/spec/io_spec.rb +++ b/spec/io_spec.rb @@ -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