Sha256: 813898ed60cdcc185bcb05fa89c0d744890df60140716e2e320400b99e4f56d0

Contents?: true

Size: 1.52 KB

Versions: 50

Compression:

Stored size: 1.52 KB

Contents

module StdStreamsHelper
  def std_stream
    Tempfile.new SecureRandom.uuid
  end

  # Capture STDOUT in a variable
  #
  # Given tempfiles are rewinded and unlinked after yield, so no cleanup
  # required. You can read from the stream using `stdout.read`.
  #
  # Usage
  #
  #     out_stream = Tempfile.new
  #     capture_stdout(out_stream) { do_something }
  def capture_stdout(stdout)
    original_stdout = $stdout.dup
    $stdout.reopen stdout

    yield
  ensure
    $stdout.reopen original_stdout
    stdout.rewind
    stdout.unlink
  end

  # Capture STDOUT and STDERR in variables
  #
  # Given tempfiles are rewinded and unlinked after yield, so no cleanup
  # required. You can read from the stream using `stdout.read`.
  #
  # Usage
  #
  #     out_stream = Tempfile.new
  #     err_stream = Tempfile.new
  #     capture_std_streams(out_stream, err_stream) { do_something }
  def capture_std_streams(stdout, stderr)
    original_stdout = $stdout.dup
    $stdout.reopen stdout
    original_stderr = $stderr.dup
    $stderr.reopen stderr

    yield
  ensure
    $stdout.reopen original_stdout
    $stderr.reopen original_stderr
    stdout.rewind
    stdout.unlink
    stderr.rewind
    stderr.unlink
  end

  def silence
    std_stream = Tempfile.new(SecureRandom.uuid)
    original_stdout = $stdout.dup
    original_stderr = $stderr.dup
    $stdout.reopen std_stream
    $stderr.reopen std_stream

    yield
  ensure
    $stdout.reopen original_stdout
    $stderr.reopen original_stderr
    std_stream.rewind
    std_stream.unlink
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
appsignal-2.5.3-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.3 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.3.alpha.2 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.3.alpha.2-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.3.alpha.1 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.3.alpha.1-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.2-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.2 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.1-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.1 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.1.beta.1-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.1.beta.1 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0.beta.1-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0.beta.1 spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0.alpha.1-java spec/support/helpers/std_streams_helper.rb
appsignal-2.5.0.alpha.1 spec/support/helpers/std_streams_helper.rb
appsignal-2.4.3 spec/support/helpers/std_streams_helper.rb
appsignal-2.4.2 spec/support/helpers/std_streams_helper.rb