From 226239da4c030ecea0803a844ce14d6ba09ba246 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sun, 31 Mar 2024 18:38:03 -0700 Subject: [PATCH] tests: remove unnecessary cache clearing This PR removes all remaining unnecessary cache clearing in tests from the codebase since we now clear all cachable classes between tests making this functionally unnecessary. Original PR to automatically clear caches: - https://github.com/Homebrew/brew/pull/16746 I also moved the `Utils::Analytics` module to use cachable so that we don't have to clear caches specifically in tests anymore. --- Library/Homebrew/test/api/cask_spec.rb | 2 -- Library/Homebrew/test/api/formula_spec.rb | 1 - .../test/api/internal_tap_json/formula_spec.rb | 6 ------ Library/Homebrew/test/api_spec.rb | 4 ---- Library/Homebrew/test/formulary_spec.rb | 3 --- Library/Homebrew/test/utils/analytics_spec.rb | 4 ---- Library/Homebrew/utils/analytics.rb | 11 ++++------- 7 files changed, 4 insertions(+), 27 deletions(-) diff --git a/Library/Homebrew/test/api/cask_spec.rb b/Library/Homebrew/test/api/cask_spec.rb index a3d787993c..932897dbe8 100644 --- a/Library/Homebrew/test/api/cask_spec.rb +++ b/Library/Homebrew/test/api/cask_spec.rb @@ -7,8 +7,6 @@ RSpec.describe Homebrew::API::Cask do before do stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir) - Homebrew::API.clear_cache - described_class.clear_cache end def mock_curl_download(stdout:) diff --git a/Library/Homebrew/test/api/formula_spec.rb b/Library/Homebrew/test/api/formula_spec.rb index 49a3101064..8b89d1916b 100644 --- a/Library/Homebrew/test/api/formula_spec.rb +++ b/Library/Homebrew/test/api/formula_spec.rb @@ -7,7 +7,6 @@ RSpec.describe Homebrew::API::Formula do before do stub_const("Homebrew::API::HOMEBREW_CACHE_API", cache_dir) - described_class.clear_cache end def mock_curl_download(stdout:) diff --git a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb index cc03354e23..a6347e1806 100644 --- a/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb +++ b/Library/Homebrew/test/api/internal_tap_json/formula_spec.rb @@ -44,12 +44,6 @@ RSpec.describe "Internal Tap JSON -- Formula" do # To allow `formula_names.txt` to be written to the cache. (HOMEBREW_CACHE/"api").mkdir - - Homebrew::API::Formula.clear_cache - end - - after do - Homebrew::API::Formula.clear_cache end it "loads tap aliases" do diff --git a/Library/Homebrew/test/api_spec.rb b/Library/Homebrew/test/api_spec.rb index 585c91f0ee..9186fc625c 100644 --- a/Library/Homebrew/test/api_spec.rb +++ b/Library/Homebrew/test/api_spec.rb @@ -8,10 +8,6 @@ RSpec.describe Homebrew::API do let(:json_hash) { JSON.parse(json) } let(:json_invalid) { '{"foo":"bar"' } - before do - described_class.clear_cache - end - def mock_curl_output(stdout: "", success: true) curl_output = instance_double(SystemCommand::Result, stdout:, success?: success) allow(Utils::Curl).to receive(:curl_output).and_return curl_output diff --git a/Library/Homebrew/test/formulary_spec.rb b/Library/Homebrew/test/formulary_spec.rb index 3bb7491bdf..6b53acf40c 100644 --- a/Library/Homebrew/test/formulary_spec.rb +++ b/Library/Homebrew/test/formulary_spec.rb @@ -81,8 +81,6 @@ RSpec.describe Formulary do end context "with sharded Formula directory" do - before { CoreTap.instance.clear_cache } - let(:formula_name) { "testball_sharded" } let(:formula_path) do core_tap = CoreTap.instance @@ -236,7 +234,6 @@ RSpec.describe Formulary do before do alias_dir.mkpath FileUtils.ln_s formula_path, alias_path - tap.clear_cache end it "returns a Formula when given a name" do diff --git a/Library/Homebrew/test/utils/analytics_spec.rb b/Library/Homebrew/test/utils/analytics_spec.rb index baaebc895b..5caaf2fa70 100644 --- a/Library/Homebrew/test/utils/analytics_spec.rb +++ b/Library/Homebrew/test/utils/analytics_spec.rb @@ -4,10 +4,6 @@ require "utils/analytics" require "formula_installer" RSpec.describe Utils::Analytics do - before do - described_class.clear_cache - end - describe "::default_package_tags" do let(:ci) { ", CI" if ENV["CI"] } diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb index c09fd7f2fe..51bdebaa45 100644 --- a/Library/Homebrew/utils/analytics.rb +++ b/Library/Homebrew/utils/analytics.rb @@ -16,6 +16,8 @@ module Utils INFLUX_HOST = "https://eu-central-1-1.aws.cloud2.influxdata.com" INFLUX_ORG = "d81a3e6d582d485f" + extend Cachable + class << self include Context @@ -277,14 +279,9 @@ module Utils nil end - def clear_cache - remove_instance_variable(:@default_package_tags) if instance_variable_defined?(:@default_package_tags) - remove_instance_variable(:@default_package_fields) if instance_variable_defined?(:@default_package_fields) - end - sig { returns(T::Hash[Symbol, String]) } def default_package_tags - @default_package_tags ||= begin + cache[:default_package_tags] ||= begin # Only display default prefixes to reduce cardinality and improve privacy prefix = Homebrew.default_prefix? ? HOMEBREW_PREFIX.to_s : "custom-prefix" @@ -305,7 +302,7 @@ module Utils # remove macOS patch release sig { returns(T::Hash[Symbol, String]) } def default_package_fields - @default_package_fields ||= begin + cache[:default_package_fields] ||= begin version = if (match_data = HOMEBREW_VERSION.match(/^[\d.]+/)) suffix = "-dev" if HOMEBREW_VERSION.include?("-") match_data[0] + suffix.to_s