components_order: allow if-else statements inside on_os blocks
Co-Authored-By: Rylan Polster <rslpolster@gmail.com>
This commit is contained in:
parent
27ce5754c9
commit
4924b7633c
@ -103,30 +103,37 @@ module RuboCop
|
|||||||
|
|
||||||
next if on_macos_blocks.length.zero? && on_linux_blocks.length.zero?
|
next if on_macos_blocks.length.zero? && on_linux_blocks.length.zero?
|
||||||
|
|
||||||
if on_macos_blocks.length == 1
|
on_os_bodies = []
|
||||||
on_macos_block = on_macos_blocks.first
|
|
||||||
child_nodes = on_macos_block.body.child_nodes
|
(on_macos_blocks + on_linux_blocks).each do |on_os_block|
|
||||||
unless child_nodes[0].method?("url") && (
|
on_os_body = on_os_block.body
|
||||||
child_nodes[1].method?("sha256") ||
|
if on_os_body.if_type?
|
||||||
(child_nodes[1].method?("version") && child_nodes[2].method?("sha256"))
|
on_os_bodies += on_os_body.branches.map { |branch| [on_os_block.method_name, branch] }
|
||||||
)
|
else
|
||||||
problem "`on_macos` blocks within resource blocks must contain only a " \
|
on_os_bodies << [on_os_block.method_name, on_os_body]
|
||||||
"url and sha256 or a url, version, and sha256 (in those orders)."
|
|
||||||
next
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if on_linux_blocks.length == 1
|
message = nil
|
||||||
on_linux_block = on_linux_blocks.first
|
allowed_methods = [
|
||||||
child_nodes = on_linux_block.body.child_nodes
|
[:url, :sha256],
|
||||||
unless child_nodes[0].method?("url") && (
|
[:url, :version, :sha256],
|
||||||
child_nodes[1].method?("sha256") ||
|
]
|
||||||
(child_nodes[1].method?("version") && child_nodes[2].method?("sha256"))
|
|
||||||
)
|
on_os_bodies.each do |method_name, on_os_body|
|
||||||
problem "`on_linux` blocks within resource blocks must contain only a " \
|
child_nodes = on_os_body.begin_type? ? on_os_body.child_nodes : [on_os_body]
|
||||||
"url and sha256 or a url, version, and sha256 (in those orders)."
|
if child_nodes.all? { |n| n.send_type? || n.block_type? }
|
||||||
next
|
method_names = child_nodes.map(&:method_name)
|
||||||
|
next if allowed_methods.include? method_names
|
||||||
end
|
end
|
||||||
|
message = "`#{method_name}` blocks within resource blocks must contain only a " \
|
||||||
|
"url and sha256 or a url, version, and sha256 (in those orders)."
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
if message.present?
|
||||||
|
problem message
|
||||||
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
if on_macos_blocks.length > 1
|
if on_macos_blocks.length > 1
|
||||||
|
|||||||
@ -545,6 +545,31 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
|
|||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "reports no offenses if the on_macos block has if-else branches and they are properly formatted" do
|
||||||
|
expect_no_offenses(<<~RUBY)
|
||||||
|
class Foo < Formula
|
||||||
|
url "https://brew.sh/foo-1.0.tgz"
|
||||||
|
|
||||||
|
resource do
|
||||||
|
on_macos do
|
||||||
|
if foo == :bar
|
||||||
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
else
|
||||||
|
url "https://brew.sh/resource1.tar.gz"
|
||||||
|
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
on_linux do
|
||||||
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
it "the content of the on_macos block is wrong and not a method" do
|
it "the content of the on_macos block is wrong and not a method" do
|
||||||
expect_offense(<<~RUBY)
|
expect_offense(<<~RUBY)
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
@ -557,8 +582,8 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
|
|||||||
url "https://brew.sh/resource2.tar.gz"
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
else
|
else
|
||||||
url "https://brew.sh/resource1.tar.gz"
|
|
||||||
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
url "https://brew.sh/resource1.tar.gz"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -592,13 +617,12 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
|
|||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
it "the content of the on_linux block is wrong and not a method" do
|
it "reports no offenses if the on_linux block has if-else branches and they are properly formatted" do
|
||||||
expect_offense(<<~RUBY)
|
expect_no_offenses(<<~RUBY)
|
||||||
class Foo < Formula
|
class Foo < Formula
|
||||||
url "https://brew.sh/foo-1.0.tgz"
|
url "https://brew.sh/foo-1.0.tgz"
|
||||||
|
|
||||||
resource do
|
resource do
|
||||||
^^^^^^^^^^^ `on_linux` blocks within resource blocks must contain only a url and sha256 or a url, version, and sha256 (in those orders).
|
|
||||||
on_macos do
|
on_macos do
|
||||||
url "https://brew.sh/resource2.tar.gz"
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
@ -617,5 +641,31 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
|
|||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "the content of the on_linux block is wrong and not a method" do
|
||||||
|
expect_offense(<<~RUBY)
|
||||||
|
class Foo < Formula
|
||||||
|
url "https://brew.sh/foo-1.0.tgz"
|
||||||
|
|
||||||
|
resource do
|
||||||
|
^^^^^^^^^^^ `on_linux` blocks within resource blocks must contain only a url and sha256 or a url, version, and sha256 (in those orders).
|
||||||
|
on_macos do
|
||||||
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
end
|
||||||
|
|
||||||
|
on_linux do
|
||||||
|
if foo == :bar
|
||||||
|
url "https://brew.sh/resource2.tar.gz"
|
||||||
|
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
else
|
||||||
|
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
|
||||||
|
url "https://brew.sh/resource1.tar.gz"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user