Sha256: 4309e9f8828c420d05921a4a045f70e124a795b9cf4be3ca82593d805763b79f

Contents?: true

Size: 990 Bytes

Versions: 3

Compression:

Stored size: 990 Bytes

Contents

require "stringio"

module MockStdIO
  def follow_prompts(*responses, &block)
    old_stdin = $stdin
    old_stdout = $stdout

    $stdin = StringIO.new("", "r+")
    fake_stdout = StringIO.new
    $stdout = fake_stdout

    responses.each { |r| $stdin << "#{r}\n" }
    $stdin.rewind

    block.call

    fake_stdout.string
  ensure
    $stdin = old_stdin
    $stdout = old_stdout
  end

  def capture_stdout(&block)
    old_stdout = $stdout

    fake_stdout = StringIO.new
    $stdout = fake_stdout

    block.call

    fake_stdout.string
  ensure
    $stdout = old_stdout
  end

  def silence_output
    @orig_stderr = $stderr
    @orig_stdout = $stdout

    # redirect stderr and stdout to /dev/null
    $stderr = File.new("/dev/null", "w")
    $stdout = File.new("/dev/null", "w")
  end

  # Replace stdout and stderr so anything else is output correctly.
  def enable_output
    $stderr = @orig_stderr
    $stdout = @orig_stdout
    @orig_stderr = nil
    @orig_stdout = nil
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stairs-0.4.2 spec/support/mock_stdout.rb
stairs-0.4.1 spec/support/mock_stdout.rb
stairs-0.4.0 spec/support/mock_stdout.rb