1# coding: US-ASCII
2require 'rubygems/test_case'
3require 'rubygems'
4require 'rubygems/installer'
5require 'pathname'
6require 'tmpdir'
7
8# TODO: push this up to test_case.rb once battle tested
9$SAFE=1
10$LOAD_PATH.map! do |path|
11  path.dup.untaint
12end
13
14class TestGem < Gem::TestCase
15
16  def setup
17    super
18
19    ENV.delete 'RUBYGEMS_GEMDEPS'
20    @additional = %w[a b].map { |d| File.join @tempdir, d }
21
22    util_remove_interrupt_command
23  end
24
25  def assert_activate expected, *specs
26    specs.each do |spec|
27      case spec
28      when String then
29        Gem::Specification.find_by_name(spec).activate
30      when Gem::Specification then
31        spec.activate
32      else
33        flunk spec.inspect
34      end
35    end
36
37    loaded = Gem.loaded_specs.values.map(&:full_name)
38
39    assert_equal expected.sort, loaded.sort if expected
40  end
41
42  def test_self_activate
43    foo = util_spec 'foo', '1'
44
45    assert_activate %w[foo-1], foo
46  end
47
48  def loaded_spec_names
49    Gem.loaded_specs.values.map(&:full_name).sort
50  end
51
52  def unresolved_names
53    Gem::Specification.unresolved_deps.values.map(&:to_s).sort
54  end
55
56  # TODO: move these to specification
57  def test_self_activate_via_require
58    a1 = new_spec "a", "1", "b" => "= 1"
59    b1 = new_spec "b", "1", nil, "lib/b/c.rb"
60    b2 = new_spec "b", "2", nil, "lib/b/c.rb"
61
62    install_specs a1, b1, b2
63
64    a1.activate
65    save_loaded_features do
66      require "b/c"
67    end
68
69    assert_equal %w(a-1 b-1), loaded_spec_names
70  end
71
72  # TODO: move these to specification
73  def test_self_activate_deep_unambiguous
74    a1 = new_spec "a", "1", "b" => "= 1"
75    b1 = new_spec "b", "1", "c" => "= 1"
76    b2 = new_spec "b", "2", "c" => "= 2"
77    c1 = new_spec "c", "1"
78    c2 = new_spec "c", "2"
79
80    install_specs a1, b1, b2, c1, c2
81
82    a1.activate
83    assert_equal %w(a-1 b-1 c-1), loaded_spec_names
84  end
85
86  def save_loaded_features
87    old_loaded_features = $LOADED_FEATURES.dup
88    yield
89  ensure
90    $LOADED_FEATURES.replace old_loaded_features
91  end
92
93  # TODO: move these to specification
94  def test_self_activate_ambiguous_direct
95    save_loaded_features do
96      a1 = new_spec "a", "1", "b" => "> 0"
97      b1 = new_spec("b", "1", { "c" => ">= 1" }, "lib/d.rb")
98      b2 = new_spec("b", "2", { "c" => ">= 2" }, "lib/d.rb")
99      c1 = new_spec "c", "1"
100      c2 = new_spec "c", "2"
101
102      Gem::Specification.reset
103      install_specs a1, b1, b2, c1, c2
104
105      a1.activate
106      assert_equal %w(a-1), loaded_spec_names
107      assert_equal ["b (> 0)"], unresolved_names
108
109      require "d"
110
111      assert_equal %w(a-1 b-2 c-2), loaded_spec_names
112      assert_equal [], unresolved_names
113    end
114  end
115
116  # TODO: move these to specification
117  def test_self_activate_ambiguous_indirect
118    save_loaded_features do
119      a1 = new_spec "a", "1", "b" => "> 0"
120      b1 = new_spec "b", "1", "c" => ">= 1"
121      b2 = new_spec "b", "2", "c" => ">= 2"
122      c1 = new_spec "c", "1", nil, "lib/d.rb"
123      c2 = new_spec "c", "2", nil, "lib/d.rb"
124
125      install_specs a1, b1, b2, c1, c2
126
127      a1.activate
128      assert_equal %w(a-1), loaded_spec_names
129      assert_equal ["b (> 0)"], unresolved_names
130
131      require "d"
132
133      assert_equal %w(a-1 b-2 c-2), loaded_spec_names
134      assert_equal [], unresolved_names
135    end
136  end
137
138  def test_self_finish_resolve
139    save_loaded_features do
140      a1 = new_spec "a", "1", "b" => "> 0"
141      b1 = new_spec "b", "1", "c" => ">= 1"
142      b2 = new_spec "b", "2", "c" => ">= 2"
143      c1 = new_spec "c", "1"
144      c2 = new_spec "c", "2"
145
146      install_specs a1, b1, b2, c1, c2
147
148      a1.activate
149
150      assert_equal %w(a-1), loaded_spec_names
151      assert_equal ["b (> 0)"], unresolved_names
152
153      Gem.finish_resolve
154
155      assert_equal %w(a-1 b-2 c-2), loaded_spec_names
156      assert_equal [], unresolved_names
157    end
158  end
159
160  def test_self_activate_via_require_wtf
161    save_loaded_features do
162      a1 = new_spec "a", "1", "b" => "> 0", "d" => "> 0"    # this
163      b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/b.rb"
164      b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/b.rb" # this
165      c1 = new_spec "c", "1"
166      c2 = new_spec "c", "2"                                # this
167      d1 = new_spec "d", "1", { "c" => "< 2" },  "lib/d.rb"
168      d2 = new_spec "d", "2", { "c" => "< 2" },  "lib/d.rb" # this
169
170      install_specs a1, b1, b2, c1, c2, d1, d2
171
172      a1.activate
173
174      assert_equal %w(a-1), loaded_spec_names
175      assert_equal ["b (> 0)", "d (> 0)"], unresolved_names
176
177      require "b"
178
179      e = assert_raises Gem::LoadError do
180        require "d"
181      end
182
183      assert_equal "unable to find a version of 'd' to activate", e.message
184
185      assert_equal %w(a-1 b-2 c-2), loaded_spec_names
186      assert_equal ["d (> 0)"], unresolved_names
187    end
188  end
189
190  def test_self_finish_resolve_wtf
191    save_loaded_features do
192      a1 = new_spec "a", "1", "b" => "> 0", "d" => "> 0"    # this
193      b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/b.rb" # this
194      b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/b.rb"
195      c1 = new_spec "c", "1"                                # this
196      c2 = new_spec "c", "2"
197      d1 = new_spec "d", "1", { "c" => "< 2" },  "lib/d.rb"
198      d2 = new_spec "d", "2", { "c" => "< 2" },  "lib/d.rb" # this
199
200      install_specs a1, b1, b2, c1, c2, d1, d2
201
202      a1.activate
203
204      assert_equal %w(a-1), loaded_spec_names
205      assert_equal ["b (> 0)", "d (> 0)"], unresolved_names
206
207      Gem.finish_resolve
208
209      assert_equal %w(a-1 b-1 c-1 d-2), loaded_spec_names
210      assert_equal [], unresolved_names
211    end
212  end
213
214  # TODO: move these to specification
215  def test_self_activate_ambiguous_unrelated
216    save_loaded_features do
217      a1 = new_spec "a", "1", "b" => "> 0"
218      b1 = new_spec "b", "1", "c" => ">= 1"
219      b2 = new_spec "b", "2", "c" => ">= 2"
220      c1 = new_spec "c", "1"
221      c2 = new_spec "c", "2"
222      d1 = new_spec "d", "1", nil, "lib/d.rb"
223
224      install_specs a1, b1, b2, c1, c2, d1
225
226      a1.activate
227      assert_equal %w(a-1), loaded_spec_names
228      assert_equal ["b (> 0)"], unresolved_names
229
230      require "d"
231
232      assert_equal %w(a-1 d-1), loaded_spec_names
233      assert_equal ["b (> 0)"], unresolved_names
234    end
235  end
236
237  # TODO: move these to specification
238  def test_self_activate_ambiguous_indirect_conflict
239    save_loaded_features do
240      a1 = new_spec "a", "1", "b" => "> 0"
241      a2 = new_spec "a", "2", "b" => "> 0"
242      b1 = new_spec "b", "1", "c" => ">= 1"
243      b2 = new_spec "b", "2", "c" => ">= 2"
244      c1 = new_spec "c", "1", nil, "lib/d.rb"
245      c2 = new_spec("c", "2", { "a" => "1" }, "lib/d.rb") # conflicts with a-2
246
247      install_specs a1, a2, b1, b2, c1, c2
248
249      a2.activate
250      assert_equal %w(a-2), loaded_spec_names
251      assert_equal ["b (> 0)"], unresolved_names
252
253      require "d"
254
255      assert_equal %w(a-2 b-1 c-1), loaded_spec_names
256      assert_equal [], unresolved_names
257    end
258  end
259
260  # TODO: move these to specification
261  def test_require_already_activated
262    save_loaded_features do
263      a1 = new_spec "a", "1", nil, "lib/d.rb"
264
265      install_specs a1 # , a2, b1, b2, c1, c2
266
267      a1.activate
268      assert_equal %w(a-1), loaded_spec_names
269      assert_equal [], unresolved_names
270
271      assert require "d"
272
273      assert_equal %w(a-1), loaded_spec_names
274      assert_equal [], unresolved_names
275    end
276  end
277
278  # TODO: move these to specification
279  def test_require_already_activated_indirect_conflict
280    save_loaded_features do
281      a1 = new_spec "a", "1", "b" => "> 0"
282      a2 = new_spec "a", "2", "b" => "> 0"
283      b1 = new_spec "b", "1", "c" => ">= 1"
284      b2 = new_spec "b", "2", "c" => ">= 2"
285      c1 = new_spec "c", "1", nil, "lib/d.rb"
286      c2 = new_spec("c", "2", { "a" => "1" }, "lib/d.rb") # conflicts with a-2
287
288      install_specs a1, a2, b1, b2, c1, c2
289
290      a1.activate
291      c1.activate
292      assert_equal %w(a-1 c-1), loaded_spec_names
293      assert_equal ["b (> 0)"], unresolved_names
294
295      assert require "d"
296
297      assert_equal %w(a-1 c-1), loaded_spec_names
298      assert_equal ["b (> 0)"], unresolved_names
299    end
300  end
301
302  def test_require_missing
303    save_loaded_features do
304      assert_raises ::LoadError do
305        require "q"
306      end
307    end
308  end
309
310  def test_require_does_not_glob
311    save_loaded_features do
312      a1 = new_spec "a", "1", nil, "lib/a1.rb"
313
314      install_specs a1
315
316      assert_raises ::LoadError do
317        require "a*"
318      end
319
320      assert_equal [], loaded_spec_names
321    end
322  end
323
324  # TODO: move these to specification
325  def test_self_activate_loaded
326    foo = util_spec 'foo', '1'
327
328    assert foo.activate
329    refute foo.activate
330  end
331
332  ##
333  # [A] depends on
334  #     [B] >= 1.0 (satisfied by 2.0)
335  # [C] depends on nothing
336
337  def test_self_activate_unrelated
338    a = util_spec 'a', '1.0', 'b' => '>= 1.0'
339        util_spec 'b', '1.0'
340    c = util_spec 'c', '1.0'
341
342    assert_activate %w[b-1.0 c-1.0 a-1.0], a, c, "b"
343  end
344
345  ##
346  # [A] depends on
347  #     [B] >= 1.0 (satisfied by 2.0)
348  #     [C]  = 1.0 depends on
349  #         [B] ~> 1.0
350  #
351  # and should resolve using b-1.0
352  # TODO: move these to specification
353
354  def test_self_activate_over
355    a = util_spec 'a', '1.0', 'b' => '>= 1.0', 'c' => '= 1.0'
356    util_spec 'b', '1.0'
357    util_spec 'b', '1.1'
358    util_spec 'b', '2.0'
359    util_spec 'c', '1.0', 'b' => '~> 1.0'
360
361    a.activate
362
363    assert_equal %w[a-1.0 c-1.0], loaded_spec_names
364    assert_equal ["b (>= 1.0, ~> 1.0)"], unresolved_names
365  end
366
367  ##
368  # [A] depends on
369  #     [B] ~> 1.0 (satisfied by 1.1)
370  #     [C]  = 1.0 depends on
371  #         [B] = 1.0
372  #
373  # and should resolve using b-1.0
374  #
375  # TODO: this is not under, but over... under would require depth
376  # first resolve through a dependency that is later pruned.
377
378  def test_self_activate_under
379    a,   _ = util_spec 'a', '1.0', 'b' => '~> 1.0', 'c' => '= 1.0'
380             util_spec 'b', '1.0'
381             util_spec 'b', '1.1'
382    c,   _ = util_spec 'c', '1.0', 'b' => '= 1.0'
383
384    assert_activate %w[b-1.0 c-1.0 a-1.0], a, c, "b"
385  end
386
387  ##
388  # [A1] depends on
389  #    [B] > 0 (satisfied by 2.0)
390  # [B1] depends on
391  #    [C] > 0 (satisfied by 1.0)
392  # [B2] depends on nothing!
393  # [C1] depends on nothing
394
395  def test_self_activate_dropped
396    a1, = util_spec 'a', '1', 'b' => nil
397          util_spec 'b', '1', 'c' => nil
398          util_spec 'b', '2'
399          util_spec 'c', '1'
400
401    assert_activate %w[b-2 a-1], a1, "b"
402  end
403
404  ##
405  # [A] depends on
406  #     [B] >= 1.0 (satisfied by 1.1) depends on
407  #         [Z]
408  #     [C] >= 1.0 depends on
409  #         [B] = 1.0
410  #
411  # and should backtrack to resolve using b-1.0, pruning Z from the
412  # resolve.
413
414  def test_self_activate_raggi_the_edgecase_generator
415    a,  _ = util_spec 'a', '1.0', 'b' => '>= 1.0', 'c' => '>= 1.0'
416            util_spec 'b', '1.0'
417            util_spec 'b', '1.1', 'z' => '>= 1.0'
418    c,  _ = util_spec 'c', '1.0', 'b' => '= 1.0'
419
420    assert_activate %w[b-1.0 c-1.0 a-1.0], a, c, "b"
421  end
422
423  def test_self_activate_conflict
424    util_spec 'b', '1.0'
425    util_spec 'b', '2.0'
426
427    gem "b", "= 1.0"
428
429    assert_raises Gem::LoadError do
430      gem "b", "= 2.0"
431    end
432  end
433
434  ##
435  # [A] depends on
436  #     [C]  = 1.0 depends on
437  #         [B] = 2.0
438  #     [B] ~> 1.0 (satisfied by 1.0)
439
440  def test_self_activate_checks_dependencies
441    a, _  = util_spec 'a', '1.0'
442            a.add_dependency 'c', '= 1.0'
443            a.add_dependency 'b', '~> 1.0'
444
445            util_spec 'b', '1.0'
446            util_spec 'b', '2.0'
447    c,  _ = util_spec 'c', '1.0', 'b' => '= 2.0'
448
449    e = assert_raises Gem::LoadError do
450      assert_activate nil, a, c, "b"
451    end
452
453    expected = "can't satisfy 'b (~> 1.0)', already activated 'b-2.0'"
454    assert_equal expected, e.message
455  end
456
457  ##
458  # [A] depends on
459  #     [B] ~> 1.0 (satisfied by 1.0)
460  #     [C]  = 1.0 depends on
461  #         [B] = 2.0
462
463  def test_self_activate_divergent
464    a, _  = util_spec 'a', '1.0', 'b' => '~> 1.0', 'c' => '= 1.0'
465            util_spec 'b', '1.0'
466            util_spec 'b', '2.0'
467    c,  _ = util_spec 'c', '1.0', 'b' => '= 2.0'
468
469    e = assert_raises Gem::LoadError do
470      assert_activate nil, a, c, "b"
471    end
472
473    assert_match(/Unable to activate c-1.0,/, e.message)
474    assert_match(/because b-1.0 conflicts with b .= 2.0/, e.message)
475  end
476
477  ##
478  # DOC
479
480  def test_self_activate_platform_alternate
481    @x1_m = util_spec 'x', '1' do |s|
482      s.platform = Gem::Platform.new %w[cpu my_platform 1]
483    end
484
485    @x1_o = util_spec 'x', '1' do |s|
486      s.platform = Gem::Platform.new %w[cpu other_platform 1]
487    end
488
489    @w1 = util_spec 'w', '1', 'x' => nil
490
491    util_set_arch 'cpu-my_platform1'
492
493    assert_activate %w[x-1-cpu-my_platform-1 w-1], @w1, @x1_m
494  end
495
496  ##
497  # DOC
498
499  def test_self_activate_platform_bump
500    @y1 = util_spec 'y', '1'
501
502    @y1_1_p = util_spec 'y', '1.1' do |s|
503      s.platform = Gem::Platform.new %w[cpu my_platform 1]
504    end
505
506    @z1 = util_spec 'z', '1', 'y' => nil
507
508    assert_activate %w[y-1 z-1], @z1, @y1
509  end
510
511  ##
512  # [C] depends on
513  #     [A] = 1.a
514  #     [B] = 1.0 depends on
515  #         [A] >= 0 (satisfied by 1.a)
516
517  def test_self_activate_prerelease
518    @c1_pre = util_spec 'c', '1.a', "a" => "1.a", "b" => "1"
519    @a1_pre = util_spec 'a', '1.a'
520    @b1     = util_spec 'b', '1' do |s|
521      s.add_dependency 'a'
522      s.add_development_dependency 'aa'
523    end
524
525    assert_activate %w[a-1.a b-1 c-1.a], @c1_pre, @a1_pre, @b1
526  end
527
528  ##
529  # DOC
530
531  def test_self_activate_old_required
532    e1, = util_spec 'e', '1', 'd' => '= 1'
533    @d1 = util_spec 'd', '1'
534    @d2 = util_spec 'd', '2'
535
536    assert_activate %w[d-1 e-1], e1, "d"
537  end
538
539  def test_self_bin_path_no_exec_name
540    e = assert_raises ArgumentError do
541      Gem.bin_path 'a'
542    end
543
544    assert_equal 'you must supply exec_name', e.message
545  end
546
547  def test_self_bin_path_bin_name
548    util_exec_gem
549    assert_equal @abin_path, Gem.bin_path('a', 'abin')
550  end
551
552  def test_self_bin_path_bin_name_version
553    util_exec_gem
554    assert_equal @abin_path, Gem.bin_path('a', 'abin', '4')
555  end
556
557  def test_self_bin_path_nonexistent_binfile
558    quick_spec 'a', '2' do |s|
559      s.executables = ['exec']
560    end
561    assert_raises(Gem::GemNotFoundException) do
562      Gem.bin_path('a', 'other', '2')
563    end
564  end
565
566  def test_self_bin_path_no_bin_file
567    quick_spec 'a', '1'
568    assert_raises(ArgumentError) do
569      Gem.bin_path('a', nil, '1')
570    end
571  end
572
573  def test_self_bin_path_not_found
574    assert_raises(Gem::GemNotFoundException) do
575      Gem.bin_path('non-existent', 'blah')
576    end
577  end
578
579  def test_self_bin_path_bin_file_gone_in_latest
580    util_exec_gem
581    quick_spec 'a', '10' do |s|
582      s.executables = []
583    end
584    # Should not find a-10's non-abin (bug)
585    assert_equal @abin_path, Gem.bin_path('a', 'abin')
586  end
587
588  def test_self_bindir
589    assert_equal File.join(@gemhome, 'bin'), Gem.bindir
590    assert_equal File.join(@gemhome, 'bin'), Gem.bindir(Gem.dir)
591    assert_equal File.join(@gemhome, 'bin'), Gem.bindir(Pathname.new(Gem.dir))
592  end
593
594  def test_self_bindir_default_dir
595    default = Gem.default_dir
596
597    assert_equal Gem.default_bindir, Gem.bindir(default)
598  end
599
600  def test_self_clear_paths
601    assert_match(/gemhome$/, Gem.dir)
602    assert_match(/gemhome$/, Gem.path.first)
603
604    Gem.clear_paths
605
606    assert_nil Gem::Specification.send(:class_variable_get, :@@all)
607  end
608
609  def test_self_configuration
610    expected = Gem::ConfigFile.new []
611    Gem.configuration = nil
612
613    assert_equal expected, Gem.configuration
614  end
615
616  def test_self_datadir
617    foo = nil
618
619    Dir.chdir @tempdir do
620      FileUtils.mkdir_p 'data'
621      File.open File.join('data', 'foo.txt'), 'w' do |fp|
622        fp.puts 'blah'
623      end
624
625      foo = quick_spec 'foo' do |s| s.files = %w[data/foo.txt] end
626      install_gem foo
627    end
628
629    gem 'foo'
630
631    expected = File.join @gemhome, 'gems', foo.full_name, 'data', 'foo'
632
633    assert_equal expected, Gem.datadir('foo')
634  end
635
636  def test_self_datadir_nonexistent_package
637    assert_nil Gem.datadir('xyzzy')
638  end
639
640  def test_self_default_exec_format
641    orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
642    Gem::ConfigMap[:ruby_install_name] = 'ruby'
643
644    assert_equal '%s', Gem.default_exec_format
645  ensure
646    Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
647  end
648
649  def test_self_default_exec_format_18
650    orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
651    Gem::ConfigMap[:ruby_install_name] = 'ruby18'
652
653    assert_equal '%s18', Gem.default_exec_format
654  ensure
655    Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
656  end
657
658  def test_self_default_exec_format_jruby
659    orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
660    Gem::ConfigMap[:ruby_install_name] = 'jruby'
661
662    assert_equal 'j%s', Gem.default_exec_format
663  ensure
664    Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
665  end
666
667  def test_self_default_sources
668    assert_equal %w[https://rubygems.org/], Gem.default_sources
669  end
670
671  def test_self_detect_gemdeps
672    rubygems_gemdeps, ENV['RUBYGEMS_GEMDEPS'] = ENV['RUBYGEMS_GEMDEPS'], '-'
673
674    FileUtils.mkdir_p 'detect/a/b'
675    FileUtils.mkdir_p 'detect/a/Isolate'
676
677    FileUtils.touch 'detect/Isolate'
678
679    begin
680      Dir.chdir 'detect/a/b'
681
682      assert_empty Gem.detect_gemdeps
683    ensure
684      Dir.chdir @tempdir
685    end
686  ensure
687    ENV['RUBYGEMS_GEMDEPS'] = rubygems_gemdeps
688  end
689
690  def test_self_dir
691    assert_equal @gemhome, Gem.dir
692  end
693
694  def test_self_ensure_gem_directories
695    FileUtils.rm_r @gemhome
696    Gem.use_paths @gemhome
697
698    Gem.ensure_gem_subdirectories @gemhome
699
700    assert File.directory? File.join(@gemhome, "cache")
701  end
702
703  def test_self_ensure_gem_directories_permissions
704    FileUtils.rm_r @gemhome
705    Gem.use_paths @gemhome
706
707    Gem.ensure_gem_subdirectories @gemhome, 0750
708
709    assert File.directory? File.join(@gemhome, "cache")
710
711    assert_equal 0750, File::Stat.new(@gemhome).mode & 0777
712    assert_equal 0750, File::Stat.new(File.join(@gemhome, "cache")).mode & 0777
713  end unless win_platform?
714
715  def test_self_ensure_gem_directories_safe_permissions
716    FileUtils.rm_r @gemhome
717    Gem.use_paths @gemhome
718
719    old_umask = File.umask
720    File.umask 0
721    Gem.ensure_gem_subdirectories @gemhome
722
723    assert_equal 0, File::Stat.new(@gemhome).mode & 002
724    assert_equal 0, File::Stat.new(File.join(@gemhome, "cache")).mode & 002
725  ensure
726    File.umask old_umask
727  end unless win_platform?
728
729  def test_self_ensure_gem_directories_missing_parents
730    gemdir = File.join @tempdir, 'a/b/c/gemdir'
731    FileUtils.rm_rf File.join(@tempdir, 'a') rescue nil
732    refute File.exist?(File.join(@tempdir, 'a')),
733           "manually remove #{File.join @tempdir, 'a'}, tests are broken"
734    Gem.use_paths gemdir
735
736    Gem.ensure_gem_subdirectories gemdir
737
738    assert File.directory?(util_cache_dir)
739  end
740
741  unless win_platform? then # only for FS that support write protection
742    def test_self_ensure_gem_directories_write_protected
743      gemdir = File.join @tempdir, "egd"
744      FileUtils.rm_r gemdir rescue nil
745      refute File.exist?(gemdir), "manually remove #{gemdir}, tests are broken"
746      FileUtils.mkdir_p gemdir
747      FileUtils.chmod 0400, gemdir
748      Gem.use_paths gemdir
749
750      Gem.ensure_gem_subdirectories gemdir
751
752      refute File.exist?(util_cache_dir)
753    ensure
754      FileUtils.chmod 0600, gemdir
755    end
756
757    def test_self_ensure_gem_directories_write_protected_parents
758      parent = File.join(@tempdir, "egd")
759      gemdir = "#{parent}/a/b/c"
760
761      FileUtils.rm_r parent rescue nil
762      refute File.exist?(parent), "manually remove #{parent}, tests are broken"
763      FileUtils.mkdir_p parent
764      FileUtils.chmod 0400, parent
765      Gem.use_paths(gemdir)
766
767      Gem.ensure_gem_subdirectories gemdir
768
769      refute File.exist? File.join(gemdir, "gems")
770    ensure
771      FileUtils.chmod 0600, parent
772    end
773  end
774
775  def test_self_find_files
776    cwd = File.expand_path("test/rubygems", @@project_dir)
777    $LOAD_PATH.unshift cwd
778
779    discover_path = File.join 'lib', 'sff', 'discover.rb'
780
781    foo1, foo2 = %w(1 2).map { |version|
782      spec = quick_gem 'sff', version do |s|
783        s.files << discover_path
784      end
785
786      write_file(File.join 'gems', spec.full_name, discover_path) do |fp|
787        fp.puts "# #{spec.full_name}"
788      end
789
790      spec
791    }
792
793    # HACK should be Gem.refresh
794    Gem.searcher = nil
795    Gem::Specification.reset
796
797    expected = [
798      File.expand_path('test/rubygems/sff/discover.rb', @@project_dir),
799      File.join(foo2.full_gem_path, discover_path),
800      File.join(foo1.full_gem_path, discover_path),
801    ]
802
803    assert_equal expected, Gem.find_files('sff/discover')
804    assert_equal expected, Gem.find_files('sff/**.rb'), '[ruby-core:31730]'
805  ensure
806    assert_equal cwd, $LOAD_PATH.shift
807  end
808
809  def test_self_latest_spec_for
810    a1  = quick_spec 'a', 1
811    a2  = quick_spec 'a', 2
812    a3a = quick_spec 'a', '3.a'
813
814    util_setup_fake_fetcher
815    util_setup_spec_fetcher a1, a2, a3a
816
817    spec = Gem.latest_spec_for 'a'
818
819    assert_equal a2, spec
820  end
821
822  def test_self_latest_rubygems_version
823    r1 = quick_spec 'rubygems-update', '1.8.23'
824    r2 = quick_spec 'rubygems-update', '1.8.24'
825    r3 = quick_spec 'rubygems-update', '2.0.0.preview3'
826
827    util_setup_fake_fetcher
828    util_setup_spec_fetcher r1, r2, r3
829
830    version = Gem.latest_rubygems_version
831
832    assert_equal Gem::Version.new('1.8.24'), version
833  end
834
835  def test_self_latest_version_for
836    a1  = quick_spec 'a', 1
837    a2  = quick_spec 'a', 2
838    a3a = quick_spec 'a', '3.a'
839
840    util_setup_fake_fetcher
841    util_setup_spec_fetcher a1, a2, a3a
842
843    version = Gem.latest_version_for 'a'
844
845    assert_equal Gem::Version.new(2), version
846  end
847
848  def test_self_loaded_specs
849    foo = quick_spec 'foo'
850    install_gem foo
851
852    foo.activate
853
854    assert_equal true, Gem.loaded_specs.keys.include?('foo')
855  end
856
857  def util_path
858    ENV.delete "GEM_HOME"
859    ENV.delete "GEM_PATH"
860  end
861
862  def test_self_path
863    assert_equal [Gem.dir], Gem.path
864  end
865
866  def test_self_path_default
867    util_path
868
869    if defined?(APPLE_GEM_HOME)
870      orig_APPLE_GEM_HOME = APPLE_GEM_HOME
871      Object.send :remove_const, :APPLE_GEM_HOME
872    end
873
874    Gem.instance_variable_set :@paths, nil
875
876    assert_equal [Gem.default_path, Gem.dir].flatten.uniq, Gem.path
877  ensure
878    Object.const_set :APPLE_GEM_HOME, orig_APPLE_GEM_HOME if orig_APPLE_GEM_HOME
879  end
880
881  unless win_platform?
882    def test_self_path_APPLE_GEM_HOME
883      util_path
884
885      Gem.clear_paths
886      apple_gem_home = File.join @tempdir, 'apple_gem_home'
887
888      old, $-w = $-w, nil
889      Object.const_set :APPLE_GEM_HOME, apple_gem_home
890      $-w = old
891
892      assert_includes Gem.path, apple_gem_home
893    ensure
894      Object.send :remove_const, :APPLE_GEM_HOME
895    end
896
897    def test_self_path_APPLE_GEM_HOME_GEM_PATH
898      Gem.clear_paths
899      ENV['GEM_PATH'] = @gemhome
900      apple_gem_home = File.join @tempdir, 'apple_gem_home'
901      Gem.const_set :APPLE_GEM_HOME, apple_gem_home
902
903      refute Gem.path.include?(apple_gem_home)
904    ensure
905      Gem.send :remove_const, :APPLE_GEM_HOME
906    end
907  end
908
909  def test_self_path_ENV_PATH
910    path_count = Gem.path.size
911    Gem.clear_paths
912
913    ENV['GEM_PATH'] = @additional.join(File::PATH_SEPARATOR)
914
915    assert_equal @additional, Gem.path[0,2]
916
917    assert_equal path_count + @additional.size, Gem.path.size,
918                 "extra path components: #{Gem.path[2..-1].inspect}"
919    assert_equal Gem.dir, Gem.path.last
920  end
921
922  def test_self_path_duplicate
923    Gem.clear_paths
924    util_ensure_gem_dirs
925    dirs = @additional + [@gemhome] + [File.join(@tempdir, 'a')]
926
927    ENV['GEM_HOME'] = @gemhome
928    ENV['GEM_PATH'] = dirs.join File::PATH_SEPARATOR
929
930    assert_equal @gemhome, Gem.dir
931
932    paths = [Gem.dir]
933    assert_equal @additional + paths, Gem.path
934  end
935
936  def test_self_path_overlap
937    Gem.clear_paths
938
939    util_ensure_gem_dirs
940    ENV['GEM_HOME'] = @gemhome
941    ENV['GEM_PATH'] = @additional.join(File::PATH_SEPARATOR)
942
943    assert_equal @gemhome, Gem.dir
944
945    paths = [Gem.dir]
946    assert_equal @additional + paths, Gem.path
947  end
948
949  def test_self_platforms
950    assert_equal [Gem::Platform::RUBY, Gem::Platform.local], Gem.platforms
951  end
952
953  def test_self_prefix
954    assert_equal @@project_dir, Gem.prefix
955  end
956
957  def test_self_prefix_libdir
958    orig_libdir = Gem::ConfigMap[:libdir]
959    Gem::ConfigMap[:libdir] = @@project_dir
960
961    assert_nil Gem.prefix
962  ensure
963    Gem::ConfigMap[:libdir] = orig_libdir
964  end
965
966  def test_self_prefix_sitelibdir
967    orig_sitelibdir = Gem::ConfigMap[:sitelibdir]
968    Gem::ConfigMap[:sitelibdir] = @@project_dir
969
970    assert_nil Gem.prefix
971  ensure
972    Gem::ConfigMap[:sitelibdir] = orig_sitelibdir
973  end
974
975  def test_self_refresh
976    util_make_gems
977
978    a1_spec = @a1.spec_file
979    moved_path = File.join @tempdir, File.basename(a1_spec)
980
981    FileUtils.mv a1_spec, moved_path
982
983    Gem.refresh
984
985    refute_includes Gem::Specification.all_names, @a1.full_name
986
987    FileUtils.mv moved_path, a1_spec
988
989    Gem.refresh
990
991    assert_includes Gem::Specification.all_names, @a1.full_name
992  end
993
994  def test_self_refresh_keeps_loaded_specs_activated
995    util_make_gems
996
997    a1_spec = @a1.spec_file
998    moved_path = File.join @tempdir, File.basename(a1_spec)
999
1000    FileUtils.mv a1_spec, moved_path
1001
1002    Gem.refresh
1003
1004    s = Gem::Specification.first
1005    s.activate
1006
1007    Gem.refresh
1008
1009    Gem::Specification.each{|spec| assert spec.activated? if spec == s}
1010
1011    Gem.loaded_specs.delete(s)
1012    Gem.refresh
1013  end
1014
1015  def test_self_ruby_escaping_spaces_in_path
1016    orig_ruby = Gem.ruby
1017    orig_bindir = Gem::ConfigMap[:bindir]
1018    orig_ruby_install_name = Gem::ConfigMap[:ruby_install_name]
1019    orig_exe_ext = Gem::ConfigMap[:EXEEXT]
1020
1021    Gem::ConfigMap[:bindir] = "C:/Ruby 1.8/bin"
1022    Gem::ConfigMap[:ruby_install_name] = "ruby"
1023    Gem::ConfigMap[:EXEEXT] = ".exe"
1024    Gem.instance_variable_set("@ruby", nil)
1025
1026    assert_equal "\"C:/Ruby 1.8/bin/ruby.exe\"", Gem.ruby
1027  ensure
1028    Gem.instance_variable_set("@ruby", orig_ruby)
1029    Gem::ConfigMap[:bindir] = orig_bindir
1030    Gem::ConfigMap[:ruby_install_name] = orig_ruby_install_name
1031    Gem::ConfigMap[:EXEEXT] = orig_exe_ext
1032  end
1033
1034  def test_self_ruby_path_without_spaces
1035    orig_ruby = Gem.ruby
1036    orig_bindir = Gem::ConfigMap[:bindir]
1037    orig_ruby_install_name = Gem::ConfigMap[:ruby_install_name]
1038    orig_exe_ext = Gem::ConfigMap[:EXEEXT]
1039
1040    Gem::ConfigMap[:bindir] = "C:/Ruby18/bin"
1041    Gem::ConfigMap[:ruby_install_name] = "ruby"
1042    Gem::ConfigMap[:EXEEXT] = ".exe"
1043    Gem.instance_variable_set("@ruby", nil)
1044
1045    assert_equal "C:/Ruby18/bin/ruby.exe", Gem.ruby
1046  ensure
1047    Gem.instance_variable_set("@ruby", orig_ruby)
1048    Gem::ConfigMap[:bindir] = orig_bindir
1049    Gem::ConfigMap[:ruby_install_name] = orig_ruby_install_name
1050    Gem::ConfigMap[:EXEEXT] = orig_exe_ext
1051  end
1052
1053  def test_self_ruby_version_1_8_5
1054    util_set_RUBY_VERSION '1.8.5'
1055
1056    assert_equal Gem::Version.new('1.8.5'), Gem.ruby_version
1057  ensure
1058    util_restore_RUBY_VERSION
1059  end
1060
1061  def test_self_ruby_version_1_8_6p287
1062    util_set_RUBY_VERSION '1.8.6', 287
1063
1064    assert_equal Gem::Version.new('1.8.6.287'), Gem.ruby_version
1065  ensure
1066    util_restore_RUBY_VERSION
1067  end
1068
1069  def test_self_ruby_version_1_9_2dev_r23493
1070    util_set_RUBY_VERSION '1.9.2', -1, 23493
1071
1072    assert_equal Gem::Version.new('1.9.2.dev.23493'), Gem.ruby_version
1073  ensure
1074    util_restore_RUBY_VERSION
1075  end
1076
1077  def test_self_rubygems_version
1078    assert_equal Gem::Version.new(Gem::VERSION), Gem.rubygems_version
1079  end
1080
1081  def test_self_paths_eq
1082    other = File.join @tempdir, 'other'
1083    path = [@userhome, other].join File::PATH_SEPARATOR
1084
1085    #
1086    # FIXME remove after fixing test_case
1087    #
1088    ENV["GEM_HOME"] = @gemhome
1089    Gem.paths = { "GEM_PATH" => path }
1090
1091    assert_equal [@userhome, other, @gemhome], Gem.path
1092  end
1093
1094  def test_self_paths_eq_nonexistent_home
1095    ENV['GEM_HOME'] = @gemhome
1096    Gem.clear_paths
1097
1098    other = File.join @tempdir, 'other'
1099
1100    ENV['HOME'] = other
1101
1102    Gem.paths = { "GEM_PATH" => other }
1103
1104    assert_equal [other, @gemhome], Gem.path
1105  end
1106
1107  def test_self_post_build
1108    assert_equal 1, Gem.post_build_hooks.length
1109
1110    Gem.post_build do |installer| end
1111
1112    assert_equal 2, Gem.post_build_hooks.length
1113  end
1114
1115  def test_self_post_install
1116    assert_equal 1, Gem.post_install_hooks.length
1117
1118    Gem.post_install do |installer| end
1119
1120    assert_equal 2, Gem.post_install_hooks.length
1121  end
1122
1123  def test_self_done_installing
1124    assert_empty Gem.done_installing_hooks
1125
1126    Gem.done_installing do |gems| end
1127
1128    assert_equal 1, Gem.done_installing_hooks.length
1129  end
1130
1131  def test_self_post_reset
1132    assert_empty Gem.post_reset_hooks
1133
1134    Gem.post_reset { }
1135
1136    assert_equal 1, Gem.post_reset_hooks.length
1137  end
1138
1139  def test_self_post_uninstall
1140    assert_equal 1, Gem.post_uninstall_hooks.length
1141
1142    Gem.post_uninstall do |installer| end
1143
1144    assert_equal 2, Gem.post_uninstall_hooks.length
1145  end
1146
1147  def test_self_pre_install
1148    assert_equal 1, Gem.pre_install_hooks.length
1149
1150    Gem.pre_install do |installer| end
1151
1152    assert_equal 2, Gem.pre_install_hooks.length
1153  end
1154
1155  def test_self_pre_reset
1156    assert_empty Gem.pre_reset_hooks
1157
1158    Gem.pre_reset { }
1159
1160    assert_equal 1, Gem.pre_reset_hooks.length
1161  end
1162
1163  def test_self_pre_uninstall
1164    assert_equal 1, Gem.pre_uninstall_hooks.length
1165
1166    Gem.pre_uninstall do |installer| end
1167
1168    assert_equal 2, Gem.pre_uninstall_hooks.length
1169  end
1170
1171  def test_self_sources
1172    assert_equal %w[http://gems.example.com/], Gem.sources
1173  end
1174
1175  def test_self_try_activate_missing_dep
1176    a = util_spec 'a', '1.0', 'b' => '>= 1.0'
1177
1178    a_file = File.join a.gem_dir, 'lib', 'a_file.rb'
1179
1180    write_file a_file do |io|
1181      io.puts '# a_file.rb'
1182    end
1183
1184    e = assert_raises Gem::LoadError do
1185      Gem.try_activate 'a_file'
1186    end
1187
1188    assert_match %r%Could not find 'b' %, e.message
1189  end
1190
1191  def test_self_use_paths
1192    util_ensure_gem_dirs
1193
1194    Gem.use_paths @gemhome, @additional
1195
1196    assert_equal @gemhome, Gem.dir
1197    assert_equal @additional + [Gem.dir], Gem.path
1198  end
1199
1200  def test_self_user_dir
1201    parts = [@userhome, '.gem', Gem.ruby_engine]
1202    parts << Gem::ConfigMap[:ruby_version] unless Gem::ConfigMap[:ruby_version].empty?
1203
1204    assert_equal File.join(parts), Gem.user_dir
1205  end
1206
1207  def test_self_user_home
1208    if ENV['HOME'] then
1209      assert_equal ENV['HOME'], Gem.user_home
1210    else
1211      assert true, 'count this test'
1212    end
1213  end
1214
1215  def test_self_needs
1216    util_clear_gems
1217    a = util_spec "a", "1"
1218    b = util_spec "b", "1", "c" => nil
1219    c = util_spec "c", "2"
1220
1221    install_specs a, b, c
1222
1223    Gem.needs do |r|
1224      r.gem "a"
1225      r.gem "b", "= 1"
1226    end
1227
1228    activated = Gem::Specification.map { |x| x.full_name }
1229
1230    assert_equal %w!a-1 b-1 c-2!, activated.sort
1231  end
1232
1233  def test_self_needs_picks_up_unresolved_deps
1234    save_loaded_features do
1235      util_clear_gems
1236      a = util_spec "a", "1"
1237      b = util_spec "b", "1", "c" => nil
1238      c = util_spec "c", "2"
1239      d =  new_spec "d", "1", {'e' => '= 1'}, "lib/d.rb"
1240      e = util_spec "e", "1"
1241
1242      install_specs a, b, c, d, e
1243
1244      Gem.needs do |r|
1245        r.gem "a"
1246        r.gem "b", "= 1"
1247
1248        require 'd'
1249      end
1250
1251      assert_equal %w!a-1 b-1 c-2 d-1 e-1!, loaded_spec_names
1252    end
1253  end
1254
1255  def test_self_gunzip
1256    input = "\x1F\x8B\b\0\xED\xA3\x1AQ\0\x03\xCBH" +
1257            "\xCD\xC9\xC9\a\0\x86\xA6\x106\x05\0\0\0"
1258
1259    output = Gem.gunzip input
1260
1261    assert_equal 'hello', output
1262
1263    return unless Object.const_defined? :Encoding
1264
1265    assert_equal Encoding::BINARY, output.encoding
1266  end
1267
1268  def test_self_gzip
1269    input = 'hello'
1270
1271    output = Gem.gzip input
1272
1273    zipped = StringIO.new output
1274
1275    assert_equal 'hello', Zlib::GzipReader.new(zipped).read
1276
1277    return unless Object.const_defined? :Encoding
1278
1279    assert_equal Encoding::BINARY, output.encoding
1280  end
1281
1282  if Gem.win_platform? && '1.9' > RUBY_VERSION
1283    # Ruby 1.9 properly handles ~ path expansion, so no need to run such tests.
1284    def test_self_user_home_userprofile
1285
1286      Gem.clear_paths
1287
1288      # safe-keep env variables
1289      orig_home, orig_user_profile = ENV['HOME'], ENV['USERPROFILE']
1290
1291      # prepare for the test
1292      ENV.delete('HOME')
1293      ENV['USERPROFILE'] = "W:\\Users\\RubyUser"
1294
1295      assert_equal 'W:/Users/RubyUser', Gem.user_home
1296
1297    ensure
1298      ENV['HOME'] = orig_home
1299      ENV['USERPROFILE'] = orig_user_profile
1300    end
1301
1302    def test_self_user_home_user_drive_and_path
1303      Gem.clear_paths
1304
1305      # safe-keep env variables
1306      orig_home, orig_user_profile = ENV['HOME'], ENV['USERPROFILE']
1307      orig_home_drive, orig_home_path = ENV['HOMEDRIVE'], ENV['HOMEPATH']
1308
1309      # prepare the environment
1310      ENV.delete('HOME')
1311      ENV.delete('USERPROFILE')
1312      ENV['HOMEDRIVE'] = 'Z:'
1313      ENV['HOMEPATH'] = "\\Users\\RubyUser"
1314
1315      assert_equal 'Z:/Users/RubyUser', Gem.user_home
1316
1317    ensure
1318      ENV['HOME'] = orig_home
1319      ENV['USERPROFILE'] = orig_user_profile
1320      ENV['HOMEDRIVE'] = orig_home_drive
1321      ENV['HOMEPATH'] = orig_home_path
1322    end
1323  end
1324
1325  def test_load_plugins
1326    plugin_path = File.join "lib", "rubygems_plugin.rb"
1327
1328    Dir.chdir @tempdir do
1329      FileUtils.mkdir_p 'lib'
1330      File.open plugin_path, "w" do |fp|
1331        fp.puts "class TestGem; TEST_SPEC_PLUGIN_LOAD = :loaded; end"
1332      end
1333
1334      foo = quick_spec 'foo', '1' do |s|
1335        s.files << plugin_path
1336      end
1337
1338      install_gem foo
1339    end
1340
1341    Gem.searcher = nil
1342    Gem::Specification.reset
1343
1344    gem 'foo'
1345
1346    Gem.load_plugins
1347
1348    assert_equal :loaded, TEST_SPEC_PLUGIN_LOAD
1349  end
1350
1351  def test_load_env_plugins
1352    with_plugin('load') { Gem.load_env_plugins }
1353    assert_equal :loaded, TEST_PLUGIN_LOAD rescue nil
1354
1355    util_remove_interrupt_command
1356
1357    # Should attempt to cause a StandardError
1358    with_plugin('standarderror') { Gem.load_env_plugins }
1359    assert_equal :loaded, TEST_PLUGIN_STANDARDERROR rescue nil
1360
1361    util_remove_interrupt_command
1362
1363    # Should attempt to cause an Exception
1364    with_plugin('exception') { Gem.load_env_plugins }
1365    assert_equal :loaded, TEST_PLUGIN_EXCEPTION rescue nil
1366  end
1367
1368  def test_gem_path_ordering
1369    refute_equal Gem.dir, Gem.user_dir
1370
1371    write_file File.join(@tempdir, 'lib', "g.rb") { |fp| fp.puts "" }
1372    write_file File.join(@tempdir, 'lib', 'm.rb') { |fp| fp.puts "" }
1373
1374    g = new_spec 'g', '1', nil, "lib/g.rb"
1375    m = new_spec 'm', '1', nil, "lib/m.rb"
1376
1377    install_gem g, :install_dir => Gem.dir
1378    m0 = install_gem m, :install_dir => Gem.dir
1379    m1 = install_gem m, :install_dir => Gem.user_dir
1380
1381    assert_equal m0.gem_dir, File.join(Gem.dir, "gems", "m-1")
1382    assert_equal m1.gem_dir, File.join(Gem.user_dir, "gems", "m-1")
1383
1384    tests = [
1385      [:dir0, [ Gem.dir, Gem.user_dir], m0],
1386      [:dir1, [ Gem.user_dir, Gem.dir], m1]
1387    ]
1388
1389    tests.each do |_name, _paths, expected|
1390      Gem.paths = { 'GEM_HOME' => _paths.first, 'GEM_PATH' => _paths }
1391      Gem::Specification.reset
1392      Gem.searcher = nil
1393
1394      assert_equal Gem::Dependency.new('m','1').to_specs,
1395                   Gem::Dependency.new('m','1').to_specs.sort
1396
1397      assert_equal \
1398        [expected.gem_dir],
1399        Gem::Dependency.new('m','1').to_specs.map(&:gem_dir).sort,
1400        "Wrong specs for #{_name}"
1401
1402      spec = Gem::Dependency.new('m','1').to_spec
1403
1404      assert_equal \
1405        File.join(_paths.first, "gems", "m-1"),
1406        spec.gem_dir,
1407        "Wrong spec before require for #{_name}"
1408      refute spec.activated?, "dependency already activated for #{_name}"
1409
1410      gem "m"
1411
1412      spec = Gem::Dependency.new('m','1').to_spec
1413      assert spec.activated?, "dependency not activated for #{_name}"
1414
1415      assert_equal \
1416        File.join(_paths.first, "gems", "m-1"),
1417        spec.gem_dir,
1418        "Wrong spec after require for #{_name}"
1419
1420      spec.instance_variable_set :@activated, false
1421      Gem.loaded_specs.delete(spec.name)
1422      $:.delete(File.join(spec.gem_dir, "lib"))
1423    end
1424  end
1425
1426  def test_gem_path_ordering_short
1427    write_file File.join(@tempdir, 'lib', "g.rb") { |fp| fp.puts "" }
1428    write_file File.join(@tempdir, 'lib', 'm.rb') { |fp| fp.puts "" }
1429
1430    g = new_spec 'g', '1', nil, "lib/g.rb"
1431    m = new_spec 'm', '1', nil, "lib/m.rb"
1432
1433    install_gem g, :install_dir => Gem.dir
1434    install_gem m, :install_dir => Gem.dir
1435    install_gem m, :install_dir => Gem.user_dir
1436
1437    Gem.paths = {
1438      'GEM_HOME' => Gem.dir,
1439      'GEM_PATH' => [ Gem.dir, Gem.user_dir]
1440    }
1441
1442    assert_equal \
1443      File.join(Gem.dir, "gems", "m-1"),
1444      Gem::Dependency.new('m','1').to_spec.gem_dir,
1445      "Wrong spec selected"
1446  end
1447
1448  def test_auto_activation_of_specific_gemdeps_file
1449    util_clear_gems
1450
1451    a = new_spec "a", "1", nil, "lib/a.rb"
1452    b = new_spec "b", "1", nil, "lib/b.rb"
1453    c = new_spec "c", "1", nil, "lib/c.rb"
1454
1455    install_specs a, b, c
1456
1457    path = File.join @tempdir, "gem.deps.rb"
1458
1459    File.open path, "w" do |f|
1460      f.puts "gem 'a'"
1461      f.puts "gem 'b'"
1462      f.puts "gem 'c'"
1463    end
1464
1465    ENV['RUBYGEMS_GEMDEPS'] = path
1466
1467    Gem.detect_gemdeps
1468
1469    assert_equal %w!a-1 b-1 c-1!, loaded_spec_names
1470  end
1471
1472  def test_auto_activation_of_detected_gemdeps_file
1473    util_clear_gems
1474
1475    a = new_spec "a", "1", nil, "lib/a.rb"
1476    b = new_spec "b", "1", nil, "lib/b.rb"
1477    c = new_spec "c", "1", nil, "lib/c.rb"
1478
1479    install_specs a, b, c
1480
1481    path = File.join @tempdir, "gem.deps.rb"
1482
1483    File.open path, "w" do |f|
1484      f.puts "gem 'a'"
1485      f.puts "gem 'b'"
1486      f.puts "gem 'c'"
1487    end
1488
1489    ENV['RUBYGEMS_GEMDEPS'] = "-"
1490
1491    assert_equal [a,b,c], Gem.detect_gemdeps
1492  end
1493
1494  LIB_PATH = File.expand_path "../../../lib".untaint, __FILE__.untaint
1495
1496  def test_looks_for_gemdeps_files_automatically_on_start
1497    util_clear_gems
1498
1499    a = new_spec "a", "1", nil, "lib/a.rb"
1500    b = new_spec "b", "1", nil, "lib/b.rb"
1501    c = new_spec "c", "1", nil, "lib/c.rb"
1502
1503    install_specs a, b, c
1504
1505    path = File.join @tempdir, "gem.deps.rb"
1506
1507    File.open path, "w" do |f|
1508      f.puts "gem 'a'"
1509      f.puts "gem 'b'"
1510      f.puts "gem 'c'"
1511    end
1512
1513    path = File.join(@tempdir, "gd-tmp")
1514    install_gem a, :install_dir => path
1515    install_gem b, :install_dir => path
1516    install_gem c, :install_dir => path
1517
1518    ENV['GEM_PATH'] = path
1519    ENV['RUBYGEMS_GEMDEPS'] = "-"
1520
1521    out = `#{Gem.ruby.dup.untaint} -I #{LIB_PATH.untaint} -rubygems -e "p Gem.loaded_specs.values.map(&:full_name).sort"`
1522
1523    assert_equal '["a-1", "b-1", "c-1"]', out.strip
1524  end
1525
1526  def test_looks_for_gemdeps_files_automatically_on_start_in_parent_dir
1527    util_clear_gems
1528
1529    a = new_spec "a", "1", nil, "lib/a.rb"
1530    b = new_spec "b", "1", nil, "lib/b.rb"
1531    c = new_spec "c", "1", nil, "lib/c.rb"
1532
1533    install_specs a, b, c
1534
1535    path = File.join @tempdir, "gem.deps.rb"
1536
1537    File.open path, "w" do |f|
1538      f.puts "gem 'a'"
1539      f.puts "gem 'b'"
1540      f.puts "gem 'c'"
1541    end
1542
1543    path = File.join(@tempdir, "gd-tmp")
1544    install_gem a, :install_dir => path
1545    install_gem b, :install_dir => path
1546    install_gem c, :install_dir => path
1547
1548    ENV['GEM_PATH'] = path
1549    ENV['RUBYGEMS_GEMDEPS'] = "-"
1550
1551    Dir.mkdir "sub1"
1552    out = Dir.chdir "sub1" do
1553      `#{Gem.ruby.dup.untaint} -I #{LIB_PATH.untaint} -rubygems -e "p Gem.loaded_specs.values.map(&:full_name).sort"`
1554    end
1555
1556    Dir.rmdir "sub1"
1557
1558    assert_equal '["a-1", "b-1", "c-1"]', out.strip
1559  end
1560
1561  def with_plugin(path)
1562    test_plugin_path = File.expand_path("test/rubygems/plugin/#{path}",
1563                                        @@project_dir)
1564
1565    # A single test plugin should get loaded once only, in order to preserve
1566    # sane test semantics.
1567    refute_includes $LOAD_PATH, test_plugin_path
1568    $LOAD_PATH.unshift test_plugin_path
1569
1570    capture_io do
1571      yield
1572    end
1573  ensure
1574    $LOAD_PATH.delete test_plugin_path
1575  end
1576
1577  def util_ensure_gem_dirs
1578    Gem.ensure_gem_subdirectories @gemhome
1579
1580    #
1581    # FIXME what does this solve precisely? -ebh
1582    #
1583    @additional.each do |dir|
1584      Gem.ensure_gem_subdirectories @gemhome
1585    end
1586  end
1587
1588  def util_exec_gem
1589    spec, _ = quick_spec 'a', '4' do |s|
1590      s.executables = ['exec', 'abin']
1591    end
1592
1593    @exec_path = File.join spec.full_gem_path, spec.bindir, 'exec'
1594    @abin_path = File.join spec.full_gem_path, spec.bindir, 'abin'
1595  end
1596
1597  def util_set_RUBY_VERSION(version, patchlevel = nil, revision = nil)
1598    if Gem.instance_variables.include? :@ruby_version or
1599       Gem.instance_variables.include? '@ruby_version' then
1600      Gem.send :remove_instance_variable, :@ruby_version
1601    end
1602
1603    @RUBY_VERSION    = RUBY_VERSION
1604    @RUBY_PATCHLEVEL = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
1605    @RUBY_REVISION   = RUBY_REVISION   if defined?(RUBY_REVISION)
1606
1607    Object.send :remove_const, :RUBY_VERSION
1608    Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
1609    Object.send :remove_const, :RUBY_REVISION   if defined?(RUBY_REVISION)
1610
1611    Object.const_set :RUBY_VERSION,    version
1612    Object.const_set :RUBY_PATCHLEVEL, patchlevel if patchlevel
1613    Object.const_set :RUBY_REVISION,   revision   if revision
1614  end
1615
1616  def util_restore_RUBY_VERSION
1617    Object.send :remove_const, :RUBY_VERSION
1618    Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
1619    Object.send :remove_const, :RUBY_REVISION   if defined?(RUBY_REVISION)
1620
1621    Object.const_set :RUBY_VERSION,    @RUBY_VERSION
1622    Object.const_set :RUBY_PATCHLEVEL, @RUBY_PATCHLEVEL if
1623      defined?(@RUBY_PATCHLEVEL)
1624    Object.const_set :RUBY_REVISION,   @RUBY_REVISION   if
1625      defined?(@RUBY_REVISION)
1626  end
1627
1628  def util_remove_interrupt_command
1629    Gem::Commands.send :remove_const, :InterruptCommand if
1630      Gem::Commands.const_defined? :InterruptCommand
1631  end
1632
1633  def util_cache_dir
1634    File.join Gem.dir, "cache"
1635  end
1636end
1637
1638