Skip to content

Commit 334bf23

Browse files
authored
Merge pull request #19709 from Homebrew/bump-cask-pr-respect-os-arch
bump-cask-pr: respect `depends_on arch`
2 parents d6da002 + 99681d7 commit 334bf23

File tree

2 files changed

+171
-28
lines changed

2 files changed

+171
-28
lines changed

Library/Homebrew/dev-cmd/bump-cask-pr.rb

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,40 @@ def shortened_version(version, cask:)
181181
end
182182
end
183183

184+
sig { params(cask: Cask::Cask).returns(T::Array[[Symbol, Symbol]]) }
185+
def generate_system_options(cask)
186+
current_os = Homebrew::SimulateSystem.current_os
187+
current_os_is_macos = MacOSVersion::SYMBOLS.include?(current_os)
188+
newest_macos = MacOSVersion::SYMBOLS.keys.first
189+
190+
depends_on_archs = cask.depends_on.arch&.filter_map { |arch| arch[:type] }&.uniq
191+
192+
# NOTE: We substitute the newest macOS (e.g. `:sequoia`) in place of
193+
# `:macos` values (when used), as a generic `:macos` value won't apply
194+
# to on_system blocks referencing macOS versions.
195+
os_values = []
196+
arch_values = depends_on_archs.presence || []
197+
if cask.on_system_blocks_exist?
198+
OnSystem::BASE_OS_OPTIONS.each do |os|
199+
os_values << if os == :macos
200+
(current_os_is_macos ? current_os : newest_macos)
201+
else
202+
os
203+
end
204+
end
205+
206+
arch_values = OnSystem::ARCH_OPTIONS if arch_values.empty?
207+
else
208+
# Architecture is only relevant if on_system blocks are present or
209+
# the cask uses `depends_on arch`, otherwise we default to ARM for
210+
# consistency.
211+
os_values << (current_os_is_macos ? current_os : newest_macos)
212+
arch_values << :arm if arch_values.empty?
213+
end
214+
215+
os_values.product(arch_values)
216+
end
217+
184218
sig {
185219
params(
186220
cask: Cask::Cask,
@@ -190,33 +224,8 @@ def shortened_version(version, cask:)
190224
).returns(T::Array[[T.any(Regexp, String), T.any(Pathname, String)]])
191225
}
192226
def replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
193-
host_os = Homebrew::SimulateSystem.current_os
194-
host_is_macos = MacOSVersion::SYMBOLS.include?(host_os)
195-
newest_macos = MacOSVersion::SYMBOLS.keys.first
196-
197-
# NOTE: We substitute the newest macOS (e.g. `:sequoia`) in place of
198-
# `:macos` values (when used), as a generic `:macos` value won't apply
199-
# to on_system blocks referencing macOS versions. We also omit the OS
200-
# when the value aligns with the host.
201-
system_options = if cask.on_system_blocks_exist?
202-
OnSystem::BASE_OS_OPTIONS.each_with_object([]) do |os, array|
203-
OnSystem::ARCH_OPTIONS.each do |arch|
204-
system_hash = { arch: }
205-
system_hash[:os] = os if host_is_macos && os != :macos
206-
system_hash[:os] = newest_macos if !host_is_macos && os == :macos
207-
array << system_hash
208-
end
209-
end.uniq
210-
else
211-
# Architecture is only relevant if on_system blocks are present. When
212-
# not present, we default to ARM for consistency.
213-
system_hash = { arch: :arm }
214-
system_hash[:os] = newest_macos unless host_is_macos
215-
[system_hash]
216-
end
217-
218-
system_options.each do |system_args|
219-
SimulateSystem.with(**system_args) do
227+
generate_system_options(cask).each do |os, arch|
228+
SimulateSystem.with(os:, arch:) do
220229
# Handle the cask being invalid for specific os/arch combinations
221230
old_cask = begin
222231
Cask::CaskLoader.load(cask.sourcefile_path)
@@ -228,7 +237,7 @@ def replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
228237
old_version = old_cask.version
229238
next unless old_version
230239

231-
bump_version = new_version.send(system_args[:arch]) || new_version.general
240+
bump_version = new_version.send(arch) || new_version.general
232241

