Module: MockStdio
- Defined in:
- lib/mock_stdio.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
- #capture_stdout(&block) ⇒ Object
-
#enable_output ⇒ Object
Replace stdout and stderr so anything else is output correctly.
- #follow_prompts(*responses, &block) ⇒ Object
- #silence_output ⇒ Object
Instance Method Details
#capture_stdout(&block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 ⇒ Object
Replace stdout and stderr so anything else is output correctly.
48 49 50 51 52 53 |
# 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) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 ⇒ Object
38 39 40 41 42 43 44 45 |
# 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 |