tap: avoid class vars

Avoiding them also allows us to write proper tests.
This commit is contained in:
Carlo Cabrera 2024-05-06 15:05:06 +01:00
parent 34387bfc8a
commit 078a328e8e
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 24 additions and 11 deletions

View File

@ -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

View File

@ -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