Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def requires_sudo?
bin_dir = bin_dir.parent until bin_dir.exist?

# if any directory is not writable, we need sudo
files = [path, bin_dir] | Dir[path.join("build_info/*").to_s] | Dir[path.join("*").to_s]
files = [path, bin_dir] | Dir[bundle_path.join("build_info/*").to_s] | Dir[bundle_path.join("*").to_s]
sudo_needed = files.any? {|f| !File.writable?(f) }
end

Expand Down
67 changes: 67 additions & 0 deletions spec/bundler/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "bundler"
require "tmpdir"

RSpec.describe Bundler do
describe "#load_gemspec_uncached" do
Expand Down Expand Up @@ -228,6 +229,72 @@
end
end

describe "#requires_sudo?" do
let!(:tmpdir) { Dir.mktmpdir }
let(:bundle_path) { Pathname("#{tmpdir}/bundle") }

def clear_cached_requires_sudo
# Private in ruby 1.8.7
return unless Bundler.instance_variable_defined?(:@requires_sudo_ran)
Bundler.send(:remove_instance_variable, :@requires_sudo_ran)
Bundler.send(:remove_instance_variable, :@requires_sudo)
end

before do
clear_cached_requires_sudo
allow(Bundler).to receive(:which).with("sudo").and_return("/usr/bin/sudo")
allow(Bundler).to receive(:bundle_path).and_return(bundle_path)
end

after do
FileUtils.rm_rf(tmpdir)
clear_cached_requires_sudo
end

subject { Bundler.requires_sudo? }

context "bundle_path doesn't exist" do
it { should be false }

context "and parent dir can't be written" do
before do
FileUtils.chmod(0o500, tmpdir)
end

it { should be true }
end

context "with unwritable files in a parent dir" do
# Regression test for https://github.com/bundler/bundler/pull/6316
# It doesn't matter if there are other unwritable files so long as
# bundle_path can be created
before do
file = File.join(tmpdir, "unrelated_file")
FileUtils.touch(file)
FileUtils.chmod(0o400, file)
end

it { should be false }
end
end

context "bundle_path exists" do
before do
FileUtils.mkdir_p(bundle_path)
end

it { should be false }

context "and is unwritable" do
before do
FileUtils.chmod(0o500, bundle_path)
end

it { should be true }
end
end
end

context "user cache dir" do
let(:home_path) { Pathname.new(ENV["HOME"]) }

Expand Down