From f627c6331dd1acde979a0b7a546020838aa8ea08 Mon Sep 17 00:00:00 2001 From: Eric Knibbe Date: Fri, 21 Jun 2024 16:04:04 -0400 Subject: [PATCH] cask/audit: support on_os blocks in audit_min_os --- Library/Homebrew/cask/audit.rb | 23 +++++++++++++------ Library/Homebrew/cask/dsl.rb | 4 +++- Library/Homebrew/extend/on_system.rb | 5 ++++ .../requirements/macos_requirement.rb | 7 ++++++ .../requirements/macos_requirement_spec.rb | 16 +++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/cask/audit.rb b/Library/Homebrew/cask/audit.rb index 6138837dd4280..83a46b39ef201 100644 --- a/Library/Homebrew/cask/audit.rb +++ b/Library/Homebrew/cask/audit.rb @@ -606,21 +606,30 @@ def audit_min_os debug_messages = [] debug_messages << "Plist #{plist_min_os}" if plist_min_os debug_messages << "Sparkle #{sparkle_min_os}" if sparkle_min_os - odebug "Minimum OS version: #{debug_messages.join(" | ")}" unless debug_messages.empty? + odebug "Detected minimum OS version: #{debug_messages.join(" | ")}" unless debug_messages.empty? min_os = [plist_min_os, sparkle_min_os].compact.max return if min_os.nil? || min_os <= HOMEBREW_MACOS_OLDEST_ALLOWED - cask_min_os = cask.depends_on.macos&.version - return if cask_min_os == min_os - - min_os_symbol = if cask_min_os.present? - cask_min_os.to_sym.inspect + cask_min_os = if cask.on_system_blocks_exist? + cask.on_system_block_min_os + else + cask.depends_on.macos&.minimum_version + end + odebug "Declared minimum OS version: #{cask_min_os&.to_sym}" + return if cask_min_os&.to_sym == min_os.to_sym + + min_os_definition = if cask_min_os.present? + if cask.on_system_blocks_exist? + "a block with a minimum OS version of #{cask.on_system_block_min_os.inspect}" + else + cask_min_os.to_sym.inspect + end else "no minimum OS version" end add_error "Upstream defined #{min_os.to_sym.inspect} as the minimum OS version " \ - "and the cask defined #{min_os_symbol}", + "and the cask declared #{min_os_definition}", strict_only: true end diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index ab05176bd1241..1c7c8a6e9fc0d 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -95,6 +95,7 @@ class DSL :livecheck, :livecheckable?, :on_system_blocks_exist?, + :on_system_block_min_os, :depends_on_set_in_block?, *ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key), *ACTIVATABLE_ARTIFACT_CLASSES.map(&:dsl_key), @@ -104,7 +105,8 @@ class DSL extend Attrable include OnSystem::MacOSOnly - attr_reader :cask, :token, :deprecation_date, :deprecation_reason, :disable_date, :disable_reason + attr_reader :cask, :token, :deprecation_date, :deprecation_reason, :disable_date, :disable_reason, + :on_system_block_min_os attr_predicate :deprecated?, :disabled?, :livecheckable?, :on_system_blocks_exist?, :depends_on_set_in_block? diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index 5df367ac844c5..225a181582fb5 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -130,6 +130,11 @@ def self.setup_macos_methods(base) os_condition = OnSystem.condition_from_method_name T.must(__method__) return unless OnSystem.os_condition_met? os_condition, or_condition + @on_system_block_min_os = if or_condition == :or_older + MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED).to_sym + else + os_condition + end @called_in_on_system_block = true result = block.call @called_in_on_system_block = false diff --git a/Library/Homebrew/requirements/macos_requirement.rb b/Library/Homebrew/requirements/macos_requirement.rb index a407559d494cb..acaa9d12f79bf 100644 --- a/Library/Homebrew/requirements/macos_requirement.rb +++ b/Library/Homebrew/requirements/macos_requirement.rb @@ -61,6 +61,13 @@ def version_specified? false end + def minimum_version + return MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) if @comparator == "<=" || !version_specified? + return @version.min if @version.respond_to?(:to_ary) + + @version + end + def allows?(other) return true unless version_specified? diff --git a/Library/Homebrew/test/requirements/macos_requirement_spec.rb b/Library/Homebrew/test/requirements/macos_requirement_spec.rb index d18e95feced81..ad15adc0c5aad 100644 --- a/Library/Homebrew/test/requirements/macos_requirement_spec.rb +++ b/Library/Homebrew/test/requirements/macos_requirement_spec.rb @@ -5,6 +5,7 @@ RSpec.describe MacOSRequirement do subject(:requirement) { described_class.new } + let(:macos_oldest_allowed) { MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) } let(:big_sur_major) { MacOSVersion.new("11.0") } describe "#satisfied?" do @@ -23,11 +24,26 @@ end end + specify "#minimum_version" do + no_requirement = described_class.new + max_requirement = described_class.new([:big_sur], comparator: "<=") + min_requirement = described_class.new([:big_sur], comparator: ">=") + exact_requirement = described_class.new([:big_sur], comparator: "==") + range_requirement = described_class.new([[:monterey, :big_sur]], comparator: "==") + expect(no_requirement.minimum_version).to eq macos_oldest_allowed + expect(max_requirement.minimum_version).to eq macos_oldest_allowed + expect(min_requirement.minimum_version).to eq big_sur_major + expect(exact_requirement.minimum_version).to eq big_sur_major + expect(range_requirement.minimum_version).to eq big_sur_major + end + specify "#allows?" do + no_requirement = described_class.new max_requirement = described_class.new([:mojave], comparator: "<=") min_requirement = described_class.new([:catalina], comparator: ">=") exact_requirement = described_class.new([:big_sur], comparator: "==") range_requirement = described_class.new([[:monterey, :big_sur]], comparator: "==") + expect(no_requirement.allows?(big_sur_major)).to be true expect(max_requirement.allows?(big_sur_major)).to be false expect(min_requirement.allows?(big_sur_major)).to be true expect(exact_requirement.allows?(big_sur_major)).to be true