Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check dependency order in on_system methods #13636

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Library/Homebrew/rubocops/components_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module FormulaAudit
class ComponentsOrder < FormulaCop
extend AutoCorrector

def on_system_methods
@on_system_methods ||= [:intel, :arm, :macos, :linux, :system, *MacOSVersions::SYMBOLS.keys].map do |m|
:"on_#{m}"
end
end

def audit_formula(_node, _class_node, _parent_class_node, body_node)
@present_components, @offensive_nodes = check_order(FORMULA_COMPONENT_PRECEDENCE_LIST, body_node)

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/rubocops/dependency_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DependencyOrder < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
check_dependency_nodes_order(body_node)
check_uses_from_macos_nodes_order(body_node)
[:head, :stable].each do |block_name|
([:head, :stable] + on_system_methods).each do |block_name|
block = find_block(body_node, block_name)
next unless block

Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/rubocops/extend/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ def file_path_allowed?

@file_path !~ Regexp.union(paths_to_exclude)
end

def on_system_methods
@on_system_methods ||= [:intel, :arm, :macos, :linux, :system, *MacOSVersions::SYMBOLS.keys].map do |m|
:"on_#{m}"
end
end
end
end
end
56 changes: 56 additions & 0 deletions Library/Homebrew/test/rubocops/dependency_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,34 @@ class Foo < Formula
end
RUBY
end

it "reports and corrects wrong conditional order within a system block" do
expect_offense(<<~RUBY)
class Foo < Formula
homepage "https://brew.sh"
url "https://brew.sh/foo-1.0.tgz"
on_arm do
uses_from_macos "apple" if build.with? "foo"
uses_from_macos "bar"
^^^^^^^^^^^^^^^^^^^^^ dependency "bar" (line 6) should be put before dependency "apple" (line 5)
uses_from_macos "foo" => :optional
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dependency "foo" (line 7) should be put before dependency "apple" (line 5)
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
homepage "https://brew.sh"
url "https://brew.sh/foo-1.0.tgz"
on_arm do
uses_from_macos "bar"
uses_from_macos "foo" => :optional
uses_from_macos "apple" if build.with? "foo"
end
end
RUBY
end
end

context "when auditing `depends_on`" do
Expand Down Expand Up @@ -224,5 +252,33 @@ class Foo < Formula
end
RUBY
end

it "reports and corrects wrong conditional order within a system block" do
expect_offense(<<~RUBY)
class Foo < Formula
homepage "https://brew.sh"
url "https://brew.sh/foo-1.0.tgz"
on_linux do
depends_on "apple" if build.with? "foo"
depends_on "bar"
^^^^^^^^^^^^^^^^ dependency "bar" (line 6) should be put before dependency "apple" (line 5)
depends_on "foo" => :optional
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dependency "foo" (line 7) should be put before dependency "apple" (line 5)
end
end
RUBY

expect_correction(<<~RUBY)
class Foo < Formula
homepage "https://brew.sh"
url "https://brew.sh/foo-1.0.tgz"
on_linux do
depends_on "bar"
depends_on "foo" => :optional
depends_on "apple" if build.with? "foo"
end
end
RUBY
end
end
end