123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Matchers::BuiltIn::CaptureStreamToTempfile Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Struct
Instance Chain:
self, Struct
Inherits: Struct
  • Object
Defined in: rspec-expectations/lib/rspec/matchers/built_in/output.rb

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#name (rw)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 216

class CaptureStreamToTempfile < Struct.new(:name, :stream)

#stream (rw)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 216

class CaptureStreamToTempfile < Struct.new(:name, :stream)

Instance Method Details

#capture(block)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/output.rb', line 217

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