Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sources command now ignores duplicate entries instead of adding them

  • Loading branch information...
commit d8be5659dedb0fe045e15b1d0a54438aad75fb12 1 parent b71afeb
Alexander E. Fischer authored
29 lib/rubygems/commands/sources_command.rb
View
@@ -68,18 +68,23 @@ def execute
source_uri = options[:add]
uri = URI.parse source_uri
- begin
- Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
- Gem.sources << source_uri
- Gem.configuration.write
-
- say "#{source_uri} added to sources"
- rescue URI::Error, ArgumentError
- say "#{source_uri} is not a URI"
- terminate_interaction 1
- rescue Gem::RemoteFetcher::FetchError => e
- say "Error fetching #{source_uri}:\n\t#{e.message}"
- terminate_interaction 1
+ if Gem.sources.include?(source_uri)
+ say "#{source_uri} is already registered. Ignoring."
+ terminate_interaction 0
+ else
+ begin
+ Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
+ Gem.sources << source_uri
+ Gem.configuration.write
+
+ say "#{source_uri} added to sources"
+ rescue URI::Error, ArgumentError
+ say "#{source_uri} is not a URI"
+ terminate_interaction 1
+ rescue Gem::RemoteFetcher::FetchError => e
+ say "Error fetching #{source_uri}:\n\t#{e.message}"
+ terminate_interaction 1
+ end
end
end
41 test/rubygems/test_gem_commands_sources_command.rb
View
@@ -69,6 +69,47 @@ def test_execute_add
assert_equal '', @ui.error
end
+ def test_execute_add_duplicate
+ util_setup_fake_fetcher
+
+ si = Gem::SourceIndex.new
+ si.add_spec @a1
+
+ specs = si.map do |_, spec|
+ [spec.name, spec.version, spec.original_platform]
+ end
+
+ specs_dump_gz = StringIO.new
+ Zlib::GzipWriter.wrap specs_dump_gz do |io|
+ Marshal.dump specs, io
+ end
+
+ @fetcher.data["#{@new_repo}/specs.#{@marshal_version}.gz"] =
+ specs_dump_gz.string
+
+ @cmd.handle_options %W[--add #{@new_repo}]
+
+ util_setup_spec_fetcher
+
+ Gem.sources << @new_repo
+
+ use_ui @ui do
+ e = assert_raises Gem::SystemExitException do
+ @cmd.execute
+ end
+ assert_equal 0, e.exit_code, @ui.error
+ end
+
+ assert_equal [@gem_repo, @new_repo], Gem.sources
+
+ expected = <<-EOF
+#{@new_repo} is already registered. Ignoring.
+ EOF
+
+ assert_equal expected, @ui.output
+ assert_equal '', @ui.error
+ end
+
def test_execute_add_nonexistent_source
util_setup_fake_fetcher
Please sign in to comment.
Something went wrong with that request. Please try again.