diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index b386eaa985..f66261d14b 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -132,11 +132,10 @@ class Tap false end - # rubocop:disable Style/ClassVars - # We want the the class variables below to be global to all `Tap` classes and subclasses. sig { returns(T::Set[Tap]) } def self.allowed_taps - @@allowed_taps ||= begin + cache_key = "allowed_taps_#{Homebrew::EnvConfig.allowed_taps.to_s.gsub(" ", "_")}".to_sym + cache[cache_key] ||= begin allowed_tap_list = Homebrew::EnvConfig.allowed_taps.to_s.split Set.new(allowed_tap_list.filter_map do |tap| @@ -144,15 +143,14 @@ class Tap rescue Tap::InvalidNameError opoo "Invalid tap name in `HOMEBREW_ALLOWED_TAPS`: #{tap}" nil - end) + end).freeze end - - @@allowed_taps.freeze end sig { returns(T::Set[Tap]) } def self.forbidden_taps - @@forbidden_taps ||= begin + cache_key = "forbidden_taps_#{Homebrew::EnvConfig.forbidden_taps.to_s.gsub(" ", "_")}".to_sym + cache[cache_key] ||= begin forbidden_tap_list = Homebrew::EnvConfig.forbidden_taps.to_s.split Set.new(forbidden_tap_list.filter_map do |tap| @@ -160,12 +158,9 @@ class Tap rescue Tap::InvalidNameError opoo "Invalid tap name in `HOMEBREW_FORBIDDEN_TAPS`: #{tap}" nil - end) + end).freeze end - - @@forbidden_taps.freeze end - # rubocop:enable Style/ClassVars # @api public extend Enumerable diff --git a/Library/Homebrew/test/tap_spec.rb b/Library/Homebrew/test/tap_spec.rb index a8cafb6799..940ac944f9 100644 --- a/Library/Homebrew/test/tap_spec.rb +++ b/Library/Homebrew/test/tap_spec.rb @@ -142,6 +142,24 @@ RSpec.describe Tap do end end + describe "::allowed_taps" do + before { allow(Homebrew::EnvConfig).to receive(:allowed_taps).and_return("homebrew/allowed") } + + it "returns a set of allowed taps according to the environment" do + expect(described_class.allowed_taps) + .to contain_exactly(described_class.fetch("homebrew/allowed")) + end + end + + describe "::forbidden_taps" do + before { allow(Homebrew::EnvConfig).to receive(:forbidden_taps).and_return("homebrew/forbidden") } + + it "returns a set of forbidden taps according to the environment" do + expect(described_class.forbidden_taps) + .to contain_exactly(described_class.fetch("homebrew/forbidden")) + end + end + specify "::names" do expect(described_class.names.sort).to eq(["homebrew/core", "homebrew/foo"]) end