-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
formulae.rb
602 lines (489 loc) · 22.8 KB
/
formulae.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# frozen_string_literal: true
module Homebrew
module Tests
class Formulae < TestFormulae
attr_writer :testing_formulae, :added_formulae, :deleted_formulae
def initialize(tap:, git:, dry_run:, fail_fast:, verbose:, output_paths:)
super(tap:, git:, dry_run:, fail_fast:, verbose:)
@built_formulae = []
@bottle_checksums = {}
@bottle_output_path = output_paths[:bottle]
@linkage_output_path = output_paths[:linkage]
@skipped_or_failed_formulae_output_path = output_paths[:skipped_or_failed_formulae]
end
def run!(args:)
test_header(:Formulae)
verify_local_bottles
with_env(HOMEBREW_DISABLE_LOAD_FORMULA: "1") do
bottle_specifier = if OS.linux?
"{linux,ubuntu}"
else
"#{MacOS.version}#{"-arm64" if Hardware::CPU.arm?}"
end
download_artifacts_from_previous_run!("bottles{,_#{bottle_specifier}*}", dry_run: args.dry_run?)
end
@bottle_checksums.merge!(
bottle_glob("*", artifact_cache, ".{json,tar.gz}", bottle_tag: "*").to_h do |bottle_file|
[bottle_file.realpath, bottle_file.sha256]
end,
)
# #run! modifies `@testing_formulae`, so we need to track this separately.
@testing_formulae_count = @testing_formulae.count
sorted_formulae.each do |f|
formula!(f, args:)
verify_local_bottles
puts
end
@deleted_formulae.each do |f|
deleted_formula!(f)
verify_local_bottles
puts
end
return unless ENV["GITHUB_ACTIONS"]
File.open(ENV.fetch("GITHUB_OUTPUT"), "a") do |f|
f.puts "skipped_or_failed_formulae=#{@skipped_or_failed_formulae.join(",")}"
end
@skipped_or_failed_formulae_output_path.write(@skipped_or_failed_formulae.join(","))
ensure
verify_local_bottles
artifact_cache.rmtree if artifact_cache.exist?
end
private
def tap_needed_taps(deps)
deps.each { |d| d.to_formula.recursive_dependencies }
rescue TapFormulaUnavailableError => e
raise if e.tap.installed?
e.tap.clear_cache
safe_system "brew", "tap", e.tap.name
retry
end
def install_ca_certificates_if_needed
return if DevelopmentTools.ca_file_handles_most_https_certificates?
test "brew", "install", "ca-certificates",
env: { "HOMEBREW_DEVELOPER" => nil }
end
def install_gcc_if_needed(formula, deps)
installed_gcc = false
begin
deps.each { |dep| CompilerSelector.select_for(dep.to_formula) }
CompilerSelector.select_for(formula)
rescue CompilerSelectionError => e
unless installed_gcc
test "brew", "install", "gcc",
env: { "HOMEBREW_DEVELOPER" => nil }
installed_gcc = true
DevelopmentTools.clear_version_cache
retry
end
skipped formula.name, e.message
end
end
def setup_formulae_deps_instances(formula, formula_name, args:)
conflicts = formula.conflicts
formula_recursive_dependencies = formula.recursive_dependencies.map(&:to_formula)
formula_recursive_dependencies.each do |dependency|
conflicts += dependency.conflicts
end
# If we depend on a versioned formula, make sure to unlink any other
# installed versions to make sure that we use the right one.
versioned_dependencies = formula_recursive_dependencies.select(&:versioned_formula?)
versioned_dependencies.each do |dependency|
alternative_versions = dependency.versioned_formulae
begin
unversioned_name = dependency.name.sub(/@\d+(\.\d+)*$/, "")
alternative_versions << Formula[unversioned_name]
rescue FormulaUnavailableError
nil
end
unneeded_alternative_versions = alternative_versions - formula_recursive_dependencies
conflicts += unneeded_alternative_versions
end
unlink_formulae = conflicts.map(&:name)
unlink_formulae.uniq.each do |name|
unlink_formula = Formulary.factory(name)
next unless unlink_formula.latest_version_installed?
next unless unlink_formula.linked_keg.exist?
test "brew", "unlink", name
end
info_header "Determining dependencies..."
installed = Utils.safe_popen_read("brew", "list", "--formula", "--full-name").split("\n")
dependencies =
Utils.safe_popen_read("brew", "deps", "--include-build", "--include-test", "--full-name", formula_name)
.split("\n")
installed_dependencies = installed & dependencies
installed_dependencies.each do |name|
link_formula = Formulary.factory(name)
next if link_formula.keg_only?
next if link_formula.linked_keg.exist?
test "brew", "link", name
end
dependencies -= installed
@unchanged_dependencies = dependencies - @testing_formulae
test "brew", "fetch", "--retry", *@unchanged_dependencies unless @unchanged_dependencies.empty?
changed_dependencies = dependencies - @unchanged_dependencies
unless changed_dependencies.empty?
test "brew", "fetch", "--retry", "--build-from-source",
*changed_dependencies
ignore_failures = !args.test_default_formula? && changed_dependencies.any? do |dep|
!bottled?(Formulary.factory(dep), no_older_versions: true)
end
# Install changed dependencies as new bottles so we don't have
# checksum problems. We have to install all `changed_dependencies`
# in one `brew install` command to make sure they are installed in
# the right order.
test("brew", "install", "--build-from-source",
named_args: changed_dependencies,
ignore_failures:)
# Run postinstall on them because the tested formula might depend on
# this step
test "brew", "postinstall", named_args: changed_dependencies, ignore_failures:
end
runtime_or_test_dependencies =
Utils.safe_popen_read("brew", "deps", "--include-test", formula_name)
.split("\n")
build_dependencies = dependencies - runtime_or_test_dependencies
@unchanged_build_dependencies = build_dependencies - @testing_formulae
end
def cleanup_bottle_etc_var(formula)
bottle_prefix = formula.opt_prefix/".bottle"
# Nuke etc/var to have them be clean to detect bottle etc/var
# file additions.
Pathname.glob("#{bottle_prefix}/{etc,var}/**/*").each do |bottle_path|
prefix_path = bottle_path.sub(bottle_prefix, HOMEBREW_PREFIX)
FileUtils.rm_rf prefix_path
end
end
def verify_local_bottles
# Setting `HOMEBREW_DISABLE_LOAD_FORMULA` probably doesn't do anything here but let's set it just to be safe.
with_env(HOMEBREW_DISABLE_LOAD_FORMULA: "1") do
missing_bottles = @bottle_checksums.keys.reject do |bottle_path|
next true if bottle_path.exist?
what = (bottle_path.extname == ".json") ? "JSON" : "tarball"
onoe "Missing bottle #{what}: #{bottle_path}"
false
end
mismatched_checksums = @bottle_checksums.reject do |bottle_path, expected_sha256|
next true unless bottle_path.exist?
next true if (actual_sha256 = bottle_path.sha256) == expected_sha256
onoe <<~ERROR
Bottle checksum mismatch for #{bottle_path}!
Expected: #{expected_sha256}
Actual: #{actual_sha256}
ERROR
false
end
unexpected_bottles = bottle_glob(
"**/*", Pathname.pwd, ".{json,tar.gz}", bottle_tag: "*"
).reject do |local_bottle|
next true if @bottle_checksums.key?(local_bottle.realpath)
what = (local_bottle.extname == ".json") ? "JSON" : "tarball"
onoe "Unexpected bottle #{what}: #{local_bottle}"
false
end
return true if missing_bottles.blank? && mismatched_checksums.blank? && unexpected_bottles.blank?
# Delete these files so we don't end up uploading them.
files_to_delete = mismatched_checksums.keys + unexpected_bottles
files_to_delete += files_to_delete.select(&:symlink?).map(&:realpath)
FileUtils.rm_rf files_to_delete
test "false" # ensure that `test-bot` exits with an error.
false
end
end
def bottle_reinstall_formula(formula, new_formula, args:)
unless build_bottle?(formula, args:)
@bottle_filename = nil
return
end
root_url = args.root_url
# GitHub Releases url
root_url ||= if tap.present? && !tap.core_tap? && !args.test_default_formula?
"#{tap.default_remote}/releases/download/#{formula.name}-#{formula.pkg_version}"
end
# This is needed where sparse files may be handled (bsdtar >=3.0).
# We use gnu-tar with sparse files disabled when --only-json-tab is passed.
ENV["HOMEBREW_BOTTLE_SUDO_PURGE"] = "1" if OS.mac? && MacOS.version >= :catalina && !args.only_json_tab?
bottle_args = ["--verbose", "--json", formula.full_name]
bottle_args << "--keep-old" if args.keep_old? && !new_formula
bottle_args << "--skip-relocation" if args.skip_relocation?
bottle_args << "--force-core-tap" if args.test_default_formula?
bottle_args << "--root-url=#{root_url}" if root_url
bottle_args << "--only-json-tab" if args.only_json_tab?
verify_local_bottles
test "brew", "bottle", *bottle_args
bottle_step = steps.last
if !bottle_step.passed? || !bottle_step.output?
failed formula.full_name, "bottling failed" unless args.dry_run?
return
end
@bottle_filename = Pathname.new(
bottle_step.output
.gsub(%r{.*(\./\S+#{HOMEBREW_BOTTLES_EXTNAME_REGEX}).*}om, '\1'),
)
@bottle_json_filename = Pathname.new(
@bottle_filename.to_s.gsub(/\.(\d+\.)?tar\.gz$/, ".json"),
)
@bottle_checksums[@bottle_filename.realpath] = @bottle_filename.sha256
@bottle_checksums[@bottle_json_filename.realpath] = @bottle_json_filename.sha256
@bottle_output_path.write(bottle_step.output, mode: "a")
bottle_merge_args =
["--merge", "--write", "--no-commit", "--no-all-checks", @bottle_json_filename]
bottle_merge_args << "--keep-old" if args.keep_old? && !new_formula
test "brew", "bottle", *bottle_merge_args
test "brew", "uninstall", "--force", formula.full_name
@testing_formulae.delete(formula.name)
unless @unchanged_build_dependencies.empty?
test "brew", "uninstall", "--force", *@unchanged_build_dependencies
@unchanged_dependencies -= @unchanged_build_dependencies
end
test "brew", "install", "--only-dependencies", @bottle_filename
test "brew", "install", @bottle_filename
end
def build_bottle?(formula, args:)
# Build and runtime dependencies must be bottled on the current OS,
# but accept an older compatible bottle for test dependencies.
return false if formula.deps.any? do |dep|
!bottled_or_built?(
dep.to_formula,
@built_formulae - @skipped_or_failed_formulae,
no_older_versions: !dep.test?,
)
end
!args.build_from_source?
end
def livecheck(formula)
return unless formula.livecheckable?
return if formula.livecheck.skip?
livecheck_step = test "brew", "livecheck", "--formula", "--json", "--full-name", formula.full_name
return if livecheck_step.failed?
return unless livecheck_step.output?
livecheck_info = JSON.parse(livecheck_step.output)&.first
if livecheck_info["status"] == "error"
error_msg = if livecheck_info["messages"].present? && livecheck_info["messages"].length.positive?
livecheck_info["messages"].join("\n")
else
# An error message should always be provided alongside an "error"
# status but this is a failsafe
"Error encountered (no message provided)"
end
if ENV["GITHUB_ACTIONS"].present?
puts GitHub::Actions::Annotation.new(
:error,
error_msg,
title: "#{formula}: livecheck error",
file: formula.path.to_s.delete_prefix("#{repository}/"),
)
else
onoe error_msg
end
end
# `status` and `version` are mutually exclusive (the presence of one
# indicates the absence of the other)
return if livecheck_info["status"].present?
return if livecheck_info["version"]["newer_than_upstream"] != true
current_version = livecheck_info["version"]["current"]
latest_version = livecheck_info["version"]["latest"]
newer_than_upstream_msg = if current_version.present? && latest_version.present?
"The formula version (#{current_version}) is newer than the " \
"version from `brew livecheck` (#{latest_version})."
else
"The formula version is newer than the version from `brew livecheck`."
end
if ENV["GITHUB_ACTIONS"].present?
puts GitHub::Actions::Annotation.new(
:warning,
newer_than_upstream_msg,
title: "#{formula}: Formula version newer than livecheck",
file: formula.path.to_s.delete_prefix("#{repository}/"),
)
else
opoo newer_than_upstream_msg
end
end
def formula!(formula_name, args:)
cleanup_during!(@testing_formulae, args:)
test_header(:Formulae, method: "formula!(#{formula_name})")
formula = Formulary.factory(formula_name)
if formula.disabled?
skipped formula_name, "#{formula.full_name} has been disabled!"
return
end
test "brew", "deps", "--tree", "--annotate", "--include-build", "--include-test", named_args: formula_name
deps_without_compatible_bottles = formula.deps.map(&:to_formula)
deps_without_compatible_bottles.reject! do |dep|
bottled_or_built?(dep, @built_formulae - @skipped_or_failed_formulae)
end
bottled_on_current_version = bottled?(formula, no_older_versions: true)
if deps_without_compatible_bottles.present? && !bottled_on_current_version
message = <<~EOS
#{formula_name} has dependencies without compatible bottles:
#{deps_without_compatible_bottles * "\n "}
EOS
skipped formula_name, message
return
end
new_formula = @added_formulae.include?(formula_name)
ignore_failures = !args.test_default_formula? && !bottled_on_current_version && !new_formula
deps = []
reqs = []
build_flag = if build_bottle?(formula, args:)
"--build-bottle"
else
if ENV["GITHUB_ACTIONS"].present?
puts GitHub::Actions::Annotation.new(
:warning,
"#{formula} has unbottled dependencies, so a bottle will not be built.",
title: "No bottle built for #{formula}!",
file: formula.path.to_s.delete_prefix("#{repository}/"),
)
else
onoe "Not building a bottle for #{formula} because it has unbottled dependencies."
end
skipped formula_name, "No bottle built."
return
end
# Online checks are a bit flaky and less useful for PRs that modify multiple formulae.
skip_online_checks = args.skip_online_checks? || (@testing_formulae_count > 5)
fetch_args = [formula_name]
fetch_args << build_flag
fetch_args << "--force" if args.cleanup?
audit_args = [formula_name]
audit_args << "--online" unless skip_online_checks
if new_formula
if !args.skip_new?
audit_args << "--new"
elsif !args.skip_new_strict?
audit_args << "--strict"
end
else
audit_args << "--git" << "--skip-style"
audit_args << "--except=unconfirmed_checksum_change" if args.skip_checksum_only_audit?
audit_args << "--except=stable_version" if args.skip_stable_version_audit?
audit_args << "--except=revision" if args.skip_revision_audit?
end
# This needs to be done before any network operation.
install_ca_certificates_if_needed
if (messages = unsatisfied_requirements_messages(formula))
test "brew", "fetch", "--retry", *fetch_args
test "brew", "audit", *audit_args
skipped formula_name, messages
return
end
deps |= formula.deps.to_a.reject(&:optional?)
reqs |= formula.requirements.to_a.reject(&:optional?)
tap_needed_taps(deps)
install_curl_if_needed(formula)
install_mercurial_if_needed(deps, reqs)
install_subversion_if_needed(deps, reqs)
setup_formulae_deps_instances(formula, formula_name, args:)
test "brew", "uninstall", "--force", formula_name if formula.latest_version_installed?
install_args = ["--verbose"]
install_args << build_flag
# Don't care about e.g. bottle failures for dependencies.
test "brew", "install", "--only-dependencies", *install_args, formula_name,
env: { "HOMEBREW_DEVELOPER" => nil }
# Do this after installing dependencies to avoid skipping formulae
# that build with and declare a dependency on GCC. See discussion at
# https://github.com/Homebrew/homebrew-core/pull/86826
install_gcc_if_needed(formula, deps)
info_header "Starting tests for #{formula_name}"
test "brew", "fetch", "--retry", *fetch_args
env = {}
env["HOMEBREW_GIT_PATH"] = nil if deps.any? do |d|
d.name == "git" && (!d.test? || d.build?)
end
install_step_passed = formula_installed_from_bottle =
artifact_cache_valid?(formula) &&
verify_local_bottles && # Checking the artifact cache loads formulae, so do this check second.
install_formula_from_bottle(formula_name,
bottle_dir: artifact_cache,
testing_formulae_dependents: false,
dry_run: args.dry_run?)
install_step_passed ||= begin
test("brew", "install", *install_args,
named_args: formula_name,
env: env.merge({ "HOMEBREW_DEVELOPER" => nil }),
ignore_failures:, report_analytics: true)
steps.last.passed?
end
livecheck(formula) if !args.skip_livecheck? && !skip_online_checks
test "brew", "style", "--formula", formula_name, report_analytics: true
test "brew", "audit", "--formula", *audit_args, report_analytics: true unless formula.deprecated?
unless install_step_passed
if ignore_failures
skipped formula_name, "install failed"
else
failed formula_name, "install failed"
end
return
end
if formula_installed_from_bottle
moved_artifacts = bottle_glob(formula_name, artifact_cache, ".{json,tar.gz}").map(&:realpath)
Pathname.pwd.install moved_artifacts
moved_artifacts.each do |old_location|
new_location = old_location.basename.realpath
@bottle_checksums[new_location] = @bottle_checksums.fetch(old_location)
@bottle_checksums.delete(old_location)
end
else
bottle_reinstall_formula(formula, new_formula, args:)
end
@built_formulae << formula.full_name
test("brew", "linkage", "--test", named_args: formula_name, ignore_failures:, report_analytics: true)
failed_linkage_or_test_messages ||= []
failed_linkage_or_test_messages << "linkage failed" unless steps.last.passed?
if steps.last.passed?
# Check for opportunistic linkage. Ignore failures because
# they can be unavoidable but we still want to know about them.
test "brew", "linkage", "--cached", "--test", "--strict",
named_args: formula_name,
ignore_failures: !args.test_default_formula?
end
test "brew", "linkage", "--cached", formula_name
@linkage_output_path.write(Formatter.headline(steps.last.command_trimmed, color: :blue), mode: "a")
@linkage_output_path.write("\n", mode: "a")
@linkage_output_path.write(steps.last.output, mode: "a")
test "brew", "install", "--only-dependencies", "--include-test", formula_name
if formula.test_defined?
env = {}
env["HOMEBREW_GIT_PATH"] = nil if deps.any? do |d|
d.name == "git" && (!d.build? || d.test?)
end
# Intentionally not passing --retry here to avoid papering over
# flaky tests when a formula isn't being pulled in as a dependent.
test("brew", "test", "--verbose", named_args: formula_name, env:, ignore_failures:, report_analytics: true)
failed_linkage_or_test_messages << "test failed" unless steps.last.passed?
end
# Move bottle and don't test dependents if the formula linkage or test failed.
if failed_linkage_or_test_messages.present?
if @bottle_filename
failed_dir = @bottle_filename.dirname/"failed"
moved_artifacts = [@bottle_filename, @bottle_json_filename].map(&:realpath)
failed_dir.install moved_artifacts
moved_artifacts.each do |old_location|
new_location = (failed_dir/old_location.basename).realpath
@bottle_checksums[new_location] = @bottle_checksums.fetch(old_location)
@bottle_checksums.delete(old_location)
end
end
if ignore_failures
skipped formula_name, failed_linkage_or_test_messages.join(", ")
else
failed formula_name, failed_linkage_or_test_messages.join(", ")
end
end
ensure
cleanup_bottle_etc_var(formula) if args.cleanup?
test "brew", "uninstall", "--force", *@unchanged_dependencies if @unchanged_dependencies.present?
end
def deleted_formula!(formula_name)
test_header(:Formulae, method: "deleted_formula!(#{formula_name})")
test "brew", "uses",
"--eval-all",
"--include-build",
"--include-optional",
"--include-test",
formula_name
end
end
end
end