summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_request_set.rb
blob: 8afab65356ea2fe5e8b2e2ea200c7454b2cccd21 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
require 'rubygems/test_case'
require 'rubygems/request_set'

class TestGemRequestSet < Gem::TestCase
  def setup
    super

    Gem::RemoteFetcher.fetcher = @fetcher = Gem::FakeFetcher.new

    @DR = Gem::Resolver
  end

  def test_gem
    util_spec "a", "2"

    rs = Gem::RequestSet.new
    rs.gem "a", "= 2"

    assert_equal [Gem::Dependency.new("a", "=2")], rs.dependencies
  end

  def test_gem_duplicate
    rs = Gem::RequestSet.new

    rs.gem 'a', '1'
    rs.gem 'a', '2'

    assert_equal [dep('a', '= 1', '= 2')], rs.dependencies
  end

  def test_import
    rs = Gem::RequestSet.new
    rs.gem 'a'

    rs.import [dep('b')]

    assert_equal [dep('a'), dep('b')], rs.dependencies
  end

  def test_install_from_gemdeps
    spec_fetcher do |fetcher|
      fetcher.gem 'a', 2
    end

    done_installing_ran = false

    Gem.done_installing do |installer, specs|
      done_installing_ran = true
    end

    rs = Gem::RequestSet.new
    installed = []

    open 'gem.deps.rb', 'w' do |io|
      io.puts 'gem "a"'
      io.flush

      result = rs.install_from_gemdeps :gemdeps => io.path do |req, installer|
        installed << req.full_name
      end

      assert_kind_of Array, result # what is supposed to be in here?
    end

    assert_includes installed, 'a-2'
    assert_path_exists File.join @gemhome, 'gems', 'a-2'
    assert_path_exists 'gem.deps.rb.lock'

    assert rs.remote
    refute done_installing_ran
  end

  def test_install_from_gemdeps_explain
    spec_fetcher do |fetcher|
      fetcher.gem 'a', 2
    end

    rs = Gem::RequestSet.new

    open 'gem.deps.rb', 'w' do |io|
      io.puts 'gem "a"'
      io.flush

      expected = <<-EXPECTED
Gems to install:
  a-2
      EXPECTED

      assert_output expected do
        rs.install_from_gemdeps :gemdeps => io.path, :explain => true
      end
    end
  end

  def test_install_from_gemdeps_install_dir
    spec_fetcher do |fetcher|
      fetcher.gem 'a', 2
    end

    util_clear_gems
    refute_path_exists File.join Gem.dir, 'gems', 'a-2'

    rs = Gem::RequestSet.new
    installed = []

    open 'gem.deps.rb', 'w' do |io|
      io.puts 'gem "a"'
    end

    options = {
      :gemdeps     => 'gem.deps.rb',
      :install_dir => "#{@gemhome}2",
    }

    rs.install_from_gemdeps options do |req, installer|
      installed << req.full_name
    end

    assert_includes installed, 'a-2'
    refute_path_exists File.join Gem.dir, 'gems', 'a-2'
  end

  def test_install_from_gemdeps_local
    spec_fetcher do |fetcher|
      fetcher.gem 'a', 2
    end

    rs = Gem::RequestSet.new

    open 'gem.deps.rb', 'w' do |io|
      io.puts 'gem "a"'
      io.flush

      assert_raises Gem::UnsatisfiableDependencyError do
        rs.install_from_gemdeps :gemdeps => io.path, :domain => :local
      end
    end

    refute rs.remote
  end

  def test_install_from_gemdeps_lockfile
    spec_fetcher do |fetcher|
      fetcher.download 'a', 1
      fetcher.download 'a', 2
      fetcher.download 'b', 1, 'a' => '>= 0'
    end

    rs = Gem::RequestSet.new
    installed = []

    open 'gem.deps.rb.lock', 'w' do |io|
      io.puts <<-LOCKFILE
