Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Use XDG directory for cache #5787

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/bundler.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
62 changes: 62 additions & 0 deletions spec/bundler/bundler_spec.rb
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be necessary, the spec helper already resets ENV before each test

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