From 65d5d23dd8df7eeeea0e1e596cc68f30403f96fd Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Sun, 15 Mar 2015 14:12:37 +1100 Subject: Move Bundler::Source.mirror_for to Settings. --- lib/bundler/fetcher.rb | 2 +- lib/bundler/rubygems_integration.rb | 4 ++-- lib/bundler/settings.rb | 8 +++++++ lib/bundler/source.rb | 10 --------- spec/bundler/settings_spec.rb | 42 +++++++++++++++++++++++++++++++++++-- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb index 1610cea879..73fe2abd9e 100644 --- a/lib/bundler/fetcher.rb +++ b/lib/bundler/fetcher.rb @@ -397,7 +397,7 @@ module Bundler private def configured_uri_for(uri) - uri = Bundler::Source.mirror_for(uri) + uri = Bundler.settings.mirror_for(uri) config_auth = Bundler.settings[uri.to_s] || Bundler.settings[uri.host] Source::Rubygems::Remote.new(uri, config_auth) end diff --git a/lib/bundler/rubygems_integration.rb b/lib/bundler/rubygems_integration.rb index d32bbb36ff..1cb9e72808 100644 --- a/lib/bundler/rubygems_integration.rb +++ b/lib/bundler/rubygems_integration.rb @@ -202,7 +202,7 @@ module Bundler end def download_gem(spec, uri, path) - uri = Bundler::Source.mirror_for(uri) + uri = Bundler.settings.mirror_for(uri) fetcher = Gem::RemoteFetcher.new(configuration[:http_proxy]) fetcher.download(spec, uri, path) end @@ -536,7 +536,7 @@ module Bundler def download_gem(spec, uri, path) require 'resolv' - uri = Bundler::Source.mirror_for(uri) + uri = Bundler.settings.mirror_for(uri) proxy, dns = configuration[:http_proxy], Resolv::DNS.new fetcher = Gem::RemoteFetcher.new(proxy, dns) fetcher.download(spec, uri, path) diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index a29e7104c7..7591a0607a 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -56,6 +56,14 @@ module Bundler repos end + def mirror_for(uri) + uri = URI(uri.to_s) unless uri.is_a?(URI) + + # Settings keys are all downcased + normalized_key = normalize_uri(uri.to_s.downcase) + gem_mirrors[normalized_key] || uri + end + def gem_mirrors all.inject({}) do |h, k| if k =~ /^mirror\./ diff --git a/lib/bundler/source.rb b/lib/bundler/source.rb index d1fd35a31e..213e98fb98 100644 --- a/lib/bundler/source.rb +++ b/lib/bundler/source.rb @@ -4,16 +4,6 @@ module Bundler autoload :Path, 'bundler/source/path' autoload :Git, 'bundler/source/git' - def self.mirror_for(uri) - uri = URI(uri.to_s) unless uri.is_a?(URI) - - # Settings keys are all downcased - mirrors = Bundler.settings.gem_mirrors - normalized_key = URI(uri.to_s.downcase) - - mirrors[normalized_key] || uri - end - attr_accessor :dependency_names def unmet_deps diff --git a/spec/bundler/settings_spec.rb b/spec/bundler/settings_spec.rb index 56ee87da6e..5392fc1370 100644 --- a/spec/bundler/settings_spec.rb +++ b/spec/bundler/settings_spec.rb @@ -2,8 +2,12 @@ require 'spec_helper' require 'bundler/settings' describe Bundler::Settings do + subject(:settings) { described_class.new(bundled_app) } + describe "#set_local" do context "when the local config file is not found" do + subject(:settings) { described_class.new(nil) } + it "raises a GemfileNotFound error with explanation" do expect{ subject.set_local("foo", "bar") }. to raise_error(Bundler::GemfileNotFound, "Could not locate Gemfile") @@ -11,9 +15,43 @@ describe Bundler::Settings do end end - describe "URI normalization" do - let(:settings) { described_class.new(bundled_app) } + describe "#mirror_for" do + let(:uri) { URI("https://rubygems.org/") } + context "with no configured mirror" do + it "returns the original URI" do + expect(settings.mirror_for(uri)).to eq(uri) + end + + it "converts a string parameter to a URI" do + expect(settings.mirror_for("https://rubygems.org/")).to eq(uri) + end + end + + context "with a configured mirror" do + let(:mirror_uri) { URI("https://rubygems-mirror.org/") } + + before { settings["mirror.https://rubygems.org/"] = mirror_uri.to_s } + + it "returns the mirror URI" do + expect(settings.mirror_for(uri)).to eq(mirror_uri) + end + + it "converts a string parameter to a URI" do + expect(settings.mirror_for("https://rubygems.org/")).to eq(mirror_uri) + end + + it "normalizes the URI" do + expect(settings.mirror_for("https://rubygems.org")).to eq(mirror_uri) + end + + it "is case insensitive" do + expect(settings.mirror_for("HTTPS://RUBYGEMS.ORG/")).to eq(mirror_uri) + end + end + end + + describe "URI normalization" do it "normalizes HTTP URIs in credentials configuration" do settings["http://gemserver.example.org"] = "username:password" expect(settings.all).to include("http://gemserver.example.org/") -- cgit v1.2.1