components_order: allow if-else statements inside on_os blocks

Co-Authored-By: Rylan Polster <rslpolster@gmail.com>
This commit is contained in:
Seeker 2020-12-23 15:00:28 -08:00 committed by Rylan Polster
parent 27ce5754c9
commit 4924b7633c
2 changed files with 81 additions and 24 deletions

View File

@ -103,30 +103,37 @@ module RuboCop
next if on_macos_blocks.length.zero? && on_linux_blocks.length.zero?
if on_macos_blocks.length == 1
on_macos_block = on_macos_blocks.first
child_nodes = on_macos_block.body.child_nodes
unless child_nodes[0].method?("url") && (
child_nodes[1].method?("sha256") ||
(child_nodes[1].method?("version") && child_nodes[2].method?("sha256"))
)
problem "`on_macos` blocks within resource blocks must contain only a " \
"url and sha256 or a url, version, and sha256 (in those orders)."
next
on_os_bodies = []
(on_macos_blocks + on_linux_blocks).each do |on_os_block|
on_os_body = on_os_block.body
if on_os_body.if_type?
on_os_bodies += on_os_body.branches.map { |branch| [on_os_block.method_name, branch] }
else
on_os_bodies << [on_os_block.method_name, on_os_body]
end
end
if on_linux_blocks.length == 1
on_linux_block = on_linux_blocks.first
child_nodes = on_linux_block.body.child_nodes
unless child_nodes[0].method?("url") && (
child_nodes[1].method?("sha256") ||
(child_nodes[1].method?("version") && child_nodes[2].method?("sha256"))
)
problem "`on_linux` blocks within resource blocks must contain only a " \
"url and sha256 or a url, version, and sha256 (in those orders)."
next
message = nil
allowed_methods = [
[:url, :sha256],
[:url, :version, :sha256],
]
on_os_bodies.each do |method_name, on_os_body|
child_nodes = on_os_body.begin_type? ? on_os_body.child_nodes : [on_os_body]
if child_nodes.all? { |n| n.send_type? || n.block_type? }
method_names = child_nodes.map(&:method_name)
next if allowed_methods.include? method_names
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
if on_macos_blocks.length > 1

View File

@ -545,6 +545,31 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY
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
expect_offense(<<~RUBY)
class Foo < Formula
@ -557,8 +582,8 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
url "https://brew.sh/resource2.tar.gz"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
else
url "https://brew.sh/resource1.tar.gz"
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
url "https://brew.sh/resource1.tar.gz"
end
end
@ -592,13 +617,12 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY
end
it "the content of the on_linux block is wrong and not a method" do
expect_offense(<<~RUBY)
it "reports no offenses if the on_linux 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_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"
@ -617,5 +641,31 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
end
RUBY
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