2 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
3 require 'rubygems/commands/sources_command'
5 class TestGemCommandsSourcesCommand < RubyGemTestCase
10 @cmd = Gem::Commands::SourcesCommand.new
12 @new_repo = "http://beta-gems.example.com"
16 util_setup_spec_fetcher
17 @cmd.handle_options []
24 *** CURRENT SOURCES ***
29 assert_equal expected, @ui.output
30 assert_equal '', @ui.error
34 util_setup_fake_fetcher
36 si = Gem::SourceIndex.new
39 specs = si.map do |_, spec|
40 [spec.name, spec.version, spec.original_platform]
43 specs_dump_gz = StringIO.new
44 Zlib::GzipWriter.wrap specs_dump_gz do |io|
45 Marshal.dump specs, io
48 @fetcher.data["#{@new_repo}/specs.#{@marshal_version}.gz"] =
51 @cmd.handle_options %W[--add #{@new_repo}]
53 util_setup_spec_fetcher
59 assert_equal [@gem_repo, @new_repo], Gem.sources
62 #{@new_repo} added to sources
65 assert_equal expected, @ui.output
66 assert_equal '', @ui.error
69 def test_execute_add_nonexistent_source
70 util_setup_fake_fetcher
72 uri = "http://beta-gems.example.com/specs.#{@marshal_version}.gz"
73 @fetcher.data[uri] = proc do
74 raise Gem::RemoteFetcher::FetchError.new('it died', uri)
77 Gem::RemoteFetcher.fetcher = @fetcher
79 @cmd.handle_options %w[--add http://beta-gems.example.com]
81 util_setup_spec_fetcher
88 Error fetching http://beta-gems.example.com:
92 assert_equal expected, @ui.output
93 assert_equal '', @ui.error
96 def test_execute_add_bad_uri
97 @cmd.handle_options %w[--add beta-gems.example.com]
99 util_setup_spec_fetcher
105 assert_equal [@gem_repo], Gem.sources
108 beta-gems.example.com is not a URI
111 assert_equal expected, @ui.output
112 assert_equal '', @ui.error
115 def test_execute_add_legacy
116 util_setup_fake_fetcher
117 util_setup_source_info_cache
119 si = Gem::SourceIndex.new
122 @fetcher.data["#{@new_repo}/yaml"] = ''
124 @cmd.handle_options %W[--add #{@new_repo}]
130 assert_equal [@gem_repo], Gem.sources
133 WARNING: RubyGems 1.2+ index not found for:
136 Will cause RubyGems to revert to legacy indexes, degrading performance.
139 assert_equal "#{@new_repo} added to sources\n", @ui.output
140 assert_equal expected, @ui.error
143 def test_execute_clear_all
144 @cmd.handle_options %w[--clear-all]
146 util_setup_source_info_cache
148 cache = Gem::SourceInfoCache.cache
152 assert File.exist?(cache.system_cache_file),
154 assert File.exist?(cache.latest_system_cache_file),
155 'latest system cache file'
157 util_setup_spec_fetcher
159 fetcher = Gem::SpecFetcher.fetcher
161 # HACK figure out how to force directory creation via fetcher
162 #assert File.directory?(fetcher.dir), 'cache dir exists'
169 *** Removed specs cache ***
170 *** Removed user source cache ***
171 *** Removed latest user source cache ***
172 *** Removed system source cache ***
173 *** Removed latest system source cache ***
176 assert_equal expected, @ui.output
177 assert_equal '', @ui.error
179 assert !File.exist?(cache.system_cache_file),
181 assert !File.exist?(cache.latest_system_cache_file),
182 'latest system cache file'
184 assert !File.exist?(fetcher.dir), 'cache dir removed'
187 def test_execute_remove
188 @cmd.handle_options %W[--remove #{@gem_repo}]
190 util_setup_spec_fetcher
196 expected = "#{@gem_repo} removed from sources\n"
198 assert_equal expected, @ui.output
199 assert_equal '', @ui.error
202 def test_execute_remove_no_network
203 @cmd.handle_options %W[--remove #{@gem_repo}]
205 util_setup_fake_fetcher
207 @fetcher.data["#{@gem_repo}Marshal.#{Gem.marshal_version}"] = proc do
208 raise Gem::RemoteFetcher::FetchError
215 expected = "#{@gem_repo} removed from sources\n"
217 assert_equal expected, @ui.output
218 assert_equal '', @ui.error
221 def test_execute_update
222 @cmd.handle_options %w[--update]
224 util_setup_fake_fetcher
225 source_index = util_setup_spec_fetcher @a1
227 specs = source_index.map do |name, spec|
228 [spec.name, spec.version, spec.original_platform]
231 @fetcher.data["#{@gem_repo}specs.#{Gem.marshal_version}.gz"] =
232 util_gzip Marshal.dump(specs)
234 latest_specs = source_index.latest_specs.map do |spec|
235 [spec.name, spec.version, spec.original_platform]
238 @fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] =
239 util_gzip Marshal.dump(latest_specs)
245 assert_equal "source cache successfully updated\n", @ui.output
246 assert_equal '', @ui.error
249 def test_execute_update_legacy
250 @cmd.handle_options %w[--update]
252 util_setup_fake_fetcher
253 util_setup_source_info_cache
254 Gem::SourceInfoCache.reset
256 si = Gem::SourceIndex.new
258 @fetcher.data["#{@gem_repo}yaml"] = YAML.dump si
259 @fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
266 Bulk updating Gem source index for: #{@gem_repo}
267 source cache successfully updated
270 assert_equal expected, @ui.output
271 assert_equal '', @ui.error