2023-04-03 22:27:46 +08:00
|
|
|
# typed: strict
|
2023-04-03 20:36:45 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cli/parser"
|
2023-04-04 13:54:35 +08:00
|
|
|
require "formula"
|
|
|
|
|
|
|
|
class TestRunnerFormula
|
|
|
|
extend T::Sig
|
|
|
|
|
|
|
|
sig { returns(String) }
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
sig { returns(Formula) }
|
|
|
|
attr_reader :formula
|
|
|
|
|
2023-04-04 23:58:32 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
attr_reader :eval_all
|
|
|
|
|
|
|
|
sig { params(formula: Formula, eval_all: T::Boolean).void }
|
|
|
|
def initialize(formula, eval_all: Homebrew::EnvConfig.eval_all?)
|
2023-04-04 16:19:46 +08:00
|
|
|
@formula = T.let(formula, Formula)
|
|
|
|
@name = T.let(formula.name, String)
|
|
|
|
@dependent_hash = T.let({}, T::Hash[Symbol, T::Array[TestRunnerFormula]])
|
2023-04-04 23:58:32 +08:00
|
|
|
@eval_all = T.let(eval_all, T::Boolean)
|
2023-04-04 13:54:35 +08:00
|
|
|
freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def macos_only?
|
|
|
|
formula.requirements.any? { |r| r.is_a?(MacOSRequirement) && !r.version_specified? }
|
|
|
|
end
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def macos_compatible?
|
|
|
|
!linux_only?
|
|
|
|
end
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def linux_only?
|
|
|
|
formula.requirements.any?(LinuxRequirement)
|
|
|
|
end
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def linux_compatible?
|
|
|
|
!macos_only?
|
|
|
|
end
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def x86_64_only?
|
|
|
|
formula.requirements.any? { |r| r.is_a?(ArchRequirement) && (r.arch == :x86_64) }
|
|
|
|
end
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def x86_64_compatible?
|
|
|
|
!arm64_only?
|
|
|
|
end
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def arm64_only?
|
|
|
|
formula.requirements.any? { |r| r.is_a?(ArchRequirement) && (r.arch == :arm64) }
|
|
|
|
end
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def arm64_compatible?
|
|
|
|
!x86_64_only?
|
|
|
|
end
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
sig { returns(T.nilable(MacOSRequirement)) }
|
|
|
|
def versioned_macos_requirement
|
|
|
|
formula.requirements.find { |r| r.is_a?(MacOSRequirement) && r.version_specified? }
|
|
|
|
end
|
|
|
|
|
2023-04-04 23:10:51 +08:00
|
|
|
sig { params(macos_version: OS::Mac::Version).returns(T::Boolean) }
|
2023-04-04 13:54:35 +08:00
|
|
|
def compatible_with?(macos_version)
|
|
|
|
# Assign to a variable to assist type-checking.
|
|
|
|
requirement = versioned_macos_requirement
|
|
|
|
return true if requirement.blank?
|
|
|
|
|
|
|
|
macos_version.public_send(requirement.comparator, requirement.version)
|
|
|
|
end
|
2023-04-04 16:19:46 +08:00
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
SIMULATE_SYSTEM_SYMBOLS = T.let({ arm64: :arm, x86_64: :intel }.freeze, T::Hash[Symbol, Symbol])
|
2023-04-04 23:58:32 +08:00
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
platform: Symbol,
|
|
|
|
arch: Symbol,
|
|
|
|
macos_version: T.nilable(Symbol),
|
|
|
|
).returns(T::Array[TestRunnerFormula])
|
|
|
|
}
|
|
|
|
def dependents(platform:, arch:, macos_version:)
|
|
|
|
cache_key = :"#{platform}_#{arch}_#{macos_version}"
|
|
|
|
|
|
|
|
@dependent_hash.fetch(cache_key) do
|
|
|
|
all = eval_all || Homebrew::EnvConfig.eval_all?
|
|
|
|
formula_selector, eval_all_env = if all
|
|
|
|
[:all, "1"]
|
|
|
|
else
|
|
|
|
[:installed, nil]
|
|
|
|
end
|
2023-04-04 16:19:46 +08:00
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
with_env(HOMEBREW_EVAL_ALL: eval_all_env) do
|
|
|
|
Formulary.clear_cache
|
|
|
|
Homebrew::SimulateSystem.arch = SIMULATE_SYSTEM_SYMBOLS.fetch(arch)
|
|
|
|
Homebrew::SimulateSystem.os = macos_version || platform
|
|
|
|
|
|
|
|
Formula.send(formula_selector)
|
|
|
|
.select { |candidate_f| candidate_f.deps.map(&:name).include?(name) }
|
|
|
|
.map { |f| TestRunnerFormula.new(f, eval_all: all) }
|
|
|
|
.freeze
|
|
|
|
ensure
|
|
|
|
Homebrew::SimulateSystem.clear
|
|
|
|
end
|
|
|
|
end
|
2023-04-04 16:19:46 +08:00
|
|
|
end
|
2023-04-04 13:54:35 +08:00
|
|
|
end
|
2023-04-03 20:36:45 +08:00
|
|
|
|
|
|
|
module Homebrew
|
2023-04-03 22:27:46 +08:00
|
|
|
extend T::Sig
|
2023-04-03 20:36:45 +08:00
|
|
|
|
2023-04-03 22:27:46 +08:00
|
|
|
sig { returns(Homebrew::CLI::Parser) }
|
|
|
|
def self.determine_test_runners_args
|
2023-04-03 20:36:45 +08:00
|
|
|
Homebrew::CLI::Parser.new do
|
|
|
|
usage_banner <<~EOS
|
|
|
|
`determine-test-runners` <testing-formulae> [<deleted-formulae>]
|
|
|
|
|
|
|
|
Determines the runners used to test formulae or their dependents.
|
|
|
|
EOS
|
2023-04-04 23:58:32 +08:00
|
|
|
switch "--eval-all",
|
|
|
|
description: "Evaluate all available formulae, whether installed or not, to determine testing " \
|
|
|
|
"dependents."
|
2023-04-03 20:36:45 +08:00
|
|
|
switch "--dependents",
|
2023-04-04 23:58:32 +08:00
|
|
|
description: "Determine runners for testing dependents. Requires `--eval-all` or `HOMEBREW_EVAL_ALL`."
|
2023-04-03 20:36:45 +08:00
|
|
|
|
|
|
|
named_args min: 1, max: 2
|
|
|
|
|
|
|
|
hide_from_man_page!
|
|
|
|
end
|
|
|
|
end
|
2023-04-05 01:09:56 +08:00
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
sig {
|
|
|
|
params(
|
2023-04-05 01:09:56 +08:00
|
|
|
testing_formulae: T::Array[TestRunnerFormula],
|
|
|
|
platform: Symbol,
|
|
|
|
arch: Symbol,
|
|
|
|
macos_version: T.nilable(OS::Mac::Version),
|
2023-04-04 16:19:46 +08:00
|
|
|
).returns(T::Boolean)
|
2023-04-04 13:54:35 +08:00
|
|
|
}
|
2023-04-05 01:09:56 +08:00
|
|
|
def self.formulae_have_untested_dependents?(testing_formulae, platform:, arch:, macos_version:)
|
2023-04-04 16:19:46 +08:00
|
|
|
testing_formulae.any? do |formula|
|
|
|
|
# If the formula has a platform/arch/macOS version requirement, then its
|
|
|
|
# dependents don't need to be tested if these requirements are not satisfied.
|
2023-04-05 01:09:56 +08:00
|
|
|
next false unless formula.send(:"#{platform}_compatible?")
|
|
|
|
next false unless formula.send(:"#{arch}_compatible?")
|
|
|
|
next false if macos_version && !formula.compatible_with?(macos_version)
|
2023-04-04 16:19:46 +08:00
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
compatible_dependents = formula.dependents(platform: platform, arch: arch, macos_version: macos_version&.to_sym)
|
|
|
|
.dup
|
2023-04-04 16:19:46 +08:00
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
compatible_dependents.select! { |dependent_f| dependent_f.send(:"#{platform}_compatible?") }
|
|
|
|
compatible_dependents.select! { |dependent_f| dependent_f.send(:"#{arch}_compatible?") }
|
|
|
|
compatible_dependents.select! { |dependent_f| dependent_f.compatible_with?(macos_version) } if macos_version
|
2023-04-04 16:19:46 +08:00
|
|
|
|
|
|
|
(compatible_dependents - testing_formulae).present?
|
|
|
|
end
|
2023-04-04 13:54:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
sig {
|
|
|
|
params(
|
2023-04-05 01:09:56 +08:00
|
|
|
formulae: T::Array[TestRunnerFormula],
|
|
|
|
dependents: T::Boolean,
|
|
|
|
deleted_formulae: T.nilable(T::Array[String]),
|
|
|
|
platform: Symbol,
|
|
|
|
arch: Symbol,
|
|
|
|
macos_version: T.nilable(OS::Mac::Version),
|
2023-04-04 13:54:35 +08:00
|
|
|
).returns(T::Boolean)
|
|
|
|
}
|
2023-04-05 01:09:56 +08:00
|
|
|
def self.add_runner?(formulae, dependents:, deleted_formulae:, platform:, arch:, macos_version: nil)
|
2023-04-04 13:54:35 +08:00
|
|
|
if dependents
|
|
|
|
formulae_have_untested_dependents?(
|
|
|
|
formulae,
|
2023-04-05 01:09:56 +08:00
|
|
|
platform: platform,
|
|
|
|
arch: arch,
|
|
|
|
macos_version: macos_version,
|
2023-04-04 13:54:35 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
return true if deleted_formulae.present?
|
|
|
|
|
|
|
|
compatible_formulae = formulae.dup
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
compatible_formulae.select! { |formula| formula.send(:"#{platform}_compatible?") }
|
|
|
|
compatible_formulae.select! { |formula| formula.send(:"#{arch}_compatible?") }
|
|
|
|
compatible_formulae.select! { |formula| formula.compatible_with?(macos_version) } if macos_version
|
2023-04-04 13:54:35 +08:00
|
|
|
|
|
|
|
compatible_formulae.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-03 22:27:46 +08:00
|
|
|
sig { void }
|
|
|
|
def self.determine_test_runners
|
2023-04-04 13:54:35 +08:00
|
|
|
args = determine_test_runners_args.parse
|
2023-04-04 16:19:46 +08:00
|
|
|
|
2023-04-04 23:58:32 +08:00
|
|
|
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
|
|
|
|
|
|
|
odie "`--dependents` requires `--eval-all` or `HOMEBREW_EVAL_ALL`!" if args.dependents? && !eval_all
|
|
|
|
|
2023-04-04 16:19:46 +08:00
|
|
|
Formulary.enable_factory_cache!
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
testing_formulae = args.named.first.split(",")
|
2023-04-04 23:58:32 +08:00
|
|
|
testing_formulae.map! { |name| TestRunnerFormula.new(Formula[name], eval_all: eval_all) }
|
2023-04-04 13:54:35 +08:00
|
|
|
.freeze
|
|
|
|
deleted_formulae = args.named.second&.split(",")
|
|
|
|
|
|
|
|
runners = []
|
|
|
|
|
|
|
|
linux_runner = ENV.fetch("HOMEBREW_LINUX_RUNNER") { raise "HOMEBREW_LINUX_RUNNER is not defined" }
|
|
|
|
linux_cleanup = ENV.fetch("HOMEBREW_LINUX_CLEANUP") { raise "HOMEBREW_LINUX_CLEANUP is not defined" }
|
|
|
|
|
|
|
|
linux_runner_spec = {
|
|
|
|
runner: linux_runner,
|
|
|
|
container: {
|
|
|
|
image: "ghcr.io/homebrew/ubuntu22.04:master",
|
|
|
|
options: "--user=linuxbrew -e GITHUB_ACTIONS_HOMEBREW_SELF_HOSTED",
|
|
|
|
},
|
|
|
|
workdir: "/github/home",
|
|
|
|
timeout: 4320,
|
|
|
|
cleanup: linux_cleanup == "true",
|
|
|
|
}
|
|
|
|
|
2023-04-04 16:19:46 +08:00
|
|
|
if add_runner?(
|
|
|
|
testing_formulae,
|
2023-04-05 01:09:56 +08:00
|
|
|
platform: :linux,
|
|
|
|
arch: :x86_64,
|
2023-04-04 16:19:46 +08:00
|
|
|
deleted_formulae: deleted_formulae,
|
|
|
|
dependents: args.dependents?,
|
|
|
|
)
|
|
|
|
runners << linux_runner_spec
|
|
|
|
end
|
|
|
|
|
|
|
|
github_run_id = ENV.fetch("GITHUB_RUN_ID") { raise "GITHUB_RUN_ID is not defined" }
|
|
|
|
github_run_attempt = ENV.fetch("GITHUB_RUN_ATTEMPT") { raise "GITHUB_RUN_ATTEMPT is not defined" }
|
|
|
|
ephemeral_suffix = "-#{github_run_id}-#{github_run_attempt}"
|
|
|
|
|
2023-04-05 01:09:56 +08:00
|
|
|
MacOSVersions::SYMBOLS.each_value do |version|
|
2023-04-04 23:10:51 +08:00
|
|
|
macos_version = OS::Mac::Version.new(version)
|
2023-04-04 16:19:46 +08:00
|
|
|
next if macos_version.outdated_release? || macos_version.prerelease?
|
|
|
|
|
2023-04-04 13:54:35 +08:00
|
|
|
if add_runner?(
|
|
|
|
testing_formulae,
|
2023-04-05 01:09:56 +08:00
|
|
|
platform: :macos,
|
|
|
|
arch: :x86_64,
|
|
|
|
macos_version: macos_version,
|
|
|
|
deleted_formulae: deleted_formulae,
|
|
|
|
dependents: args.dependents?,
|
2023-04-04 13:54:35 +08:00
|
|
|
)
|
2023-04-04 16:19:46 +08:00
|
|
|
runners << { runner: "#{version}#{ephemeral_suffix}", cleanup: false }
|
2023-04-04 13:54:35 +08:00
|
|
|
end
|
|
|
|
|
2023-04-04 16:19:46 +08:00
|
|
|
next unless add_runner?(
|
2023-04-04 13:54:35 +08:00
|
|
|
testing_formulae,
|
2023-04-05 01:09:56 +08:00
|
|
|
platform: :macos,
|
|
|
|
arch: :arm64,
|
|
|
|
macos_version: macos_version,
|
|
|
|
deleted_formulae: deleted_formulae,
|
|
|
|
dependents: args.dependents?,
|
2023-04-04 13:54:35 +08:00
|
|
|
)
|
2023-04-04 16:19:46 +08:00
|
|
|
|
|
|
|
runner_name = "#{version}-arm64"
|
|
|
|
# Use bare metal runner when testing dependents on Monterey.
|
|
|
|
if macos_version >= :ventura || (macos_version >= :monterey && !args.dependents?)
|
|
|
|
runners << { runner: "#{runner_name}#{ephemeral_suffix}", cleanup: false }
|
|
|
|
elsif macos_version >= :big_sur
|
|
|
|
runners << { runner: runner_name, cleanup: true }
|
2023-04-04 13:54:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if !args.dependents? && runners.blank?
|
|
|
|
# If there are no tests to run, add a runner that is meant to do nothing
|
|
|
|
# to support making the `tests` job a required status check.
|
|
|
|
runners << { runner: "ubuntu-latest", no_op: true }
|
|
|
|
end
|
|
|
|
|
|
|
|
github_output = ENV.fetch("GITHUB_OUTPUT") { raise "GITHUB_OUTPUT is not defined" }
|
|
|
|
File.open(github_output, "a") do |f|
|
|
|
|
f.puts("runners=#{runners.to_json}")
|
|
|
|
f.puts("runners_present=#{runners.present?}")
|
|
|
|
end
|
2023-04-03 20:36:45 +08:00
|
|
|
end
|
|
|
|
end
|