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.).
This commit is contained in:
Sam Ford 2025-06-08 00:37:02 -04:00
parent ccfc31bc18
commit 473f448b22
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
2 changed files with 12 additions and 6 deletions

View File

@ -77,16 +77,17 @@ module OnSystem
raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}" raise ArgumentError, "Invalid OS `or_*` condition: #{or_condition.inspect}"
end end
return false if Homebrew::SimulateSystem.simulating_or_running_on_linux? current_os_symbol = Homebrew::SimulateSystem.current_os
current_os = if current_os_symbol == :macos
base_os = MacOSVersion.from_symbol(os_name)
current_os = if Homebrew::SimulateSystem.current_os == :macos
# Assume the oldest macOS version when simulating a generic macOS version # Assume the oldest macOS version when simulating a generic macOS version
# Version::NULL is always treated as less than any other version. # Version::NULL is always treated as less than any other version.
Version::NULL Version::NULL
elsif MacOSVersion::SYMBOLS.key?(current_os_symbol)
MacOSVersion.from_symbol(current_os_symbol)
else else
MacOSVersion.from_symbol(Homebrew::SimulateSystem.current_os) return false
end 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_newer
return current_os <= base_os if or_condition == :or_older return current_os <= base_os if or_condition == :or_older
@ -194,7 +195,7 @@ module OnSystem
comparator = OnSystem.comparator_from_or_condition(or_condition) comparator = OnSystem.comparator_from_or_condition(or_condition)
@uses_on_system.macos_requirements << MacOSRequirement.new([os_condition], comparator:) @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( @on_system_block_min_os = T.let(
if or_condition == :or_older if or_condition == :or_older

View File

@ -105,6 +105,11 @@ RSpec.describe OnSystem do
end end
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 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. # A generic macOS version is treated as less than any other version.
Homebrew::SimulateSystem.with(os: :macos) do Homebrew::SimulateSystem.with(os: :macos) do