233242
old_version_regex = old_version.latest? ? ":latest" : %Q(["']#{Regexp.escape(old_version.to_s)}["'])
234243
replacement_pairs << [/version\s+#{old_version_regex}/m,

Library/Homebrew/test/dev-cmd/bump-cask-pr_spec.rb

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,139 @@
44
require "dev-cmd/bump-cask-pr"
55

66
RSpec.describe Homebrew::DevCmd::BumpCaskPr do
7+
subject(:bump_cask_pr) { described_class.new(["test"]) }
8+
9+
let(:newest_macos) { MacOSVersion::SYMBOLS.keys.first }
10+
11+
let(:c) do
12+
Cask::Cask.new("test") do
13+
version "0.0.1,2"
14+
15+
url "https://brew.sh/test-0.0.1.dmg"
16+
name "Test"
17+
desc "Test cask"
18+
homepage "https://brew.sh"
19+
end
20+
end
21+
22+
let(:c_depends_on_intel) do
23+
Cask::Cask.new("test-depends-on-intel") do
24+
version "0.0.1,2"
25+
26+
url "https://brew.sh/test-0.0.1.dmg"
27+
name "Test"
28+
desc "Test cask"
29+
homepage "https://brew.sh"
30+
31+
depends_on arch: :x86_64
32+
end
33+
end
34+
35+
let(:c_on_system) do
36+
Cask::Cask.new("test-on-system") do
37+
os macos: "darwin", linux: "linux"
38+
39+
version "0.0.1,2"
40+
41+
url "https://brew.sh/test-0.0.1.dmg"
42+
name "Test"
43+
desc "Test cask"
44+
homepage "https://brew.sh"
45+
end
46+
end
47+
48+
let(:c_on_system_depends_on_intel) do
49+
Cask::Cask.new("test-on-system-depends-on-intel") do
50+
os macos: "darwin", linux: "linux"
51+
52+
version "0.0.1,2"
53+
54+
url "https://brew.sh/test-0.0.1.dmg"
55+
name "Test"
56+
desc "Test cask"
57+
homepage "https://brew.sh"
58+
59+
depends_on arch: :x86_64
60+
end
61+
end
62+
763
it_behaves_like "parseable arguments"
64+
65+
describe "::generate_system_options" do
66+
# We simulate a macOS version older than the newest, as the method will use
67+
# the host macOS version instead of the default (the newest macOS version).
68+
let(:older_macos) { :big_sur }
69+
70+
context "when cask does not have on_system blocks/calls or `depends_on arch`" do
71+
it "returns an array only including macOS/ARM" do
72+
Homebrew::SimulateSystem.with(os: :linux) do
73+
expect(bump_cask_pr.send(:generate_system_options, c))
74+
.to eq([[newest_macos, :arm]])
75+
end
76+
77+
Homebrew::SimulateSystem.with(os: older_macos) do
78+
expect(bump_cask_pr.send(:generate_system_options, c))
79+
.to eq([[older_macos, :arm]])
80+
end
81+
end
82+
end
83+
84+
context "when cask does not have on_system blocks/calls but has `depends_on arch`" do
85+
it "returns an array only including macOS/`depends_on arch` value" do
86+
Homebrew::SimulateSystem.with(os: :linux, arch: :arm) do
87+
expect(bump_cask_pr.send(:generate_system_options, c_depends_on_intel))
88+
.to eq([[newest_macos, :intel]])
89+
end
90+
91+
Homebrew::SimulateSystem.with(os: older_macos, arch: :arm) do
92+
expect(bump_cask_pr.send(:generate_system_options, c_depends_on_intel))
93+
.to eq([[older_macos, :intel]])
94+
end
95+
end
96+
end
97+
98+
context "when cask has on_system blocks/calls but does not have `depends_on arch`" do
99+
it "returns an array with combinations of `OnSystem::BASE_OS_OPTIONS` and `OnSystem::ARCH_OPTIONS`" do
100+
Homebrew::SimulateSystem.with(os: :linux) do
101+
expect(bump_cask_pr.send(:generate_system_options, c_on_system))
102+
.to eq([
103+
[newest_macos, :intel],
104+
[newest_macos, :arm],
105+
[:linux, :intel],
106+
[:linux, :arm],
107+
])
108+
end
109+
110+
Homebrew::SimulateSystem.with(os: older_macos) do
111+
expect(bump_cask_pr.send(:generate_system_options, c_on_system))
112+
.to eq([
113+
[older_macos, :intel],
114+
[older_macos, :arm],
115+
[:linux, :intel],
116+
[:linux, :arm],
117+
])
118+
end
119+
end
120+
end
121+
122+
context "when cask has on_system blocks/calls and `depends_on arch`" do
123+
it "returns an array with combinations of `OnSystem::BASE_OS_OPTIONS` and `depends_on arch` value" do
124+
Homebrew::SimulateSystem.with(os: :linux, arch: :arm) do
125+
expect(bump_cask_pr.send(:generate_system_options, c_on_system_depends_on_intel))
126+
.to eq([
127+
[newest_macos, :intel],
128+
[:linux, :intel],
129+
])
130+
end
131+
132+
Homebrew::SimulateSystem.with(os: older_macos, arch: :arm) do
133+
expect(bump_cask_pr.send(:generate_system_options, c_on_system_depends_on_intel))
134+
.to eq([
135+
[older_macos, :intel],
136+
[:linux, :intel],
137+
])
138+
end
139+
end
140+
end
141+
end
8142
end

0 commit comments

Comments
 (0)