From b8948129b8a2477cd6f49931bf9f3f062b611b1a Mon Sep 17 00:00:00 2001 From: Dominyk Tiller Date: Tue, 7 Aug 2018 03:04:42 +0100 Subject: [PATCH 1/2] class_cop: tighten test audit --- Library/Homebrew/rubocops/class_cop.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/rubocops/class_cop.rb b/Library/Homebrew/rubocops/class_cop.rb index 6f1ffc1444..8e0df3cbc8 100644 --- a/Library/Homebrew/rubocops/class_cop.rb +++ b/Library/Homebrew/rubocops/class_cop.rb @@ -25,13 +25,24 @@ module RuboCop end module FormulaAuditStrict - # - `test do ..end` should be defined in the formula + # - `test do ..end` should be meaningfully defined in the formula class Test < FormulaCop - MSG = "A `test do` test block should be added".freeze - def audit_formula(_node, _class_node, _parent_class_node, body_node) - return if find_block(body_node, :test) - problem MSG + test = 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? && + test.body.source.to_s == "true" + problem "`test do` should contain a real test" end end end From 7d5f0df71d6f5cdaf2406fca862dfc878253c5bb Mon Sep 17 00:00:00 2001 From: Dominyk Tiller Date: Tue, 7 Aug 2018 03:39:47 +0100 Subject: [PATCH 2/2] class_cop_spec: add tests for tighter test audit --- .../Homebrew/test/rubocops/class_cop_spec.rb | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Library/Homebrew/test/rubocops/class_cop_spec.rb b/Library/Homebrew/test/rubocops/class_cop_spec.rb index 71abbda210..deeb6b781c 100644 --- a/Library/Homebrew/test/rubocops/class_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/class_cop_spec.rb @@ -59,4 +59,29 @@ describe RuboCop::Cop::FormulaAuditStrict::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://example.com/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://example.com/foo-1.0.tgz' + + test do + ^^^^^^^ `test do` should contain a real test + true + end + end + RUBY + end end