From 10cc4396f4f5a740dc73ee6d2ea78d6d1b204628 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Sun, 18 Jun 2017 20:13:07 +0200 Subject: [PATCH 1/2] Use XDG directory for cache Falls back to ~/.bundle/cache if the directory exists. --- lib/bundler.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/bundler.rb b/lib/bundler.rb index c6d68c49dea..0981f91847b 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -181,6 +181,12 @@ def tmp_home_path(login, warning) raise e.exception("#{warning}\nBundler also failed to create a temporary home directory at `#{path}':\n#{e}") end + def xdg_home(type, alt) + xdg_path = Pathname.new(ENV.fetch("XDG_#{type}_HOME", alt)).join("bundler") + xdg_path.mkpath unless xdg_path.exist? + xdg_path + end + def user_bundle_path Pathname.new(user_home).join(".bundle") end @@ -202,7 +208,12 @@ def cache end def user_cache - user_bundle_path.join("cache") + legacy_path = user_bundle_path.join("cache") + if legacy_path.exist? + legacy_path + else + xdg_home("CACHE", user_home.join(".cache")) + end end def root From 47fbe99387fea73fa652ad6692349b24cad6fe2e Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Mon, 19 Jun 2017 19:49:06 +0200 Subject: [PATCH 2/2] Add tests for user cache dir --- spec/bundler/bundler_spec.rb | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/spec/bundler/bundler_spec.rb b/spec/bundler/bundler_spec.rb index c73bbf3670d..3a97ca27580 100644 --- a/spec/bundler/bundler_spec.rb +++ b/spec/bundler/bundler_spec.rb @@ -208,4 +208,66 @@ expect(Bundler.tmp_home_path("USER", "")).to eq(Pathname("/TMP/bundler/home/USER")) end end + + context "user cache dir" do + let(:home_path) { "/home/oggy" } + let(:xdg_cache_default) { "#{home_path}/.cache" } + let(:xdg_cache_custom) { "#{home_path}/User/cache" } + let(:fallback_dir) { "#{xdg_cache_default}/bundler" } + let(:cache_dir) { "#{xdg_cache_custom}/bundler" } + let(:legacy_cache_dir) { "#{home_path}/.bundle/cache" } + + describe "#xdg_home" do + before do + allow(Bundler.rubygems).to receive(:user_home).and_return(home_path) + allow(File).to receive(:writable?).with(home_path).and_return(true) + allow(File).to receive(:directory?).with(home_path).and_return(true) + allow(File).to receive(:directory?).with(fallback_dir).and_return(true) + allow(File).to receive(:directory?).with(cache_dir).and_return(true) + end + + before(:each) do + ENV.delete("XDG_CACHE_HOME") + end + + it "should use the value of XDG_CACHE_HOME" do + ENV["XDG_CACHE_HOME"] = xdg_cache_custom + expect(Bundler.xdg_home("CACHE", fallback_dir)).to eq(Pathname(cache_dir)) + end + + it "should fall back to the alternative directory" do + expect(Bundler.xdg_home("CACHE", xdg_cache_default)).to eq(Pathname(fallback_dir)) + end + end + + describe "#user_cache" do + before do + allow(Bundler.rubygems).to receive(:user_home).and_return(home_path) + allow(File).to receive(:writable?).with(home_path).and_return(true) + allow(File).to receive(:directory?).with(home_path).and_return(true) + end + + before(:each) do + ENV.delete("XDG_CACHE_HOME") + end + + it "should use ~/.bundle/cache if it exists" do + allow(FileTest).to receive(:exist?).with(legacy_cache_dir).and_return(true) + expect(Bundler.user_cache).to eq(Pathname(legacy_cache_dir)) + end + + it "should use XDG_CACHE_HOME if set" do + allow(FileTest).to receive(:exist?).with(legacy_cache_dir).and_return(false) + allow(FileTest).to receive(:exist?).with(cache_dir).and_return(true) + ENV["XDG_CACHE_HOME"] = xdg_cache_custom + expect(Bundler.user_cache).to eq(Pathname(cache_dir)) + end + + it "should use ~/.cache/bundler as default cache path" do + allow(FileTest).to receive(:exist?).with(legacy_cache_dir).and_return(false) + allow(FileTest).to receive(:exist?).with(fallback_dir).and_return(true) + expect(Bundler.user_cache).to eq(Pathname(fallback_dir)) + end + end + end end