diff --git a/Library/Homebrew/rubocops/components_redundancy.rb b/Library/Homebrew/rubocops/components_redundancy.rb index 724f0af7bf..4a203ce17a 100644 --- a/Library/Homebrew/rubocops/components_redundancy.rb +++ b/Library/Homebrew/rubocops/components_redundancy.rb @@ -13,7 +13,7 @@ module RuboCop # - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present # - `stable do` should not be present without a `head` spec # - `stable do` should not be present with only `url|checksum|mirror|version` - # - `head do` should not be present with only `url` + # - `head do` should not be present with only `url|branch` class ComponentsRedundancy < FormulaCop HEAD_MSG = "`head` and `head do` should not be simultaneously present" BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present" @@ -54,8 +54,9 @@ module RuboCop head_block = find_block(body_node, :head) if head_block && !head_block.body.nil? child_nodes = head_block.body.begin_type? ? head_block.body.child_nodes : [head_block.body] - if child_nodes.all? { |n| n.send_type? && n.method_name == :url } - problem "`head do` should not be present with only `url`" + shorthand_head_methods = [:url, :branch] + if child_nodes.all? { |n| n.send_type? && shorthand_head_methods.include?(n.method_name) } + problem "`head do` should not be present with only #{shorthand_head_methods.join("/")}" end end diff --git a/Library/Homebrew/test/rubocops/components_redundancy_spec.rb b/Library/Homebrew/test/rubocops/components_redundancy_spec.rb index 8e2b5076e6..73b66318c9 100644 --- a/Library/Homebrew/test/rubocops/components_redundancy_spec.rb +++ b/Library/Homebrew/test/rubocops/components_redundancy_spec.rb @@ -82,13 +82,26 @@ RSpec.describe RuboCop::Cop::FormulaAudit::ComponentsRedundancy do end head do - ^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only `url` + ^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch url "https://brew.sh/foo.git" end end RUBY end + it "reports an offense if `head do` is present with only `url` and `branch`" do + expect_offense(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + + head do + ^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch + url "https://brew.sh/foo.git", branch: "develop" + end + end + RUBY + end + it "reports no offenses if `stable do` is present with `url` and `depends_on`" do expect_no_offenses(<<~RUBY) class Foo < Formula