module MockStdio

Constants

VERSION

Public Instance Methods

capture_stdout(&block) click to toggle source
# File lib/mock_stdio.rb, line 25
def capture_stdout(&block)
  old_stdout = $stdout

  fake_stdout = StringIO.new
  $stdout = fake_stdout

  block.call

  fake_stdout.string
ensure
  $stdout = old_stdout
end
enable_output() click to toggle source

Replace stdout and stderr so anything else is output correctly.

# File lib/mock_stdio.rb, line 48
def enable_output
  $stderr = @orig_stderr
  $stdout = @orig_stdout
  @orig_stderr = nil
  @orig_stdout = nil
end
follow_prompts(*responses, &block) click to toggle source
# File lib/mock_stdio.rb, line 6
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
silence_output() click to toggle source
# File lib/mock_stdio.rb, line 38
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