GEM
  remote: #{@gem_repo}
  specs:
    a (1)
    b (1)
      a (~> 1.0)

PLATFORMS
  #{Gem::Platform::RUBY}

DEPENDENCIES
  b
      LOCKFILE
    end

    open 'gem.deps.rb', 'w' do |io|
      io.puts 'gem "b"'
    end

    rs.install_from_gemdeps :gemdeps => 'gem.deps.rb' do |req, installer|
      installed << req.full_name
    end

    assert_includes installed, 'b-1'
    assert_includes installed, 'a-1'

    assert_path_exists File.join @gemhome, 'specifications', 'a-1.gemspec'
    assert_path_exists File.join @gemhome, 'specifications', 'b-1.gemspec'
  end

  def test_install_from_gemdeps_version_mismatch
    spec_fetcher do |fetcher|
      fetcher.gem 'a', 2
    end

    rs = Gem::RequestSet.new
    installed = []

    open 'gem.deps.rb', 'w' do |io|
      io.puts <<-GEM_DEPS
gem "a"
ruby "0"
      GEM_DEPS

      io.flush

      rs.install_from_gemdeps :gemdeps => io.path do |req, installer|
        installed << req.full_name
      end
    end

    assert_includes installed, 'a-2'
  end

  def test_load_gemdeps
    rs = Gem::RequestSet.new

    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.puts 'gem "a"'
      io.flush

      gem_deps = rs.load_gemdeps io.path

      assert_kind_of Gem::RequestSet::GemDependencyAPI, gem_deps
      io
    end
    tf.close! if tf.respond_to? :close!

    assert_equal [dep('a')], rs.dependencies

    assert rs.git_set
    assert rs.vendor_set
  end

  def test_load_gemdeps_installing
    rs = Gem::RequestSet.new

    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.puts 'ruby "0"'
      io.puts 'gem "a"'
      io.flush

      gem_deps = rs.load_gemdeps io.path, [], true

      assert_kind_of Gem::RequestSet::GemDependencyAPI, gem_deps
      io
    end
    tf.close! if tf.respond_to? :close!

    assert_equal [dep('a')], rs.dependencies
  end

  def test_load_gemdeps_without_groups
    rs = Gem::RequestSet.new

    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.puts 'gem "a", :group => :test'
      io.flush

      rs.load_gemdeps io.path, [:test]
      io
    end
    tf.close! if tf.respond_to? :close!

    assert_empty rs.dependencies
  end

  def test_resolve
    a = util_spec "a", "2", "b" => ">= 2"
    b = util_spec "b", "2"

    rs = Gem::RequestSet.new
    rs.gem "a"

    orig_errors = rs.errors

    res = rs.resolve StaticSet.new([a, b])
    assert_equal 2, res.size

    names = res.map { |s| s.full_name }.sort

    assert_equal ["a-2", "b-2"], names

    refute_same orig_errors, rs.errors
  end

  def test_bug_bug_990
    a = util_spec 'a', '1.b',  'b' => '~> 1.a'
    b = util_spec 'b', '1.b',  'c' => '>= 1'
    c = util_spec 'c', '1.1.b'

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.prerelease = true

    res = rs.resolve StaticSet.new([a, b, c])
    assert_equal 3, res.size

    names = res.map { |s| s.full_name }.sort

    assert_equal %w[a-1.b b-1.b c-1.1.b], names
  end

  def test_resolve_development
    a = util_spec 'a', 1
    spec = Gem::Resolver::SpecSpecification.new nil, a

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.development = true

    res = rs.resolve StaticSet.new [spec]
    assert_equal 1, res.size

    assert rs.resolver.development
    refute rs.resolver.development_shallow
  end

  def test_resolve_development_shallow
    a = util_spec 'a', 1 do |s| s.add_development_dependency 'b' end
    b = util_spec 'b', 1 do |s| s.add_development_dependency 'c' end
    c = util_spec 'c', 1

    a_spec = Gem::Resolver::SpecSpecification.new nil, a
    b_spec = Gem::Resolver::SpecSpecification.new nil, b
    c_spec = Gem::Resolver::SpecSpecification.new nil, c

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.development = true
    rs.development_shallow = true

    res = rs.resolve StaticSet.new [a_spec, b_spec, c_spec]
    assert_equal 2, res.size

    assert rs.resolver.development
    assert rs.resolver.development_shallow
  end

  def test_resolve_git
    name, _, repository, = git_gem

    rs = Gem::RequestSet.new

    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.puts <<-gems_deps_rb
        gem "#{name}", :git => "#{repository}"
      gems_deps_rb

      io.flush

      rs.load_gemdeps io.path
      io
    end
    tf.close! if tf.respond_to? :close!

    res = rs.resolve
    assert_equal 1, res.size

    names = res.map { |s| s.full_name }.sort

    assert_equal %w[a-1], names

    assert_equal [@DR::BestSet, @DR::GitSet, @DR::VendorSet],
                 rs.sets.map { |set| set.class }
  end

  def test_resolve_ignore_dependencies
    a = util_spec "a", "2", "b" => ">= 2"
    b = util_spec "b", "2"

    rs = Gem::RequestSet.new
    rs.gem "a"
    rs.ignore_dependencies = true

    res = rs.resolve StaticSet.new([a, b])
    assert_equal 1, res.size

    names = res.map { |s| s.full_name }.sort

    assert_equal %w[a-2], names
  end

  def test_resolve_incompatible
    a1 = util_spec 'a', 1
    a2 = util_spec 'a', 2

    rs = Gem::RequestSet.new
    rs.gem 'a', '= 1'
    rs.gem 'a', '= 2'

    set = StaticSet.new [a1, a2]

    assert_raises Gem::UnsatisfiableDependencyError do
      rs.resolve set
    end
  end

  def test_resolve_vendor
    a_name, _, a_directory = vendor_gem 'a', 1 do |s|
      s.add_dependency 'b', '~> 2.0'
    end

    b_name, _, b_directory = vendor_gem 'b', 2

    rs = Gem::RequestSet.new

    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.puts <<-gems_deps_rb
        gem "#{a_name}", :path => "#{a_directory}"
        gem "#{b_name}", :path => "#{b_directory}"
      gems_deps_rb

      io.flush

      rs.load_gemdeps io.path
      io
    end
    tf.close! if tf.respond_to? :close!

    res = rs.resolve
    assert_equal 2, res.size

    names = res.map { |s| s.full_name }.sort

    assert_equal ["a-1", "b-2"], names

    assert_equal [@DR::BestSet, @DR::GitSet, @DR::VendorSet],
                 rs.sets.map { |set| set.class }
  end

  def test_sorted_requests
    a = util_spec "a", "2", "b" => ">= 2"
    b = util_spec "b", "2", "c" => ">= 2"
    c = util_spec "c", "2"

    rs = Gem::RequestSet.new
    rs.gem "a"

    rs.resolve StaticSet.new([a, b, c])

    names = rs.sorted_requests.map { |s| s.full_name }
    assert_equal %w!c-2 b-2 a-2!, names
  end

  def test_install
    done_installing_ran = false

    Gem.done_installing do
      done_installing_ran = true
    end

    spec_fetcher do |fetcher|
      fetcher.download "a", "1", "b" => "= 1"
      fetcher.download "b", "1"
    end

    rs = Gem::RequestSet.new
    rs.gem 'a'

    rs.resolve

    reqs       = []
    installers = []

    installed = rs.install({}) do |req, installer|
      reqs       << req
      installers << installer
    end

    assert_equal %w[b-1 a-1], reqs.map { |req| req.full_name }
    assert_equal %w[b-1 a-1],
                 installers.map { |installer| installer.spec.full_name }

    assert_path_exists File.join @gemhome, 'specifications', 'a-1.gemspec'
    assert_path_exists File.join @gemhome, 'specifications', 'b-1.gemspec'

    assert_equal %w[b-1 a-1], installed.map { |s| s.full_name }

    assert done_installing_ran
  end

  def test_install_into
    spec_fetcher do |fetcher|
      fetcher.gem "a", "1", "b" => "= 1"
      fetcher.gem "b", "1"
    end

    rs = Gem::RequestSet.new
    rs.gem "a"

    rs.resolve

    installed = rs.install_into @tempdir do
      assert_equal @tempdir, ENV['GEM_HOME']
    end

    assert_path_exists File.join @tempdir, 'specifications', 'a-1.gemspec'
    assert_path_exists File.join @tempdir, 'specifications', 'b-1.gemspec'

    assert_equal %w!b-1 a-1!, installed.map { |s| s.full_name }
  end

  def test_install_into_development_shallow
    spec_fetcher do |fetcher|
      fetcher.gem 'a', '1' do |s|
        s.add_development_dependency 'b', '= 1'
      end

      fetcher.gem 'b', '1' do |s|
        s.add_development_dependency 'c', '= 1'
      end

      fetcher.spec 'c', '1'
    end

    rs = Gem::RequestSet.new
    rs.development         = true
    rs.development_shallow = true
    rs.gem 'a'

    rs.resolve

    options = {
      :development         => true,
      :development_shallow => true,
    }

    installed = rs.install_into @tempdir, true, options do
      assert_equal @tempdir, ENV['GEM_HOME']
    end

    assert_equal %w[a-1 b-1], installed.map { |s| s.full_name }.sort
  end

  def test_sorted_requests_development_shallow
    a = util_spec 'a', 1 do |s| s.add_development_dependency 'b' end
    b = util_spec 'b', 1 do |s| s.add_development_dependency 'c' end
    c = util_spec 'c', 1

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.development = true
    rs.development_shallow = true

    a_spec = Gem::Resolver::SpecSpecification.new nil, a
    b_spec = Gem::Resolver::SpecSpecification.new nil, b
    c_spec = Gem::Resolver::SpecSpecification.new nil, c

    rs.resolve StaticSet.new [a_spec, b_spec, c_spec]

    assert_equal %w[b-1 a-1], rs.sorted_requests.map { |req| req.full_name }
  end

  def test_tsort_each_child_development
    a = util_spec 'a', 1 do |s| s.add_development_dependency 'b' end
    b = util_spec 'b', 1 do |s| s.add_development_dependency 'c' end
    c = util_spec 'c', 1

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.development = true
    rs.development_shallow = true

    a_spec = Gem::Resolver::SpecSpecification.new nil, a
    b_spec = Gem::Resolver::SpecSpecification.new nil, b
    c_spec = Gem::Resolver::SpecSpecification.new nil, c

    rs.resolve StaticSet.new [a_spec, b_spec, c_spec]

    a_req = Gem::Resolver::ActivationRequest.new a_spec, nil

    deps = rs.enum_for(:tsort_each_child, a_req).to_a

    assert_equal %w[b], deps.map { |dep| dep.name }
  end

  def test_tsort_each_child_development_shallow
    a = util_spec 'a', 1 do |s| s.add_development_dependency 'b' end
    b = util_spec 'b', 1 do |s| s.add_development_dependency 'c' end
    c = util_spec 'c', 1

    rs = Gem::RequestSet.new
    rs.gem 'a'
    rs.development = true
    rs.development_shallow = true

    a_spec = Gem::Resolver::SpecSpecification.new nil, a
    b_spec = Gem::Resolver::SpecSpecification.new nil, b
    c_spec = Gem::Resolver::SpecSpecification.new nil, c

    rs.resolve StaticSet.new [a_spec, b_spec, c_spec]

    b_req = Gem::Resolver::ActivationRequest.new b_spec, nil

    deps = rs.enum_for(:tsort_each_child, b_req).to_a

    assert_empty deps
  end

end