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
6 changes: 5 additions & 1 deletion lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,11 @@ def requires_sudo?

# if any directory is not writable, we need sudo
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) }
unwritable_files = files.reject {|f| File.writable?(f) }
sudo_needed = !unwritable_files.empty?
if sudo_needed
Bundler.ui.warn "Following files may not be writable, so sudo is needed:\n #{unwritable_files.sort.map(&:to_s).join("\n ")}"
end
end

@requires_sudo_ran = true
Expand Down
43 changes: 43 additions & 0 deletions spec/bundler/bundler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,49 @@ def clear_cached_requires_sudo
end
end

describe "#requires_sudo?" do
before do
allow(Bundler).to receive(:which).with("sudo").and_return("/usr/bin/sudo")
FileUtils.mkdir_p("tmp/vendor/bundle")
end
after do
FileUtils.rm_rf("tmp/vendor/bundle")
if Bundler.respond_to?(:remove_instance_variable)
Bundler.remove_instance_variable(:@requires_sudo_ran)
Bundler.remove_instance_variable(:@requires_sudo)
else
# TODO: Remove these code when Bundler drops Ruby 1.8.7 support
Bundler.send(:remove_instance_variable, :@requires_sudo_ran)
Bundler.send(:remove_instance_variable, :@requires_sudo)
end
end
context "writable paths" do
it "should return false and display nothing" do
allow(Bundler).to receive(:bundle_path).and_return(Pathname("tmp/vendor/bundle"))
expect(Bundler.ui).to_not receive(:warn)
expect(Bundler.requires_sudo?).to eq(false)
end
end
context "unwritable paths" do
before do
FileUtils.touch("tmp/vendor/bundle/unwritable1.txt")
FileUtils.touch("tmp/vendor/bundle/unwritable2.txt")
FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable1.txt")
FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable2.txt")
end
it "should return true and display warn message" do
allow(Bundler).to receive(:bundle_path).and_return(Pathname("tmp/vendor/bundle"))
message = <<-MESSAGE.chomp
Following files may not be writable, so sudo is needed:
tmp/vendor/bundle/unwritable1.txt
tmp/vendor/bundle/unwritable2.txt
MESSAGE
expect(Bundler.ui).to receive(:warn).with(message)
expect(Bundler.requires_sudo?).to eq(true)
end
end
end

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

Expand Down