Files
brew/Library/Homebrew/simulate_system.rb
T
Mike McQuaid e678490656 Fix cask Linux installs without os stanza
- `Cask#to_hash_with_variations` skipped all Linux variations for
  casks declaring Linux support only inside `on_linux`/`on_system`
  blocks, e.g. `zen`, as the API JSON is generated on macOS where
  those blocks are invisible; the published JSON then fell back to
  macOS artifacts on Linux and installs failed with a misleading
  "This cask requires macOS." error. Track `on_linux`/`on_system`
  blocks and emit Linux variations for casks that have them.
- `sha256` raised for an architecture missing a checksum for the
  running OS, e.g. `unity-hub` on `arm64_linux`, so its variation
  was silently dropped and ARM64 Linux fell back to macOS data too.
  Raise only on the real system: under `SimulateSystem` simulation
  `sha256` is now nil so variations for missing architectures carry
  their `on_linux` `depends_on arch:` and artifacts and Linux users
  get the correct unsupported-architecture error instead.
- `depends_on macos:` inside an `on_arm`/`on_intel` block did not
  mark a cask macOS-only, but arch blocks are evaluated on every OS
  so the dependency applies there too. Casks like `qlc+` that only
  declare macOS inside arch blocks claimed to support Linux while
  carrying a macOS requirement. Only OS blocks now scope a
  dependency to one OS; the checks for combining macOS `depends_on`
  forms still treat every `on_system` block alike.
- `Readall.valid_casks?` skipped casks whose files textually matched
  `depends_on macos:` anywhere, including inside `on_macos` blocks,
  so cross-OS casks like `unity-hub` were never validated for Linux.
  Rely on `supports_linux?` instead and let `depends_on arch:` excuse
  architectures a cask deliberately omits Linux checksums for.
- Add `appimagedir` to the test cask config so `app_image` artifacts
  can be serialised in tests.

Fixes https://github.com/Homebrew/brew/issues/23427.
2026-08-04 19:28:30 +01:00

116 lines
2.8 KiB
Ruby

# typed: strict
# frozen_string_literal: true
require "hardware"
require "macos_version"
require "utils/bottles"
module Homebrew
# Helper module for simulating different system configurations.
class SimulateSystem
class << self
sig { returns(T.nilable(Symbol)) }
attr_reader :arch
sig { returns(T.nilable(Symbol)) }
attr_reader :os
sig { returns(T::Hash[Symbol, Symbol]) }
def arch_symbols
{ arm64: :arm, x86_64: :intel }.freeze
end
sig {
type_parameters(:U).params(
os: Symbol,
arch: Symbol,
_block: T.proc.returns(T.type_parameter(:U)),
).returns(T.type_parameter(:U))
}
def with(os: T.unsafe(nil), arch: T.unsafe(nil), &_block)
raise ArgumentError, "At least one of `os` or `arch` must be specified." if !os && !arch
old_os = self.os
old_arch = self.arch
begin
self.os = os if os && os != current_os
self.arch = arch if arch && arch != current_arch
yield
ensure
@os = old_os
@arch = old_arch
end
end
sig {
type_parameters(:U).params(
tag: Utils::Bottles::Tag,
block: T.proc.returns(T.type_parameter(:U)),
).returns(T.type_parameter(:U))
}
def with_tag(tag, &block)
raise ArgumentError, "Invalid tag: #{tag}" unless tag.valid_combination?
with(os: tag.system, arch: tag.arch, &block)
end
sig { params(new_os: Symbol).void }
def os=(new_os)
os_options = [:macos, :linux, *MacOSVersion::SYMBOLS.keys]
raise "Unknown OS: #{new_os}" unless os_options.include?(new_os)
@os = T.let(new_os, T.nilable(Symbol))
end
sig { params(new_arch: Symbol).void }
def arch=(new_arch)
raise "New arch must be :arm or :intel" unless OnSystem::ARCH_OPTIONS.include?(new_arch)
@arch = T.let(new_arch, T.nilable(Symbol))
end
sig { void }
def clear
@os = @arch = nil
end
sig { returns(T::Boolean) }
def simulating?
os.present? || arch.present?
end
sig { returns(T::Boolean) }
def simulating_or_running_on_macos?
[:macos, *MacOSVersion::SYMBOLS.keys].include?(os)
end
sig { returns(T::Boolean) }
def simulating_or_running_on_linux?
os == :linux
end
sig { returns(Symbol) }
def current_arch
@arch || Hardware::CPU.type
end
sig { returns(Symbol) }
def current_os
os || :generic
end
sig { returns(Utils::Bottles::Tag) }
def current_tag
Utils::Bottles::Tag.new(
system: current_os,
arch: current_arch,
)
end
end
end
end
require "extend/os/simulate_system"