Skip to content

Commit

Permalink
Don't fallback to tempdir when required directories exist.
Browse files Browse the repository at this point in the history
When home directory is not writable, but the required .gem and .bundle
are, we should use them instead of falling back to use tempdirs.

This creates a workaround for more restrictive setups using Omnibus
Docker or any hardened setup, to overcome the annoyances introduced by rubygems#4951.
  • Loading branch information
brodock committed May 23, 2018
1 parent c793c38 commit 5162c94
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ def ruby_scope
def user_home
@user_home ||= begin
home = Bundler.rubygems.user_home
user_dirs = home ? %w[.bundle .gem].map {|path| File.join(home, path) } : []

warning = if home.nil?
"Your home directory is not set."
elsif !File.directory?(home)
"`#{home}` is not a directory."
elsif !File.writable?(home)
elsif !File.writable?(home) && (user_dirs.any? {|path| !File.directory?(path) || !File.writable?(path) } || user_dirs.empty?)
"`#{home}` is not writable."
end

Expand Down
54 changes: 54 additions & 0 deletions spec/bundler/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,60 @@
allow(File).to receive(:writable?).with(path).and_return true
expect(Bundler.user_home).to eq(Pathname(path))
end

context "is not a directory" do
it "should issue a warning and return a temporary user home" do
path = "/home/oggy"
allow(Bundler.rubygems).to receive(:user_home).and_return(path)
allow(File).to receive(:directory?).with(path).and_return false
allow(Etc).to receive(:getlogin).and_return("USER")
allow(Dir).to receive(:tmpdir).and_return("/TMP")
allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true)
expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER")
message = <<~EOF
`/home/oggy` is not a directory.
Bundler will use `/TMP/bundler/home/USER' as your home directory temporarily.
EOF
expect(Bundler.ui).to receive(:warn).with(message)
expect(Bundler.user_home).to eq(Pathname("/TMP/bundler/home/USER"))
end
end

context "is not writable" do
let(:path) { "/home/oggy" }
let(:dotbundle) { "/home/oggy/.bundle" }
let(:dotgem) { "/home/oggy/.gem" }

it "should issue a warning and return a temporary user home" do
allow(Bundler.rubygems).to receive(:user_home).and_return(path)
allow(File).to receive(:directory?).with(path).and_return true
allow(File).to receive(:writable?).with(path).and_return false
allow(File).to receive(:directory?).with(dotbundle).and_return false
allow(Etc).to receive(:getlogin).and_return("USER")
allow(Dir).to receive(:tmpdir).and_return("/TMP")
allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true)
expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER")
message = <<~EOF
`/home/oggy` is not writable.
Bundler will use `/TMP/bundler/home/USER' as your home directory temporarily.
EOF
expect(Bundler.ui).to receive(:warn).with(message)
expect(Bundler.user_home).to eq(Pathname("/TMP/bundler/home/USER"))
end

context ".gem and .bundle exists and have correct permissions" do
it "should return the user home" do
allow(Bundler.rubygems).to receive(:user_home).and_return(path)
allow(File).to receive(:directory?).with(path).and_return true
allow(File).to receive(:writable?).with(path).and_return false
allow(File).to receive(:directory?).with(dotbundle).and_return true
allow(File).to receive(:writable?).with(dotbundle).and_return true
allow(File).to receive(:directory?).with(dotgem).and_return true
allow(File).to receive(:writable?).with(dotgem).and_return true
expect(Bundler.user_home).to eq(Pathname(path))
end
end
end
end

context "home directory is not set" do
Expand Down

0 comments on commit 5162c94

Please sign in to comment.