diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index e89dced925..d56d0c81fe 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -2,8 +2,12 @@ module Hbc class CLI class Search < AbstractCommand def run - results = self.class.search(*args) - self.class.render_results(*results) + if args.empty? + puts Formatter.columns(CLI.nice_listing(Hbc.all_tokens)) + else + results = self.class.search(*args) + self.class.render_results(*results) + end end def self.extract_regexp(string) @@ -15,8 +19,17 @@ module Hbc end def self.search_remote(query) - matches = GitHub.search_code(user: "caskroom", path: "Casks", - filename: query, extension: "rb") + matches = begin + GitHub.search_code( + user: "caskroom", + path: "Casks", + filename: query, + extension: "rb", + ) + rescue GitHub::Error => error + opoo "Error searching on GitHub: #{error}\n" + [] + end matches.map do |match| tap = Tap.fetch(match["repository"]["full_name"]) next if tap.installed? diff --git a/Library/Homebrew/manpages/brew-cask.1.md b/Library/Homebrew/manpages/brew-cask.1.md index bfb9cd7a53..715d8fd770 100644 --- a/Library/Homebrew/manpages/brew-cask.1.md +++ b/Library/Homebrew/manpages/brew-cask.1.md @@ -1,5 +1,5 @@ brew-cask(1) - a friendly binary installer for macOS -======================================================== +==================================================== ## SYNOPSIS @@ -85,7 +85,7 @@ names, and other aspects of this manual are still subject to change. If is given, summarize the staged files associated with the given Cask. - + * `outdated` [--greedy] [--verbose|--quiet] [ ...]: Without token arguments, display all the installed Casks that have newer versions available in the tap; otherwise check only the tokens given @@ -101,9 +101,10 @@ names, and other aspects of this manual are still subject to change. Reinstall the given Cask. * `search` or `-S` [ | //]: - Without an argument, display all Casks available for install; otherwise - perform a substring search of known Cask tokens for or, if the - text is delimited by slashes (//), it is interpreted as a + Without an argument, display all locally available Casks for install; no + online search is performed. + Otherwise perform a substring search of known Cask tokens for or, + if the text is delimited by slashes (//), it is interpreted as a Ruby regular expression. * `style` [--fix] [ ... ]: @@ -255,7 +256,7 @@ Environment variables specific to Homebrew-Cask: export HOMEBREW_CASK_OPTS='--appdir=~/Applications --fontdir=/Library/Fonts' Other environment variables: - + * `SUDO_ASKPASS`: When this variable is set, Homebrew-Cask will call `sudo`(8) with the `-A` option. diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index e237ad464f..6dc9805903 100644 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ b/Library/Homebrew/test/cask/cli/search_spec.rb @@ -22,16 +22,36 @@ describe Hbc::CLI::Search, :cask do EOS end + it "returns matches even when online search failed" do + allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason")) + expect { + Hbc::CLI::Search.run("local") + }.to output(<<-EOS.undent).to_stdout + local-caffeine + local-transmission + EOS + .and output(/^Warning: Error searching on GitHub: reason/).to_stderr + 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") - }.to output("No Cask found for \"foo-bar-baz\".\n").to_stdout.as_tty + }.to output(<<-EOS.undent).to_stdout.as_tty + No Cask found for "foo-bar-baz". + EOS end - it "lists all available Casks with no search term" do - expect { - Hbc::CLI::Search.run - }.to output(/local-caffeine/).to_stdout.as_tty + it "doesn't output anything to non-TTY stdout when there are no matches" do + expect { Hbc::CLI::Search.run("foo-bar-baz") } + .to not_to_output.to_stdout + .and not_to_output.to_stderr + end + + it "lists all Casks available offline with no search term" do + allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason")) + expect { Hbc::CLI::Search.run } + .to output(/local-caffeine/).to_stdout.as_tty + .and not_to_output.to_stderr end it "ignores hyphens in search terms" do @@ -55,19 +75,29 @@ describe Hbc::CLI::Search, :cask do 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.as_tty + }.to output(<<-EOS.undent).to_stdout.as_tty + ==> Regexp Matches + local-caffeine + EOS end - it "Returns both exact and partial matches" do + 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.as_tty + }.to output(<<-EOS.undent).to_stdout.as_tty + ==> Exact Match + test-opera + ==> Partial Matches + test-opera-mail + EOS 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.as_tty + }.to output(<<-EOS.undent).to_stdout.as_tty + No Cask found for "caskroom". + EOS end it "doesn't highlight packages that aren't installed" do diff --git a/manpages/brew-cask.1 b/manpages/brew-cask.1 index 763d78ebe6..f8f58a123b 100644 --- a/manpages/brew-cask.1 +++ b/manpages/brew-cask.1 @@ -103,7 +103,7 @@ Reinstall the given Cask\. . .TP \fBsearch\fR or \fB\-S\fR [\fItext\fR | /\fIregexp\fR/] -Without an argument, display all Casks available for install; otherwise perform a substring search of known Cask tokens for \fItext\fR or, if the text is delimited by slashes (/\fIregexp\fR/), it is interpreted as a Ruby regular expression\. +Without an argument, display all locally available Casks for install; no online search is performed\. Otherwise perform a substring search of known Cask tokens for \fItext\fR or, if the text is delimited by slashes (/\fIregexp\fR/), it is interpreted as a Ruby regular expression\. . .TP \fBstyle\fR [\-\-fix] [ \fItoken\fR \.\.\. ]