Skip to content

Commit 8350b8a

Browse files
committed
MacOSRequirement: add #highest_allowed method
This adds a `#highest_allowed` method to `MacOSRequirement`, so we can easily identify the highest supported macOS version that a requirement allows, if any. This can be used when producing a minimal set of supported macOS versions that meet requirements in `UsesOnSystem.macos_requirements`. One of the intended use cases for this is to identify which macOS versions we could simulate to work with all variations of a cask that uses macOS on_system blocks.
1 parent 01825ac commit 8350b8a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Library/Homebrew/requirements/macos_requirement.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def allows?(other)
8888
end
8989
end
9090

91+
# Finds the highest supported {MacOSVersion} that meets the requirement, if
92+
# any.
93+
sig { returns(T.nilable(MacOSVersion)) }
94+
def highest_allowed
95+
MacOSVersion::SYMBOLS.each_key do |sym|
96+
candidate_version = MacOSVersion.from_symbol(sym)
97+
return candidate_version if allows?(candidate_version)
98+
end
99+
end
100+
91101
def message(type: :formula)
92102
return "macOS is required for this software." unless version_specified?
93103

Library/Homebrew/test/requirements/macos_requirement_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
let(:macos_oldest_allowed) { MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED) }
99
let(:macos_newest_allowed) { MacOSVersion.new(HOMEBREW_MACOS_NEWEST_UNSUPPORTED) }
10+
let(:macos_newest_supported) { MacOSVersion.new(MacOSVersion::SYMBOLS.values.first) }
1011
let(:big_sur_major) { MacOSVersion.new("11.0") }
1112

1213
describe "#satisfied?" do
@@ -63,4 +64,19 @@
6364
expect(exact_requirement.allows?(big_sur_major)).to be true
6465
expect(range_requirement.allows?(big_sur_major)).to be true
6566
end
67+
68+
specify "#highest_allowed" do
69+
macos_version_sonoma = MacOSVersion.new("14")
70+
71+
no_requirement = described_class.new
72+
max_requirement = described_class.new([:sonoma], comparator: "<=")
73+
min_requirement = described_class.new([:sonoma], comparator: ">=")
74+
exact_requirement = described_class.new([:sonoma], comparator: "==")
75+
range_requirement = described_class.new([[:sonoma, :monterey]], comparator: "==")
76+
expect(no_requirement.highest_allowed).to eq macos_newest_supported
77+
expect(max_requirement.highest_allowed).to eq macos_version_sonoma
78+
expect(min_requirement.highest_allowed).to eq macos_newest_supported
79+
expect(exact_requirement.highest_allowed).to eq macos_version_sonoma
80+
expect(range_requirement.highest_allowed).to eq macos_version_sonoma
81+
end
6682
end

0 commit comments

Comments
 (0)