3 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
5 # See LICENSE.txt for permissions.
10 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
11 require 'rubygems/dependency_list'
13 class TestGemDependencyList < RubyGemTestCase
18 @deplist = Gem::DependencyList.new
20 @a1 = quick_gem 'a', '1'
21 @a2 = quick_gem 'a', '2'
22 @a3 = quick_gem 'a', '3'
24 @b1 = quick_gem 'b', '1' do |s| s.add_dependency 'a', '>= 1' end
25 @b2 = quick_gem 'b', '2' do |s| s.add_dependency 'a', '>= 1' end
27 @c1 = quick_gem 'c', '1' do |s| s.add_dependency 'b', '>= 1' end
28 @c2 = quick_gem 'c', '2'
30 @d1 = quick_gem 'd', '1' do |s| s.add_dependency 'c', '>= 1' end
33 def test_self_from_source_index
39 si = Gem::SourceIndex.new hash
40 deps = Gem::DependencyList.from_source_index si
42 assert_equal %w[b-2 a-1], deps.dependency_order.map { |s| s.full_name }
46 assert_equal 0, @deplist.send(:active_count, [], {})
47 assert_equal 1, @deplist.send(:active_count, [@a1], {})
48 assert_equal 0, @deplist.send(:active_count, [@a1],
49 { @a1.full_name => true })
53 assert_equal [], @deplist.dependency_order
57 assert_equal [@b2, @a1], @deplist.dependency_order
60 def test_dependency_order
61 @deplist.add @a1, @b1, @c1, @d1
63 order = @deplist.dependency_order
65 assert_equal %w[d-1 c-1 b-1 a-1], order.map { |s| s.full_name }
68 def test_dependency_order_circle
69 @a1.add_dependency 'c', '>= 1'
70 @deplist.add @a1, @b1, @c1
72 order = @deplist.dependency_order
74 assert_equal %w[b-1 c-1 a-1], order.map { |s| s.full_name }
77 def test_dependency_order_diamond
79 e1 = quick_gem 'e', '1'
81 @a1.add_dependency 'e', '>= 1'
83 order = @deplist.dependency_order
85 assert_equal %w[d-1 c-2 b-1 a-2 e-1], order.map { |s| s.full_name },
86 'deps of trimmed specs not included'
89 def test_dependency_order_no_dependendencies
92 order = @deplist.dependency_order
94 assert_equal %w[c-2 a-1], order.map { |s| s.full_name }
100 assert_equal "a-1", @deplist.find_name("a-1").full_name
101 assert_equal "b-2", @deplist.find_name("b-2").full_name
103 assert_nil @deplist.find_name("c-2")
107 assert @deplist.ok?, 'no dependencies'
111 assert ! @deplist.ok?, 'unsatisfied dependency'
115 assert @deplist.ok?, 'satisfied dependency'
118 def test_ok_eh_mismatch
119 a1 = quick_gem 'a', '1'
120 a2 = quick_gem 'a', '2'
122 b = quick_gem 'b', '1' do |s| s.add_dependency 'a', '= 1' end
123 c = quick_gem 'c', '1' do |s| s.add_dependency 'a', '= 2' end
125 d = quick_gem 'd', '1' do |s|
130 @deplist.add a1, a2, b, c, d
132 assert @deplist.ok?, 'this will break on require'
135 def test_ok_eh_redundant
136 @deplist.add @a1, @a3, @b2
138 @deplist.remove_by_name("a-1")
143 def test_ok_to_remove_eh
146 assert @deplist.ok_to_remove?("a-1")
150 assert ! @deplist.ok_to_remove?("a-1")
154 assert @deplist.ok_to_remove?("a-1")
155 assert @deplist.ok_to_remove?("a-2")
156 assert @deplist.ok_to_remove?("b-2")
159 def test_ok_to_remove_eh_after_sibling_removed
160 @deplist.add @a1, @a2, @b2
162 assert @deplist.ok_to_remove?("a-1")
163 assert @deplist.ok_to_remove?("a-2")
165 @deplist.remove_by_name("a-1")
167 assert ! @deplist.ok_to_remove?("a-2")
170 def test_remove_by_name
171 @deplist.add @a1, @b2
173 @deplist.remove_by_name "a-1"
175 assert ! @deplist.ok?
178 def test_tsort_each_node
181 order = %w[a-1 a-2 b-1 c-2 d-1]
183 @deplist.tsort_each_node do |node|
184 assert_equal order.shift, node.full_name
190 def test_tsort_each_child
195 @deplist.tsort_each_child(@b1) do |node|
196 assert_equal order.shift, node.full_name
205 @c2.add_dependency 'a', '>= 2'
206 @d1.add_dependency 'b'
208 @deplist.add @a1, @a2, @b1, @c2, @d1