Skip to content

Commit

Permalink
Merge pull request #17548 from Homebrew/audit_min_os-fixes
Browse files Browse the repository at this point in the history
cask/audit: support on_os blocks in audit_min_os
  • Loading branch information
EricFromCanada authored Jun 24, 2024
2 parents c53f600 + f627c63 commit 347529b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
23 changes: 16 additions & 7 deletions Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion Library/Homebrew/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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?

Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/extend/on_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Library/Homebrew/requirements/macos_requirement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
16 changes: 16 additions & 0 deletions Library/Homebrew/test/requirements/macos_requirement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 347529b

Please sign in to comment.