From d29cb450f729fc88ba045c68e3b70e86cdecfaec Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 6 Jul 2017 01:08:59 +0200 Subject: [PATCH 1/3] Output plain list when running `brew cask search` without a TTY. --- Library/Homebrew/cask/lib/hbc/cli/search.rb | 5 +++++ Library/Homebrew/test/cask/cli/search_spec.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 9d1a16f157..643d18d554 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -46,6 +46,11 @@ module Hbc end def self.render_results(exact_match, partial_matches, remote_matches, search_term) + unless $stdout.tty? + puts [*exact_match, *partial_matches, *remote_matches] + return + end + if !exact_match && partial_matches.empty? puts "No Cask found for \"#{search_term}\"." return diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index 00fcf73824..c382614a46 100644 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ b/Library/Homebrew/test/cask/cli/search_spec.rb @@ -1,4 +1,8 @@ describe Hbc::CLI::Search, :cask do + before(:each) do + allow($stdout).to receive(:tty?).and_return(true) + end + it "lists the available Casks that match the search term" do expect { Hbc::CLI::Search.run("local") From dccdac55a835a22d46c36fe915e6e8cdf43a4adc Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 8 Jul 2017 00:57:08 +0200 Subject: [PATCH 2/3] Add helper for testing TTY output. --- Library/Homebrew/test/cask/cli/search_spec.rb | 20 ++-- Library/Homebrew/test/spec_helper.rb | 2 + .../test/support/helper/output_as_tty.rb | 99 +++++++++++++++++++ 3 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 Library/Homebrew/test/support/helper/output_as_tty.rb diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index c382614a46..4c779cae3d 100644 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ b/Library/Homebrew/test/cask/cli/search_spec.rb @@ -1,12 +1,12 @@ describe Hbc::CLI::Search, :cask do before(:each) do - allow($stdout).to receive(:tty?).and_return(true) + allow(Tty).to receive(:width).and_return(0) end it "lists the available Casks that match the search term" do expect { Hbc::CLI::Search.run("local") - }.to output(<<-EOS.undent).to_stdout + }.to output(<<-EOS.undent).to_stdout.as_tty ==> Partial Matches local-caffeine local-transmission @@ -16,49 +16,49 @@ describe Hbc::CLI::Search, :cask do 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") - }.to output("No Cask found for \"foo-bar-baz\".\n").to_stdout + }.to output("No Cask found for \"foo-bar-baz\".\n").to_stdout.as_tty end it "lists all available Casks with no search term" do expect { Hbc::CLI::Search.run - }.to output(/local-caffeine/).to_stdout + }.to output(/local-caffeine/).to_stdout.as_tty end it "ignores hyphens in search terms" do expect { Hbc::CLI::Search.run("lo-cal-caffeine") - }.to output(/local-caffeine/).to_stdout + }.to output(/local-caffeine/).to_stdout.as_tty end it "ignores hyphens in Cask tokens" do expect { Hbc::CLI::Search.run("localcaffeine") - }.to output(/local-caffeine/).to_stdout + }.to output(/local-caffeine/).to_stdout.as_tty end it "accepts multiple arguments" do expect { Hbc::CLI::Search.run("local caffeine") - }.to output(/local-caffeine/).to_stdout + }.to output(/local-caffeine/).to_stdout.as_tty end it "accepts a regexp argument" do expect { Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/") - }.to output("==> Regexp Matches\nlocal-caffeine\n").to_stdout + }.to output("==> Regexp Matches\nlocal-caffeine\n").to_stdout.as_tty end it "Returns both exact and partial matches" do expect { Hbc::CLI::Search.run("test-opera") - }.to output(/^==> Exact Match\ntest-opera\n==> Partial Matches\ntest-opera-mail/).to_stdout + }.to output(/^==> Exact Match\ntest-opera\n==> Partial Matches\ntest-opera-mail/).to_stdout.as_tty end it "does not search the Tap name" do expect { Hbc::CLI::Search.run("caskroom") - }.to output(/^No Cask found for "caskroom"\.\n/).to_stdout + }.to output(/^No Cask found for "caskroom"\.\n/).to_stdout.as_tty end it "doesn't highlight packages that aren't installed" do diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index 75540caadd..d04ca00886 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -23,6 +23,7 @@ require "test/support/helper/shutup" require "test/support/helper/fixtures" require "test/support/helper/formula" require "test/support/helper/mktmpdir" +require "test/support/helper/output_as_tty" require "test/support/helper/rubocop" require "test/support/helper/spec/shared_context/homebrew_cask" if OS.mac? @@ -45,6 +46,7 @@ RSpec.configure do |config| config.include(Test::Helper::Fixtures) config.include(Test::Helper::Formula) config.include(Test::Helper::MkTmpDir) + config.include(Test::Helper::OutputAsTTY) config.include(Test::Helper::RuboCop) config.before(:each, :needs_compat) do 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 0000000000..bd72a8f3f0 --- /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 From 4e26fdfcf6922dca9a82b15697b4c76c6bf9212b Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 8 Jul 2017 18:26:46 +0200 Subject: [PATCH 3/3] Add test for non-TTY `brew cask search`. --- Library/Homebrew/test/cask/cli/search_spec.rb | 9 +++ .../test/support/helper/output_as_tty.rb | 59 +++++++------------ 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index 4c779cae3d..f43610767b 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 bd72a8f3f0..aa9da73cc0 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 + instance_eval <<-EOS + begin + captured_stream = StringIO.new - old_stdout = $stdout - old_stderr = $stderr + original_stream = $#{@output} + $#{@output} = captured_stream - $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 + 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