diff --git a/Library/Homebrew/rubocops/class.rb b/Library/Homebrew/rubocops/class.rb index 16f9339fc6..7fde505269 100644 --- a/Library/Homebrew/rubocops/class.rb +++ b/Library/Homebrew/rubocops/class.rb @@ -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 diff --git a/Library/Homebrew/test/rubocops/class_spec.rb b/Library/Homebrew/test/rubocops/class_spec.rb index 803611b294..06a3c94704 100644 --- a/Library/Homebrew/test/rubocops/class_spec.rb +++ b/Library/Homebrew/test/rubocops/class_spec.rb @@ -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