Change soft-fail to use the config, rather than env
[rbx.git] / test / rubygems / test_gem_source_index.rb
blob263826759aba056e2a7af8cffde3ec7bcb7b115a
1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
7 require 'test/unit'
8 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
9 require 'rubygems/source_index'
10 require 'rubygems/config_file'
12 class Gem::SourceIndex
13   public :fetcher, :fetch_bulk_index, :fetch_quick_index,
14          :find_missing, :gems, :remove_extra,
15          :update_with_missing, :unzip
16 end
18 class TestGemSourceIndex < RubyGemTestCase
20   def setup
21     super
23     util_setup_fake_fetcher
24   end
26   def test_self_from_gems_in
27     spec_dir = File.join @gemhome, 'specifications'
29     FileUtils.rm_r spec_dir
31     FileUtils.mkdir_p spec_dir
33     a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
35     spec_file = File.join spec_dir, "#{a1.full_name}.gemspec"
37     File.open spec_file, 'w' do |fp|
38       fp.write a1.to_ruby
39     end
41     si = Gem::SourceIndex.from_gems_in spec_dir
43     assert_equal [spec_dir], si.spec_dirs
44     assert_equal [a1.full_name], si.gems.keys
45   end
47   def test_self_load_specification
48     spec_dir = File.join @gemhome, 'specifications'
50     FileUtils.rm_r spec_dir
52     FileUtils.mkdir_p spec_dir
54     a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
56     spec_file = File.join spec_dir, "#{a1.full_name}.gemspec"
58     File.open spec_file, 'w' do |fp|
59       fp.write a1.to_ruby
60     end
62     spec = Gem::SourceIndex.load_specification spec_file
64     assert_equal a1.author, spec.author
65   end
67   def test_self_load_specification_exception
68     spec_dir = File.join @gemhome, 'specifications'
70     FileUtils.mkdir_p spec_dir
72     spec_file = File.join spec_dir, 'a-1.gemspec'
74     File.open spec_file, 'w' do |fp|
75       fp.write 'raise Exception, "epic fail"'
76     end
78     use_ui @ui do
79       assert_equal nil, Gem::SourceIndex.load_specification(spec_file)
80     end
82     assert_equal '', @ui.output
84     expected = <<-EOF
85 WARNING:  #<Exception: epic fail>
86 raise Exception, "epic fail"
87 WARNING:  Invalid .gemspec format in '#{spec_file}'
88     EOF
90     assert_equal expected, @ui.error
91   end
93   def test_self_load_specification_interrupt
94     spec_dir = File.join @gemhome, 'specifications'
96     FileUtils.mkdir_p spec_dir
98     spec_file = File.join spec_dir, 'a-1.gemspec'
100     File.open spec_file, 'w' do |fp|
101       fp.write 'raise Interrupt, "^C"'
102     end
104     use_ui @ui do
105       assert_raise Interrupt do
106         Gem::SourceIndex.load_specification(spec_file)
107       end
108     end
110     assert_equal '', @ui.output
111     assert_equal '', @ui.error
112   end
114   def test_self_load_specification_syntax_error
115     spec_dir = File.join @gemhome, 'specifications'
117     FileUtils.mkdir_p spec_dir
119     spec_file = File.join spec_dir, 'a-1.gemspec'
121     File.open spec_file, 'w' do |fp|
122       fp.write '1 +'
123     end
125     use_ui @ui do
126       assert_equal nil, Gem::SourceIndex.load_specification(spec_file)
127     end
129     assert_equal '', @ui.output
131     assert_match(/syntax error/, @ui.error)
132     assert_match(/1 \+/, @ui.error)
133   end
135   def test_self_load_specification_system_exit
136     spec_dir = File.join @gemhome, 'specifications'
138     FileUtils.mkdir_p spec_dir
140     spec_file = File.join spec_dir, 'a-1.gemspec'
142     File.open spec_file, 'w' do |fp|
143       fp.write 'raise SystemExit, "bye-bye"'
144     end
146     use_ui @ui do
147       assert_raise SystemExit do
148         Gem::SourceIndex.load_specification(spec_file)
149       end
150     end
152     assert_equal '', @ui.output
153     assert_equal '', @ui.error
154   end
156   def test_create_from_directory
157     # TODO
158   end
160   def test_fetcher
161     assert_equal @fetcher, @source_index.fetcher
162   end
164   def test_fetch_bulk_index_compressed
165     util_setup_bulk_fetch true
167     use_ui @ui do
168       fetched_index = @source_index.fetch_bulk_index @uri
169       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
170                     @c1_2.full_name].sort,
171                    fetched_index.gems.map { |n,s| n }.sort
172     end
174     paths = @fetcher.paths
176     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
178     assert paths.empty?, paths.join(', ')
179   end
181   def test_fetch_bulk_index_error
182     @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] = proc { raise SocketError }
183     @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = proc { raise SocketError }
184     @fetcher.data["#{@gem_repo}yaml.Z"] = proc { raise SocketError }
185     @fetcher.data["#{@gem_repo}yaml"] = proc { raise SocketError }
187     e = assert_raise Gem::RemoteSourceException do
188       use_ui @ui do
189         @source_index.fetch_bulk_index @uri
190       end
191     end
193     paths = @fetcher.paths
195     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
196     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
197     assert_equal "#{@gem_repo}yaml.Z", paths.shift
198     assert_equal "#{@gem_repo}yaml", paths.shift
200     assert paths.empty?, paths.join(', ')
202     assert_equal 'Error fetching remote gem cache: SocketError',
203                  e.message
204   end
206   def test_fetch_bulk_index_fallback
207     @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] =
208       proc { raise SocketError }
209     @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] =
210       proc { raise SocketError }
211     @fetcher.data["#{@gem_repo}yaml.Z"] = proc { raise SocketError }
212     @fetcher.data["#{@gem_repo}yaml"] = @source_index.to_yaml
214     use_ui @ui do
215       fetched_index = @source_index.fetch_bulk_index @uri
216       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
217                     @c1_2.full_name].sort,
218                    fetched_index.gems.map { |n,s| n }.sort
219     end
221     paths = @fetcher.paths
223     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
224     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
225     assert_equal "#{@gem_repo}yaml.Z", paths.shift
226     assert_equal "#{@gem_repo}yaml", paths.shift
228     assert paths.empty?, paths.join(', ')
229   end
231   def test_fetch_bulk_index_marshal_mismatch
232     marshal = @source_index.dump
233     marshal[0] = (Marshal::MAJOR_VERSION - 1).chr
235     @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = marshal
236     @fetcher.data["#{@gem_repo}yaml"] = @source_index.to_yaml
238     use_ui @ui do
239       fetched_index = @source_index.fetch_bulk_index @uri
240       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
241                     @c1_2.full_name].sort,
242                    fetched_index.gems.map { |n,s| n }.sort
243     end
245     paths = @fetcher.paths
247     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
248     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
249     assert_equal "#{@gem_repo}yaml.Z", paths.shift
250     assert_equal "#{@gem_repo}yaml", paths.shift
252     assert paths.empty?, paths.join(', ')
253   end
255   def test_fetch_bulk_index_uncompressed
256     util_setup_bulk_fetch false
257     use_ui @ui do
258       fetched_index = @source_index.fetch_bulk_index @uri
259       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
260                     @c1_2.full_name].sort,
261                    fetched_index.gems.map { |n,s| n }.sort
262     end
264     paths = @fetcher.paths
266     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
267     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}", paths.shift
269     assert paths.empty?, paths.join(', ')
270   end
272   def test_fetch_quick_index
273     index = util_zip @gem_names
274     latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
276     @fetcher.data["#{@gem_repo}quick/index.rz"] = index
277     @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
279     quick_index = @source_index.fetch_quick_index @uri, false
280     assert_equal [@a2.full_name, @b2.full_name].sort,
281                  quick_index.sort
283     paths = @fetcher.paths
285     assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
287     assert paths.empty?, paths.join(', ')
288   end
290   def test_fetch_quick_index_all
291     index = util_zip @gem_names
292     latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
294     @fetcher.data["#{@gem_repo}quick/index.rz"] = index
295     @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
297     quick_index = @source_index.fetch_quick_index @uri, true
298     assert_equal [@a1.full_name, @a2.full_name, @b2.full_name].sort,
299                  quick_index.sort
301     paths = @fetcher.paths
303     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
305     assert paths.empty?, paths.join(', ')
306   end
308   def test_fetch_quick_index_error
309     @fetcher.data["#{@gem_repo}quick/index.rz"] =
310       proc { raise Exception }
312     e = assert_raise Gem::OperationNotSupportedError do
313       @source_index.fetch_quick_index @uri, true
314     end
316     assert_equal 'No quick index found: Exception', e.message
318     paths = @fetcher.paths
320     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
322     assert paths.empty?, paths.join(', ')
323   end
325   def test_fetch_quick_index_fallback
326     index = util_zip @gem_names
328     @fetcher.data["#{@gem_repo}quick/index.rz"] = index
330     quick_index = @source_index.fetch_quick_index @uri, false
331     assert_equal @gem_names.split, quick_index.sort
333     paths = @fetcher.paths
335     assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
336     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
338     assert paths.empty?, paths.join(', ')
339   end
341   def test_fetch_quick_index_subdir
342     latest_index = util_zip [@a2.full_name, @b2.full_name].join("\n")
343     repo = URI.parse "#{@gem_repo}~nobody/mirror/"
345     @fetcher.data["#{repo}quick/latest_index.rz"] = latest_index
347     quick_index = @source_index.fetch_quick_index repo, false
348     assert_equal [@a2.full_name, @b2.full_name].sort,
349                  quick_index.sort
351     paths = @fetcher.paths
353     assert_equal "#{repo}quick/latest_index.rz", paths.shift
355     assert paths.empty?, paths.join(', ')
356   end
358   def test_fetch_single_spec
359     a1_spec_url = "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz"
360     @fetcher.data[a1_spec_url] = util_zip Marshal.dump(@a1)
362     spec = @source_index.send :fetch_single_spec, URI.parse(@gem_repo),
363                               @a1.full_name
365     assert_equal @a1.full_name, spec.full_name
367     paths = @fetcher.paths
369     assert_equal a1_spec_url, paths.shift
371     assert paths.empty?, paths.join(', ')
372   end
374   def test_fetch_single_spec_subdir
375     repo = URI.parse "#{@gem_repo}~nobody/mirror/"
377     a1_spec_url = "#{repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz"
378     @fetcher.data[a1_spec_url] = util_zip Marshal.dump(@a1)
380     spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
382     assert_equal @a1.full_name, spec.full_name
384     paths = @fetcher.paths
386     assert_equal a1_spec_url, paths.shift
388     assert paths.empty?, paths.join(', ')
389   end
391   def test_fetch_single_spec_yaml
392     a1_spec_url = "#{@gem_repo}quick/#{@a1.full_name}.gemspec.rz"
393     @fetcher.data[a1_spec_url] = util_zip @a1.to_yaml
395     repo = URI.parse @gem_repo
397     spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
399     assert_equal @a1.full_name, spec.full_name
401     paths = @fetcher.paths
403     assert_equal "#{@gem_repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz", paths.shift
404     assert_equal a1_spec_url, paths.shift
406     assert paths.empty?, paths.join(', ')
407   end
409   def test_fetch_single_spec_yaml_subdir
410     repo = URI.parse "#{@gem_repo}~nobody/mirror/"
412     a1_spec_url = "#{repo}quick/#{@a1.full_name}.gemspec.rz"
413     @fetcher.data[a1_spec_url] = util_zip @a1.to_yaml
415     spec = @source_index.send :fetch_single_spec, repo, @a1.full_name
417     assert_equal @a1.full_name, spec.full_name
419     paths = @fetcher.paths
421     assert_equal "#{repo}quick/Marshal.#{Gem.marshal_version}/#{@a1.full_name}.gemspec.rz", paths.shift
422     assert_equal a1_spec_url, paths.shift
424     assert paths.empty?, paths.join(', ')
425   end
427   def test_find_missing
428     missing = @source_index.find_missing [@b2.full_name]
429     assert_equal [@b2.full_name], missing
430   end
432   def test_find_missing_none_missing
433     missing = @source_index.find_missing [
434       @a1.full_name, @a2.full_name, @c1_2.full_name
435     ]
437     assert_equal [], missing
438   end
440   def test_latest_specs
441     p1_ruby = quick_gem 'p', '1'
442     p1_platform = quick_gem 'p', '1' do |spec|
443       spec.platform = Gem::Platform::CURRENT
444     end
446     a1_platform = quick_gem @a1.name, (@a1.version) do |s|
447       s.platform = Gem::Platform.new 'x86-my_platform1'
448     end
450     a2_platform = quick_gem @a2.name, (@a2.version) do |s|
451       s.platform = Gem::Platform.new 'x86-my_platform1'
452     end
454     a2_platform_other = quick_gem @a2.name, (@a2.version) do |s|
455       s.platform = Gem::Platform.new 'x86-other_platform1'
456     end
458     a3_platform_other = quick_gem @a2.name, (@a2.version.bump) do |s|
459       s.platform = Gem::Platform.new 'x86-other_platform1'
460     end
462     @source_index.add_spec p1_ruby
463     @source_index.add_spec p1_platform
464     @source_index.add_spec a1_platform
465     @source_index.add_spec a2_platform
466     @source_index.add_spec a2_platform_other
467     @source_index.add_spec a3_platform_other
469     expected = [
470       @a2.full_name,
471       a2_platform.full_name,
472       a3_platform_other.full_name,
473       @c1_2.full_name,
474       @a_evil9.full_name,
475       p1_ruby.full_name,
476       p1_platform.full_name,
477     ].sort
479     latest_specs = @source_index.latest_specs.map { |s| s.full_name }.sort
481     assert_equal expected, latest_specs
482   end
484   def test_load_gems_in
485     spec_dir1 = File.join @gemhome, 'specifications'
486     spec_dir2 = File.join @tempdir, 'gemhome2', 'specifications'
488     FileUtils.rm_r spec_dir1
490     FileUtils.mkdir_p spec_dir1
491     FileUtils.mkdir_p spec_dir2
493     a1 = quick_gem 'a', '1' do |spec| spec.author = 'author 1' end
494     a2 = quick_gem 'a', '1' do |spec| spec.author = 'author 2' end
496     File.open File.join(spec_dir1, "#{a1.full_name}.gemspec"), 'w' do |fp|
497       fp.write a1.to_ruby
498     end
500     File.open File.join(spec_dir2, "#{a2.full_name}.gemspec"), 'w' do |fp|
501       fp.write a2.to_ruby
502     end
504     @source_index.load_gems_in spec_dir1, spec_dir2
506     assert_equal a1.author, @source_index.specification(a1.full_name).author
507   end
509   def test_outdated
510     util_setup_spec_fetcher
512     assert_equal [], @source_index.outdated
514     updated = quick_gem @a2.name, (@a2.version.bump)
515     util_setup_spec_fetcher updated
517     assert_equal [updated.name], @source_index.outdated
519     updated_platform = quick_gem @a2.name, (updated.version.bump) do |s|
520       s.platform = Gem::Platform.new 'x86-other_platform1'
521     end
523     util_setup_spec_fetcher updated, updated_platform
525     assert_equal [updated_platform.name], @source_index.outdated
526   end
528   def test_refresh_bang
529     a1_spec = File.join @gemhome, "specifications", "#{@a1.full_name}.gemspec" 
531     FileUtils.mv a1_spec, @tempdir
533     source_index = Gem::SourceIndex.from_installed_gems
535     assert !source_index.gems.include?(@a1.full_name)
537     FileUtils.mv File.join(@tempdir, "#{@a1.full_name}.gemspec"), a1_spec
539     source_index.refresh!
541     assert source_index.gems.include?(@a1.full_name)
542   end
544   def test_refresh_bang_not_from_dir
545     source_index = Gem::SourceIndex.new
547     e = assert_raise RuntimeError do
548       source_index.refresh!
549     end
551     assert_equal 'source index not created from disk', e.message
552   end
554   def test_remove_extra
555     @source_index.add_spec @a1
556     @source_index.add_spec @a2
557     @source_index.add_spec @pl1
559     @source_index.remove_extra [@a1.full_name, @pl1.full_name]
561     assert_equal [@a1.full_name],
562                  @source_index.gems.map { |n,s| n }.sort
563   end
565   def test_remove_extra_no_changes
566     gems = [@a1.full_name, @a2.full_name]
567     @source_index.add_spec @a1
568     @source_index.add_spec @a2
570     @source_index.remove_extra gems
572     assert_equal gems, @source_index.gems.map { |n,s| n }.sort
573   end
575   def test_search
576     assert_equal [@a1, @a2, @a_evil9], @source_index.search('a')
577     assert_equal [@a2], @source_index.search('a', '= 2')
579     assert_equal [], @source_index.search('bogusstring')
580     assert_equal [], @source_index.search('a', '= 3')
582     source_index = Gem::SourceIndex.new
583     source_index.add_spec @a1
584     source_index.add_spec @a2
586     assert_equal [@a1], source_index.search(@a1.name, '= 1')
588     r1 = Gem::Requirement.create '= 1'
589     assert_equal [@a1], source_index.search(@a1.name, r1)
591     dep = Gem::Dependency.new @a1.name, r1
592     assert_equal [@a1], source_index.search(dep)
593   end
595   def test_search_empty_cache
596     empty_source_index = Gem::SourceIndex.new({})
597     assert_equal [], empty_source_index.search("foo")
598   end
600   def test_search_platform
601     util_set_arch 'x86-my_platform1'
603     a1 = quick_gem 'a', '1'
604     a1_mine = quick_gem 'a', '1' do |s|
605       s.platform = Gem::Platform.new 'x86-my_platform1'
606     end
607     a1_other = quick_gem 'a', '1' do |s|
608       s.platform = Gem::Platform.new 'x86-other_platform1'
609     end
611     si = Gem::SourceIndex.new(a1.full_name => a1, a1_mine.full_name => a1_mine,
612                               a1_other.full_name => a1_other)
614     dep = Gem::Dependency.new 'a', Gem::Requirement.new('1')
616     gems = si.search dep, true
618     assert_equal [a1, a1_mine], gems.sort
619   end
621   def test_signature
622     sig = @source_index.gem_signature('foo-1.2.3')
623     assert_equal 64, sig.length
624     assert_match(/^[a-f0-9]{64}$/, sig)
625   end
627   def test_specification
628     assert_equal @a1, @source_index.specification(@a1.full_name)
630     assert_nil @source_index.specification("foo-1.2.4")
631   end
633   def test_index_signature
634     sig = @source_index.index_signature
635     assert_match(/^[a-f0-9]{64}$/, sig)
636   end
638   def test_unzip
639     input = "x\234+\316\317MU(I\255(\001\000\021\350\003\232"
640     assert_equal 'some text', @source_index.unzip(input)
641   end
643   def test_update_bulk
644     util_setup_bulk_fetch true
646     @source_index.gems.replace({})
647     assert_equal [], @source_index.gems.keys.sort
649     use_ui @ui do
650       @source_index.update @uri, true
652       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
653                     @c1_2.full_name],
654                    @source_index.gems.keys.sort
655     end
657     paths = @fetcher.paths
659     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
660     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
662     assert paths.empty?, paths.join(', ')
663   end
665   def test_update_incremental
666     old_gem_conf = Gem.configuration
667     Gem.configuration = Gem::ConfigFile.new([])
669     latest_names = [@a2, @a_evil9, @b2, @c1_2].map { |s| s.full_name }
670     latest_index = util_zip latest_names.join("\n")
671     @fetcher.data["#{@gem_repo}quick/latest_index.rz"] = latest_index
673     marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
674                             "#{@b2.full_name}.gemspec.rz"
675     @fetcher.data[marshal_uri] = util_zip Marshal.dump(@b2)
677     use_ui @ui do
678       @source_index.update @uri, false
680       assert_equal latest_names, @source_index.gems.keys.sort
681     end
683     paths = @fetcher.paths
684     assert_equal "#{@gem_repo}quick/latest_index.rz", paths.shift
685     assert_equal marshal_uri, paths.shift
687     assert paths.empty?, paths.join(', ')
688   ensure
689     Gem.configuration = old_gem_conf
690   end
692   def test_update_incremental_all
693     old_gem_conf = Gem.configuration
694     Gem.configuration = Gem::ConfigFile.new([])
696     quick_index = util_zip @all_gem_names.join("\n")
697     @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
699     marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
700                             "#{@b2.full_name}.gemspec.rz"
701     @fetcher.data[marshal_uri] = util_zip Marshal.dump(@b2)
703     use_ui @ui do
704       @source_index.update @uri, true
706       assert_equal @all_gem_names, @source_index.gems.keys.sort
707     end
709     paths = @fetcher.paths
710     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
711     assert_equal marshal_uri, paths.shift
713     assert paths.empty?, paths.join(', ')
714   ensure
715     Gem.configuration = old_gem_conf
716   end
718   def test_update_incremental_fallback
719     old_gem_conf = Gem.configuration
720     Gem.configuration = Gem::ConfigFile.new([])
722     quick_index = util_zip @all_gem_names.join("\n")
723     @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
725     marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
726                             "#{@b2.full_name}.gemspec.rz"
728     yaml_uri = "#{@gem_repo}quick/#{@b2.full_name}.gemspec.rz"
729     @fetcher.data[yaml_uri] = util_zip @b2.to_yaml
731     use_ui @ui do
732       @source_index.update @uri, true
734       assert_equal @all_gem_names, @source_index.gems.keys.sort
735     end
737     paths = @fetcher.paths
738     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
739     assert_equal marshal_uri, paths.shift
740     assert_equal yaml_uri, paths.shift
742     assert paths.empty?, paths.join(', ')
743   ensure
744     Gem.configuration = old_gem_conf
745   end
747   def test_update_incremental_marshal_mismatch
748     old_gem_conf = Gem.configuration
749     Gem.configuration = Gem::ConfigFile.new([])
751     quick_index = util_zip @all_gem_names.join("\n")
752     @fetcher.data["#{@gem_repo}quick/index.rz"] = quick_index
754     marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
755                             "#{@b2.full_name}.gemspec.rz"
756     marshal_data = Marshal.dump(@b2)
757     marshal_data[0] = (Marshal::MAJOR_VERSION - 1).chr
758     @fetcher.data[marshal_uri] = util_zip marshal_data
760     yaml_uri = "#{@gem_repo}quick/#{@b2.full_name}.gemspec.rz"
761     @fetcher.data[yaml_uri] = util_zip @b2.to_yaml
763     use_ui @ui do
764       @source_index.update @uri, true
766       assert_equal @all_gem_names, @source_index.gems.keys.sort
767     end
769     paths = @fetcher.paths
770     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
771     assert_equal marshal_uri, paths.shift
772     assert_equal yaml_uri, paths.shift
774     assert paths.empty?, paths.join(', ')
775   ensure
776     Gem.configuration = old_gem_conf
777   end
779   def test_update_subdir
780     @gem_repo = @gem_repo + 'subdir/'
782     util_setup_bulk_fetch true
784     @source_index.gems.replace({})
785     assert_equal [], @source_index.gems.keys.sort
787     uri = @uri.to_s + 'subdir/'
789     use_ui @ui do
790       @source_index.update uri, true
792       assert_equal [@a1.full_name, @a2.full_name, @a_evil9.full_name,
793                     @c1_2.full_name],
794                    @source_index.gems.keys.sort
795     end
797     paths = @fetcher.paths
799     assert_equal "#{@gem_repo}quick/index.rz", paths.shift
800     assert_equal "#{@gem_repo}Marshal.#{@marshal_version}.Z", paths.shift
802     assert paths.empty?, paths.join(', ')
803   end
805   def test_update_with_missing
806     marshal_uri = File.join @gem_repo, "quick", "Marshal.#{@marshal_version}",
807                             "#{@c1_2.full_name}.gemspec.rz"
808     dumped = Marshal.dump @c1_2
809     @fetcher.data[marshal_uri] = util_zip(dumped)
811     use_ui @ui do
812       @source_index.update_with_missing @uri, [@c1_2.full_name]
813     end
815     spec = @source_index.specification(@c1_2.full_name)
816     # We don't care about the equality of undumped attributes
817     @c1_2.files = spec.files
818     @c1_2.loaded_from = spec.loaded_from
820     assert_equal @c1_2, spec
821   end
823   def util_setup_bulk_fetch(compressed)
824     source_index = @source_index.dump
826     if compressed then
827       @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}.Z"] = util_zip source_index
828     else
829       @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = source_index
830     end
831   end