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
Changes from 1 commit
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
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