style: fix on_macos/on_linux resource block checks

This commit is contained in:
Rylan Polster 2020-12-23 14:03:37 -05:00
parent cb8e9a695f
commit 27ce5754c9
2 changed files with 89 additions and 8 deletions

View File

@ -106,9 +106,12 @@ module RuboCop
if on_macos_blocks.length == 1
on_macos_block = on_macos_blocks.first
child_nodes = on_macos_block.body.child_nodes
if child_nodes[0].method_name.to_s != "url" && child_nodes[1].method_name.to_s != "sha256"
problem "only an url and a sha256 (in the right order) are allowed in a `on_macos` " \
"block within a resource block."
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
end
end
@ -116,9 +119,13 @@ module RuboCop
if on_linux_blocks.length == 1
on_linux_block = on_linux_blocks.first
child_nodes = on_linux_block.body.child_nodes
if child_nodes[0].method_name.to_s != "url" && child_nodes[1].method_name.to_s != "sha256"
problem "only an url and a sha256 (in the right order) are allowed in a `on_linux` " \
"block within a resource block."
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
end
end

View File

@ -432,6 +432,28 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY
end
it "correctly uses an on_macos and on_linux block with versions" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
homepage "https://brew.sh"
resource do
on_macos do
url "https://brew.sh/resource1.tar.gz"
version "1.2.3"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
end
on_linux do
url "https://brew.sh/resource2.tar.gz"
version "1.2.3"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
end
end
end
RUBY
end
it "there are two on_macos blocks, which is not allowed" do
expect_offense(<<~RUBY)
class Foo < Formula
@ -508,7 +530,7 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
url "https://brew.sh/foo-1.0.tgz"
resource do
^^^^^^^^^^^ only an url and a sha256 (in the right order) are allowed in a `on_macos` block within a resource block.
^^^^^^^^^^^ `on_macos` blocks within resource blocks must contain only a url and sha256 or a url, version, and sha256 (in those orders).
on_macos do
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
url "https://brew.sh/resource2.tar.gz"
@ -523,13 +545,39 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY
end
it "the content of the on_macos 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_macos` blocks within resource blocks must contain only a url and sha256 or a url, version, and sha256 (in those orders).
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_linux block is wrong in a resource block" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
resource do
^^^^^^^^^^^ only an url and a sha256 (in the right order) are allowed in a `on_linux` block within a resource block.
^^^^^^^^^^^ `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"
@ -543,5 +591,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
url "https://brew.sh/resource1.tar.gz"
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
end
end
end
end
RUBY
end
end
end