diff options
| author | Markus Reiter | 2017-07-24 00:04:43 +0200 |
|---|---|---|
| committer | GitHub | 2017-07-24 00:04:43 +0200 |
| commit | aa8eb21b8c650a2d0a5a060ff27507da60a17bee (patch) | |
| tree | 8a149b8cd30f43f61b6c1a5c805b106c5eb07f9d /Library/Homebrew/test/support | |
| parent | a947bfbf47a5d314ab689681c6ef0c42a66ec246 (diff) | |
| parent | 4e26fdfcf6922dca9a82b15697b4c76c6bf9212b (diff) | |
| download | brew-aa8eb21b8c650a2d0a5a060ff27507da60a17bee.tar.bz2 | |
Merge pull request #2861 from reitermarkus/cask-search-tty
Output plain list when running `brew cask search` without a TTY.
Diffstat (limited to 'Library/Homebrew/test/support')
| -rw-r--r-- | Library/Homebrew/test/support/helper/output_as_tty.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Library/Homebrew/test/support/helper/output_as_tty.rb b/Library/Homebrew/test/support/helper/output_as_tty.rb new file mode 100644 index 000000000..aa9da73cc --- /dev/null +++ b/Library/Homebrew/test/support/helper/output_as_tty.rb @@ -0,0 +1,84 @@ +require "delegate" + +module Test + module Helper + module OutputAsTTY + # This is a custom wrapper for the `output` matcher, + # used for testing output to a TTY: + # + # expect { + # print "test" if $stdout.tty? + # }.to output("test").to_stdout.as_tty + # + # expect { + # # command + # }.to output(...).to_stderr.as_tty.with_color + # + class Output < SimpleDelegator + def matches?(block) + return super(block) unless @tty + + 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 + 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) + end + + def to_stdout + @output = :stdout + super + self + end + + def to_stderr + @output = :stderr + super + self + end + + def as_tty + @tty = true + return self if [:stdout, :stderr].include?(@output) + raise "`as_tty` can only be chained to `stdout` or `stderr`." + end + + def with_color + @colors = true + return self if @tty + raise "`with_color` can only be chained to `as_tty`." + end + end + + def output(*args) + core_matcher = super(*args) + Output.new(core_matcher) + end + end + end +end |
