Merge pull request #10118 from Rylan12/fix-style-on-os-blocks

style: fix on_macos/on_linux resource block checks
This commit is contained in:
Rylan Polster 2020-12-23 22:34:54 -05:00 committed by GitHub
commit e6e76a403a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 161 additions and 23 deletions

View File

@ -103,23 +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|
if child_nodes[0].method_name.to_s != "url" && child_nodes[1].method_name.to_s != "sha256" on_os_body = on_os_block.body
problem "only an url and a sha256 (in the right order) are allowed in a `on_macos` " \ if on_os_body.if_type?
"block within a resource block." on_os_bodies += on_os_body.branches.map { |branch| [on_os_block.method_name, branch] }
next else
on_os_bodies << [on_os_block.method_name, on_os_body]
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],
if child_nodes[0].method_name.to_s != "url" && child_nodes[1].method_name.to_s != "sha256" [:url, :version, :sha256],
problem "only an url and a sha256 (in the right order) are allowed in a `on_linux` " \ ]
"block within a resource block."
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 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

View File

@ -411,8 +411,8 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
context "resource" do context "in a resource block" do
it "correctly uses an on_macos and on_linux block" do it "reports no offenses for a valid on_macos and on_linux block" do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
class Foo < Formula class Foo < Formula
homepage "https://brew.sh" homepage "https://brew.sh"
@ -432,7 +432,29 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "there are two on_macos blocks, which is not allowed" do it "reports no offenses for a valid 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 "reports an offense if there are two on_macos blocks" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
@ -453,7 +475,7 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "there are two on_linux blocks, which is not allowed" do it "reports an offense if there are two on_linux blocks" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
@ -474,7 +496,7 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "there is a on_macos block but no on_linux block" do it "reports no offenses if there is an on_macos block but no on_linux block" do
expect_no_offenses(<<~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"
@ -488,7 +510,7 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "there is a on_linux block but no on_macos block" do it "reports no offenses if there is an on_linux block but no on_macos block" do
expect_no_offenses(<<~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"
@ -502,13 +524,13 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "the content of the on_macos block is wrong in a resource block" do it "reports an offense if the content of an on_macos block is improperly formatted" do
expect_offense(<<~RUBY) expect_offense(<<~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
^^^^^^^^^^^ 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 on_macos do
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
url "https://brew.sh/resource2.tar.gz" url "https://brew.sh/resource2.tar.gz"
@ -523,13 +545,64 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
RUBY RUBY
end end
it "the content of the on_linux block is wrong in a resource block" do it "reports no offenses if an on_macos block has if-else branches that 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 "reports an offense if an on_macos block has if-else branches that aren't properly formatted" do
expect_offense(<<~RUBY) expect_offense(<<~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
^^^^^^^^^^^ only an url and a sha256 (in the right order) are allowed in a `on_linux` 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
if foo == :bar
url "https://brew.sh/resource2.tar.gz"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
else
sha256 "686372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
url "https://brew.sh/resource1.tar.gz"
end
end
on_linux do
url "https://brew.sh/resource2.tar.gz"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
end
end
end
RUBY
end
it "reports an offense if the content of an on_linux block is improperly formatted" 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 on_macos do
url "https://brew.sh/resource2.tar.gz" url "https://brew.sh/resource2.tar.gz"
sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35" sha256 "586372eb92059873e29eba4f9dec8381541b4d3834660707faf8ba59146dfc35"
@ -543,5 +616,56 @@ describe RuboCop::Cop::FormulaAudit::ComponentsOrder do
end end
RUBY RUBY
end end
it "reports no offenses if an on_linux block has if-else branches that are properly formatted" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
resource do
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
it "reports an offense if an on_linux block has if-else branches that aren't properly formatted" 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