From dccdac55a835a22d46c36fe915e6e8cdf43a4adc Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 8 Jul 2017 00:57:08 +0200 Subject: Add helper for testing TTY output. --- .../Homebrew/test/support/helper/output_as_tty.rb | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Library/Homebrew/test/support/helper/output_as_tty.rb (limited to 'Library/Homebrew/test/support') 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..bd72a8f3f --- /dev/null +++ b/Library/Homebrew/test/support/helper/output_as_tty.rb @@ -0,0 +1,99 @@ +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: + # + # 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 = 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`." + 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 + 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 + self + 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 -- cgit v1.2.3 From 4e26fdfcf6922dca9a82b15697b4c76c6bf9212b Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 8 Jul 2017 18:26:46 +0200 Subject: Add test for non-TTY `brew cask search`. --- .../Homebrew/test/support/helper/output_as_tty.rb | 63 +++++++++------------- 1 file changed, 24 insertions(+), 39 deletions(-) (limited to 'Library/Homebrew/test/support') 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 -- cgit v1.2.3