aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorGautham Goli2017-08-14 02:18:46 +0530
committerGautham Goli2017-08-14 02:18:46 +0530
commit3efba57cd936f1e53b72a9e66561d594fcf37d65 (patch)
tree4aaecdf7dd83e96947c91099e7849f0018618828 /Library
parente14fedd1b35480ea3707689db044140d18662b9c (diff)
downloadbrew-3efba57cd936f1e53b72a9e66561d594fcf37d65.tar.bz2
Add negated? method to formula cop and add tests for negated build.without?
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/rubocops/extend/formula_cop.rb5
-rw-r--r--Library/Homebrew/rubocops/lines_cop.rb12
-rw-r--r--Library/Homebrew/test/rubocops/lines_cop_spec.rb24
3 files changed, 35 insertions, 6 deletions
diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb
index 94952d1f5..862dabfda 100644
--- a/Library/Homebrew/rubocops/extend/formula_cop.rb
+++ b/Library/Homebrew/rubocops/extend/formula_cop.rb
@@ -297,6 +297,11 @@ module RuboCop
true
end
+ # Check if negation is present in the given node
+ def negated?(node)
+ method_called?(node, :!)
+ end
+
# Return all the caveats' string nodes in an array
def caveats_strings
find_strings(find_method_def(@body, :caveats))
diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb
index 4c82b42cf..6c0c3ec73 100644
--- a/Library/Homebrew/rubocops/lines_cop.rb
+++ b/Library/Homebrew/rubocops/lines_cop.rb
@@ -288,15 +288,15 @@ module RuboCop
end
find_instance_method_call(body_node, :build, :with?) do |m|
- next unless method_called?(m.parent, :!)
+ next unless negated?(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?'"
- # end
- #
+ find_instance_method_call(body_node, :build, :without?) do |m|
+ next unless negated?(m.parent)
+ problem "Don't negate 'build.without?': use 'build.with?'"
+ end
+
# find_instance_method_call(body_node, :build, :without?) do |m|
# arg = parameters(m).first
# next unless match = regex_match_group(arg, %r{-?-?without-(.*)})
diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb
index 8e3f42adf..e00834963 100644
--- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb
+++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb
@@ -691,6 +691,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.without? "bar"
+ end
+ end
+ EOS
+
+ expected_offenses = [{ message: "Don't negate 'build.without?': use 'build.with?'",
+ 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])