diff options
author | Bundlerbot <bot@bundler.io> | 2018-09-24 12:55:37 +0000 |
---|---|---|
committer | Colby Swandale <me@colby.fyi> | 2018-10-09 22:34:30 +1100 |
commit | 17572f0e883617ae9b222a856f151cd8f0cd1ed9 (patch) | |
tree | 9476e6924f26b28c1b274644465689c476d8a733 | |
parent | 949e557de0e0051f05540f2fc922766371af8a96 (diff) | |
download | bundler-17572f0e883617ae9b222a856f151cd8f0cd1ed9.tar.gz |
Merge #6316
6316: Display reason to require sudo r=colby-swandale a=okkez
This is useful for non-interactive installation with bundler.
### What was the end-user problem that led to this PR?
https://github.com/treasure-data/omnibus-td-agent/issues/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.
Co-authored-by: Kenji Okimoto <okimoto@clear-code.com>
(cherry picked from commit 1bd53e3d930e4f915db5536c68b1ed7282304045)
-rw-r--r-- | lib/bundler.rb | 6 | ||||
-rw-r--r-- | spec/bundler/bundler_spec.rb | 43 |
2 files changed, 48 insertions, 1 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index 5da316ec4d..2411ac20c2 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -368,7 +368,11 @@ EOF # 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 diff --git a/spec/bundler/bundler_spec.rb b/spec/bundler/bundler_spec.rb index 06435b9888..4759005c0c 100644 --- a/spec/bundler/bundler_spec.rb +++ b/spec/bundler/bundler_spec.rb @@ -374,6 +374,49 @@ EOF 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"]) } |