summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_request_set_gem_dependency_api.rb
blob: 959c28580f9427ee6b5e5ccc3abb0c88a4a550fd (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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/request_set'

class TestGemRequestSetGemDependencyAPI < Gem::TestCase

  def setup
    super

    @GDA = Gem::RequestSet::GemDependencyAPI

    @set = Gem::RequestSet.new

    @git_set    = Gem::Resolver::GitSet.new
    @vendor_set = Gem::Resolver::VendorSet.new

    @gda = @GDA.new @set, 'gem.deps.rb'
    @gda.instance_variable_set :@git_set,    @git_set
    @gda.instance_variable_set :@vendor_set, @vendor_set
  end

  def with_engine_version(name, version)
    engine               = RUBY_ENGINE if Object.const_defined? :RUBY_ENGINE
    engine_version_const = "#{Gem.ruby_engine.upcase}_VERSION"
    engine_version       = Object.const_get engine_version_const

    Object.send :remove_const, :RUBY_ENGINE         if engine
    Object.send :remove_const, engine_version_const if name == 'ruby' and
      Object.const_defined? engine_version_const

    new_engine_version_const = "#{name.upcase}_VERSION"
    Object.const_set :RUBY_ENGINE,             name    if name
    Object.const_set new_engine_version_const, version if version

    Gem.instance_variable_set :@ruby_version, Gem::Version.new(version)

    begin
      yield
    ensure
      Object.send :remove_const, :RUBY_ENGINE             if name
      Object.send :remove_const, new_engine_version_const if version

      Object.send :remove_const, engine_version_const     if name == 'ruby' and
        Object.const_defined? engine_version_const

      Object.const_set :RUBY_ENGINE,         engine         if engine
      Object.const_set engine_version_const, engine_version unless
        Object.const_defined? engine_version_const

      Gem.send :remove_instance_variable, :@ruby_version if
        Gem.instance_variables.include? :@ruby_version
    end
  end

  def test_gempspec_with_multiple_runtime_deps
    save_gemspec 'foo', '1.0' do |s|
      s.add_runtime_dependency 'bar', '>= 1.6.0', '< 1.6.4'
    end
    @gda.gemspec
    assert_equal %w{ foo bar }.sort, @set.dependencies.map(&:name).sort
    bar = @set.dependencies.find { |d| d.name == 'bar' }
    assert_equal [["<", Gem::Version.create('1.6.4')],
                  [">=", Gem::Version.create('1.6.0')]], bar.requirement.requirements.sort
  end

  def test_gemspec_without_group
    @gda.send :add_dependencies, [:development], [dep('a', '= 1')]

    assert_equal [dep('a', '= 1')], @set.dependencies

    @gda.without_groups << :development

    @gda.send :add_dependencies, [:development], [dep('b', '= 2')]

    assert_equal [dep('a', '= 1')], @set.dependencies
  end

  def test_gem
    @gda.gem 'a'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[a], @gda.requires['a']

    expected = { 'a' => Gem::Requirement.default }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_duplicate
    @gda.gem 'a'

    _, err = capture_io do
      @gda.gem 'a'
    end

    expected = "Gem dependencies file gem.deps.rb requires a more than once."

    assert_match expected, err
  end

  def test_gem_git
    @gda.gem 'a', :git => 'git/a'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git/a master], @git_set.repositories['a']

    expected = { 'a' => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_bitbucket
    @gda.gem 'a', :bitbucket => 'example/repository'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[https://example@bitbucket.org/example/repository.git master],
                 @git_set.repositories['a']

    expected = { 'a' => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_bitbucket_expand_path
    @gda.gem 'a', :bitbucket => 'example'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[https://example@bitbucket.org/example/example.git master],
                 @git_set.repositories['a']

    expected = { 'a' => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_git_branch
    _, err = capture_io do
      @gda.gem 'a', :git => 'git/a', :branch => 'other', :tag => 'v1'
    end
    expected = "Gem dependencies file gem.deps.rb includes git reference for both ref/branch and tag but only ref/branch is used."
    assert_match expected, err

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git/a other], @git_set.repositories['a']
  end

  def test_gem_git_gist
    @gda.gem 'a', :gist => 'a'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[https://gist.github.com/a.git master],
                 @git_set.repositories['a']
  end

  def test_gem_git_ref
    _, err = capture_io do
      @gda.gem 'a', :git => 'git/a', :ref => 'abcd123', :branch => 'other'
    end
    expected = "Gem dependencies file gem.deps.rb includes git reference for both ref and branch but only ref is used."
    assert_match expected, err

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git/a abcd123], @git_set.repositories['a']
  end

  def test_gem_git_submodules
    @gda.gem 'a', :git => 'git/a', :submodules => true

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git/a master], @git_set.repositories['a']
    assert_equal %w[git/a], @git_set.need_submodules.keys
  end

  def test_gem_git_tag
    @gda.gem 'a', :git => 'git/a', :tag => 'v1'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git/a v1], @git_set.repositories['a']
  end

  def test_gem_github
    @gda.gem 'a', :github => 'example/repository'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git://github.com/example/repository.git master],
                 @git_set.repositories['a']

    expected = { 'a' => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_github_expand_path
    @gda.gem 'a', :github => 'example'

    assert_equal [dep('a')], @set.dependencies

    assert_equal %w[git://github.com/example/example.git master],
                 @git_set.repositories['a']

    expected = { 'a' => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_group
    @gda.gem 'a', :group => :test

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

  def test_gem_group_without
    @gda.without_groups << :test

    @gda.gem 'a', :group => :test

    assert_empty @set.dependencies

    expected = { 'a' => Gem::Requirement.default }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_groups
    @gda.gem 'a', :groups => [:test, :development]

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

  def test_gem_path
    name, version, directory = vendor_gem

    @gda.gem name, :path => directory

    assert_equal [dep(name)], @set.dependencies

    loaded = @vendor_set.load_spec(name, version, Gem::Platform::RUBY, nil)

    assert_equal "#{name}-#{version}", loaded.full_name

    expected = { name => Gem::Requirement.create('!') }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_platforms
    win_platform, Gem.win_platform = Gem.win_platform?, false

    with_engine_version 'ruby', '2.0.0' do
      @gda.gem 'a', :platforms => :ruby

      refute_empty @set.dependencies
    end
  ensure
    Gem.win_platform = win_platform
  end

  def test_gem_platforms_bundler_ruby
    win_platform, Gem.win_platform = Gem.win_platform?, false

    with_engine_version 'ruby', '2.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      refute_empty set.dependencies
    end

    with_engine_version 'rbx', '2.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      refute_empty set.dependencies
    end

    with_engine_version 'truffleruby', '2.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      refute_empty set.dependencies
    end

    with_engine_version 'jruby', '1.7.6' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      assert_empty set.dependencies
    end

    Gem.win_platform = true

    with_engine_version 'ruby', '2.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      assert_empty set.dependencies
    end

    Gem.win_platform = win_platform
  end

  def test_gem_platforms_engine
    with_engine_version 'jruby', '1.7.6' do
      @gda.gem 'a', :platforms => :mri

      assert_empty @set.dependencies
    end

    with_engine_version 'truffleruby', '1.2.3' do
      @gda.gem 'a', :platforms => :mri

      assert_empty @set.dependencies
    end
  end

  def test_gem_platforms_maglev
    win_platform, Gem.win_platform = Gem.win_platform?, false

    with_engine_version 'maglev', '1.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :ruby

      refute_empty set.dependencies

      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :maglev

      refute_empty set.dependencies
    end
  ensure
    Gem.win_platform = win_platform
  end

  def test_gem_platforms_truffleruby
    with_engine_version 'truffleruby', '1.0.0' do
      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :truffleruby

      refute_empty set.dependencies

      set = Gem::RequestSet.new
      gda = @GDA.new set, 'gem.deps.rb'
      gda.gem 'a', :platforms => :maglev

      assert_empty set.dependencies
    end
  end

  def test_gem_platforms_multiple
    win_platform, Gem.win_platform = Gem.win_platform?, false

    with_engine_version 'ruby', '2.0.0' do
      @gda.gem 'a', :platforms => [:mswin, :jruby]

      assert_empty @set.dependencies
    end

  ensure
    Gem.win_platform = win_platform
  end

  def test_gem_platforms_platform
    win_platform, Gem.win_platform = Gem.win_platform?, false

    with_engine_version 'ruby', '2.0.0' do
      @gda.gem 'a', :platforms => :jruby, :platform => :ruby

      refute_empty @set.dependencies
    end
  ensure
    Gem.win_platform = win_platform
  end

  def test_gem_platforms_version
    with_engine_version 'ruby', '2.0.0' do
      @gda.gem 'a', :platforms => :ruby_18

      assert_empty @set.dependencies
    end
  end

  def test_gem_platforms_unknown
    e = assert_raises ArgumentError do
      @gda.gem 'a', :platforms => :unknown
    end

    assert_equal 'unknown platform :unknown', e.message
  end

  def test_gem_requires
    @gda.gem 'a', :require => %w[b c]
    @gda.gem 'd', :require => 'e'

    assert_equal [dep('a'), dep('d')], @set.dependencies

    assert_equal %w[b c], @gda.requires['a']
    assert_equal %w[e],   @gda.requires['d']
  end

  def test_gem_requires_false
    @gda.gem 'a', :require => false

    assert_equal [dep('a')], @set.dependencies

    assert_empty @gda.requires
  end

  def test_gem_requires_without_group
    @gda.without_groups << :test

    @gda.gem 'a', :group => :test

    assert_empty @set.dependencies

    assert_empty @gda.requires['a']
  end

  def test_gem_requirement
    @gda.gem 'a', '~> 1.0'

    assert_equal [dep('a', '~> 1.0')], @set.dependencies

    expected = { 'a' => Gem::Requirement.create(['~> 1.0']) }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_requirements
    @gda.gem 'b', '~> 1.0', '>= 1.0.2'

    assert_equal [dep('b', '~> 1.0', '>= 1.0.2')], @set.dependencies

    expected = { 'b' => Gem::Requirement.create(['~> 1.0', '>= 1.0.2']) }

    assert_equal expected, @gda.dependencies
  end

  def test_gem_requirements_options
    @gda.gem 'c', :git => 'https://example/c.git'

    assert_equal [dep('c')], @set.dependencies
  end

  def test_gem_source_mismatch
    name, _, directory = vendor_gem

    gda = @GDA.new @set, nil
    gda.gem name

    e = assert_raises ArgumentError do
      gda.gem name, :path => directory
    end

    assert_equal "duplicate source path: #{directory} for gem #{name}",
                 e.message

    gda = @GDA.new @set, nil
    gda.instance_variable_set :@vendor_set, @vendor_set
    gda.gem name, :path => directory

    e = assert_raises ArgumentError do
      gda.gem name
    end

    assert_equal "duplicate source (default) for gem #{name}",
                 e.message
  end

  def test_gem_deps_file
    assert_equal 'gem.deps.rb', @gda.gem_deps_file

    gda = @GDA.new @set, 'foo/Gemfile'

    assert_equal 'Gemfile', gda.gem_deps_file
  end

  def test_gem_group_method
    groups = []

    @gda.group :a do
      groups = @gda.send :gem_group, 'a', :group => :b, :groups => [:c, :d]
    end

    assert_equal [:a, :b, :c, :d], groups.sort_by { |group| group.to_s }
  end

  def test_gemspec
    save_gemspec 'a', 1 do |s|
      s.add_dependency 'b', 2
      s.add_development_dependency 'c', 3
    end

    @gda.gemspec

    assert_equal [dep('a', '= 1'), dep('b', '= 2'), dep('c', '=3')],
                 @set.dependencies

    assert_equal %w[a], @gda.requires['a']

    expected = {
      'a' => Gem::Requirement.create('!'),
      'b' => req('= 2'),
      'c' => req('= 3'),
    }

    assert_equal expected, @gda.dependencies
  end

  def test_gemspec_bad
    FileUtils.touch 'a.gemspec'

    e = assert_raises ArgumentError do
      capture_io do
        @gda.gemspec
      end
    end

    assert_equal 'invalid gemspec ./a.gemspec', e.message
  end

  def test_gemspec_development_group
    save_gemspec 'a', 1 do |s|
      s.add_dependency 'b', 2
      s.add_development_dependency 'c', 3
    end

    @gda.without_groups << :other

    @gda.gemspec :development_group => :other

    assert_equal [dep('a', '= 1'), dep('b', '= 2')], @set.dependencies

    assert_equal %w[a], @gda.requires['a']
  end

  def test_gemspec_multiple
    save_gemspec 'a', 1 do |s|
      s.add_dependency 'b', 2
    end

    save_gemspec 'b', 2 do |s|
      s.add_dependency 'c', 3
    end

    e = assert_raises ArgumentError do
      @gda.gemspec
    end

    assert_equal "found multiple gemspecs at #{@tempdir}, use the name: option to specify the one you want", e.message
  end

  def test_gemspec_name
    save_gemspec 'a', 1 do |s|
      s.add_dependency 'b', 2
    end

    save_gemspec 'b', 2 do |s|
      s.add_dependency 'c', 3
    end

    @gda.gemspec :name => 'b'

    assert_equal [dep('b', '= 2'), dep('c', '= 3')], @set.dependencies
  end

  def test_gemspec_named
    save_gemspec 'a', 1 do |s|
      s.add_dependency 'b', 2
    end

    @gda.gemspec

    assert_equal [dep('a', '= 1'), dep('b', '= 2')], @set.dependencies
  end

  def test_gemspec_none
    e = assert_raises ArgumentError do
      @gda.gemspec
    end

    assert_equal "no gemspecs found at #{@tempdir}", e.message
  end

  def test_gemspec_path
    FileUtils.mkdir 'other'

    save_gemspec 'a', 1, 'other' do |s|
      s.add_dependency 'b', 2
    end

    @gda.gemspec :path => 'other'

    assert_equal [dep('a', '= 1'), dep('b', '= 2')], @set.dependencies
  end

  def test_git
    @gda.git 'git://example/repo.git' do
      @gda.gem 'a'
      @gda.gem 'b'
    end

    assert_equal [dep('a'), dep('b')], @set.dependencies

    assert_equal %w[git://example/repo.git master], @git_set.repositories['a']
    assert_equal %w[git://example/repo.git master], @git_set.repositories['b']
  end

  def test_git_source
    @gda.git_source :example do |repo_name|
      "git://example/#{repo_name}.git"
    end

    @gda.gem 'a', :example => 'repo'

    assert_equal %w[git://example/repo.git master], @git_set.repositories['a']
  end

  def test_group
    @gda.group :test do
      @gda.gem 'a'
    end

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

  def test_load
    tf = Tempfile.open 'gem.deps.rb' do |io|
      io.write <<-GEM_DEPS
gem 'a'

group :test do
  gem 'b'
end
      GEM_DEPS
      io.flush

      gda = @GDA.new @set, io.path

      assert_equal gda, gda.load

      assert_equal [dep('a'), dep('b')], @set.dependencies
      io
    end
    tf.close!
  end

  def test_pin_gem_source
    gda = @GDA.new @set, nil

    gda.send :pin_gem_source, 'a'
    gda.send :pin_gem_source, 'a'

    e = assert_raises ArgumentError do
      gda.send :pin_gem_source, 'a', :path, 'vendor/a'
    end

    assert_equal "duplicate source path: vendor/a for gem a",
                 e.message

    e = assert_raises ArgumentError do
      gda.send :pin_gem_source, 'a', :git, 'git://example/repo.git'
    end

    assert_equal "duplicate source git: git://example/repo.git for gem a",
                 e.message
  end

  def test_platform_mswin
    if win_platform?
      util_set_arch 'x86-mswin32-60' do
        @gda.platform :mswin do
          @gda.gem 'a'
        end

        assert_equal [dep('a')], @set.dependencies
        refute_empty @set.dependencies
      end
    else
      util_set_arch 'i686-darwin8.10.1' do
        @gda.platform :mswin do
          @gda.gem 'a'
        end

        assert_empty @set.dependencies
      end
    end
  end

  def test_platform_multiple
    win_platform, Gem.win_platform = Gem.win_platform?, false

    gda = @GDA.new @set, nil

    with_engine_version 'ruby', '1.8.7' do
      gda.platform :mri_19, :mri_20 do
        gda.gem 'a'
      end
    end

    assert_empty @set.dependencies

    gda = @GDA.new @set, nil

    with_engine_version 'ruby', '2.0.0' do
      gda.platform :mri_19, :mri_20 do
        gda.gem 'a'
      end
    end

    refute_empty @set.dependencies
  ensure
    Gem.win_platform = win_platform
  end

  def test_platform_ruby
    win_platform, Gem.win_platform = Gem.win_platform?, false

    @gda.platform :ruby do
      @gda.gem 'a'
    end

    assert_equal [dep('a')], @set.dependencies
  ensure
    Gem.win_platform = win_platform
  end

  def test_platforms
    unless win_platform?
      util_set_arch 'i686-darwin8.10.1' do
        @gda.platforms :ruby do
          @gda.gem 'a'
        end

        assert_equal [dep('a')], @set.dependencies

        @gda.platforms :mswin do
          @gda.gem 'b'
        end

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

  def test_ruby
    assert @gda.ruby RUBY_VERSION
  end

  def test_ruby_engine
    with_engine_version 'jruby', '1.7.6' do
      assert @gda.ruby RUBY_VERSION,
               :engine => 'jruby', :engine_version => '1.7.6'

    end

    with_engine_version 'truffleruby', '1.0.0-rc11' do
      assert @gda.ruby RUBY_VERSION,
               :engine => 'truffleruby', :engine_version => '1.0.0-rc11'

    end
  end

  def test_ruby_engine_mismatch_engine
    with_engine_version 'ruby', '2.0.0' do
      e = assert_raises Gem::RubyVersionMismatch do
        @gda.ruby RUBY_VERSION, :engine => 'jruby', :engine_version => '1.7.4'
      end

      assert_equal 'Your Ruby engine is ruby, but your gem.deps.rb requires jruby',
                   e.message
    end
  end

  def test_ruby_engine_mismatch_version
    with_engine_version 'jruby', '1.7.6' do
      e = assert_raises Gem::RubyVersionMismatch do
        @gda.ruby RUBY_VERSION, :engine => 'jruby', :engine_version => '1.7.4'
      end

      assert_equal 'Your Ruby engine version is jruby 1.7.6, but your gem.deps.rb requires jruby 1.7.4',
                   e.message
    end
  end

  def test_ruby_engine_no_engine_version
    e = assert_raises ArgumentError do
      @gda.ruby RUBY_VERSION, :engine => 'jruby'
    end

    assert_equal 'You must specify engine_version along with the Ruby engine',
                 e.message
  end

  def test_ruby_mismatch
    e = assert_raises Gem::RubyVersionMismatch do
      @gda.ruby '1.8.0'
    end

    assert_equal "Your Ruby version is #{RUBY_VERSION}, but your gem.deps.rb requires 1.8.0", e.message
  end

  def test_ruby_mismatch_installing
    @gda.installing = true

    assert @gda.ruby '1.8.0'
  end

  def test_source
    sources = Gem.sources

    @gda.source 'http://first.example'

    assert_equal %w[http://first.example], Gem.sources

    assert_same sources, Gem.sources

    @gda.source 'http://second.example'

    assert_equal %w[http://first.example http://second.example], Gem.sources
  end

  def test_with_engine_version
    version = RUBY_VERSION
    engine  = Gem.ruby_engine

    engine_version_const = "#{Gem.ruby_engine.upcase}_VERSION"
    engine_version       = Object.const_get engine_version_const

    with_engine_version 'other', '1.2.3' do
      assert_equal 'other', Gem.ruby_engine
      assert_equal '1.2.3', OTHER_VERSION

      assert_equal version, RUBY_VERSION if engine
    end

    assert_equal version, RUBY_VERSION
    assert_equal engine,  Gem.ruby_engine

    assert_equal engine_version, Object.const_get(engine_version_const) if
      engine
  end

end unless Gem.java_platform?