From e14fedd1b35480ea3707689db044140d18662b9c Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Mon, 14 Aug 2017 02:14:20 +0530 Subject: [PATCH] Add test for negated build.with? --- .../Homebrew/rubocops/extend/formula_cop.rb | 8 +++++-- Library/Homebrew/rubocops/lines_cop.rb | 11 +++++---- .../Homebrew/test/rubocops/lines_cop_spec.rb | 24 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index cb404046cb..94952d1f53 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -252,11 +252,15 @@ module RuboCop end # Check if method_name is called among the direct children nodes in the given node + # Check if the node itself is the method def method_called?(node, method_name) + if node.send_type? && node.method_name == method_name + offending_node(node) + return true + end node.each_child_node(:send) do |call_node| next unless call_node.method_name == method_name - @offensive_node = call_node - @offense_source_range = call_node.source_range + offending_node(call_node) return true end false diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 2b146b79ed..4c82b42cf1 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -287,11 +287,11 @@ module RuboCop problem "Use if #{correct} instead of unless #{m.source}" end - # find_instance_method_call(body_node, :build, :with?) do |m| - # next unless negation?(m) - # problem "Don't negate 'build.with?': use 'build.without?'" - # end - # + find_instance_method_call(body_node, :build, :with?) do |m| + next unless method_called?(m.parent, :!) + problem "Don't negate 'build.with?': use 'build.without?'" + end + # find_instance_method_call(body_node, :build, :without?) do |m| # next unless negation?(m) # problem "Don't negate 'build.without?': use 'build.with?'" @@ -324,6 +324,7 @@ module RuboCop end def unless_modifier?(node) + return false unless node.if_type? node.modifier_form? && node.unless? end diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index dda1b96e61..8e3f42adf6 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -667,6 +667,30 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect_offense(expected, actual) end end + + it "with negated build.with?" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if !build.with? "bar" + end + end + EOS + + expected_offenses = [{ message: "Don't negate 'build.with?': use 'build.without?'", + severity: :convention, + line: 5, + column: 14, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end end def expect_offense(expected, actual) expect(actual.message).to eq(expected[:message])