Skip to content

Commit fd7e783

Browse files
committed
macos_version: define a method instead
1 parent 5ae4f25 commit fd7e783

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

Library/Homebrew/macos_version.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def initialize(version)
1818

1919
# NOTE: When removing symbols here, ensure that they are added
2020
# to `DEPRECATED_MACOS_VERSIONS` in `MacOSRequirement`.
21-
# When adding or removing symbols here, ensure that you
22-
# also update the KERNEL_MAJOR_VERSIONS map below.
2321
SYMBOLS = {
2422
sequoia: "15",
2523
sonoma: "14",
@@ -33,20 +31,15 @@ def initialize(version)
3331
el_capitan: "10.11",
3432
}.freeze
3533

36-
# Map of macOS version strings to kernel major versions.
37-
# https://en.wikipedia.org/wiki/MacOS_version_history#Releases
38-
KERNEL_MAJOR_VERSIONS = {
39-
"15" => "24",
40-
"14" => "23",
41-
"13" => "22",
42-
"12" => "21",
43-
"11" => "20",
44-
"10.15" => "19",
45-
"10.14" => "18",
46-
"10.13" => "17",
47-
"10.12" => "16",
48-
"10.11" => "15",
49-
}.freeze
34+
def self.kernel_major_version(macos_version)
35+
version_major = macos_version.major.to_i
36+
if version_major > 10
37+
T.must(superclass).new((version_major + 9).to_s)
38+
else
39+
version_minor = macos_version.minor.to_i
40+
T.must(superclass).new((version_minor + 4).to_s)
41+
end
42+
end
5043

5144
sig { params(version: Symbol).returns(T.attached_class) }
5245
def self.from_symbol(version)

Library/Homebrew/test/macos_version_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
let(:big_sur_major) { described_class.new("11.0") }
88
let(:big_sur_update) { described_class.new("11.1") }
99

10+
describe ".kernel_major_version" do
11+
it "returns the kernel major version" do
12+
expect(described_class.kernel_major_version(version)).to eq "18"
13+
expect(described_class.kernel_major_version(big_sur_major)).to eq "20"
14+
expect(described_class.kernel_major_version(big_sur_update)).to eq "20"
15+
end
16+
17+
it "matches the major version returned by OS.kernel_version", :needs_macos do
18+
expect(described_class.kernel_major_version(OS::Mac.version)).to eq OS.kernel_version.major
19+
end
20+
end
21+
1022
specify "comparison with Symbol" do
1123
expect(version).to be > :high_sierra
1224
expect(version).to eq :mojave

0 commit comments

Comments
 (0)