class RSpec::Matchers::BuiltIn::CaptureStreamToTempfile

@private

Public Instance Methods

# File rspec-expectations/lib/rspec/matchers/built_in/output.rb, line 180
def capture(block)
  # We delay loading tempfile until it is actually needed because
  # we want to minimize stdlibs loaded so that users who use a
  # portion of the stdlib can't have passing specs while forgetting
  # to load it themselves. `CaptureStreamToTempfile` is rarely used
  # and `tempfile` pulls in a bunch of things (delegate, tmpdir,
  # thread, fileutils, etc), so it's worth delaying it until this point.
  require 'tempfile'

  original_stream = stream.clone
  captured_stream = Tempfile.new(name)

  begin
    captured_stream.sync = true
    stream.reopen(captured_stream)
    block.call
    captured_stream.rewind
    captured_stream.read
  ensure
    stream.reopen(original_stream)
    captured_stream.close
    captured_stream.unlink
  end
end