rubocops/class: rename, move cops, readd strict.

This commit is contained in:
Mike McQuaid 2020-04-13 14:34:05 +01:00
parent 3d9cf83fec
commit 476a61f51c
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
2 changed files with 40 additions and 46 deletions

View File

@ -26,11 +26,18 @@ module RuboCop
end
end
class TestCalls < FormulaCop
class Test < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
test = find_block(body_node, :test)
return unless test
if test.body.nil?
problem "`test do` should not be empty"
return
end
problem "`test do` should contain a real test" if test.body.single_line? && test.body.source.to_s == "true"
test_calls(test) do |node, params|
p1, p2 = params
if match = string_content(p1).match(%r{(/usr/local/(s?bin))})
@ -70,26 +77,13 @@ module RuboCop
end
end
module FormulaAudit
# - `test do ..end` should be meaningfully defined in the formula.
class Test < FormulaCop
module FormulaAuditStrict
# - `test do ..end` should defined in the formula.
class TestPresent < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
test = find_block(body_node, :test)
return if find_block(body_node, :test)
unless test
problem "A `test do` test block should be added"
return
end
if test.body.nil?
problem "`test do` should not be empty"
return
end
return unless test.body.single_line?
return if test.body.source.to_s != "true"
problem "`test do` should contain a real test"
problem "A `test do` test block should be added"
end
end
end

View File

@ -50,7 +50,7 @@ describe RuboCop::Cop::FormulaAudit::ClassName do
end
end
describe RuboCop::Cop::FormulaAudit::TestCalls do
describe RuboCop::Cop::FormulaAudit::Test do
subject(:cop) { described_class.new }
it "reports an offense when /usr/local/bin is found in test calls" do
@ -79,6 +79,31 @@ describe RuboCop::Cop::FormulaAudit::TestCalls do
RUBY
end
it "reports an offense when there is an empty test block" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
test do
^^^^^^^ `test do` should not be empty
end
end
RUBY
end
it "reports an offense when test is falsely true" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
test do
^^^^^^^ `test do` should contain a real test
true
end
end
RUBY
end
it "supports auto-correcting test calls" do
source = <<~RUBY
class Foo < Formula
@ -105,7 +130,7 @@ describe RuboCop::Cop::FormulaAudit::TestCalls do
end
end
describe RuboCop::Cop::FormulaAudit::Test do
describe RuboCop::Cop::FormulaAuditStrict::TestPresent do
subject(:cop) { described_class.new }
it "reports an offense when there is no test block" do
@ -116,29 +141,4 @@ describe RuboCop::Cop::FormulaAudit::Test do
end
RUBY
end
it "reports an offense when there is an empty test block" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
test do
^^^^^^^ `test do` should not be empty
end
end
RUBY
end
it "reports an offense when test is falsely true" do
expect_offense(<<~RUBY)
class Foo < Formula
url 'https://brew.sh/foo-1.0.tgz'
test do
^^^^^^^ `test do` should contain a real test
true
end
end
RUBY
end
end