Merge pull request #16746 from apainintheneck/simplify-cache-clearing-in-tests

cachable: make sure to clear caches between tests
This commit is contained in:
Mike McQuaid 2024-02-27 08:35:38 +00:00 committed by GitHub
commit ca9405de5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 284 additions and 253 deletions

View File

@ -10,18 +10,17 @@ module Homebrew
# #
# @api private # @api private
module Cask module Cask
class << self extend Cachable
include Cachable
private :cache private_class_method :cache
sig { params(token: String).returns(Hash) } sig { params(token: String).returns(Hash) }
def fetch(token) def self.fetch(token)
Homebrew::API.fetch "cask/#{token}.json" Homebrew::API.fetch "cask/#{token}.json"
end end
sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) } sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) }
def source_download(cask) def self.source_download(cask)
path = cask.ruby_source_path.to_s || "Casks/#{cask.token}.rb" path = cask.ruby_source_path.to_s || "Casks/#{cask.token}.rb"
sha256 = cask.ruby_source_checksum[:sha256] sha256 = cask.ruby_source_checksum[:sha256]
checksum = Checksum.new(sha256) if sha256 checksum = Checksum.new(sha256) if sha256
@ -42,7 +41,7 @@ module Homebrew
end end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def download_and_cache_data! def self.download_and_cache_data!
json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json" json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json"
cache["renames"] = {} cache["renames"] = {}
@ -58,10 +57,10 @@ module Homebrew
updated updated
end end
private :download_and_cache_data! private_class_method :download_and_cache_data!
sig { returns(T::Hash[String, Hash]) } sig { returns(T::Hash[String, Hash]) }
def all_casks def self.all_casks
unless cache.key?("casks") unless cache.key?("casks")
json_updated = download_and_cache_data! json_updated = download_and_cache_data!
write_names(regenerate: json_updated) write_names(regenerate: json_updated)
@ -71,7 +70,7 @@ module Homebrew
end end
sig { returns(T::Hash[String, String]) } sig { returns(T::Hash[String, String]) }
def all_renames def self.all_renames
unless cache.key?("renames") unless cache.key?("renames")
json_updated = download_and_cache_data! json_updated = download_and_cache_data!
write_names(regenerate: json_updated) write_names(regenerate: json_updated)
@ -81,12 +80,11 @@ module Homebrew
end end
sig { params(regenerate: T::Boolean).void } sig { params(regenerate: T::Boolean).void }
def write_names(regenerate: false) def self.write_names(regenerate: false)
download_and_cache_data! unless cache.key?("casks") download_and_cache_data! unless cache.key?("casks")
Homebrew::API.write_names_file(all_casks.keys, "cask", regenerate: regenerate) Homebrew::API.write_names_file(all_casks.keys, "cask", regenerate: regenerate)
end end
end end
end end
end
end end

View File

