summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_dependency_list.rb
blob: 590e042315c48b14aa90ac56f1562e38bdb9f807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: true

require_relative "helper"
require "rubygems/dependency_list"

class TestGemDependencyList < Gem::TestCase
  def setup
    super

    @deplist = Gem::DependencyList.new

    # TODO: switch to util_spec
    @a1 = util_spec "a", "1"
    @a2 = util_spec "a", "2"
    @a3 = util_spec "a", "3"

    @b1 = util_spec "b", "1" do |s|
      s.add_dependency "a", ">= 1"
    end

    @b2 = util_spec "b", "2" do |s|
      s.add_dependency "a", ">= 1"
    end

    @c1 = util_spec "c", "1" do |s|
      s.add_dependency "b", ">= 1"
    end

    @c2 = util_spec "c", "2"

    @d1 = util_spec "d", "1" do |s|
      s.add_dependency "c", ">= 1"
    end
  end

  def test_active_count
    assert_equal 0, @deplist.send(:active_count, [], {})
    assert_equal 1, @deplist.send(:active_count, [@a1], {})
    assert_equal 0, @deplist.send(:active_count, [@a1],
                                  { @a1.full_name => true })
  end

  def test_add
    assert_equal [], @deplist.dependency_order

    @deplist.add @a1, @b2

    assert_equal [@b2, @a1], @deplist.dependency_order
  end

  def test_dependency_order
    @deplist.add @a1, @b1, @c1, @d1

    order = @deplist.dependency_order

    assert_equal %w[d-1 c-1 b-1 a-1], order.map(&:full_name)
  end

  def test_dependency_order_circle
    @a1.add_dependency "c", ">= 1"
    @deplist.add @a1, @b1, @c1

    order = @deplist.dependency_order

    assert_equal %w[b-1 c-1 a-1], order.map(&:full_name)
  end

  def test_dependency_order_development
    e1 = util_spec "e", "1"
    f1 = util_spec "f", "1"
    g1 = util_spec "g", "1"

    @a1.add_dependency "e"
    @a1.add_dependency "f"
    @a1.add_dependency "g"
    g1.add_development_dependency "a"

    deplist = Gem::DependencyList.new true
    deplist.add @a1, e1, f1, g1

    order = deplist.dependency_order

    assert_equal %w[g-1 a-1 f-1 e-1], order.map(&:full_name),
                 "development on"

    deplist2 = Gem::DependencyList.new
    deplist2.add @a1, e1, f1, g1

    order = deplist2.dependency_order

    assert_equal %w[a-1 g-1 f-1 e-1], order.map(&:full_name),
                 "development off"
  end

  def test_dependency_order_diamond
    util_diamond
    e1 = util_spec "e", "1"
    @deplist.add e1
    @a1.add_dependency "e", ">= 1"

    order = @deplist.dependency_order

    assert_equal %w[d-1 c-2 b-1 a-2 e-1], order.map(&:full_name),
                 "deps of trimmed specs not included"
  end

  def test_dependency_order_no_dependencies
    @deplist.add @a1, @c2

    order = @deplist.dependency_order

    assert_equal %w[c-2 a-1], order.map(&:full_name)
  end

  def test_find_name
    @deplist.add @a1, @b2

    assert_equal "a-1", @deplist.find_name("a-1").full_name
    assert_equal "b-2", @deplist.find_name("b-2").full_name

    assert_nil @deplist.find_name("c-2")
  end

  def test_ok_eh
    assert @deplist.ok?, "no dependencies"

    @deplist.add @b2

    refute @deplist.ok?, "unsatisfied dependency"

    @deplist.add @a1

    assert @deplist.ok?, "satisfied dependency"
  end

  def test_why_not_ok_eh
    assert_equal({}, @deplist.why_not_ok?)

    @deplist.add @b2

    exp = {
      "b" => [
        Gem::Dependency.new("a", ">= 1"),
      ],
    }

    assert_equal exp, @deplist.why_not_ok?
  end

  def test_why_not_ok_eh_old_dependency
    a  = util_spec "a", "1",
                  "b" => "~> 1.0"

    b0 = util_spec "b", "1.0",
                  "d" => ">= 0"

    b1 = util_spec "b", "1.1"

    util_clear_gems

    @deplist.clear

    @deplist.add a, b0, b1

    assert_equal({}, @deplist.why_not_ok?)
  end

  def test_ok_eh_mismatch
    a1 = util_spec "a", "1"
    a2 = util_spec "a", "2"

    b = util_spec "b", "1" do |s|
      s.add_dependency "a", "= 1"
    end

    c = util_spec "c", "1" do |s|
      s.add_dependency "a", "= 2"
    end

    d = util_spec "d", "1" do |s|
      s.add_dependency "b"
      s.add_dependency "c"
    end

    @deplist.add a1, a2, b, c, d

    assert @deplist.ok?, "this will break on require"
  end

  def test_ok_eh_redundant
    @deplist.add @a1, @a3, @b2

    @deplist.remove_by_name("a-1")

    assert @deplist.ok?
  end

  def test_ok_to_remove_eh
    @deplist.add @a1

    assert @deplist.ok_to_remove?("a-1")

    @deplist.add @b2

    refute @deplist.ok_to_remove?("a-1")

    @deplist.add @a2

    assert @deplist.ok_to_remove?("a-1")
    assert @deplist.ok_to_remove?("a-2")
    assert @deplist.ok_to_remove?("b-2")
  end

  def test_ok_to_remove_eh_after_sibling_removed
    @deplist.add @a1, @a2, @b2

    assert @deplist.ok_to_remove?("a-1")
    assert @deplist.ok_to_remove?("a-2")

    @deplist.remove_by_name("a-1")

    refute @deplist.ok_to_remove?("a-2")
  end

  def test_remove_by_name
    @deplist.add @a1, @b2

    @deplist.remove_by_name "a-1"

    refute @deplist.ok?
  end

  def test_tsort_each_node
    util_diamond

    order = %w[a-1 a-2 b-1 c-2 d-1]

    @deplist.tsort_each_node do |node|
      assert_equal order.shift, node.full_name
    end

    assert_empty order
  end

  def test_tsort_each_child
    util_diamond

    order = %w[a-2]

    @deplist.tsort_each_child(@b1) do |node|
      assert_equal order.shift, node.full_name
    end

    assert_empty order
  end

  # d1 -> b1 -> a1
  # d1 -> c2 -> a2
  def util_diamond
    @c2.add_dependency "a", ">= 2"
    @d1.add_dependency "b"

    @deplist.add @a1, @a2, @b1, @c2, @d1
  end
end