summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonan Scheffler <jonanscheffler@gmail.com>2014-04-03 00:13:35 -0700
committerAndre Arko <andre@arko.net>2014-04-07 15:28:00 +0900
commitc1b3fd165b2ec97fb254a76eaa3900bc4857a357 (patch)
treef01bb5d876c0762d29611ef0b44699b3effbbcf4
parentd7d0f7907179969b91c97e3ca75b8374ba27deaf (diff)
downloadbundler-c1b3fd165b2ec97fb254a76eaa3900bc4857a357.tar.gz
Print warning when bundler is run by root.
When a user runs bundle install with sudo bundler will print a warning, letting them know of potential consequences. closes #2936
-rw-r--r--lib/bundler/cli.rb18
-rw-r--r--spec/bundler/cli_spec.rb27
-rw-r--r--spec/support/helpers.rb11
3 files changed, 56 insertions, 0 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index df54c47f96..bf3af6460a 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -143,9 +143,27 @@ module Bundler
def install
require 'bundler/cli/install'
+ warn_if_root
Install.new(options.dup).run
end
+ no_commands {
+ def warn_if_root
+ warning = <<-W
+
+WARNING ****************************************************************
+Running bundler with sudo will likely have unintended consequences.
+If bundler requires you to run a command with sudo it will let you know.
+************************************************************************
+
+ W
+
+ if Process.uid == 0
+ puts warning
+ end
+ end
+ }
+
desc "update [OPTIONS]", "update the current environment"
long_desc <<-D
Update will install the newest versions of the gems listed in the Gemfile. Use
diff --git a/spec/bundler/cli_spec.rb b/spec/bundler/cli_spec.rb
index ca15290e3d..e0aef4a600 100644
--- a/spec/bundler/cli_spec.rb
+++ b/spec/bundler/cli_spec.rb
@@ -1,6 +1,33 @@
require 'spec_helper'
+require 'bundler/cli'
describe "bundle executable" do
+ let(:source_uri) { "http://localgemserver.test" }
+
+ context "#warn_if_root" do
+ it "warns the user when install is run as root" do
+ expect(Process).to receive(:uid).and_return(0)
+
+ gemfile <<-G
+ source "#{source_uri}"
+ G
+
+ warning = <<-W
+
+WARNING ****************************************************************
+Running bundler with sudo will likely have unintended consequences.
+If bundler requires you to run a command with sudo it will let you know.
+************************************************************************
+
+ W
+
+ output = capture_output {
+ Bundler::CLI.new.warn_if_root
+ }
+ expect(output).to include(warning)
+ end
+ end
+
it "returns non-zero exit status when passed unrecognized options" do
bundle '--invalid_argument', :exitstatus => true
expect(exitstatus).to_not be_zero
diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb
index 029b43aca6..865f3241a9 100644
--- a/spec/support/helpers.rb
+++ b/spec/support/helpers.rb
@@ -329,5 +329,16 @@ module Spec
def revision_for(path)
Dir.chdir(path) { `git rev-parse HEAD`.strip }
end
+
+ def capture_output
+ fake_stdout = StringIO.new
+ actual_stdout = $stdout
+ $stdout = fake_stdout
+ yield
+ fake_stdout.rewind
+ fake_stdout.read
+ ensure
+ $stdout = actual_stdout
+ end
end
end