rubocop: Fix audit for dependency order with multiple tags

Dependencies that have multiple tags (`[:build, :test]`) get sorted into
multiple locations resulting in the cop always reporting an offense
regardless of order.
This commit is contained in:
Kevin Abel 2018-03-28 20:54:39 -05:00
parent c1cab8a24d
commit 1b22e16a08
No known key found for this signature in database
GPG Key ID: E0F7636DDDB96BF6
2 changed files with 23 additions and 6 deletions

View File

@ -44,14 +44,19 @@ module RuboCop
end
# Separate dependencies according to precedence order:
# build-time > run-time > normal > recommended > optional
# build-time > test > normal > recommended > optional
def sort_dependencies_by_type(dependency_nodes)
unsorted_deps = dependency_nodes.to_a
ordered = []
ordered.concat(dependency_nodes.select { |dep| buildtime_dependency? dep }.to_a)
ordered.concat(dependency_nodes.select { |dep| test_dependency? dep }.to_a)
ordered.concat(dependency_nodes.reject { |dep| negate_normal_dependency? dep }.to_a)
ordered.concat(dependency_nodes.select { |dep| recommended_dependency? dep }.to_a)
ordered.concat(dependency_nodes.select { |dep| optional_dependency? dep }.to_a)
ordered.concat(unsorted_deps.select { |dep| buildtime_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| test_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.reject { |dep| negate_normal_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| recommended_dependency? dep })
unsorted_deps -= ordered
ordered.concat(unsorted_deps.select { |dep| optional_dependency? dep })
end
# `depends_on :apple if build.with? "foo"` should always be defined

View File

@ -46,6 +46,18 @@ describe RuboCop::Cop::NewFormulaAudit::DependencyOrder do
end
RUBY
end
it "correct depends_on order for multiple tags" do
expect_no_offenses(<<~RUBY)
class Foo < Formula
homepage "http://example.com"
url "http://example.com/foo-1.0.tgz"
depends_on "bar" => [:build, :test]
depends_on "foo" => :build
depends_on "apple"
end
RUBY
end
end
context "autocorrect" do