add tests for condition dependencies

This commit is contained in:
Gautham Goli 2017-08-15 16:09:32 +05:30
parent efabd4b5c2
commit afdd0e2437
2 changed files with 87 additions and 30 deletions

View File

@ -181,27 +181,22 @@ module RuboCop
problem "Use MacOS.full_version instead of MACOS_FULL_VERSION" problem "Use MacOS.full_version instead of MACOS_FULL_VERSION"
end end
# dependency(body_node) do |m| conditional_dependencies(body_node) do |node, method, param, dep_node|
# # handle symbols and shit: WIP dep = string_content(dep_node)
# next unless modifier?(m.parent) if node.if?
# dep = parameters(m).first if (method == :include? && regex_match_group(param, /with-#{dep}$/)) ||
# condition = m.parent.condition (method == :with? && regex_match_group(param, /#{dep}$/))
# if (condition.if? && condition.method_name == :include? && parameters_passed(condition, /with-#{string_content(dep)}$/))|| offending_node(dep_node.parent)
# (condition.if? && condition.method_name == :with? && parameters_passed?(condition, /#{string_content(dep)}$/)) problem "Replace #{node.source} with #{dep_node.parent.source} => :optional"
# problem "Replace #{m.parent.source} with #{dep.source} => :optional" end
# end elsif node.unless?
# if (condition.unless? && condition.method_name == :include? && parameters_passed?(condition, /without-#{string_content(dep)}$/))|| if (method == :include? && regex_match_group(param, /without-#{dep}$/)) ||
# (condition.unless? && condition.method_name == :without? && parameters_passed?(condition, /#{string_content(dep)}$/)) (method == :without? && regex_match_group(param, /#{dep}$/))
# problem "Replace #{m.parent.source} with #{dep.source} => :recommended" offending_node(dep_node.parent)
# end problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended"
# end end
# end
# find_every_method_call_by_name(body_node, :depends_on).each do |m| end
# next unless modifier?(m.parent)
# dep = parameters(m).first
# next if dep.hash_type?
# condition = m.parent.node_parts
# end
find_method_with_args(body_node, :fails_with, :llvm) do find_method_with_args(body_node, :fails_with, :llvm) do
problem "'fails_with :llvm' is now a no-op so should be removed" problem "'fails_with :llvm' is now a no-op so should be removed"
@ -334,12 +329,12 @@ module RuboCop
node.modifier_form? node.modifier_form?
end end
def_node_search :condition, <<-EOS.undent def_node_search :conditional_dependencies, <<-EOS.undent
(send (send nil :build) $_ $({str sym} _)) {$(if (send (send nil :build) ${:include? :with? :without?} $(str _))
EOS (send nil :depends_on $({str sym} _)) nil)
def_node_search :dependency, <<-EOS.undent $(if (send (send nil :build) ${:include? :with? :without?} $(str _)) nil
(send nil :depends_on ({str sym} _)) (send nil :depends_on $({str sym} _)))}
EOS EOS
# Match depends_on with hash as argument # Match depends_on with hash as argument
@ -357,7 +352,6 @@ module RuboCop
(dstr _ (begin (send nil %1)) $(str _ ))} (dstr _ (begin (send nil %1)) $(str _ ))}
EOS EOS
def_node_matcher :negation?, '(send ... :!)'
# This is Pattern Matching method for AST # This is Pattern Matching method for AST
# Takes the AST node as argument and yields matching node if block given # Takes the AST node as argument and yields matching node if block given
# Else returns boolean for the match # Else returns boolean for the match
@ -365,9 +359,6 @@ module RuboCop
(const (const nil :Language) :Node) (const (const nil :Language) :Node)
PATTERN PATTERN
def_node_search :dirPattern, <<-PATTERN
(send (const nil :Dir) :[] (str $_))
PATTERN
end end
end end
end end

View File

@ -1213,6 +1213,72 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
end end
end end
it "with if conditional dep" do
source = <<-EOS.undent
class Foo < Formula
desc "foo"
url 'http://example.com/foo-1.0.tgz'
depends_on "foo" if build.with? "with-foo"
end
EOS
expected_offenses = [{ message: 'Replace depends_on "foo" if build.with? "with-foo" with depends_on "foo" => :optional',
severity: :convention,
line: 4,
column: 2,
source: source }]
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end
it "with unless conditional dep and symbol" do
source = <<-EOS.undent
class Foo < Formula
desc "foo"
url 'http://example.com/foo-1.0.tgz'
depends_on :foo unless build.without? "foo"
end
EOS
expected_offenses = [{ message: 'Replace depends_on :foo unless build.without? "foo" with depends_on :foo => :recommended',
severity: :convention,
line: 4,
column: 2,
source: source }]
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end
it "with unless conditional dep with build.include?" do
source = <<-EOS.undent
class Foo < Formula
desc "foo"
url 'http://example.com/foo-1.0.tgz'
depends_on :foo unless build.include? "without-foo"
end
EOS
expected_offenses = [{ message: 'Replace depends_on :foo unless build.include? "without-foo" with depends_on :foo => :recommended',
severity: :convention,
line: 4,
column: 2,
source: source }]
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end
end end
def expect_offense(expected, actual) def expect_offense(expected, actual)
expect(actual.message).to eq(expected[:message]) expect(actual.message).to eq(expected[:message])