Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve duplicate and cycle detection in resolver #1038

Merged
merged 1 commit into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions lib/inspec/dependencies/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,43 @@ def self.resolve(dependencies, vendor_index, working_dir, backend)
new.resolve(reqs)
end

def resolve(deps, top_level = true, seen_items = {}, path_string = '')
def detect_duplicates(deps, top_level, path_string)
seen_items_local = []
deps.each do |dep|
if seen_items_local.include?(dep.name)
problem_cookbook = if top_level
'the inspec.yml for this profile.'
else
"the dependency information for #{path_string.split(' ').last}"
end
fail Inspec::DuplicateDep, "The dependency #{dep.name} is listed twice in #{problem_cookbook}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may makes sense to add a test for that behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test that ensures we detect duplicates:

https://github.com/chef/inspec/pull/1038/files#diff-a1477bbe856a2707fce873a5096df630R28

Do you think we should test the content of the error message as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh missed that. Thanks @stevendanna

else
seen_items_local << dep.name
end
end
end

def resolve(deps, top_level = true, seen_items = {}, path_string = '') # rubocop:disable Metrics/AbcSize
graph = {}
if top_level
Inspec::Log.debug("Starting traversal of dependencies #{deps.map(&:name)}")
else
Inspec::Log.debug("Traversing dependency tree of transitive dependency #{deps.map(&:name)}")
end

detect_duplicates(deps, top_level, path_string)
deps.each do |dep|
path_string = if path_string.empty?
dep.name
else
path_string + " -> #{dep.name}"
end
new_seen_items = seen_items.dup
new_path_string = if path_string.empty?
dep.name
else
path_string + " -> #{dep.name}"
end

if seen_items.key?(dep.resolved_source)
fail Inspec::CyclicDependencyError, "Dependency #{dep} would cause a dependency cycle (#{path_string})"
if new_seen_items.key?(dep.resolved_source)
fail Inspec::CyclicDependencyError, "Dependency #{dep} would cause a dependency cycle (#{new_path_string})"
else
seen_items[dep.resolved_source] = true
new_seen_items[dep.resolved_source] = true
end

if !dep.source_satisfies_spec?
Expand All @@ -59,8 +77,7 @@ def resolve(deps, top_level = true, seen_items = {}, path_string = '')
Inspec::Log.debug("Adding dependency #{dep.name} (#{dep.resolved_source})")
graph[dep.name] = dep
if !dep.dependencies.empty?
# Recursively resolve any transitive dependencies.
resolve(dep.dependencies, false, seen_items.dup, path_string)
resolve(dep.dependencies, false, new_seen_items.dup, new_path_string)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/inspec/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class Error < StandardError; end
# dependency resolution
class CyclicDependencyError < Error; end
class UnsatisfiedVersionSpecification < Error; end
class DuplicateDep < Error; end
end
22 changes: 22 additions & 0 deletions test/unit/dependencies/resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def initialize(name)
def resolved_source
{ path: name }
end

def source_satisfies_spec?
true
end
end

describe Inspec::Resolver do
Expand All @@ -21,6 +25,24 @@ def resolved_source
subject.resolve([]).must_equal({})
end

it "errors if a dependency is listed twice at the same level" do
dep = FakeDep.new("fake_dep_0")
lambda { subject.resolve([dep, dep]) }.must_raise Inspec::DuplicateDep
end

it "fails if there is a simple cycle " do
dep0 = FakeDep.new("fake_dep_0")
dep1 = FakeDep.new("fake_dep_1")
dep2 = FakeDep.new("fake_dep_2")

dep0.stubs(:dependencies).returns([dep1])

dep1.stubs(:dependencies).returns([dep2])
dep2.stubs(:dependencies).returns([dep1])
lambda { subject.resolve([dep0]) }.must_raise Inspec::CyclicDependencyError
end


it "errors if the source version doesn't match the requirement" do
dep = FakeDep.new("fake_dep_0")
dep.expects(:source_satisfies_spec?).returns(false)
Expand Down