Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API for description searches #17582

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run
end

if search_type.present?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all? && Homebrew::EnvConfig.no_install_from_api?
raise UsageError, "`brew desc --search` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!"
end

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def run
string_or_regex = Search.query_regexp(query)

if args.desc?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all? && Homebrew::EnvConfig.no_install_from_api?
raise UsageError, "`brew search --desc` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!"
end

Expand Down
6 changes: 4 additions & 2 deletions Library/Homebrew/descriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
# Helper class for printing and searching descriptions.
class Descriptions
# Given a regex, find all formulae whose specified fields contain a match.
def self.search(string_or_regex, field, cache_store, eval_all = Homebrew::EnvConfig.eval_all?)
cache_store.populate_if_empty!(eval_all:)
def self.search(string_or_regex, field, cache_store,
eval_all = Homebrew::EnvConfig.eval_all?, cache_store_hash: false)

cache_store.populate_if_empty!(eval_all:) unless cache_store_hash

results = case field
when :name
Expand Down
32 changes: 26 additions & 6 deletions Library/Homebrew/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,39 @@ def self.search_descriptions(string_or_regex, args, search_type: :desc)

if args.formula? || both
ohai "Formulae"
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
if eval_all
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
else
unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.formula_files.size }
if unofficial.positive?
opoo "Use `--eval-all` to search #{unofficial} additional " \
"#{Utils.pluralize("formula", unofficial, plural: "e")} in third party taps."
end
descriptions = Homebrew::API::Formula.all_formulae.transform_values { |data| data["desc"] }
rrotter marked this conversation as resolved.
Show resolved Hide resolved
Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
end
end
return if !args.cask? && !both

puts if both

ohai "Casks"
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
if eval_all
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
else
unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.cask_files.size }
if unofficial.positive?
opoo "Use `--eval-all` to search #{unofficial} additional " \
"#{Utils.pluralize("cask", unofficial)} in third party taps."
end
descriptions = Homebrew::API::Cask.all_casks.transform_values { |c| [c["name"].join(", "), c["desc"]] }
Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
end
end

Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/test/cmd/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@
.to output(/testball: Some test/).to_stdout
.and not_to_output.to_stderr
end

it "successfully searches without --eval-all, with API", :integration_test do
setup_test_formula "testball"

expect { brew "desc", "--search", "testball", "HOMEBREW_NO_INSTALL_FROM_API" => nil }
.to be_a_success
end
end
32 changes: 32 additions & 0 deletions Library/Homebrew/test/search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "search"
require "descriptions"
require "cmd/desc"

RSpec.describe Homebrew::Search do
describe "#query_regexp" do
Expand Down Expand Up @@ -57,4 +59,34 @@
end
end
end

describe "#search_descriptions" do
let(:args) { Homebrew::Cmd::Desc.new(["min_arg_placeholder"]).args }

context "with api" do
let(:api_formulae) do
{ "testball" => { "desc" => "Some test" } }
end

let(:api_casks) do
{ "testball" => { "desc" => "Some test", "name" => ["Test Ball"] } }
end

before do
allow(Homebrew::API::Formula).to receive(:all_formulae).and_return(api_formulae)
allow(Homebrew::API::Cask).to receive(:all_casks).and_return(api_casks)
end

it "searches formula descriptions" do
expect { described_class.search_descriptions(described_class.query_regexp("some"), args) }
.to output(/testball: Some test/).to_stdout
end

it "searches cask descriptions", :needs_macos do
expect { described_class.search_descriptions(described_class.query_regexp("ball"), args) }
.to output(/testball: \(Test Ball\) Some test/).to_stdout
.and not_to_output(/testball: Some test/).to_stdout
end
end
end
end