Merge pull request #2861 from reitermarkus/cask-search-tty

Output plain list when running `brew cask search` without a TTY.
This commit is contained in:
Markus Reiter 2017-07-24 00:04:43 +02:00 committed by GitHub
commit aa8eb21b8c
4 changed files with 113 additions and 9 deletions

View File

@ -46,6 +46,11 @@ module Hbc
end end
def self.render_results(exact_match, partial_matches, remote_matches, search_term) 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? if !exact_match && partial_matches.empty?
puts "No Cask found for \"#{search_term}\"." puts "No Cask found for \"#{search_term}\"."
return return

View File

@ -1,60 +1,73 @@
describe Hbc::CLI::Search, :cask do describe Hbc::CLI::Search, :cask do
before(:each) do
allow(Tty).to receive(:width).and_return(0)
end
it "lists the available Casks that match the search term" do it "lists the available Casks that match the search term" do
expect { expect {
Hbc::CLI::Search.run("local") Hbc::CLI::Search.run("local")
}.to output(<<-EOS.undent).to_stdout }.to output(<<-EOS.undent).to_stdout.as_tty
==> Partial Matches ==> Partial Matches
local-caffeine local-caffeine
local-transmission local-transmission
EOS EOS
end 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 it "shows that there are no Casks matching a search term that did not result in anything" do
expect { expect {
Hbc::CLI::Search.run("foo-bar-baz") 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 end
it "lists all available Casks with no search term" do it "lists all available Casks with no search term" do
expect { expect {
Hbc::CLI::Search.run Hbc::CLI::Search.run
}.to output(/local-caffeine/).to_stdout }.to output(/local-caffeine/).to_stdout.as_tty
end end
it "ignores hyphens in search terms" do it "ignores hyphens in search terms" do
expect { expect {
Hbc::CLI::Search.run("lo-cal-caffeine") Hbc::CLI::Search.run("lo-cal-caffeine")
}.to output(/local-caffeine/).to_stdout }.to output(/local-caffeine/).to_stdout.as_tty
end end
it "ignores hyphens in Cask tokens" do it "ignores hyphens in Cask tokens" do
expect { expect {
Hbc::CLI::Search.run("localcaffeine") Hbc::CLI::Search.run("localcaffeine")
}.to output(/local-caffeine/).to_stdout }.to output(/local-caffeine/).to_stdout.as_tty
end end
it "accepts multiple arguments" do it "accepts multiple arguments" do
expect { expect {
Hbc::CLI::Search.run("local caffeine") Hbc::CLI::Search.run("local caffeine")
}.to output(/local-caffeine/).to_stdout }.to output(/local-caffeine/).to_stdout.as_tty
end end
it "accepts a regexp argument" do it "accepts a regexp argument" do
expect { expect {
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/") 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 end
it "Returns both exact and partial matches" do it "Returns both exact and partial matches" do
expect { expect {
Hbc::CLI::Search.run("test-opera") 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 end
it "does not search the Tap name" do it "does not search the Tap name" do
expect { expect {
Hbc::CLI::Search.run("caskroom") 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 end
it "doesn't highlight packages that aren't installed" do it "doesn't highlight packages that aren't installed" do

View File

@ -23,6 +23,7 @@ require "test/support/helper/shutup"
require "test/support/helper/fixtures" require "test/support/helper/fixtures"
require "test/support/helper/formula" require "test/support/helper/formula"
require "test/support/helper/mktmpdir" require "test/support/helper/mktmpdir"
require "test/support/helper/output_as_tty"
require "test/support/helper/rubocop" require "test/support/helper/rubocop"
require "test/support/helper/spec/shared_context/homebrew_cask" if OS.mac? require "test/support/helper/spec/shared_context/homebrew_cask" if OS.mac?
@ -47,6 +48,7 @@ RSpec.configure do |config|
config.include(Test::Helper::Fixtures) config.include(Test::Helper::Fixtures)
config.include(Test::Helper::Formula) config.include(Test::Helper::Formula)
config.include(Test::Helper::MkTmpDir) config.include(Test::Helper::MkTmpDir)
config.include(Test::Helper::OutputAsTTY)
config.include(Test::Helper::RuboCop) config.include(Test::Helper::RuboCop)
config.before(:each, :needs_compat) do config.before(:each, :needs_compat) do

View File

@ -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