summaryrefslogtreecommitdiff
path: root/spec/support/capture_output_helper.rb
blob: 363a57c701dc9406800a95c7fc4046af456fa8a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module CaptureOutputHelper
  def capture_output(&block)
    old_stdout = $stdout
    old_stderr = $stderr

    stream_out = StringIO.new
    stream_err = StringIO.new

    begin
      $stdout = stream_out
      $stderr = stream_err
      yield
    ensure
      $stdout = old_stdout
      $stderr = old_stderr
    end
    stream_out.rewind
    stream_err.rewind

    [stream_out.read, stream_err.read]
  end
end