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

Commit

Permalink
Auto merge of #6316 - okkez:display-reason, r=segiddins
Browse files Browse the repository at this point in the history
Display reason to require sudo

This is useful for non-interactive installation with bundler.

### What was the end-user problem that led to this PR?

treasure-data/omnibus-td-agent#166

I could not notice that bundler needs sudo privilege from logs.
So I checked bundler code.

### What was your diagnosis of the problem?

Bundler does not show the reason to need sudo privilege.

### What is your fix for the problem, implemented in this PR?

Display reason to require sudo.

### Why did you choose this fix out of the possible options?

If bundler displays reason to require sudo, we can notice permission problems as soon as possible.
  • Loading branch information
bundlerbot committed Sep 10, 2018
2 parents 02af3c2 + 025110c commit 7882798
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/bundler.rb
Expand Up @@ -368,7 +368,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
Expand Up @@ -374,6 +374,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

0 comments on commit 7882798

Please sign in to comment.