From cfc423e1839442f605072db14aac42f6f5fa6174 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Tue, 15 Aug 2017 00:05:50 +0530 Subject: [PATCH] add tests for dependencies --- Library/Homebrew/rubocops/lines_cop.rb | 23 +++++++++++-------- .../Homebrew/test/rubocops/lines_cop_spec.rb | 22 ++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index e20fa9f8b4..0c465f9f57 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -148,13 +148,15 @@ module RuboCop problem "Use ENV instead of invoking '#{match[1]}' to modify the environment" end - # find_every_method_call_by_name(body_node, :depends_on).each do |m| - # next unless modifier?(m) - # dep, option = hash_dep(m) - # next if dep.nil? || option.nil? - # problem "Dependency #{string_content(dep)} should not use option #{string_content(option)}" - # end - # + find_every_method_call_by_name(body_node, :depends_on).each do |m| + next if modifier?(m.parent) + param = parameters(m).first + dep, option = hash_dep(param) + next if dep.nil? || option.nil? + offending_node(param) + problem "Dependency #{string_content(dep)} should not use option #{string_content(option)}" + end + # find_instance_method_call(body_node, :version, :==) do |m| # next unless parameters_passed?(m, "HEAD") # problem "Use 'build.head?' instead of inspecting 'version'" @@ -328,6 +330,7 @@ module RuboCop end def modifier?(node) + return false unless node.if_type? node.modifier_form? end @@ -340,9 +343,9 @@ module RuboCop EOS # Match depends_on with hash as argument - def_node_search :hash_dep, <<-EOS.undent - {$(hash (pair $(str _) $(str _))) - $(hash (pair $(str _) (array $(str _) ...)))} + def_node_matcher :hash_dep, <<-EOS.undent + {(hash (pair $(str _) $(str _))) + (hash (pair $(str _) (array $(str _) ...)))} EOS def_node_matcher :destructure_hash, <<-EOS.undent diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index be2b63c47b..1273ef9c9e 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -1095,6 +1095,28 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end + it "with dependencies with invalid options" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + depends_on "foo" => "with-bar" + end + EOS + + expected_offenses = [{ message: "Dependency foo should not use option with-bar", + severity: :convention, + line: 4, + column: 13, + 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])