Refactor search_taps.

This commit is contained in:
Markus Reiter 2018-06-06 13:24:55 +02:00
parent 845cb99e29
commit cc93997fb7
5 changed files with 33 additions and 26 deletions

View File

@ -36,7 +36,7 @@ module Hbc
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
end
_, remote_matches = search_taps(search_term, silent: true)
remote_matches = search_taps(search_term, silent: true)[:casks]
[partial_matches, remote_matches, search_term]
end

View File

@ -281,7 +281,7 @@ module Homebrew
# Do not search taps if the formula name is qualified
return if e.name.include?("/")
ohai "Searching taps..."
taps_search_results, = search_taps(e.name)
taps_search_results = search_taps(e.name)[:formulae]
case taps_search_results.length
when 0
ofail "No formulae found in taps."

View File

@ -61,12 +61,12 @@ module Homebrew
elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX
query = args.remaining.first
begin
result = Formulary.factory(query).name
results = Array(result)
results = begin
[Formulary.factory(query).name]
rescue FormulaUnavailableError
_, _, name = query.split("/", 3)
results = search_taps(name).flatten.sort
remote_results = search_taps(name)
[*remote_results[:formulae], *remote_results[:casks]].sort
end
puts Formatter.columns(results) unless results.empty?
@ -76,7 +76,8 @@ module Homebrew
local_results = search_formulae(string_or_regex)
puts Formatter.columns(local_results.sort) unless local_results.empty?
tap_results = search_taps(query).flatten.sort
remote_results = search_taps(query)
tap_results = [*remote_results[:formulae], *remote_results[:casks]].sort
puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty?

View File

@ -13,36 +13,42 @@ module Homebrew
end
def search_taps(query, silent: false)
return [], [] if ENV["HOMEBREW_NO_GITHUB_API"]
results = { formulae: [], casks: [] }
return results if ENV["HOMEBREW_NO_GITHUB_API"]
unless silent
# Use stderr to avoid breaking parsed output
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
end
matches = GitHub.search_code(
matches = begin
GitHub.search_code(
user: "Homebrew",
path: ["Formula", "Casks", "."],
filename: query,
extension: "rb",
)
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n"
return results
end
matches.inject([[], []]) do |(formulae, casks), match|
matches.each do |match|
name = File.basename(match["path"], ".rb")
tap = Tap.fetch(match["repository"]["full_name"])
full_name = "#{tap.name}/#{name}"
if tap.installed? && !match["path"].start_with?("Casks/")
[formulae, casks]
elsif match["path"].start_with?("Casks/")
[formulae, [*casks, full_name].sort]
next if tap.installed? && !match["path"].start_with?("Casks/")
if match["path"].start_with?("Casks/")
results[:casks] = [*results[:casks], full_name].sort
else
[[*formulae, full_name].sort, casks]
results[:formulae] = [*results[:formulae], full_name].sort
end
end
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n"
[[], []]
results
end
def search_formulae(string_or_regex)

View File

@ -14,14 +14,14 @@ describe Homebrew::Search do
it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
expect(mod.search_taps("some-formula")).to match([[], []])
expect(mod.search_taps("some-formula")).to match(formulae: [], casks: [])
end
it "does not raise if the network fails" do
allow(GitHub).to receive(:open_api).and_raise(GitHub::Error)
expect(mod.search_taps("some-formula"))
.to match([[], []])
.to match(formulae: [], casks: [])
end
it "returns Formulae and Casks separately" do
@ -45,7 +45,7 @@ describe Homebrew::Search do
allow(GitHub).to receive(:open_api).and_yield(json_response)
expect(mod.search_taps("some-formula"))
.to match([["homebrew/foo/some-formula"], ["homebrew/bar/some-cask"]])
.to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"])
end
end