Skip to content

Commit bafa2ec

Browse files
committed
OnSystem: add tests
`OnSystem` is exercised by other tests that include its modules but this adds some baseline tests to ensure some of these methods work as expected when tested in isolation. Between these added tests and existing tests, we should have 100% coverage when run on Homebrew/brew CI.
1 parent 2d603b0 commit bafa2ec

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# frozen_string_literal: true
2+
3+
require "extend/on_system"
4+
5+
RSpec.describe OnSystem do
6+
shared_examples "a class with on_arch methods" do
7+
it "adds on_arch methods to class for `ARCH_OPTIONS`" do
8+
OnSystem::ARCH_OPTIONS.each do |arch|
9+
expect(subject.method_defined?(:"on_#{arch}")).to be true
10+
end
11+
end
12+
end
13+
14+
shared_examples "a class with on_base_os methods" do
15+
it "adds on_os methods to class for `BASE_OS_OPTIONS`" do
16+
OnSystem::BASE_OS_OPTIONS.each do |os|
17+
expect(subject.method_defined?(:"on_#{os}")).to be true
18+
end
19+
end
20+
end
21+
22+
shared_examples "a class with on_macos methods" do
23+
it "adds on_os methods to class for `MacOSVersion::SYMBOLS` keys" do
24+
MacOSVersion::SYMBOLS.each_key do |os|
25+
expect(subject.method_defined?(:"on_#{os}")).to be true
26+
end
27+
end
28+
end
29+
30+
describe "::arch_condition_met?" do
31+
it "returns true if current arch equals provided arch" do
32+
Homebrew::SimulateSystem.with(arch: :arm) do
33+
expect(described_class.arch_condition_met?(:arm)).to be true
34+
end
35+
end
36+
37+
it "returns false if current arch does not equal provided arch" do
38+
Homebrew::SimulateSystem.with(arch: :arm) do
39+
expect(described_class.arch_condition_met?(:intel)).to be false
40+
end
41+
end
42+
43+
it "raises error if provided arch is not valid" do
44+
expect { described_class.arch_condition_met?(:nonexistent_arch) }
45+
.to raise_error(ArgumentError)
46+
end
47+
end
48+
49+
describe "::os_condition_met?" do
50+
let(:newest_macos) { MacOSVersion::SYMBOLS.keys.first }
51+
52+
it "returns result of `SimulateSystem.simulating_or_running_on_<os_name>` for supported OS" do
53+
Homebrew::SimulateSystem.with(os: newest_macos) do
54+
# This needs to use a value from `BASE_OS_OPTIONS`
55+
expect(described_class.os_condition_met?(:macos)).to be true
56+
end
57+
end
58+
59+
it "returns false if `os_name` is a macOS version but OS is Linux" do
60+
Homebrew::SimulateSystem.with(os: :linux) do
61+
expect(described_class.os_condition_met?(newest_macos)).to be false
62+
end
63+
end
64+
65+
it "returns false if current OS is `:macos` and `os_name` is a macOS version" do
66+
# A generic macOS version is treated as less than any other version.
67+
Homebrew::SimulateSystem.with(os: :macos) do
68+
expect(described_class.os_condition_met?(newest_macos)).to be false
69+
end
70+
end
71+
72+
it "returns true if current OS equals the `os_name` macOS version" do
73+
Homebrew::SimulateSystem.with(os: newest_macos) do
74+
expect(described_class.os_condition_met?(newest_macos)).to be true
75+
end
76+
end
77+
78+
it "returns true if current OS meets the `or_condition` for `os_name` macOS version" do
79+
current_os = :ventura
80+
Homebrew::SimulateSystem.with(os: current_os) do
81+
expect(described_class.os_condition_met?(current_os, :or_newer)).to be true
82+
expect(described_class.os_condition_met?(:big_sur, :or_newer)).to be true
83+
expect(described_class.os_condition_met?(current_os, :or_older)).to be true
84+
expect(described_class.os_condition_met?(newest_macos, :or_older)).to be true
85+
end
86+
end
87+
88+
it "returns false if current OS does not meet the `or_condition` for `os_name` macOS version" do
89+
Homebrew::SimulateSystem.with(os: :ventura) do
90+
expect(described_class.os_condition_met?(newest_macos, :or_newer)).to be false
91+
expect(described_class.os_condition_met?(:big_sur, :or_older)).to be false
92+
end
93+
end
94+
95+
it "raises an error if `os_name` is not valid" do
96+
expect { described_class.os_condition_met?(:nonexistent_os) }
97+
.to raise_error(ArgumentError, /Invalid OS condition:/)
98+
end
99+
100+
it "raises an error if `os_name` is a macOS version but `or_condition` is not valid" do
101+
expect do
102+
described_class.os_condition_met?(newest_macos, :nonexistent_condition)
103+
end.to raise_error(ArgumentError, /Invalid OS `or_\*` condition:/)
104+
end
105+
end
106+
107+
describe "::condition_from_method_name" do
108+
it "returns a symbol with any `on_` prefix removed" do
109+
expect(described_class.condition_from_method_name(:on_arm)).to eq(:arm)
110+
end
111+
112+
it "returns provided symbol if there is no `on_` prefix" do
113+
expect(described_class.condition_from_method_name(:arm)).to eq(:arm)
114+
end
115+
end
116+
117+
describe "::setup_arch_methods" do
118+
subject do
119+
klass = Class.new
120+
described_class.setup_arch_methods(klass)
121+
klass
122+
end
123+
124+
it_behaves_like "a class with on_arch methods"
125+
end
126+
127+
describe "::setup_base_os_methods" do
128+
subject do
129+
klass = Class.new
130+
described_class.setup_base_os_methods(klass)
131+
klass
132+
end
133+
134+
it_behaves_like "a class with on_base_os methods"
135+
end
136+
137+
describe "::setup_macos_methods" do
138+
subject do
139+
klass = Class.new
140+
described_class.setup_macos_methods(klass)
141+
klass
142+
end
143+
144+
it_behaves_like "a class with on_macos methods"
145+
end
146+
147+
describe "::included" do
148+
it "raises an error" do
149+
expect { Class.new { include OnSystem } }
150+
.to raise_error(/Do not include `OnSystem` directly/)
151+
end
152+
end
153+
154+
describe "::MacOSAndLinux" do
155+
subject { Class.new { include OnSystem::MacOSAndLinux } }
156+
157+
it "can be included" do
158+
expect { Class.new { include OnSystem::MacOSAndLinux } }.not_to raise_error
159+
end
160+
161+
it_behaves_like "a class with on_arch methods"
162+
it_behaves_like "a class with on_base_os methods"
163+
it_behaves_like "a class with on_macos methods"
164+
end
165+
166+
describe "::MacOSOnly" do
167+
subject { Class.new { include OnSystem::MacOSAndLinux } }
168+
169+
it "can be included" do
170+
expect { Class.new { include OnSystem::MacOSOnly } }.not_to raise_error
171+
end
172+
173+
it_behaves_like "a class with on_arch methods"
174+
it_behaves_like "a class with on_macos methods"
175+
end
176+
end

0 commit comments

Comments
 (0)