Code review changes

This commit is contained in:
Douglas Eichelberger 2023-03-27 09:28:27 -07:00
parent eaeceda31e
commit a7b80532bf
2 changed files with 121 additions and 122 deletions

View File

@ -15,7 +15,6 @@ module Homebrew
CLEANUP_DEFAULT_DAYS = Homebrew::EnvConfig.cleanup_periodic_full_days.to_i.freeze CLEANUP_DEFAULT_DAYS = Homebrew::EnvConfig.cleanup_periodic_full_days.to_i.freeze
private_constant :CLEANUP_DEFAULT_DAYS private_constant :CLEANUP_DEFAULT_DAYS
module PathnameUtils
class << self class << self
extend T::Sig extend T::Sig
@ -139,7 +138,6 @@ module Homebrew
false false
end end
end end
end
extend Predicable extend Predicable
@ -192,9 +190,10 @@ module Homebrew
end end
def self.skip_clean_formula?(formula) def self.skip_clean_formula?(formula)
return false if Homebrew::EnvConfig.no_cleanup_formulae.blank? no_cleanup_formula = Homebrew::EnvConfig.no_cleanup_formulae
return false if no_cleanup_formula.blank?
@skip_clean_formulae ||= T.must(Homebrew::EnvConfig.no_cleanup_formulae).split(",") @skip_clean_formulae ||= no_cleanup_formula.split(",")
@skip_clean_formulae.include?(formula.name) || (@skip_clean_formulae & formula.aliases).present? @skip_clean_formulae.include?(formula.name) || (@skip_clean_formulae & formula.aliases).present?
end end
@ -315,7 +314,7 @@ module Homebrew
logs_days = [days, CLEANUP_DEFAULT_DAYS].min logs_days = [days, CLEANUP_DEFAULT_DAYS].min
HOMEBREW_LOGS.subdirs.each do |dir| HOMEBREW_LOGS.subdirs.each do |dir|
cleanup_path(dir) { dir.rmtree } if PathnameUtils.prune?(dir, logs_days) cleanup_path(dir) { dir.rmtree } if self.class.prune?(dir, logs_days)
end end
end end
@ -331,7 +330,7 @@ module Homebrew
.map(&:resolved_path) .map(&:resolved_path)
(downloads - referenced_downloads).each do |download| (downloads - referenced_downloads).each do |download|
if PathnameUtils.incomplete?(download) if self.class.incomplete?(download)
begin begin
LockFile.new(download.basename).with_lock do LockFile.new(download.basename).with_lock do
download.unlink download.unlink
@ -354,11 +353,11 @@ module Homebrew
entries.each do |path| entries.each do |path|
next if path == PERIODIC_CLEAN_FILE next if path == PERIODIC_CLEAN_FILE
FileUtils.chmod_R 0755, path if PathnameUtils.go_cache_directory?(path) && !dry_run? FileUtils.chmod_R 0755, path if self.class.go_cache_directory?(path) && !dry_run?
next cleanup_path(path) { path.unlink } if PathnameUtils.incomplete?(path) next cleanup_path(path) { path.unlink } if self.class.incomplete?(path)
next cleanup_path(path) { FileUtils.rm_rf path } if PathnameUtils.nested_cache?(path) next cleanup_path(path) { FileUtils.rm_rf path } if self.class.nested_cache?(path)
if PathnameUtils.prune?(path, days) if self.class.prune?(path, days)
if path.file? || path.symlink? if path.file? || path.symlink?
cleanup_path(path) { path.unlink } cleanup_path(path) { path.unlink }
elsif path.directory? && path.to_s.include?("--") elsif path.directory? && path.to_s.include?("--")
@ -368,7 +367,7 @@ module Homebrew
end end
# If we've specified --prune don't do the (expensive) .stale? check. # If we've specified --prune don't do the (expensive) .stale? check.
cleanup_path(path) { path.unlink } if !prune? && PathnameUtils.stale?(path, scrub: scrub?) cleanup_path(path) { path.unlink } if !prune? && self.class.stale?(path, scrub: scrub?)
end end
cleanup_unreferenced_downloads cleanup_unreferenced_downloads
@ -489,7 +488,7 @@ module Homebrew
if child.basename.to_s == "__pycache__" if child.basename.to_s == "__pycache__"
child.find do |path| child.find do |path|
next unless path.extname == ".pyc" next unless path.extname == ".pyc"
next unless PathnameUtils.prune?(path, days) next unless self.class.prune?(path, days)
unused_pyc_files << path unused_pyc_files << path
end end

View File

@ -25,7 +25,7 @@ describe Homebrew::Cleanup do
FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew" FileUtils.rm_rf HOMEBREW_LIBRARY/"Homebrew"
end end
describe "::PathnameUtils.prune?" do describe "::prune?" do
subject(:path) { HOMEBREW_CACHE/"foo" } subject(:path) { HOMEBREW_CACHE/"foo" }
before do before do
@ -35,11 +35,11 @@ describe Homebrew::Cleanup do
it "returns true when ctime and mtime < days_default" do it "returns true when ctime and mtime < days_default" do
allow_any_instance_of(Pathname).to receive(:ctime).and_return((DateTime.now - 2).to_time) allow_any_instance_of(Pathname).to receive(:ctime).and_return((DateTime.now - 2).to_time)
allow_any_instance_of(Pathname).to receive(:mtime).and_return((DateTime.now - 2).to_time) allow_any_instance_of(Pathname).to receive(:mtime).and_return((DateTime.now - 2).to_time)
expect(described_class::PathnameUtils.prune?(path, 1)).to be true expect(described_class.prune?(path, 1)).to be true
end end
it "returns false when ctime and mtime >= days_default" do it "returns false when ctime and mtime >= days_default" do
expect(described_class::PathnameUtils.prune?(path, 2)).to be false expect(described_class.prune?(path, 2)).to be false
end end
end end