Skip to content

Commit 0e01bc7

Browse files
authored
Merge pull request #23016 from Homebrew/spec-helper-package-api-collision
Fix test setup crash when multiple package APIs are cached
2 parents c4f8c9b + 93ec18d commit 0e01bc7

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

Library/Homebrew/test/spec_helper.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,44 @@
299299
@__stdin = $stdin.clone
300300

301301
# Link original API cache files to test cache directory.
302-
Pathname("#{ENV.fetch("HOMEBREW_CACHE")}/api").glob("*.json").each do |path|
303-
FileUtils.ln_s path, HOMEBREW_CACHE/"api/#{path.basename}"
302+
source_api_cache = Pathname("#{ENV.fetch("HOMEBREW_CACHE")}/api")
303+
304+
source_api_cache.glob("*.json").each do |path|
305+
target = HOMEBREW_CACHE/"api/#{path.basename}"
306+
FileUtils.ln_s path, target unless target.exist?
304307
end
305-
Pathname("#{ENV.fetch("HOMEBREW_CACHE")}/api").glob("*.txt").each do |path|
306-
FileUtils.cp path, HOMEBREW_CACHE/"api/#{path.basename}"
308+
source_api_cache.glob("*.txt").each do |path|
309+
target = HOMEBREW_CACHE/"api/#{path.basename}"
310+
FileUtils.cp path, target unless target.exist?
307311
end
308-
Pathname("#{ENV.fetch("HOMEBREW_CACHE")}/api/internal").glob("*.{json,txt}").each do |path|
309-
target = HOMEBREW_CACHE/"api/internal/#{path.basename}"
310-
target.dirname.mkpath
312+
313+
source_api_internal_cache = source_api_cache/"internal"
314+
target_api_internal_cache = HOMEBREW_CACHE/"api/internal"
315+
target_api_internal_cache.mkpath
316+
317+
# The real cache can hold package API files for multiple OS tags. Fan out
318+
# from one source so generated test-cache aliases do not collide.
319+
package_paths = source_api_internal_cache.glob("packages.*.jws.json")
320+
package_path = package_paths.find do |path|
321+
path.basename.to_s == "packages.#{Homebrew::SimulateSystem.current_tag}.jws.json"
322+
end || package_paths.first
323+
324+
source_api_internal_cache.glob("*.{json,txt}").each do |path|
325+
next if path.basename.to_s.start_with?("packages.")
326+
327+
target = target_api_internal_cache/path.basename
328+
next if target.exist?
329+
311330
(path.extname == ".txt") ? FileUtils.cp(path, target) : FileUtils.ln(path, target)
312-
next unless path.basename.to_s.start_with?("packages.")
331+
end
313332

333+
if package_path
314334
[:generic, :linux, :macos, *MacOSVersion::SYMBOLS.keys].product([:arm, :intel]).each do |system, arch|
315335
tag = Utils::Bottles::Tag.new(system:, arch:)
316336
next unless tag.valid_combination?
317337

318-
target = HOMEBREW_CACHE/"api/internal/packages.#{tag}.jws.json"
319-
FileUtils.ln path, target unless target.exist?
338+
target = target_api_internal_cache/"packages.#{tag}.jws.json"
339+
FileUtils.ln package_path, target unless target.exist?
320340
end
321341
end
322342

0 commit comments

Comments
 (0)