Sha256: 5b91f2889bb7648d527715e1805233c3ab3ce6e0a0f29eb359298a0688772d6f

Contents?: true

Size: 1.05 KB

Versions: 26

Compression:

Stored size: 1.05 KB

Contents

module SpecHelpers
  module Reporting
    # Captures the given stream and returns it:
    #
    #   stream = capture(:stdout) { puts 'notice' }
    #   stream # => "notice\n"
    #
    #   stream = capture(:stderr) { warn 'error' }
    #   stream # => "error\n"
    #
    # even for subprocesses:
    #
    #   stream = capture(:stdout) { system('echo notice') }
    #   stream # => "notice\n"
    #
    #   stream = capture(:stderr) { system('echo error 1>&2') }
    #   stream # => "error\n"
    def capture(stream)
      stream = stream.to_s
      captured_stream = Tempfile.new(stream)

      # rubocop:disable Lint/Eval
      stream_io = eval("$#{stream}")
      # rubocop:enable Lint/Eval

      origin_stream = stream_io.dup
      stream_io.reopen(captured_stream)

      yield

      stream_io.rewind
      return captured_stream.read
    ensure
      captured_stream.close
      captured_stream.unlink
      stream_io.reopen(origin_stream)
    end
    alias_method :silence, :capture
  end
end

RSpec.configure do |config|
  config.include SpecHelpers::Reporting
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
aruba-0.13.0 spec/support/helpers/reporting.rb
aruba-0.12.0 spec/support/helpers/reporting.rb
aruba-0.11.2 spec/support/helpers/reporting.rb
aruba-0.11.1 spec/support/helpers/reporting.rb
aruba-0.11.0.pre4 spec/support/helpers/reporting.rb
aruba-0.11.0.pre3 spec/support/helpers/reporting.rb
aruba-0.11.0.pre2 spec/support/helpers/reporting.rb
aruba-0.11.0.pre spec/support/helpers/reporting.rb
aruba-0.10.2 spec/support/helpers/reporting.rb
aruba-0.10.1 spec/support/helpers/reporting.rb
aruba-0.10.0 spec/support/helpers/reporting.rb
aruba-0.10.0.pre2 spec/support/helpers/reporting.rb
aruba-0.10.0.pre spec/support/helpers/reporting.rb
aruba-0.9.0 spec/support/helpers/reporting.rb
aruba-0.9.0.pre2 spec/support/helpers/reporting.rb
aruba-0.9.0.pre spec/support/helpers/reporting.rb
aruba-0.8.1 spec/support/helpers/reporting.rb
aruba-0.8.0 spec/support/helpers/reporting.rb
aruba-0.8.0.pre3 spec/support/helpers/reporting.rb
aruba-0.8.0.pre2 spec/support/helpers/reporting.rb