From 473f448b224afc4626dfab75e27ee685355673f7 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sun, 8 Jun 2025 00:37:02 -0400 Subject: [PATCH] OnSystem: handle non-macOS current_os values The `OnSystem.os_condition_met?` method only handles Linux and macOS values for the current OS, so this fails when testing with a generic OS. This shortcoming is only being surfaced now because there weren't any tests for `OnSystem` before. This addresses the issue by accounting for macOS values (`:macos` or a symbol from `MacOSVersion::SYMBOLS`) and returning `false` for any other values (`:linux`, `:generic`, etc.). --- Library/Homebrew/extend/on_system.rb | 13 +++++++------ Library/Homebrew/test/extend/on_system_spec.rb | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index 35fedde1d1..a187cdeab7 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -77,16 +77,17 @@ module OnSystem raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}" end - return false if Homebrew::SimulateSystem.simulating_or_running_on_linux? - - base_os = MacOSVersion.from_symbol(os_name) - current_os = if Homebrew::SimulateSystem.current_os == :macos + current_os_symbol = Homebrew::SimulateSystem.current_os + current_os = if current_os_symbol == :macos # Assume the oldest macOS version when simulating a generic macOS version # Version::NULL is always treated as less than any other version. Version::NULL + elsif MacOSVersion::SYMBOLS.key?(current_os_symbol) + MacOSVersion.from_symbol(current_os_symbol) else - MacOSVersion.from_symbol(Homebrew::SimulateSystem.current_os) + return false end + base_os = MacOSVersion.from_symbol(os_name) return current_os >= base_os if or_condition == :or_newer return current_os <= base_os if or_condition == :or_older @@ -194,7 +195,7 @@ module OnSystem comparator = OnSystem.comparator_from_or_condition(or_condition) @uses_on_system.macos_requirements << MacOSRequirement.new([os_condition], comparator:) - return unless OnSystem.os_condition_met? os_condition, or_condition + return unless OnSystem.os_condition_met?(os_condition, or_condition) @on_system_block_min_os = T.let( if or_condition == :or_older diff --git a/Library/Homebrew/test/extend/on_system_spec.rb b/Library/Homebrew/test/extend/on_system_spec.rb index 1789999a52..428b307f71 100644 --- a/Library/Homebrew/test/extend/on_system_spec.rb +++ b/Library/Homebrew/test/extend/on_system_spec.rb @@ -105,6 +105,11 @@ RSpec.describe OnSystem do end end + it "returns false if `os_name` is a macOS version but current OS is `:generic`" do + allow(Homebrew::SimulateSystem).to receive(:current_os).and_return(:generic) + expect(described_class.os_condition_met?(newest_macos)).to be false + end + it "returns false if current OS is `:macos` and `os_name` is a macOS version" do # A generic macOS version is treated as less than any other version. Homebrew::SimulateSystem.with(os: :macos) do