diff options
author | André Arko <mail@arko.net> | 2015-03-14 10:30:43 -0700 |
---|---|---|
committer | André Arko <mail@arko.net> | 2015-03-14 10:30:43 -0700 |
commit | 7a3de55969a3d229389812b65c91846944e03f26 (patch) | |
tree | baed9d92a50872d79da485ec53c63f60eb2908dd | |
parent | c2e6daa57dabfc211cc64df7dc8cde852bc20803 (diff) | |
parent | 38f6b319bfb189b8f4e591cb5513099602934350 (diff) | |
download | bundler-7a3de55969a3d229389812b65c91846944e03f26.tar.gz |
Merge pull request #3476 from TimMoore/rubygems-remote-refactor
Rubygems remote refactor
-rw-r--r-- | lib/bundler.rb | 1 | ||||
-rw-r--r-- | lib/bundler/anonymizable_uri.rb | 32 | ||||
-rw-r--r-- | lib/bundler/endpoint_specification.rb | 2 | ||||
-rw-r--r-- | lib/bundler/fetcher.rb | 10 | ||||
-rw-r--r-- | lib/bundler/lazy_specification.rb | 2 | ||||
-rw-r--r-- | lib/bundler/remote_specification.rb | 2 | ||||
-rw-r--r-- | lib/bundler/rubygems_ext.rb | 2 | ||||
-rw-r--r-- | lib/bundler/source/rubygems.rb | 16 | ||||
-rw-r--r-- | lib/bundler/source/rubygems/remote.rb | 36 | ||||
-rw-r--r-- | spec/bundler/anonymizable_uri_spec.rb | 44 | ||||
-rw-r--r-- | spec/bundler/cli_spec.rb | 2 | ||||
-rw-r--r-- | spec/bundler/source/rubygems/remote_spec.rb | 65 |
12 files changed, 119 insertions, 95 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index 38a3de0f38..83c066e5e5 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -12,7 +12,6 @@ module Bundler preserve_gem_path ORIGINAL_ENV = ENV.to_hash - autoload :AnonymizableURI, 'bundler/anonymizable_uri' autoload :Definition, 'bundler/definition' autoload :Dependency, 'bundler/dependency' autoload :DepProxy, 'bundler/dep_proxy' diff --git a/lib/bundler/anonymizable_uri.rb b/lib/bundler/anonymizable_uri.rb deleted file mode 100644 index 333a568f84..0000000000 --- a/lib/bundler/anonymizable_uri.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Bundler - class AnonymizableURI - attr_reader :original_uri, - :without_credentials - - def initialize(original_uri, fallback_auth = nil) - @original_uri = apply_auth(original_uri, fallback_auth).freeze - @without_credentials = remove_auth(@original_uri).freeze - end - - private - - def apply_auth(uri, auth = nil) - if auth && uri.userinfo.nil? - uri = uri.dup - uri.userinfo = auth - end - - uri - end - - def remove_auth(uri) - if uri.userinfo - uri = uri.dup - uri.user = uri.password = nil - end - - uri - end - - end -end diff --git a/lib/bundler/endpoint_specification.rb b/lib/bundler/endpoint_specification.rb index 71d5e115fc..31365e5b42 100644 --- a/lib/bundler/endpoint_specification.rb +++ b/lib/bundler/endpoint_specification.rb @@ -4,7 +4,7 @@ module Bundler include MatchPlatform attr_reader :name, :version, :platform, :dependencies - attr_accessor :source, :source_uri + attr_accessor :source, :remote def initialize(name, version, platform, dependencies) @name = name diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb index 46ac8e44e8..1610cea879 100644 --- a/lib/bundler/fetcher.rb +++ b/lib/bundler/fetcher.rb @@ -106,7 +106,7 @@ module Bundler @api_timeout = 10 # How long to wait for each API call @max_retries = 3 # How many retries for the API call - @anonymizable_uri = configured_uri_for(remote_uri) + @remote = configured_uri_for(remote_uri) Socket.do_not_reverse_lookup = true connection # create persistent connection @@ -140,7 +140,7 @@ module Bundler end def uri - @anonymizable_uri.without_credentials + @remote.anonymized_uri end # fetch a gem specification @@ -195,7 +195,7 @@ module Bundler spec = RemoteSpecification.new(name, version, platform, self) end spec.source = source - spec.source_uri = @anonymizable_uri + spec.remote = @remote index << spec end @@ -399,7 +399,7 @@ module Bundler def configured_uri_for(uri) uri = Bundler::Source.mirror_for(uri) config_auth = Bundler.settings[uri.to_s] || Bundler.settings[uri.host] - AnonymizableURI.new(uri, config_auth) + Source::Rubygems::Remote.new(uri, config_auth) end def fetch_uri @@ -415,7 +415,7 @@ module Bundler end def remote_uri - @anonymizable_uri.original_uri + @remote.uri end end end diff --git a/lib/bundler/lazy_specification.rb b/lib/bundler/lazy_specification.rb index bdcf2df2cc..16f46b8580 100644 --- a/lib/bundler/lazy_specification.rb +++ b/lib/bundler/lazy_specification.rb @@ -7,7 +7,7 @@ module Bundler include MatchPlatform attr_reader :name, :version, :dependencies, :platform - attr_accessor :source, :source_uri + attr_accessor :source, :remote def initialize(name, version, platform, source = nil) @name = name diff --git a/lib/bundler/remote_specification.rb b/lib/bundler/remote_specification.rb index d08bd9d3d8..c80eaa40fc 100644 --- a/lib/bundler/remote_specification.rb +++ b/lib/bundler/remote_specification.rb @@ -10,7 +10,7 @@ module Bundler include MatchPlatform attr_reader :name, :version, :platform - attr_accessor :source, :source_uri + attr_accessor :source, :remote def initialize(name, version, platform, spec_fetcher) @name = name diff --git a/lib/bundler/rubygems_ext.rb b/lib/bundler/rubygems_ext.rb index 1a9acb54ac..bf1b3cb35a 100644 --- a/lib/bundler/rubygems_ext.rb +++ b/lib/bundler/rubygems_ext.rb @@ -13,7 +13,7 @@ module Gem @loaded_stacks = Hash.new { |h,k| h[k] = [] } class Specification - attr_accessor :source_uri, :location, :relative_loaded_from + attr_accessor :remote, :location, :relative_loaded_from remove_method :source if instance_methods(false).include?(:source) attr_accessor :source diff --git a/lib/bundler/source/rubygems.rb b/lib/bundler/source/rubygems.rb index b29d1ad0e9..81b6954f1b 100644 --- a/lib/bundler/source/rubygems.rb +++ b/lib/bundler/source/rubygems.rb @@ -5,6 +5,8 @@ require 'rubygems/spec_fetcher' module Bundler class Source class Rubygems < Source + autoload :Remote, "bundler/source/rubygems/remote" + # Use the API when installing less than X gems API_REQUEST_LIMIT = 500 # Ask for X gems per API request @@ -84,10 +86,10 @@ module Bundler # Download the gem to get the spec, because some specs that are returned # by rubygems.org are broken and wrong. - if spec.source_uri + if spec.remote # Check for this spec from other sources - uris = [spec.source_uri.without_credentials] - uris += source_uris_for_spec(spec) + uris = [spec.remote.anonymized_uri] + uris += remotes_for_spec(spec).map { |remote| remote.anonymized_uri } uris.uniq! Installer.ambiguous_gems << [spec.name, *uris] if uris.length > 1 @@ -200,9 +202,9 @@ module Bundler protected - def source_uris_for_spec(spec) + def remotes_for_spec(spec) specs.search_all(spec.name).inject([]) do |uris, s| - uris << s.source_uri.without_credentials if s.source_uri + uris << s.remote if s.remote uris end end @@ -358,8 +360,8 @@ module Bundler end def fetch_gem(spec) - return false unless spec.source_uri - Fetcher.download_gem_from_uri(spec, spec.source_uri.original_uri) + return false unless spec.remote + Fetcher.download_gem_from_uri(spec, spec.remote.uri) end def builtin_gem?(spec) diff --git a/lib/bundler/source/rubygems/remote.rb b/lib/bundler/source/rubygems/remote.rb new file mode 100644 index 0000000000..1b9854897d --- /dev/null +++ b/lib/bundler/source/rubygems/remote.rb @@ -0,0 +1,36 @@ +module Bundler + class Source + class Rubygems + class Remote + attr_reader :uri, + :anonymized_uri + + def initialize(uri, fallback_auth = nil) + @uri = apply_auth(uri, fallback_auth).freeze + @anonymized_uri = remove_auth(@uri).freeze + end + + private + + def apply_auth(uri, auth = nil) + if auth && uri.userinfo.nil? + uri = uri.dup + uri.userinfo = auth + end + + uri + end + + def remove_auth(uri) + if uri.userinfo + uri = uri.dup + uri.user = uri.password = nil + end + + uri + end + + end + end + end +end diff --git a/spec/bundler/anonymizable_uri_spec.rb b/spec/bundler/anonymizable_uri_spec.rb deleted file mode 100644 index c444ea04d7..0000000000 --- a/spec/bundler/anonymizable_uri_spec.rb +++ /dev/null @@ -1,44 +0,0 @@ -require 'spec_helper' -require 'bundler/anonymizable_uri' - -describe Bundler::AnonymizableURI do - def auri(uri, auth = nil) - Bundler::AnonymizableURI.new(uri, auth) - end - - describe "#without_credentials" do - context "when the original URI has no credentials" do - let(:original_uri) { URI('https://rubygems.org') } - - it "returns the original URI" do - expect(auri(original_uri).without_credentials).to eq(original_uri) - end - - it "applies given credentials" do - with_auth = original_uri.dup - with_auth.userinfo = "user:pass" - expect(auri(original_uri, "user:pass").original_uri).to eq(with_auth) - end - end - - context "when the original URI has a username and password" do - let(:original_uri) { URI("https://username:password@gems.example.com") } - - it "returns the URI without username and password" do - expect(auri(original_uri).without_credentials).to eq(URI("https://gems.example.com")) - end - - it "does not apply given credentials" do - expect(auri(original_uri, "other:stuff").original_uri).to eq(original_uri) - end - end - - context "when the original URI has only a username" do - let(:original_uri) { URI("https://SeCrEt-ToKeN@gem.fury.io/me/") } - - it "returns the URI without username and password" do - expect(auri(original_uri).without_credentials).to eq(URI("https://gem.fury.io/me/")) - end - end - end -end diff --git a/spec/bundler/cli_spec.rb b/spec/bundler/cli_spec.rb index 7917726efe..2ebe69d26b 100644 --- a/spec/bundler/cli_spec.rb +++ b/spec/bundler/cli_spec.rb @@ -2,8 +2,6 @@ require 'spec_helper' require 'bundler/cli' describe "bundle executable" do - let(:source_uri) { "http://localgemserver.test" } - it "returns non-zero exit status when passed unrecognized options" do bundle '--invalid_argument' expect(exitstatus).to_not be_zero if exitstatus diff --git a/spec/bundler/source/rubygems/remote_spec.rb b/spec/bundler/source/rubygems/remote_spec.rb new file mode 100644 index 0000000000..2421b3c669 --- /dev/null +++ b/spec/bundler/source/rubygems/remote_spec.rb @@ -0,0 +1,65 @@ +require "spec_helper" +require "bundler/source/rubygems/remote" + +describe Bundler::Source::Rubygems::Remote do + def remote(uri, auth = nil) + Bundler::Source::Rubygems::Remote.new(uri, auth) + end + + let(:uri_no_auth) { URI("https://gems.example.com") } + let(:uri_with_auth) { URI("https://username:password@gems.example.com") } + + context "when the original URI has no credentials" do + describe "#uri" do + it "returns the original URI" do + expect(remote(uri_no_auth).uri).to eq(uri_no_auth) + end + + it "applies given credentials" do + expect(remote(uri_no_auth, "username:password").uri).to eq(uri_with_auth) + end + end + + describe "#anonymized_uri" do + it "returns the original URI" do + expect(remote(uri_no_auth).anonymized_uri).to eq(uri_no_auth) + end + + it "does not apply given credentials" do + expect(remote(uri_no_auth, "username:password").anonymized_uri).to eq(uri_no_auth) + end + end + end + + context "when the original URI has a username and password" do + describe "#uri" do + it "returns the original URI" do + expect(remote(uri_with_auth).uri).to eq(uri_with_auth) + end + + it "does not apply given credentials" do + expect(remote(uri_with_auth, "other:stuff").uri).to eq(uri_with_auth) + end + end + + describe "#anonymized_uri" do + it "returns the URI without username and password" do + expect(remote(uri_with_auth).anonymized_uri).to eq(uri_no_auth) + end + + it "does not apply given credentials" do + expect(remote(uri_with_auth, "other:stuff").anonymized_uri).to eq(uri_no_auth) + end + end + end + + context "when the original URI has only a username" do + let(:uri) { URI("https://SeCrEt-ToKeN@gem.fury.io/me/") } + + describe "#anonymized_uri" do + it "returns the URI without username and password" do + expect(remote(uri).anonymized_uri).to eq(URI("https://gem.fury.io/me/")) + end + end + end +end |