diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 89a7c19ad9..c30dfa7c22 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -6,6 +6,7 @@ require "cask/config" require "cask/dsl" require "cask/metadata" require "searchable" +require "utils/bottles" module Cask # An instance of a cask. @@ -58,6 +59,10 @@ module Cask def config=(config) @config = config + refresh + end + + def refresh @dsl = DSL.new(self) return unless @block @@ -89,7 +94,7 @@ module Cask version_os_hash = {} actual_version = MacOS.full_version.to_s - MacOS::Version::SYMBOLS.each do |os_name, os_version| + MacOSVersions::SYMBOLS.each do |os_name, os_version| MacOS.full_version = os_version cask = CaskLoader.load(token) version_os_hash[os_name] = cask.version if cask.version != version @@ -214,7 +219,7 @@ module Cask end alias == eql? - def to_h + def to_hash { "token" => token, "full_token" => full_name, @@ -238,6 +243,42 @@ module Cask } end + def to_h + hash = to_hash + variations = {} + + hash_keys_to_skip = %w[outdated installed versions] + + if @dsl.on_system_blocks_exist? + [:arm, :intel].each do |arch| + MacOSVersions::SYMBOLS.each_key do |os_name| + # Big Sur is the first version of macOS that supports arm + next if arch == :arm && MacOS::Version.from_symbol(os_name) < MacOS::Version.from_symbol(:big_sur) + + Homebrew::SimulateSystem.os = os_name + Homebrew::SimulateSystem.arch = arch + + refresh + + bottle_tag = ::Utils::Bottles::Tag.new(system: os_name, arch: arch).to_sym + to_hash.each do |key, value| + next if hash_keys_to_skip.include? key + next if value.to_s == hash[key].to_s + + variations[bottle_tag] ||= {} + variations[bottle_tag][key] = value + end + end + end + end + + Homebrew::SimulateSystem.clear + refresh + + hash["variations"] = variations + hash + end + private def to_h_string_gsubs(string) diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index e4d1ac47a2..6367141d8c 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -25,6 +25,8 @@ require "cask/dsl/version" require "cask/url" require "cask/utils" +require "extend/on_system" + module Cask # Class representing the domain-specific language used for casks. # @@ -89,8 +91,13 @@ module Cask *ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] }, ]).freeze + extend Predicable + include OnSystem + attr_reader :cask, :token + attr_predicate :on_system_blocks_exist? + def initialize(cask) @cask = cask @token = cask.token @@ -112,10 +119,17 @@ module Cask def set_unique_stanza(stanza, should_return) return instance_variable_get("@#{stanza}") if should_return - if !@cask.allow_reassignment && instance_variable_defined?("@#{stanza}") - raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.") + unless @cask.allow_reassignment + if instance_variable_defined?("@#{stanza}") && !@called_in_on_system_block + raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.") + end + + if instance_variable_defined?("@#{stanza}_set_in_block") && @called_in_on_system_block + raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only be overridden once.") + end end + instance_variable_set("@#{stanza}_set_in_block", true) if @called_in_on_system_block instance_variable_set("@#{stanza}", yield) rescue CaskInvalidError raise diff --git a/Library/Homebrew/cask/dsl/depends_on.rb b/Library/Homebrew/cask/dsl/depends_on.rb index d7ac942dfd..1d4e3a2448 100644 --- a/Library/Homebrew/cask/dsl/depends_on.rb +++ b/Library/Homebrew/cask/dsl/depends_on.rb @@ -55,7 +55,7 @@ module Cask begin @macos = if args.count > 1 MacOSRequirement.new([args], comparator: "==") - elsif MacOS::Version::SYMBOLS.key?(args.first) + elsif MacOSVersions::SYMBOLS.key?(args.first) MacOSRequirement.new([args.first], comparator: "==") elsif /^\s*(?<|>|[=<>]=)\s*:(?\S+)\s*$/ =~ args.first MacOSRequirement.new([version.to_sym], comparator: comparator) diff --git a/Library/Homebrew/extend/on_os.rb b/Library/Homebrew/extend/on_os.rb deleted file mode 100644 index aacadd608e..0000000000 --- a/Library/Homebrew/extend/on_os.rb +++ /dev/null @@ -1,26 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - extend T::Sig - - # Block only executed on macOS. No-op on Linux. - #
on_macos do
-  # # Do something Mac-specific
-  # end
- sig { params(block: T.proc.void).void } - def on_macos(&block) - raise "No block content defined for 'on_macos' block" unless T.unsafe(block) - end - - # Block only executed on Linux. No-op on macOS. - #
on_linux do
-  # # Do something Linux-specific
-  # end
- sig { params(block: T.proc.void).void } - def on_linux(&block) - raise "No block content defined for 'on_linux' block" unless T.unsafe(block) - end -end - -require "extend/os/on_os" diff --git a/Library/Homebrew/extend/on_os.rbi b/Library/Homebrew/extend/on_os.rbi deleted file mode 100644 index a53474fd86..0000000000 --- a/Library/Homebrew/extend/on_os.rbi +++ /dev/null @@ -1,5 +0,0 @@ -# typed: strict - -module OnOS - include Kernel -end diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb new file mode 100644 index 0000000000..01e489b0f4 --- /dev/null +++ b/Library/Homebrew/extend/on_system.rb @@ -0,0 +1,103 @@ +# typed: false +# frozen_string_literal: true + +require "simulate_system" + +module OnSystem + extend T::Sig + + ARCH_OPTIONS = [:intel, :arm].freeze + BASE_OS_OPTIONS = [:macos, :linux].freeze + + module_function + + sig { params(arch: Symbol).returns(T::Boolean) } + def arch_condition_met?(arch) + raise ArgumentError, "Invalid arch condition: #{arch.inspect}" if ARCH_OPTIONS.exclude?(arch) + + current_arch = Homebrew::SimulateSystem.arch || Hardware::CPU.type + arch == current_arch + end + + sig { params(os_name: Symbol, or_condition: T.nilable(Symbol)).returns(T::Boolean) } + def os_condition_met?(os_name, or_condition = nil) + if Homebrew::EnvConfig.simulate_macos_on_linux? + return false if os_name == :linux + return true if [:macos, *MacOSVersions::SYMBOLS.keys].include?(os_name) + end + + if BASE_OS_OPTIONS.include?(os_name) + if Homebrew::SimulateSystem.none? + return OS.linux? if os_name == :linux + return OS.mac? if os_name == :macos + end + + return Homebrew::SimulateSystem.send("#{os_name}?") + end + + raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOSVersions::SYMBOLS.key?(os_name) + + if or_condition.present? && [:or_newer, :or_older].exclude?(or_condition) + raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}" + end + + base_os = MacOS::Version.from_symbol(os_name) + current_os = MacOS::Version.from_symbol(Homebrew::SimulateSystem.os || MacOS.version.to_sym) + + return current_os >= base_os if or_condition == :or_newer + return current_os <= base_os if or_condition == :or_older + + current_os == base_os + end + + sig { params(method_name: Symbol).returns(Symbol) } + def condition_from_method_name(method_name) + method_name.to_s.sub(/^on_/, "").to_sym + end + + sig { params(base: Class).void } + def self.included(base) + ARCH_OPTIONS.each do |arch| + base.define_method("on_#{arch}") do |&block| + @on_system_blocks_exist = true + + return unless OnSystem.arch_condition_met? OnSystem.condition_from_method_name(__method__) + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + + BASE_OS_OPTIONS.each do |base_os| + base.define_method("on_#{base_os}") do |&block| + @on_system_blocks_exist = true + + return unless OnSystem.os_condition_met? OnSystem.condition_from_method_name(__method__) + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + + MacOSVersions::SYMBOLS.each_key do |os_name| + base.define_method("on_#{os_name}") do |or_condition = nil, &block| + @on_system_blocks_exist = true + + os_condition = OnSystem.condition_from_method_name __method__ + return unless OnSystem.os_condition_met? os_condition, or_condition + + @called_in_on_system_block = true + result = block.call + @called_in_on_system_block = false + + result + end + end + end +end diff --git a/Library/Homebrew/extend/os/linux/on_os.rb b/Library/Homebrew/extend/os/linux/on_os.rb deleted file mode 100644 index 4373a45ab2..0000000000 --- a/Library/Homebrew/extend/os/linux/on_os.rb +++ /dev/null @@ -1,10 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - def on_linux(&block) - raise "No block content defined for 'on_linux' block" unless T.unsafe(block) - - yield - end -end diff --git a/Library/Homebrew/extend/os/mac/on_os.rb b/Library/Homebrew/extend/os/mac/on_os.rb deleted file mode 100644 index 78759c1a9d..0000000000 --- a/Library/Homebrew/extend/os/mac/on_os.rb +++ /dev/null @@ -1,10 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module OnOS - def on_macos(&block) - raise "No block content defined for 'on_macos' block" unless T.unsafe(block) - - yield - end -end diff --git a/Library/Homebrew/extend/os/on_os.rb b/Library/Homebrew/extend/os/on_os.rb deleted file mode 100644 index c74ea9d459..0000000000 --- a/Library/Homebrew/extend/os/on_os.rb +++ /dev/null @@ -1,8 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -if OS.mac? || Homebrew::EnvConfig.simulate_macos_on_linux? - require "extend/os/mac/on_os" -elsif OS.linux? - require "extend/os/linux/on_os" -end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 25ff2c3920..09ede72647 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -28,7 +28,7 @@ require "tab" require "mktemp" require "find" require "utils/spdx" -require "extend/on_os" +require "extend/on_system" require "api" # A formula provides instructions and metadata for Homebrew to install a piece @@ -64,7 +64,7 @@ class Formula include Utils::Shebang include Utils::Shell include Context - include OnOS + include OnSystem extend Forwardable extend Cachable extend Predicable @@ -2470,7 +2470,7 @@ class Formula # The methods below define the formula DSL. class << self include BuildEnvironment::DSL - include OnOS + include OnSystem def method_added(method) super diff --git a/Library/Homebrew/formula.rbi b/Library/Homebrew/formula.rbi index c573f64b3c..f77f4c0c50 100644 --- a/Library/Homebrew/formula.rbi +++ b/Library/Homebrew/formula.rbi @@ -48,4 +48,7 @@ class Formula def env; end def conflicts; end + + # This method is included by `OnSystem` + def self.on_macos(&block); end end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 08a898c3aa..1a11464f69 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -72,6 +72,7 @@ HOMEBREW_BOTTLES_EXTNAME_REGEX = /\.([a-z0-9_]+)\.bottle\.(?:(\d+)\.)?tar\.gz$/. require "env_config" require "compat/early" unless Homebrew::EnvConfig.no_compat? +require "macos_versions" require "os" require "messages" require "default_prefix" diff --git a/Library/Homebrew/macos_versions.rb b/Library/Homebrew/macos_versions.rb new file mode 100644 index 0000000000..16d4bd47e4 --- /dev/null +++ b/Library/Homebrew/macos_versions.rb @@ -0,0 +1,20 @@ +# typed: true +# frozen_string_literal: true + +# Helper functions for querying operating system information. +# +# @api private +module MacOSVersions + # TODO: when removing symbols here, ensure that they are added to + # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. + SYMBOLS = { + ventura: "13", + monterey: "12", + big_sur: "11", + catalina: "10.15", + mojave: "10.14", + high_sierra: "10.13", + sierra: "10.12", + el_capitan: "10.11", + }.freeze +end diff --git a/Library/Homebrew/os/linux.rb b/Library/Homebrew/os/linux.rb index 6c029ef8be..277e3de3a9 100644 --- a/Library/Homebrew/os/linux.rb +++ b/Library/Homebrew/os/linux.rb @@ -31,11 +31,7 @@ module OS module Mac module_function - # rubocop:disable Naming/ConstantName - # rubocop:disable Style/MutableConstant ::MacOS = OS::Mac - # rubocop:enable Naming/ConstantName - # rubocop:enable Style/MutableConstant raise "Loaded OS::Linux on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] diff --git a/Library/Homebrew/os/mac.rb b/Library/Homebrew/os/mac.rb index bb75f0de9c..5f422450e9 100644 --- a/Library/Homebrew/os/mac.rb +++ b/Library/Homebrew/os/mac.rb @@ -13,11 +13,7 @@ module OS module_function - # rubocop:disable Naming/ConstantName - # rubocop:disable Style/MutableConstant ::MacOS = OS::Mac - # rubocop:enable Naming/ConstantName - # rubocop:enable Style/MutableConstant raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] diff --git a/Library/Homebrew/os/mac/version.rb b/Library/Homebrew/os/mac/version.rb index 130d1c4f63..5f86f697a2 100644 --- a/Library/Homebrew/os/mac/version.rb +++ b/Library/Homebrew/os/mac/version.rb @@ -13,22 +13,10 @@ module OS class Version < ::Version extend T::Sig - # TODO: when removing symbols here, ensure that they are added to - # DEPRECATED_MACOS_VERSIONS in MacOSRequirement. - SYMBOLS = { - ventura: "13", - monterey: "12", - big_sur: "11", - catalina: "10.15", - mojave: "10.14", - high_sierra: "10.13", - sierra: "10.12", - el_capitan: "10.11", - }.freeze - # TODO: bump version when new macOS is released or announced - # and also update references in docs/Installation.md and - # https://github.com/Homebrew/install/blob/HEAD/install.sh + # and also update references in docs/Installation.md, + # https://github.com/Homebrew/install/blob/HEAD/install.sh and + # MacOSVersions::SYMBOLS NEWEST_UNSUPPORTED = "13" private_constant :NEWEST_UNSUPPORTED @@ -42,7 +30,7 @@ module OS sig { params(version: Symbol).returns(T.attached_class) } def self.from_symbol(version) - str = SYMBOLS.fetch(version) { raise MacOSVersionError, version } + str = MacOSVersions::SYMBOLS.fetch(version) { raise MacOSVersionError, version } new(str) end @@ -60,10 +48,10 @@ module OS sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } def <=>(other) @comparison_cache.fetch(other) do - if SYMBOLS.key?(other) && to_sym == other + if MacOSVersions::SYMBOLS.key?(other) && to_sym == other 0 else - v = SYMBOLS.fetch(other) { other.to_s } + v = MacOSVersions::SYMBOLS.fetch(other) { other.to_s } @comparison_cache[other] = super(::Version.new(v)) end end @@ -81,7 +69,7 @@ module OS sig { returns(Symbol) } def to_sym - @to_sym ||= SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) + @to_sym ||= MacOSVersions::SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) end sig { returns(String) } diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 49d9e362a8..9d407c5aab 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -5,7 +5,7 @@ require "download_strategy" require "checksum" require "version" require "mktemp" -require "extend/on_os" +require "extend/on_system" # Resource is the fundamental representation of an external resource. The # primary formula download, along with other declared resources, are instances @@ -17,7 +17,7 @@ class Resource include Context include FileUtils - include OnOS + include OnSystem attr_reader :mirrors, :specs, :using, :source_modified_time, :patches, :owner attr_writer :version diff --git a/Library/Homebrew/simulate_system.rb b/Library/Homebrew/simulate_system.rb new file mode 100644 index 0000000000..d52a2af752 --- /dev/null +++ b/Library/Homebrew/simulate_system.rb @@ -0,0 +1,50 @@ +# typed: true +# frozen_string_literal: true + +module Homebrew + # Helper module for simulating different system condfigurations. + # + # @api private + class SimulateSystem + class << self + extend T::Sig + + attr_reader :os, :arch + + sig { params(new_os: Symbol).void } + def os=(new_os) + os_options = [:macos, :linux, *MacOSVersions::SYMBOLS.keys] + raise "Unknown OS: #{new_os}" unless os_options.include?(new_os) + + @os = new_os + end + + sig { params(new_arch: Symbol).void } + def arch=(new_arch) + raise "New arch must be :arm or :intel" unless [:arm, :intel].include?(new_arch) + + @arch = new_arch + end + + sig { void } + def clear + @os = @arch = nil + end + + sig { returns(T::Boolean) } + def none? + @os.nil? && @arch.nil? + end + + sig { returns(T::Boolean) } + def macos? + [:macos, *MacOSVersions::SYMBOLS.keys].include?(@os) + end + + sig { returns(T::Boolean) } + def linux? + @os == :linux + end + end + end +end diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 437af9f0a8..35734f1aa5 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -12,13 +12,13 @@ require "utils/bottles" require "patch" require "compilers" require "os/mac/version" -require "extend/on_os" +require "extend/on_system" class SoftwareSpec extend T::Sig extend Forwardable - include OnOS + include OnSystem PREDEFINED_OPTIONS = { universal: Option.new("universal", "Build a universal binary"), @@ -583,7 +583,7 @@ class BottleSpecification end class PourBottleCheck - include OnOS + include OnSystem def initialize(formula) @formula = formula diff --git a/Library/Homebrew/test/cask/cmd/list_spec.rb b/Library/Homebrew/test/cask/cmd/list_spec.rb index 81f7a83c8e..77b6e7af89 100644 --- a/Library/Homebrew/test/cask/cmd/list_spec.rb +++ b/Library/Homebrew/test/cask/cmd/list_spec.rb @@ -120,7 +120,9 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } }, { "token": "local-transmission", @@ -149,7 +151,9 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } }, { "token": "multiple-versions", @@ -160,11 +164,13 @@ describe Cask::Cmd::List, :cask do ], "desc": null, "homepage": "https://brew.sh/", - "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip", + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.3/arm.zip", "appcast": null, "version": "1.2.3", "versions": { - "test_os": "1.2.0" + "big_sur": "1.2.0", + "catalina": "1.0.0", + "mojave": "1.0.0" }, "installed": "1.2.3", "outdated": false, @@ -179,7 +185,32 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + "arm_big_sur": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.0/arm.zip", + "version": "1.2.0", + "sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + }, + "intel_monterey": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.3/intel.zip" + }, + "intel_big_sur": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.2.0/intel.zip", + "version": "1.2.0", + "sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + }, + "intel_catalina": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.0.0/intel.zip", + "version": "1.0.0", + "sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + }, + "intel_mojave": { + "url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/1.0.0/intel.zip", + "version": "1.0.0", + "sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + } + } }, { "token": "third-party-cask", @@ -208,19 +239,34 @@ describe Cask::Cmd::List, :cask do }, "conflicts_with": null, "container": null, - "auto_updates": null + "auto_updates": null, + "variations": { + } } ] EOS } + let(:original_macos_version) { MacOS.full_version.to_s } before do - casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) + # Use a more limited symbols list to shorten the variations hash + symbols = { + monterey: "12", + big_sur: "11", + catalina: "10.15", + mojave: "10.14", + } + stub_const("MacOSVersions::SYMBOLS", symbols) - # Add a test OS to ensure that all cask versions are listed regardless of OS. - symbols = MacOS::Version::SYMBOLS.dup - symbols[:test_os] = "10.9" - stub_const("MacOS::Version::SYMBOLS", symbols) + # For consistency, always run on Monterey and ARM + MacOS.full_version = "12" + allow(Hardware::CPU).to receive(:type).and_return(:arm) + + casks.map(&Cask::CaskLoader.method(:load)).each(&InstallHelper.method(:install_with_caskfile)) + end + + after do + MacOS.full_version = original_macos_version end it "of all installed Casks" do diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 8c5ee1b2f5..f9e3f93116 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -1580,4 +1580,117 @@ describe Formula do expect(f.test).to eq(2) end end + + describe "on_{os_version} blocks", :needs_macos do + before do + Homebrew::SimulateSystem.os = :monterey + end + + after do + Homebrew::SimulateSystem.clear + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_monterey :or_newer do + @test = 1 + end + on_big_sur do + @test = 2 + end + on_catalina :or_older do + @test = 3 + end + end + end.new + end + + it "only calls code within `on_monterey`" do + Homebrew::SimulateSystem.os = :monterey + f.brew { f.install } + expect(f.test).to eq(1) + end + + it "only calls code within `on_monterey :or_newer`" do + Homebrew::SimulateSystem.os = :ventura + f.brew { f.install } + expect(f.test).to eq(1) + end + + it "only calls code within `on_big_sur`" do + Homebrew::SimulateSystem.os = :big_sur + f.brew { f.install } + expect(f.test).to eq(2) + end + + it "only calls code within `on_catalina`" do + Homebrew::SimulateSystem.os = :catalina + f.brew { f.install } + expect(f.test).to eq(3) + end + + it "only calls code within `on_catalina :or_older`" do + Homebrew::SimulateSystem.os = :mojave + f.brew { f.install } + expect(f.test).to eq(3) + end + end + + describe "#on_arm" do + before do + allow(Hardware::CPU).to receive(:type).and_return(:arm) + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_arm do + @test = 1 + end + on_intel do + @test = 2 + end + end + end.new + end + + it "only calls code within on_arm" do + f.brew { f.install } + expect(f.test).to eq(1) + end + end + + describe "#on_intel" do + before do + allow(Hardware::CPU).to receive(:type).and_return(:intel) + end + + let(:f) do + Class.new(Testball) do + @test = 0 + attr_reader :test + + def install + on_arm do + @test = 1 + end + on_intel do + @test = 2 + end + end + end.new + end + + it "only calls code within on_intel" do + f.brew { f.install } + expect(f.test).to eq(2) + end + end end diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb index 0107d0ec8a..ad05491a2d 100644 --- a/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/multiple-versions.rb @@ -1,12 +1,23 @@ cask "multiple-versions" do - if MacOS.version == :test_os - version "1.2.0" - else - version "1.2.3" - end + arch = "arm" + version "1.2.3" sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94" - url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip" + on_intel do + arch = "intel" + end + + on_big_sur do + version "1.2.0" + sha256 "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b" + end + + on_catalina :or_older do + version "1.0.0" + sha256 "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216" + end + + url "file://#{TEST_FIXTURE_DIR}/cask/caffeine/#{version}/#{arch}.zip" homepage "https://brew.sh/" app "Caffeine.app"