Sha256: 4f236d6588cfd6bba5ada0e17ea2d37a1f088897b42d7478dfc3e1909c745b99

Contents?: true

Size: 678 Bytes

Versions: 3

Compression:

Stored size: 678 Bytes

Contents

# frozen_string_literal: true

require "tempfile"
require "securerandom"

module OutputHelper
  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 = std_stream)
    original_stdout = $stdout.dup
    $stdout.reopen stdout

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
commit_format-0.2.1 spec/support/output_helper.rb
commit_format-0.2.0 spec/support/output_helper.rb
commit_format-0.1.0 spec/support/output_helper.rb