Skip to content

Commit 8ce86ba

Browse files
committed
cask: extract refresh_for_tag to tolerate asymmetric variations
Add `Cask::Cask#refresh_for_tag`, which refreshes a cask for a simulated OS/arch tag and tolerates casks that intentionally omit some platforms (e.g. an `on_linux` block defining only `x86_64_linux`). Use it from `to_hash_with_variations`, `bump-cask-pr` and `generate-cask-ci-matrix` instead of repeating the rescue in each caller. Previously `to_hash_with_variations` raised `invalid 'sha256' value: nil` for such casks, aborting variation generation entirely. Signed-off-by: Patrick Linnane <patrick@linnane.io>
1 parent edf610a commit 8ce86ba

5 files changed

Lines changed: 71 additions & 19 deletions

File tree

Library/Homebrew/cask/cask.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ def refresh
163163
raise CaskInvalidError.new(token, e.message), e.backtrace
164164
end
165165

166+
# Refresh the cask as evaluated on `tag` and yield. Returns `nil` instead of
167+
# raising when the cask has `on_system` blocks that omit the tag.
168+
sig {
169+
type_parameters(:U)
170+
.params(tag: ::Utils::Bottles::Tag, _block: T.proc.returns(T.type_parameter(:U)))
171+
.returns(T.nilable(T.type_parameter(:U)))
172+
}
173+
def refresh_for_tag(tag, &_block)
174+
Homebrew::SimulateSystem.with(os: tag.system, arch: tag.arch) do
175+
refresh
176+
yield
177+
end
178+
rescue CaskInvalidError, CaskUnreadableError
179+
raise unless on_system_blocks_exist?
180+
181+
nil
182+
end
183+
166184
def_delegators :@dsl, *::Cask::DSL::DSL_METHODS
167185

168186
sig { returns(DSL::Caveats) }
@@ -592,9 +610,7 @@ def to_hash_with_variations
592610
!dsl!.depends_on_set_in_block? &&
593611
macos_requirements.any? { |requirement| !requirement.allows?(bottle_tag.to_macos_version) }
594612

595-
Homebrew::SimulateSystem.with_tag(bottle_tag) do
596-
refresh
597-
613+
refresh_for_tag(bottle_tag) do
598614
to_h.each do |key, value|
599615
next if HASH_KEYS_TO_SKIP.include? key
600616
next if value.to_s == hash[key].to_s

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,11 @@ def replace_version_and_checksum(cask, new_hash, new_version, contents)
293293
cask_sourcefile_path = cask.sourcefile_path
294294
raise "unexpected nil cask.sourcefile_path" unless cask_sourcefile_path
295295

296+
old_cask = Cask::CaskLoader.load(cask_sourcefile_path)
296297
generate_system_options(cask, new_version).each do |os, arch|
297-
SimulateSystem.with(os:, arch:) do
298-
# Handle the cask being invalid for specific os/arch combinations
299-
old_cask = begin
300-
Cask::CaskLoader.load(cask_sourcefile_path)
301-
rescue Cask::CaskInvalidError, Cask::CaskUnreadableError
302-
raise unless cask.on_system_blocks_exist?
303-
end
304-
next if old_cask.nil?
305-
306-
# Skip archs excluded by the reloaded cask's `depends_on arch:`.
298+
tag = Utils::Bottles::Tag.new(system: os, arch:)
299+
old_cask.refresh_for_tag(tag) do
300+
# Skip archs excluded by the cask's `depends_on arch:`.
307301
reloaded_archs = old_cask.depends_on.arch&.filter_map { |a| a[:type] }&.uniq
308302
next if reloaded_archs.present? && reloaded_archs.exclude?(arch)
309303

Library/Homebrew/dev-cmd/generate-cask-ci-matrix.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,15 @@ def runner_arch_pairs(runners:, multi_os:)
188188
def architectures(cask:, os:)
189189
architectures = T.let([], T::Array[Symbol])
190190
[:arm, :intel].each do |arch|
191-
tag = Utils::Bottles::Tag.new(system: os, arch: arch)
192-
Homebrew::SimulateSystem.with_tag(tag) do
193-
cask.refresh
194-
191+
tag = Utils::Bottles::Tag.new(system: os, arch:)
192+
cask.refresh_for_tag(tag) do
195193
if cask.depends_on.arch.blank?
196194
architectures = RUNNERS.keys.map { |r| r.fetch(:arch).to_sym }.uniq.sort
197195
next
198196
end
199197

200198
architectures = cask.depends_on.arch.map { |arch| arch[:type] }
201199
end
202-
rescue ::Cask::CaskInvalidError
203-
# Can't read cask for this system-arch combination.
204200
end
205201

206202
architectures

Library/Homebrew/test/cask/cask_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,20 @@ def write_auto_updates_cask(path, version:, artifacts:, token: "auto-updates-bun
596596
end
597597
end
598598

599+
describe "#refresh_for_tag" do
600+
let(:cask) { Cask::CaskLoader.load("on-linux-asymmetric") }
601+
602+
it "yields with the cask refreshed for a supported tag" do
603+
tag = Utils::Bottles::Tag.new(system: :sonoma, arch: :intel)
604+
expect(cask.refresh_for_tag(tag) { cask.url.to_s }).to include("caffeine-intel-darwin")
605+
end
606+
607+
it "returns nil for a tag the cask does not support" do
608+
tag = Utils::Bottles::Tag.new(system: :linux, arch: :arm)
609+
expect(cask.refresh_for_tag(tag) { cask.url }).to be_nil
610+
end
611+
end
612+
599613
describe "#to_hash_with_variations" do
600614
let!(:original_macos_version) { MacOS.full_version.to_s }
601615
let(:expected_versions_variations) do
@@ -744,6 +758,14 @@ def write_auto_updates_cask(path, version:, artifacts:, token: "auto-updates-bun
744758
expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations_os.strip
745759
end
746760

761+
it "omits tags a cask intentionally doesn't define in on_system blocks" do
762+
c = Cask::CaskLoader.load("on-linux-asymmetric")
763+
h = c.to_hash_with_variations
764+
765+
expect(h["variations"]).to include(:x86_64_linux)
766+
expect(h["variations"]).not_to include(:arm64_linux)
767+
end
768+
747769
# NOTE: The calls to `Cask.generating_hash!` and `Cask.generated_hash!`
748770
# are not idempotent so they can only be used in one test.
749771
it "returns the correct hash placeholders" do
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# typed: false
2+
3+
cask "on-linux-asymmetric" do
4+
arch arm: "arm", intel: "intel"
5+
os macos: "darwin", linux: "linux"
6+
7+
version "1.2.3"
8+
9+
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine-#{arch}-#{os}.zip"
10+
homepage "https://brew.sh/"
11+
12+
on_macos do
13+
sha256 arm: "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94",
14+
intel: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
15+
end
16+
17+
on_linux do
18+
sha256 x86_64_linux: "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
19+
20+
depends_on arch: :x86_64
21+
end
22+
23+
app "Caffeine.app"
24+
end

0 commit comments

Comments
 (0)