@ -10,18 +10,17 @@ module Homebrew
# #
# @api private # @api private
module Formula module Formula
class << self extend Cachable
include Cachable
private :cache private_class_method :cache
sig { params(name: String).returns(Hash) } sig { params(name: String).returns(Hash) }
def fetch(name) def self.fetch(name)
Homebrew::API.fetch "formula/#{name}.json" Homebrew::API.fetch "formula/#{name}.json"
end end
sig { params(formula: ::Formula).returns(::Formula) } sig { params(formula: ::Formula).returns(::Formula) }
def source_download(formula) def self.source_download(formula)
path = formula.ruby_source_path || "Formula/#{formula.name}.rb" path = formula.ruby_source_path || "Formula/#{formula.name}.rb"
git_head = formula.tap_git_head || "HEAD" git_head = formula.tap_git_head || "HEAD"
tap = formula.tap&.full_name || "Homebrew/homebrew-core" tap = formula.tap&.full_name || "Homebrew/homebrew-core"
@ -39,7 +38,7 @@ module Homebrew
end end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def download_and_cache_data! def self.download_and_cache_data!
json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json" json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json"
cache["aliases"] = {} cache["aliases"] = {}
@ -57,10 +56,10 @@ module Homebrew
updated updated
end end
private :download_and_cache_data! private_class_method :download_and_cache_data!
sig { returns(T::Hash[String, Hash]) } sig { returns(T::Hash[String, Hash]) }
def all_formulae def self.all_formulae
unless cache.key?("formulae") unless cache.key?("formulae")
json_updated = download_and_cache_data! json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated) write_names_and_aliases(regenerate: json_updated)
@ -70,7 +69,7 @@ module Homebrew
end end
sig { returns(T::Hash[String, String]) } sig { returns(T::Hash[String, String]) }
def all_aliases def self.all_aliases
unless cache.key?("aliases") unless cache.key?("aliases")
json_updated = download_and_cache_data! json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated) write_names_and_aliases(regenerate: json_updated)
@ -80,7 +79,7 @@ module Homebrew
end end
sig { returns(T::Hash[String, String]) } sig { returns(T::Hash[String, String]) }
def all_renames def self.all_renames
unless cache.key?("renames") unless cache.key?("renames")
json_updated = download_and_cache_data! json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated) write_names_and_aliases(regenerate: json_updated)
@ -90,7 +89,7 @@ module Homebrew
end end
sig { params(regenerate: T::Boolean).void } sig { params(regenerate: T::Boolean).void }
def write_names_and_aliases(regenerate: false) def self.write_names_and_aliases(regenerate: false)
download_and_cache_data! unless cache.key?("formulae") download_and_cache_data! unless cache.key?("formulae")
return unless Homebrew::API.write_names_file(all_formulae.keys, "formula", regenerate: regenerate) return unless Homebrew::API.write_names_file(all_formulae.keys, "formula", regenerate: regenerate)
@ -103,5 +102,4 @@ module Homebrew
end end
end end
end end
end
end end

View File

@ -9,17 +9,16 @@ require "system_command"
# #
# @api private # @api private
module Readall module Readall
class << self extend Cachable
include Cachable extend SystemCommand::Mixin
include SystemCommand::Mixin
# TODO: remove this once the `MacOS` module is undefined on Linux # TODO: remove this once the `MacOS` module is undefined on Linux
MACOS_MODULE_REGEX = /\b(MacOS|OS::Mac)(\.|::)\b/ MACOS_MODULE_REGEX = /\b(MacOS|OS::Mac)(\.|::)\b/
private_constant :MACOS_MODULE_REGEX private_constant :MACOS_MODULE_REGEX
private :cache private_class_method :cache
def valid_ruby_syntax?(ruby_files) def self.valid_ruby_syntax?(ruby_files)
failed = T.let(false, T::Boolean) failed = T.let(false, T::Boolean)
ruby_files.each do |ruby_file| ruby_files.each do |ruby_file|
# As a side effect, print syntax errors/warnings to `$stderr`. # As a side effect, print syntax errors/warnings to `$stderr`.
@ -28,7 +27,7 @@ module Readall
!failed !failed
end end
def valid_aliases?(alias_dir, formula_dir) def self.valid_aliases?(alias_dir, formula_dir)
return true unless alias_dir.directory? return true unless alias_dir.directory?
failed = T.let(false, T::Boolean) failed = T.let(false, T::Boolean)
@ -49,7 +48,7 @@ module Readall
!failed !failed
end end
def valid_formulae?(tap, bottle_tag: nil) def self.valid_formulae?(tap, bottle_tag: nil)
cache[:valid_formulae] ||= {} cache[:valid_formulae] ||= {}
success = T.let(true, T::Boolean) success = T.let(true, T::Boolean)
@ -82,11 +81,12 @@ module Readall
success success
end end
def valid_casks?(_tap, os_name: nil, arch: nil) def self.valid_casks?(_tap, os_name: nil, arch: nil)
true true
end end
def valid_tap?(tap, aliases: false, no_simulate: false, os_arch_combinations: OnSystem::ALL_OS_ARCH_COMBINATIONS) def self.valid_tap?(tap, aliases: false, no_simulate: false,
os_arch_combinations: OnSystem::ALL_OS_ARCH_COMBINATIONS)
success = true success = true
if aliases if aliases
@ -112,9 +112,7 @@ module Readall
success success
end end
private private_class_method def self.syntax_errors_or_warnings?(filename)
def syntax_errors_or_warnings?(filename)
# Retrieve messages about syntax errors/warnings printed to `$stderr`. # Retrieve messages about syntax errors/warnings printed to `$stderr`.
_, err, status = system_command(RUBY_PATH, args: ["-c", "-w", filename], print_stderr: false) _, err, status = system_command(RUBY_PATH, args: ["-c", "-w", filename], print_stderr: false)
@ -130,7 +128,6 @@ module Readall
# warnings we also need to inspect the output to `$stderr`. # warnings we also need to inspect the output to `$stderr`.
!status.success? || !messages.chomp.empty? !status.success? || !messages.chomp.empty?
end end
end
end end
require "extend/os/readall" require "extend/os/readall"

