Only allow mapping with environment variable

Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
Rylan Polster 2021-06-16 10:27:15 -04:00
parent 618c41926b
commit 8b04bcb994
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
2 changed files with 28 additions and 14 deletions

View File

@ -395,7 +395,11 @@ module Formulary
) )
raise ArgumentError, "Formulae must have a ref!" unless ref raise ArgumentError, "Formulae must have a ref!" unless ref
ref = @name_mappings[ref] if @name_mappings.present? && @name_mappings.key?(ref) if ENV["HOMEBREW_BOTTLE_JSON"].present? &&
@formula_name_local_bottle_path_map.present? &&
@formula_name_local_bottle_path_map.key?(ref)
ref = @formula_name_local_bottle_path_map[ref]
end
cache_key = "#{ref}-#{spec}-#{alias_path}-#{from}" cache_key = "#{ref}-#{spec}-#{alias_path}-#{from}"
if factory_cached? && cache[:formulary_factory] && if factory_cached? && cache[:formulary_factory] &&
@ -413,18 +417,22 @@ module Formulary
formula formula
end end
# Map a formula name to a bottle archive. This mapping will be used by {Formulary::factory} # Map a formula name to a local/fetched bottle archive. This mapping will be used by {Formulary::factory}
# to allow formulae to be loaded automatically from their bottle archive without # to allow formulae to be loaded automatically from their local bottle archive without
# needing to exist in a tap or be passed as a complete filepath. For example, # needing to exist in a tap or be passed as a complete path. For example,
# to map `foo` to the `hello` formula from its bottle archive: # to map `hello` from its bottle archive:
# <pre>Formulary.map_name_to_bottle "foo", HOMEBREW_CACHE/"hello--2.10" # <pre>Formulary.map_formula_name_to_local_bottle_path "hello", HOMEBREW_CACHE/"hello--2.10"
# Formulary.factory "foo" # returns the hello formula from the bottle archive # Formulary.factory "hello" # returns the hello formula from the local bottle archive
# </pre> # </pre>
# @param name the string to map. # @param formula_name the formula name string to map.
# @param bottle a path pointing to the target bottle archive. # @param local_bottle_path a path pointing to the target bottle archive.
def self.map_name_to_bottle(name, bottle) def self.map_formula_name_to_local_bottle_path(formula_name, local_bottle_path)
@name_mappings ||= {} if ENV["HOMEBREW_BOTTLE_JSON"].blank?
@name_mappings[name] = Pathname(bottle).realpath raise UsageError, "HOMEBREW_BOTTLE_JSON not set but required for #{__method__}!"
end
@formula_name_local_bottle_path_map ||= {}
@formula_name_local_bottle_path_map[formula_name] = Pathname(local_bottle_path).realpath
end end
# Return a {Formula} instance for the given rack. # Return a {Formula} instance for the given rack.

View File

@ -205,7 +205,7 @@ describe Formulary do
end end
end end
describe "::map_name_to_bottle" do describe "::map_formula_name_to_local_bottle_path" do
before do before do
formula_path.dirname.mkpath formula_path.dirname.mkpath
formula_path.write formula_content formula_path.write formula_content
@ -216,7 +216,13 @@ describe Formulary do
described_class.factory("formula-to-map") described_class.factory("formula-to-map")
}.to raise_error(FormulaUnavailableError) }.to raise_error(FormulaUnavailableError)
described_class.map_name_to_bottle "formula-to-map", formula_path ENV["HOMEBREW_BOTTLE_JSON"] = nil
expect {
described_class.map_formula_name_to_local_bottle_path "formula-to-map", formula_path
}.to raise_error(UsageError, /HOMEBREW_BOTTLE_JSON not set/)
ENV["HOMEBREW_BOTTLE_JSON"] = "1"
described_class.map_formula_name_to_local_bottle_path "formula-to-map", formula_path
expect(described_class.factory("formula-to-map")).to be_kind_of(Formula) expect(described_class.factory("formula-to-map")).to be_kind_of(Formula)
end end