diff --git a/lib/rspec/support/spec/stderr_splitter.rb b/lib/rspec/support/spec/stderr_splitter.rb index 9caa34cd..44315cec 100644 --- a/lib/rspec/support/spec/stderr_splitter.rb +++ b/lib/rspec/support/spec/stderr_splitter.rb @@ -21,6 +21,10 @@ def method_missing(name, *args, &block) @orig_stderr.__send__(name, *args, &block) end + def clone + StdErrSplitter.new(@orig_stderr.clone) + end + def ==(other) @orig_stderr == other end diff --git a/spec/rspec/support/spec/stderr_splitter_spec.rb b/spec/rspec/support/spec/stderr_splitter_spec.rb index 7cb7f6d5..1b1ac0d6 100644 --- a/spec/rspec/support/spec/stderr_splitter_spec.rb +++ b/spec/rspec/support/spec/stderr_splitter_spec.rb @@ -95,4 +95,25 @@ end end + it 'does not reuse the stream when cloned' do + expect(splitter.to_io).not_to eq(splitter.clone.to_io) + end + + # This is essentially what the `to_stderr_from_any_process` matcher attempts + # to do in CaptureStreamToTempfile. + it 'is able to restore the stream from a cloned StdErrSplitter', :pending => RSpec::Support::Ruby.jruby? do + cloned = $stderr.clone + expect($stderr.to_io).not_to be_a(File) + + tempfile = Tempfile.new("foo") + begin + $stderr.reopen(tempfile) + expect($stderr.to_io).to be_a(File) + ensure + $stderr.reopen(cloned) + tempfile.close + tempfile.unlink + end + expect($stderr.to_io).not_to be_a(File) + end end