Sha256: 67b3a02d8e70d56031254cc1cd3fccfc3f35ed3b63eb1bc14e029e82ed64315d

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 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 silence capture
  end
end

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

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
aruba-0.14.14 spec/support/helpers/reporting.rb
aruba-0.14.13 spec/support/helpers/reporting.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/aruba-0.14.12/spec/support/helpers/reporting.rb
aruba-0.14.12 spec/support/helpers/reporting.rb
aruba-0.14.11 spec/support/helpers/reporting.rb
aruba-0.14.10 spec/support/helpers/reporting.rb
aruba-0.14.9 spec/support/helpers/reporting.rb
aruba-0.14.8 spec/support/helpers/reporting.rb
aruba-0.14.7 spec/support/helpers/reporting.rb
aruba-0.14.6 spec/support/helpers/reporting.rb
aruba-0.14.5 spec/support/helpers/reporting.rb
aruba-0.14.4 spec/support/helpers/reporting.rb
aruba-0.14.3 spec/support/helpers/reporting.rb
aruba-win-fix-0.14.2 spec/support/helpers/reporting.rb
aruba-0.14.2 spec/support/helpers/reporting.rb
aruba-0.14.1 spec/support/helpers/reporting.rb
aruba-0.14.0 spec/support/helpers/reporting.rb