audit: Port audit_options non-strict rules to rubocop and add tests

This commit is contained in:
Gautham Goli 2017-05-24 21:07:50 +05:30
parent f1fa475c4f
commit b8adc1a8aa
6 changed files with 65 additions and 13 deletions

View File

@ -21,6 +21,9 @@ FormulaAudit/ChecksumCase:
FormulaAudit/Conflicts: FormulaAudit/Conflicts:
Enabled: true Enabled: true
FormulaAudit/Options:
Enabled: true
FormulaAuditStrict/BottleBlock: FormulaAuditStrict/BottleBlock:
Enabled: true Enabled: true

View File

@ -555,10 +555,6 @@ class FormulaAuditor
def audit_options def audit_options
formula.options.each do |o| formula.options.each do |o|
if o.name == "32-bit"
problem "macOS has been 64-bit only since 10.6 so 32-bit options are deprecated."
end
next unless @strict next unless @strict
if o.name == "universal" if o.name == "universal"

View File

@ -8,3 +8,4 @@ require_relative "./rubocops/caveats_cop"
require_relative "./rubocops/checksum_cop" require_relative "./rubocops/checksum_cop"
require_relative "./rubocops/legacy_patches_cop" require_relative "./rubocops/legacy_patches_cop"
require_relative "./rubocops/conflicts_cop" require_relative "./rubocops/conflicts_cop"
require_relative "./rubocops/options_cop"

View File

@ -102,14 +102,11 @@ module RuboCop
# Returns nil if does not depend on dependency_name # Returns nil if does not depend on dependency_name
# args: node - dependency_name - dependency's name # args: node - dependency_name - dependency's name
def depends_on?(dependency_name) def depends_on?(dependency_name, *types)
types = [:required, :build, :optional, :recommended, :run] if types.empty?
dependency_nodes = find_every_method_call_by_name(@body, :depends_on) dependency_nodes = find_every_method_call_by_name(@body, :depends_on)
idx = dependency_nodes.index do |n| idx = dependency_nodes.index do |n|
depends_on_name_type?(n, dependency_name, :required) || types.any? { |type| depends_on_name_type?(n, dependency_name, type) }
depends_on_name_type?(n, dependency_name, :build) ||
depends_on_name_type?(n, dependency_name, :optional) ||
depends_on_name_type?(n, dependency_name, :recommended) ||
depends_on_name_type?(n, dependency_name, :run)
end end
return if idx.nil? return if idx.nil?
@offense_source_range = dependency_nodes[idx].source_range @offense_source_range = dependency_nodes[idx].source_range
@ -138,6 +135,8 @@ module RuboCop
if type_match && !name_match if type_match && !name_match
name_match = node_equals?(node.method_args.first.keys.first.children.first, name) name_match = node_equals?(node.method_args.first.keys.first.children.first, name)
end end
else
type_match = false
end end
if type_match || name_match if type_match || name_match
@ -334,11 +333,13 @@ module RuboCop
def string_content(node) def string_content(node)
case node.type case node.type
when :str when :str
return node.str_content if node.type == :str node.str_content
when :dstr when :dstr
return node.each_child_node(:str).map(&:str_content).join("") if node.type == :dstr node.each_child_node(:str).map(&:str_content).join("")
when :const when :const
return node.const_name if node.type == :const node.const_name
when :sym
node.children.first.to_s
else else
"" ""
end end

View File

@ -0,0 +1,20 @@
require_relative "./extend/formula_cop"
module RuboCop
module Cop
module FormulaAudit
# This cop audits `options` in Formulae
class Options < FormulaCop
DEPRECATION_MSG = "macOS has been 64-bit only since 10.6 so 32-bit options are deprecated.".freeze
def audit_formula(_node, _class_node, _parent_class_node, body_node)
option_call_nodes = find_every_method_call_by_name(body_node, :option)
option_call_nodes.each do |option_call|
option = parameters(option_call).first
problem DEPRECATION_MSG if regex_match_group(option, /32-bit/)
end
end
end
end
end
end

View File

@ -0,0 +1,31 @@
require "rubocop"
require "rubocop/rspec/support"
require_relative "../../extend/string"
require_relative "../../rubocops/options_cop"
describe RuboCop::Cop::FormulaAudit::Options do
subject(:cop) { described_class.new }
context "When auditing options" do
it "32-bit" do
source = <<-EOS.undent
class Foo < Formula
url 'http://example.com/foo-1.0.tgz'
option "32-bit", "with 32-bit"
end
EOS
expected_offenses = [{ message: described_class::DEPRECATION_MSG,
severity: :convention,
line: 3,
column: 10,
source: source }]
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end
end
end