Module: Gorillib::TestHelpers

Defined in:
lib/gorillib/utils/capture_output.rb

Class Method Summary collapse

Class Method Details

.capture_outputObject Also known as: quiet_output

Temporarily sets the global variables $stderr and $stdout to a capturable StringIO; restores them at the end, even if there is an error



22
23
24
# File 'lib/gorillib/utils/capture_output.rb', line 22

def capture_output
  dummy_stdio{ yield }
end

.dummy_stdio(stdin_text = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/gorillib/utils/capture_output.rb', line 5

def dummy_stdio(stdin_text=nil)
  stdin = stdin_text.nil? ? $stdin : StringIO.new(stdin_text)
  new_fhs = [stdin,  StringIO.new('', 'w'), StringIO.new('', 'w')]
  old_fhs = [$stdin, $stdout,               $stderr]
  begin
    $stdin, $stdout, $stderr = new_fhs
    yield
  ensure
    $stdin, $stdout, $stderr = old_fhs
  end
  new_fhs[1..2]
end