summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2016-01-04 20:30:32 -0600
committerSamuel Giddins <segiddins@segiddins.me>2016-01-25 10:49:51 -0600
commitaef6682fcfd3bf7eb6f75a2c2f5dbdaca7e44743 (patch)
tree136ecda1129cd79c99192dab3d62a8d1535d05f0
parentc2432bc4d967ab498c9b2eb4f83479668b4b197e (diff)
downloadbundler-aef6682fcfd3bf7eb6f75a2c2f5dbdaca7e44743.tar.gz
Refactor cache slug logic into the Remote
-rw-r--r--lib/bundler/fetcher/compact_index.rb7
-rw-r--r--lib/bundler/source/rubygems/remote.rb18
-rw-r--r--spec/bundler/fetcher_spec.rb7
-rw-r--r--spec/bundler/source/rubygems/remote_spec.rb38
4 files changed, 41 insertions, 29 deletions
diff --git a/lib/bundler/fetcher/compact_index.rb b/lib/bundler/fetcher/compact_index.rb
index d4be8931b4..d58c165985 100644
--- a/lib/bundler/fetcher/compact_index.rb
+++ b/lib/bundler/fetcher/compact_index.rb
@@ -74,12 +74,7 @@ module Bundler
end
def cache_path
- cache_uri = remote.cache_uri
- uri_parts = [cache_uri.host, cache_uri.port, cache_uri.path].compact.join(".")
- uri_digest = Digest::MD5.hexdigest(uri_parts)
-
- cache_path = [cache_uri.host, cache_uri.port, uri_digest[0..5]].compact.join(".")
- Bundler.user_cache.join("compact_index", cache_path)
+ Bundler.user_cache.join("compact_index", remote.cache_slug)
end
end
end
diff --git a/lib/bundler/source/rubygems/remote.rb b/lib/bundler/source/rubygems/remote.rb
index 376ce3ee7d..4cd8ea23b8 100644
--- a/lib/bundler/source/rubygems/remote.rb
+++ b/lib/bundler/source/rubygems/remote.rb
@@ -2,7 +2,7 @@ module Bundler
class Source
class Rubygems
class Remote
- attr_reader :uri, :anonymized_uri, :original_uri, :cache_uri
+ attr_reader :uri, :anonymized_uri, :original_uri
def initialize(uri)
orig_uri = uri
@@ -12,7 +12,21 @@ module Bundler
@uri = apply_auth(uri, fallback_auth).freeze
@anonymized_uri = remove_auth(@uri).freeze
- @cache_uri = remove_auth(orig_uri.dup).freeze
+ end
+
+ # @return [String] A slug suitable for use as a cache key for this
+ # remote.
+ #
+ def cache_slug
+ @cache_slug ||= begin
+ cache_uri = original_uri || uri
+
+ uri_parts = [cache_uri.host, cache_uri.user, cache_uri.port, cache_uri.path]
+ uri_digest = Digest::MD5.hexdigest(uri_parts.compact.join("."))
+
+ uri_parts[-1] = uri_digest
+ uri_parts.compact.join(".")
+ end
end
private
diff --git a/spec/bundler/fetcher_spec.rb b/spec/bundler/fetcher_spec.rb
index 0bb6cb934a..0b10371f25 100644
--- a/spec/bundler/fetcher_spec.rb
+++ b/spec/bundler/fetcher_spec.rb
@@ -50,13 +50,6 @@ describe Bundler::Fetcher do
fetcher.send(:connection).override_headers["X-Gemfile-Source"]
).to eq("http://zombo.com")
end
-
- it "caches things based upon the original source" do
- compact_index_fetcher = fetcher.fetchers.first
- expect(compact_index_fetcher).to be_a(described_class::CompactIndex)
- expect(compact_index_fetcher.send(:compact_index_client).directory).
- to eq(home + ".bundle/cache/compact_index/cache_zombo.com.80.fc7f36")
- end
end
context "when there is no rubygems source mirror set" do
diff --git a/spec/bundler/source/rubygems/remote_spec.rb b/spec/bundler/source/rubygems/remote_spec.rb
index 008af117ee..599887d47a 100644
--- a/spec/bundler/source/rubygems/remote_spec.rb
+++ b/spec/bundler/source/rubygems/remote_spec.rb
@@ -6,6 +6,10 @@ describe Bundler::Source::Rubygems::Remote do
Bundler::Source::Rubygems::Remote.new(uri)
end
+ before do
+ allow(Digest::MD5).to receive(:hexdigest).with(duck_type(:to_s)) {|string| "MD5HEX(#{string})" }
+ end
+
let(:uri_no_auth) { URI("https://gems.example.com") }
let(:uri_with_auth) { URI("https://#{credentials}@gems.example.com") }
let(:credentials) { "username:password" }
@@ -33,14 +37,14 @@ describe Bundler::Source::Rubygems::Remote do
end
end
- describe "#cache_uri" do
- it "returns the original_uri" do
- expect(remote(uri_no_auth).cache_uri).to eq(uri_no_auth)
+ describe "#cache_slug" do
+ it "returns the correct slug" do
+ expect(remote(uri_no_auth).cache_slug).to eq("gems.example.com.443.MD5HEX(gems.example.com.443./)")
end
- it "does not apply given credentials" do
+ it "only applies the given user" do
Bundler.settings[uri_no_auth.to_s] = credentials
- expect(remote(uri_no_auth).cache_uri).to eq(uri_no_auth)
+ expect(remote(uri_no_auth).cache_slug).to eq("gems.example.com.username.443.MD5HEX(gems.example.com.username.443./)")
end
end
end
@@ -68,14 +72,14 @@ describe Bundler::Source::Rubygems::Remote do
end
end
- describe "#cache_uri" do
- it "returns the original_uri" do
- expect(remote(uri_no_auth).cache_uri).to eq(uri_no_auth)
+ describe "#cache_slug" do
+ it "returns the correct slug" do
+ expect(remote(uri_with_auth).cache_slug).to eq("gems.example.com.username.443.MD5HEX(gems.example.com.username.443./)")
end
it "does not apply given credentials" do
- Bundler.settings[uri_no_auth.to_s] = credentials
- expect(remote(uri_no_auth).cache_uri).to eq(uri_no_auth)
+ Bundler.settings[uri_with_auth.to_s] = credentials
+ expect(remote(uri_with_auth).cache_slug).to eq("gems.example.com.username.443.MD5HEX(gems.example.com.username.443./)")
end
end
end
@@ -88,6 +92,12 @@ describe Bundler::Source::Rubygems::Remote do
expect(remote(uri).anonymized_uri).to eq(URI("https://gem.fury.io/me/"))
end
end
+
+ describe "#cache_slug" do
+ it "returns the correct slug" do
+ expect(remote(uri).cache_slug).to eq("gem.fury.io.SeCrEt-ToKeN.443.MD5HEX(gem.fury.io.SeCrEt-ToKeN.443./me/)")
+ end
+ end
end
context "when a mirror with inline credentials is configured for the URI" do
@@ -109,8 +119,8 @@ describe Bundler::Source::Rubygems::Remote do
expect(remote(uri).original_uri).to eq(uri)
end
- specify "#cache_uri returns the original source" do
- expect(remote(uri).cache_uri).to eq(uri)
+ specify "#cache_slug returns the correct slug" do
+ expect(remote(uri).cache_slug).to eq("rubygems.org.443.MD5HEX(rubygems.org.443./)")
end
end
@@ -136,8 +146,8 @@ describe Bundler::Source::Rubygems::Remote do
expect(remote(uri).original_uri).to eq(uri)
end
- specify "#cache_uri returns the original source" do
- expect(remote(uri).cache_uri).to eq(uri)
+ specify "#cache_slug returns the original source" do
+ expect(remote(uri).cache_slug).to eq("rubygems.org.443.MD5HEX(rubygems.org.443./)")
end
end