Skip to content

Commit 34c245c

Browse files
committed
Disallow head do blocks with only url and branch
- Since `head` must now specify a url and branch, the `head do` block with only these stanzas can be condensed to the single-line `head "url", branch: "branch"` format.
1 parent 4a76544 commit 34c245c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Library/Homebrew/rubocops/components_redundancy.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module FormulaAudit
1313
# - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present
1414
# - `stable do` should not be present without a `head` spec
1515
# - `stable do` should not be present with only `url|checksum|mirror|version`
16-
# - `head do` should not be present with only `url`
16+
# - `head do` should not be present with only `url|branch`
1717
class ComponentsRedundancy < FormulaCop
1818
HEAD_MSG = "`head` and `head do` should not be simultaneously present"
1919
BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present"
@@ -54,8 +54,9 @@ def audit_formula(formula_nodes)
5454
head_block = find_block(body_node, :head)
5555
if head_block && !head_block.body.nil?
5656
child_nodes = head_block.body.begin_type? ? head_block.body.child_nodes : [head_block.body]
57-
if child_nodes.all? { |n| n.send_type? && n.method_name == :url }
58-
problem "`head do` should not be present with only `url`"
57+
shorthand_head_methods = [:url, :branch]
58+
if child_nodes.all? { |n| n.send_type? && shorthand_head_methods.include?(n.method_name) }
59+
problem "`head do` should not be present with only #{shorthand_head_methods.join("/")}"
5960
end
6061
end
6162

Library/Homebrew/test/rubocops/components_redundancy_spec.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,38 @@ class Foo < Formula
8282
end
8383
8484
head do
85-
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only `url`
85+
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch
8686
url "https://brew.sh/foo.git"
8787
end
8888
end
8989
RUBY
9090
end
9191

92+
it "reports an offense if `head do` is present with only `url` and `branch`" do
93+
expect_offense(<<~RUBY)
94+
class Foo < Formula
95+
url "https://brew.sh/foo-1.0.tgz"
96+
97+
head do
98+
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch
99+
url "https://brew.sh/foo.git"
100+
branch "develop"
101+
end
102+
end
103+
RUBY
104+
105+
expect_offense(<<~RUBY)
106+
class Foo < Formula
107+
url "https://brew.sh/foo-1.0.tgz"
108+
109+
head do
110+
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch
111+
url "https://brew.sh/foo.git", branch: "develop"
112+
end
113+
end
114+
RUBY
115+
end
116+
92117
it "reports no offenses if `stable do` is present with `url` and `depends_on`" do
93118
expect_no_offenses(<<~RUBY)
94119
class Foo < Formula

0 commit comments

Comments
 (0)