Skip to content

Commit 0160cd1

Browse files
authored
Merge pull request #21698 from Homebrew/audit-os-checks-in-extend-os
rubocops/move_to_extend_os: audit OS checks in `extend/os`
2 parents d4c058e + 47a58ba commit 0160cd1

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

Library/Homebrew/.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Bundler/GemFilename:
77
Homebrew/MoveToExtendOS:
88
Enabled: true
99
Exclude:
10-
- "{extend,test,requirements}/**/*"
10+
- "{test,requirements}/**/*"
1111
- "os.rb"
1212

1313
# We don't use Sorbet for RSpec tests so let's disable this there.

Library/Homebrew/rubocops/move_to_extend_os.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,37 @@
44
module RuboCop
55
module Cop
66
module Homebrew
7-
# This cop ensures that platform specific code ends up in `extend/os`.
7+
# This cop ensures that platform specific code ends up in `extend/os`, and
8+
# that `extend/os` doesn't contain incorrect or redundant OS checks.
89
class MoveToExtendOS < Base
9-
MSG = "Move `OS.linux?` and `OS.mac?` calls to `extend/os`."
10+
NON_EXTEND_OS_MSG = "Move `OS.linux?` and `OS.mac?` calls to `extend/os`."
1011

11-
def_node_matcher :os_check?, <<~PATTERN
12-
(send (const nil? :OS) {:mac? | :linux?})
12+
def_node_matcher :os_mac?, <<~PATTERN
13+
(send (const nil? :OS) :mac?)
1314
PATTERN
1415

16+
def_node_matcher :os_linux?, <<~PATTERN
17+
(send (const nil? :OS) :linux?)
18+
PATTERN
19+
20+
sig { params(extend_os: String, os_method: String).returns(String) }
21+
def extend_offense_message(extend_os, os_method)
22+
"Don't use `OS.#{os_method}?` in `extend/os/#{extend_os}`, it is " \
23+
"always `#{(extend_os == os_method) ? "true" : "false"}`."
24+
end
25+
1526
sig { params(node: RuboCop::AST::Node).void }
1627
def on_send(node)
17-
return unless os_check?(node)
18-
19-
add_offense(node)
28+
file_path = processed_source.file_path
29+
if file_path.include?("extend/os/mac/")
30+
add_offense(node, message: extend_offense_message("mac", "mac")) if os_mac?(node)
31+
add_offense(node, message: extend_offense_message("mac", "linux")) if os_linux?(node)
32+
elsif file_path.include?("extend/os/linux/")
33+
add_offense(node, message: extend_offense_message("linux", "mac")) if os_mac?(node)
34+
add_offense(node, message: extend_offense_message("linux", "linux")) if os_linux?(node)
35+
elsif !file_path.include?("extend/os/") && (os_mac?(node) || os_linux?(node))
36+
add_offense(node, message: NON_EXTEND_OS_MSG)
37+
end
2038
end
2139
end
2240
end

Library/Homebrew/sorbet/rbi/dsl/rubo_cop/cop/homebrew/move_to_extend_os.rbi

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Library/Homebrew/test/rubocops/move_to_extend_os_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,36 @@
1818
^^^^^^^ Homebrew/MoveToExtendOS: Move `OS.linux?` and `OS.mac?` calls to `extend/os`.
1919
RUBY
2020
end
21+
22+
context "when in extend/os/mac" do
23+
it "registers an offense when using `OS.linux?`" do
24+
expect_offense(<<~RUBY, "Library/Homebrew/extend/os/mac/foo.rb")
25+
OS.linux?
26+
^^^^^^^^^ Homebrew/MoveToExtendOS: Don't use `OS.linux?` in `extend/os/mac`, it is always `false`.
27+
RUBY
28+
end
29+
30+
it "registers an offense when using `OS.mac?`" do
31+
expect_offense(<<~RUBY, "Library/Homebrew/extend/os/mac/foo.rb")
32+
OS.mac?
33+
^^^^^^^ Homebrew/MoveToExtendOS: Don't use `OS.mac?` in `extend/os/mac`, it is always `true`.
34+
RUBY
35+
end
36+
end
37+
38+
context "when in extend/os/linux" do
39+
it "registers an offense when using `OS.mac?`" do
40+
expect_offense(<<~RUBY, "Library/Homebrew/extend/os/linux/foo.rb")
41+
OS.mac?
42+
^^^^^^^ Homebrew/MoveToExtendOS: Don't use `OS.mac?` in `extend/os/linux`, it is always `false`.
43+
RUBY
44+
end
45+
46+
it "registers an offense when using `OS.linux?`" do
47+
expect_offense(<<~RUBY, "Library/Homebrew/extend/os/linux/foo.rb")
48+
OS.linux?
49+
^^^^^^^^^ Homebrew/MoveToExtendOS: Don't use `OS.linux?` in `extend/os/linux`, it is always `true`.
50+
RUBY
51+
end
52+
end
2153
end

0 commit comments

Comments
 (0)