summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_dependency.rb
blob: 7ddeafedce710deea398a5705db656d545fbe815 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/dependency'

class TestGemDependency < Gem::TestCase
  def test_initialize
    d = dep "pkg", "> 1.0"

    assert_equal "pkg", d.name
    assert_equal req("> 1.0"), d.requirement
  end

  def test_initialize_type_bad
    e = assert_raises ArgumentError do
      Gem::Dependency.new 'monkey' => '1.0'
    end

    assert_equal 'dependency name must be a String, was {"monkey"=>"1.0"}',
                 e.message
  end

  def test_initialize_double
    d = dep "pkg", "> 1.0", "< 2.0"
    assert_equal req("> 1.0", "< 2.0"), d.requirement
  end

  def test_initialize_empty
    d = dep "pkg"
    assert_equal req(">= 0"), d.requirement
  end

  def test_initialize_prerelease
    d = dep 'd', '1.a'

    assert d.prerelease?

    d = dep 'd', '= 1.a'

    assert d.prerelease?
  end

  def test_initialize_type
    assert_equal :runtime, dep("pkg").type
    assert_equal :development, dep("pkg", [], :development).type

    assert_raises ArgumentError do
      dep "pkg", :sometimes
    end
  end

  def test_initialize_version
    d = dep "pkg", v("2")
    assert_equal req("= 2"), d.requirement
  end

  def test_equals2
    o  = dep "other"
    d  = dep "pkg", "> 1.0"
    d1 = dep "pkg", "> 1.1"

    assert_equal d, d.dup
    assert_equal d.dup, d

    refute_equal d, d1
    refute_equal d1, d

    refute_equal d, o
    refute_equal o, d

    refute_equal d, Object.new
    refute_equal Object.new, d
  end

  def test_equals2_type
    refute_equal dep("pkg", :runtime), dep("pkg", :development)
  end

  def test_equals_tilde
    d = dep "a", "0"

    assert_match d,                  d,             "match self"
    assert_match dep("a", ">= 0"),   d,             "match version exact"
    assert_match dep("a", ">= 0"),   dep("a", "1"), "match version"
    refute_match dep("a"), Object.new

    Gem::Deprecate.skip_during do
      assert_match dep(/a/, ">= 0"),   d,             "match simple regexp"
      assert_match dep(/a|b/, ">= 0"), d,             "match scary regexp"
      refute_match dep(/a/), dep("b")
    end
  end

  def test_equals_tilde_escape
    refute_match dep("a|b"), dep("a", "1")
    Gem::Deprecate.skip_during do
      assert_match dep(/a|b/), dep("a", "1")
    end
  end

  def test_equals_tilde_object
    o = Object.new
    def o.name    ; 'a' end
    def o.version ; '0' end

    assert_match dep("a"), o
  end

  def test_equals_tilde_spec
    assert_match dep("a", ">= 0"),   spec("a", "0")
    assert_match dep("a", "1"),      spec("a", "1")
    Gem::Deprecate.skip_during do
      assert_match dep(/a/, ">= 0"),   spec("a", "0")
      assert_match dep(/a|b/, ">= 0"), spec("b", "0")
      refute_match dep(/a/, ">= 0"),   spec("b", "0")
    end
  end

  def test_hash
    d = dep "pkg", "1.0"

    assert_equal d.hash, d.dup.hash
    assert_equal d.dup.hash, d.hash

    refute_equal dep("pkg", "1.0").hash,   dep("pkg", "2.0").hash, "requirement"
    refute_equal dep("pkg", "1.0").hash,   dep("abc", "1.0").hash, "name"
    refute_equal dep("pkg", :development), dep("pkg", :runtime), "type"
  end

  def test_match_eh_name_tuple
    a_dep = dep 'a'

    a_tup = Gem::NameTuple.new 'a', 1
    b_tup = Gem::NameTuple.new 'b', 2
    c_tup = Gem::NameTuple.new 'c', '2.a'

    assert a_dep.match? a_tup
    refute a_dep.match? b_tup

    b_dep = dep 'b', '>= 3'

    refute b_dep.match? b_tup

    c_dep = dep 'c', '>= 1'

    refute c_dep.match? c_tup

    c_dep = dep 'c'

    refute c_dep.match? c_tup

    c_dep = dep 'c', '2.a'

    assert c_dep.match? c_tup
  end

  def test_match_eh_allow_prerelease
    a_dep = dep 'a'

    a_tup = Gem::NameTuple.new 'a', 1
    b_tup = Gem::NameTuple.new 'b', 2
    c_tup = Gem::NameTuple.new 'c', '2.a'

    assert a_dep.match? a_tup, nil, true
    refute a_dep.match? b_tup, nil, true

    b_dep = dep 'b', '>= 3'

    refute b_dep.match? b_tup, nil, true

    c_dep = dep 'c', '>= 1'

    assert c_dep.match? c_tup, nil, true

    c_dep = dep 'c'

    assert c_dep.match? c_tup, nil, true

    c_dep = dep 'c', '2.a'

    assert c_dep.match? c_tup, nil, true
  end

  def test_match_eh_specification
    a_dep = dep 'a'

    a_spec = util_spec 'a', 1
    b_spec = util_spec 'b', 2
    c_spec = util_spec 'c', '2.a'

    assert a_dep.match? a_spec
    refute a_dep.match? b_spec

    b_dep = dep 'b', '>= 3'

    refute b_dep.match? b_spec

    c_dep = dep 'c', '>= 1'

    refute c_dep.match? c_spec

    c_dep = dep 'c'

    refute c_dep.match? c_spec

    c_dep = dep 'c', '2.a'

    assert c_dep.match? c_spec
  end

  def test_matches_spec_eh
    spec = util_spec 'b', 2

    refute dep('a')        .matches_spec?(spec), 'name mismatch'
    assert dep('b')        .matches_spec?(spec), 'name match'
    refute dep('b', '= 1') .matches_spec?(spec), 'requirement mismatch'
    assert dep('b', '~> 2').matches_spec?(spec), 'requirement match'
  end

  def test_matches_spec_eh_prerelease
    spec = util_spec 'b', '2.1.a'

    refute dep('a')          .matches_spec?(spec), 'name mismatch'
    assert dep('b')          .matches_spec?(spec), 'name match'
    refute dep('b', '= 1')   .matches_spec?(spec), 'requirement mismatch'
    assert dep('b', '~> 2')  .matches_spec?(spec), 'requirement match'
    assert dep('b', '~> 2.a').matches_spec?(spec), 'prerelease requirement'
  end

  def test_merge
    a1 = dep 'a', '~> 1.0'
    a2 = dep 'a', '= 1.0'

    a3 = a1.merge a2

    assert_equal dep('a', '~> 1.0', '= 1.0'), a3
  end

  def test_merge_default
    a1 = dep 'a'
    a2 = dep 'a', '1'

    a3 = a1.merge a2

    assert_equal dep('a', '1'), a3
  end

  def test_merge_name_mismatch
    a = dep 'a'
    b = dep 'b'

    e = assert_raises ArgumentError do
      a.merge b
    end

    assert_equal 'a (>= 0) and b (>= 0) have different names',
                 e.message
  end

  def test_merge_other_default
    a1 = dep 'a', '1'
    a2 = dep 'a'

    a3 = a1.merge a2

    assert_equal dep('a', '1'), a3
  end

  def test_prerelease_eh
    d = dep "pkg", "= 1"

    refute d.prerelease?

    d.prerelease = true

    assert d.prerelease?

    d = dep "pkg", "= 1.a"

    assert d.prerelease?

    d.prerelease = false

    assert d.prerelease?

    d = dep "pkg", "> 1.a", "> 2"

    assert d.prerelease?
  end

  def test_specific
    refute dep('a', '> 1').specific?

    assert dep('a', '= 1').specific?
  end

  def test_to_spec
    a_1 = util_spec 'a', '1'
    a_2 = util_spec 'a', '2'

    a_dep = dep 'a', '>= 0'
    install_specs a_1, a_2

    assert_equal a_2, a_dep.to_spec
  end

  def test_to_spec_prerelease
    a_1     = util_spec 'a', '1'
    a_1_1_a = util_spec 'a', '1.1.a'

    a_dep = dep 'a', '>= 0'
    install_specs a_1, a_1_1_a

    assert_equal a_1, a_dep.to_spec

    a_pre_dep = dep 'a', '>= 0'
    a_pre_dep.prerelease = true

    assert_equal a_1_1_a, a_pre_dep.to_spec
  end

  def test_to_specs_suggests_other_versions
    a = util_spec 'a', '1.0'
    install_specs a

    a_file = File.join a.gem_dir, 'lib', 'a_file.rb'

    write_file a_file do |io|
      io.puts '# a_file.rb'
    end

    dep = Gem::Dependency.new "a", "= 2.0"

    e = assert_raises Gem::MissingSpecVersionError do
      dep.to_specs
    end

    assert_match "Could not find 'a' (= 2.0) - did find: [a-1.0]", e.message
  end

  def test_to_specs_respects_bundler_version
    b = util_spec 'bundler', '2.0.0.pre.1'
    b_1 = util_spec 'bundler', '1'
    install_specs b, b_1

    b_file = File.join b.gem_dir, 'lib', 'bundler', 'setup.rb'

    write_file b_file do |io|
      io.puts '# setup.rb'
    end

    dep = Gem::Dependency.new "bundler", ">= 0.a"

    assert_equal [b, b_1], dep.to_specs

    Gem::BundlerVersionFinder.stub(:bundler_version_with_reason, ["3.5", "reason"]) do
      e = assert_raises Gem::MissingSpecVersionError do
        dep.to_specs
      end

      assert_match "Could not find 'bundler' (3.5) required by reason.\nTo update to the latest version installed on your system, run `bundle update --bundler`.\nTo install the missing version, run `gem install bundler:3.5`\n", e.message
    end

    Gem::BundlerVersionFinder.stub(:bundler_version_with_reason, ["2.0.0.pre.1", "reason"]) do
      assert_equal [b], dep.to_specs
    end
  end

  def test_to_specs_indicates_total_gem_set_size
    a = util_spec 'a', '1.0'
    install_specs a

    a_file = File.join a.gem_dir, 'lib', 'a_file.rb'

    write_file a_file do |io|
      io.puts '# a_file.rb'
    end

    dep = Gem::Dependency.new "b", "= 2.0"

    e = assert_raises Gem::MissingSpecError do
      dep.to_specs
    end

    assert_match "Could not find 'b' (= 2.0) among 1 total gem(s)", e.message
  end

  def test_identity
    assert_equal dep("a", "= 1").identity, :released
    assert_equal dep("a", "= 1.a").identity, :complete
    assert_equal dep("a", " >= 1.a").identity, :abs_latest
    assert_equal dep("a").identity, :latest
  end
end