View File

@ -35,6 +35,8 @@ require "timeout"
$LOAD_PATH.unshift(File.expand_path("#{ENV.fetch("HOMEBREW_LIBRARY")}/Homebrew/test/support/lib")) $LOAD_PATH.unshift(File.expand_path("#{ENV.fetch("HOMEBREW_LIBRARY")}/Homebrew/test/support/lib"))
require_relative "support/extend/cachable"
require_relative "../global" require_relative "../global"
require "test/support/quiet_progress_formatter" require "test/support/quiet_progress_formatter"
@ -202,16 +204,7 @@ RSpec.configure do |config|
config.around do |example| config.around do |example|
Homebrew.raise_deprecation_exceptions = true Homebrew.raise_deprecation_exceptions = true
Formulary.clear_cache Cachable::Registry.clear_all_caches
Tap.each(&:clear_cache)
Tap.clear_cache
DependencyCollector.clear_cache
Formula.clear_cache
Keg.clear_cache
Tab.clear_cache
Dependency.clear_cache
Requirement.clear_cache
Readall.clear_cache if defined?(Readall)
FormulaInstaller.clear_attempted FormulaInstaller.clear_attempted
FormulaInstaller.clear_installed FormulaInstaller.clear_installed
FormulaInstaller.clear_fetched FormulaInstaller.clear_fetched
@ -263,15 +256,7 @@ RSpec.configure do |config|
@__stderr.close @__stderr.close
@__stdin.close @__stdin.close
Formulary.clear_cache Cachable::Registry.clear_all_caches
Tap.clear_cache
DependencyCollector.clear_cache
Formula.clear_cache
Keg.clear_cache
Tab.clear_cache
Dependency.clear_cache
Requirement.clear_cache
Readall.clear_cache if defined?(Readall)
FileUtils.rm_rf [ FileUtils.rm_rf [
*TEST_DIRECTORIES, *TEST_DIRECTORIES,

View File

@ -0,0 +1,53 @@
# frozen_string_literal: true
raise "This needs to be required before Cachable gets loaded normally." if defined?(Cachable)
# Collect all classes that mix in Cachable so that those caches can be cleared in-between tests.
module Cachable
private_class_method def self.included(klass)
# It's difficult to backtrack from a singleton class to find the original class
# and you can always just extend this module instead for equivalent behavior.
raise ArgumentError, "Don't use Cachable with singleton classes" if klass.singleton_class?
super if defined?(super)
end
private_class_method def self.extended(klass)
Registry.class_list << klass
# Ignore the `Formula` class that gets inherited from a lot and
# that has caches that we don't need to clear on the class level.
klass.extend(Inherited) if klass.name != "Formula"
super if defined?(super)
end
module Inherited
private
def inherited(klass)
# A class might inherit Cachable at the instance level
# and in that case we just want to skip registering it.
Registry.class_list << klass if klass.respond_to?(:clear_cache)
super if defined?(super)
end
end
module Registry
# A list of all classes that have been loaded into memory that mixin or
# inherit `Cachable` at the class or module level.
#
# Note: Classes that inherit from `Formula` are excluded since it's not
# necessary to track and clear individual formula caches.
def self.class_list
@class_list ||= []
end
# Clear the cache of every class or module that mixes in or inherits
# `Cachable` at the class or module level.
#
# Note: Classes that inherit from `Formula` are excluded since it's not
# necessary to track and clear individual formula caches.
def self.clear_all_caches
class_list.each(&:clear_cache)
end
end
end