diff options
| -rw-r--r-- | Library/Homebrew/test/cask/cli/search_spec.rb | 9 | ||||
| -rw-r--r-- | Library/Homebrew/test/support/helper/output_as_tty.rb | 63 |
2 files changed, 33 insertions, 39 deletions
diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index 4c779cae3..f43610767 100644 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ b/Library/Homebrew/test/cask/cli/search_spec.rb @@ -13,6 +13,15 @@ describe Hbc::CLI::Search, :cask do EOS end + it "outputs a plain list when stdout is not a TTY" do + expect { + Hbc::CLI::Search.run("local") + }.to output(<<-EOS.undent).to_stdout + local-caffeine + local-transmission + EOS + end + it "shows that there are no Casks matching a search term that did not result in anything" do expect { Hbc::CLI::Search.run("foo-bar-baz") diff --git a/Library/Homebrew/test/support/helper/output_as_tty.rb b/Library/Homebrew/test/support/helper/output_as_tty.rb index bd72a8f3f..aa9da73cc 100644 --- a/Library/Homebrew/test/support/helper/output_as_tty.rb +++ b/Library/Homebrew/test/support/helper/output_as_tty.rb @@ -3,14 +3,6 @@ require "delegate" module Test module Helper module OutputAsTTY - module TTYTrue - def tty? - true - end - - alias isatty tty? - end - # This is a custom wrapper for the `output` matcher, # used for testing output to a TTY: # @@ -26,41 +18,33 @@ module Test def matches?(block) return super(block) unless @tty - colored_tty_block = if @output == :stdout - lambda do - $stdout.extend(TTYTrue) - block.call - end - elsif @output == :stderr - lambda do - $stderr.extend(TTYTrue) - block.call - end - else - raise "`as_tty` can only be chained to `stdout` or `stderr`." + colored_tty_block = lambda do + instance_eval("$#{@output}").extend(Module.new do + def tty? + true + end + + alias_method :isatty, :tty? + end) + block.call end return super(colored_tty_block) if @colors uncolored_tty_block = lambda do - begin - out_stream = StringIO.new - err_stream = StringIO.new - - old_stdout = $stdout - old_stderr = $stderr - - $stdout = out_stream - $stderr = err_stream - - colored_tty_block.call - ensure - $stdout = old_stdout - $stderr = old_stderr - - $stdout.print Tty.strip_ansi(out_stream.string) - $stderr.print Tty.strip_ansi(err_stream.string) - end + instance_eval <<-EOS + begin + captured_stream = StringIO.new + + original_stream = $#{@output} + $#{@output} = captured_stream + + colored_tty_block.call + ensure + $#{@output} = original_stream + $#{@output}.print Tty.strip_ansi(captured_stream.string) + end + EOS end super(uncolored_tty_block) @@ -80,7 +64,8 @@ module Test def as_tty @tty = true - self + return self if [:stdout, :stderr].include?(@output) + raise "`as_tty` can only be chained to `stdout` or `stderr`." end def with_color |
