Skip to content
This repository has been archived by the owner on Mar 10, 2018. It is now read-only.

Commit

Permalink
Sources command now ignores duplicate entries instead of adding them
Browse files Browse the repository at this point in the history
  • Loading branch information
aef committed Feb 9, 2011
1 parent b71afeb commit d8be565
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/rubygems/commands/sources_command.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,18 +68,23 @@ def execute
source_uri = options[:add] source_uri = options[:add]
uri = URI.parse source_uri uri = URI.parse source_uri


begin if Gem.sources.include?(source_uri)
Gem::SpecFetcher.fetcher.load_specs uri, 'specs' say "#{source_uri} is already registered. Ignoring."
Gem.sources << source_uri terminate_interaction 0
Gem.configuration.write else

begin
say "#{source_uri} added to sources" Gem::SpecFetcher.fetcher.load_specs uri, 'specs'
rescue URI::Error, ArgumentError Gem.sources << source_uri
say "#{source_uri} is not a URI" Gem.configuration.write
terminate_interaction 1
rescue Gem::RemoteFetcher::FetchError => e say "#{source_uri} added to sources"
say "Error fetching #{source_uri}:\n\t#{e.message}" rescue URI::Error, ArgumentError
terminate_interaction 1 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
end end


Expand Down
41 changes: 41 additions & 0 deletions test/rubygems/test_gem_commands_sources_command.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,6 +69,47 @@ def test_execute_add
assert_equal '', @ui.error assert_equal '', @ui.error
end 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 def test_execute_add_nonexistent_source
util_setup_fake_fetcher util_setup_fake_fetcher


Expand Down

0 comments on commit d8be565

Please sign in to comment.