diff options
author | Homu <homu@barosl.com> | 2015-11-22 04:27:33 +0900 |
---|---|---|
committer | Homu <homu@barosl.com> | 2015-11-22 04:27:33 +0900 |
commit | 3720418f362aac4f5da9c0a510fdb838c37708bd (patch) | |
tree | 6650861ca9e0794bb6df3ae5136466db4ba66021 | |
parent | 771714745965c4a8d1a9c52266919862b5e4ce3e (diff) | |
parent | b00e4daa160e2ea74d741bb60511bae07f4b8aad (diff) | |
download | bundler-3720418f362aac4f5da9c0a510fdb838c37708bd.tar.gz |
Auto merge of #4113 - blackxored:silence-root-warning, r=segiddins
Check silence_root_warning option to skip warning on root install cmds
Adds a new setting `silence_root_warning` (environment variable: `BUNDLE_SILENCE_ROOT_WARNING`) that skips the default warning when `install` is run as root.
The warning is useful but I think it should be able to be disabled if the user knows what he's doing, it's handy when run inside Docker containers (which by default run as root), or other environments where root is used.
Also adds a new `preserve_env` option to `bundle` helper in tests which allows running commands under sudo with the `-E` flag (preserve environment) as opposed to the default env_reset. Without this, the command would be run without our flag which makes it impossible to test. There might be cleaner ways to achieve this, though.
-rw-r--r-- | lib/bundler/cli/install.rb | 2 | ||||
-rw-r--r-- | lib/bundler/settings.rb | 2 | ||||
-rw-r--r-- | spec/install/gems/sudo_spec.rb | 30 | ||||
-rw-r--r-- | spec/support/helpers.rb | 4 |
4 files changed, 33 insertions, 5 deletions
diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 1f5337d46e..256ae0a7d2 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -157,7 +157,7 @@ module Bundler private def warn_if_root - return if Bundler::WINDOWS || !Process.uid.zero? + return if Bundler.settings[:silence_root_warning] || Bundler::WINDOWS || !Process.uid.zero? Bundler.ui.warn "Don't run Bundler as root. Bundler can ask for sudo " \ "if it is needed, and installing your bundle as root will break this " \ "application for all non-root users on this machine.", :wrap => true diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb index f5286c9a22..c859a7677b 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -2,7 +2,7 @@ require "uri" module Bundler class Settings - BOOL_KEYS = %w(frozen cache_all no_prune disable_local_branch_check ignore_messages gem.mit gem.coc).freeze + BOOL_KEYS = %w(frozen cache_all no_prune disable_local_branch_check ignore_messages gem.mit gem.coc silence_root_warning).freeze NUMBER_KEYS = %w(retry timeout redirect ssl_verify_mode).freeze DEFAULT_CONFIG = { :retry => 3, :timeout => 10, :redirect => 5 } diff --git a/spec/install/gems/sudo_spec.rb b/spec/install/gems/sudo_spec.rb index 640572ed6d..ed5ac405e0 100644 --- a/spec/install/gems/sudo_spec.rb +++ b/spec/install/gems/sudo_spec.rb @@ -143,10 +143,36 @@ describe "when using sudo", :sudo => true do end describe "and root runs install" do - it "warns against that" do + let(:warning) { "Don't run Bundler as root." } + + before do gemfile %|source "file://#{gem_repo1}"| + end + + it "warns against that" do bundle :install, :sudo => true - expect(out).to include("Don't run Bundler as root.") + expect(out).to include(warning) + end + + context "when ENV['BUNDLE_SILENCE_ROOT_WARNING'] is set" do + it "skips the warning" do + bundle :install, :sudo => :preserve_env, :env => { "BUNDLE_SILENCE_ROOT_WARNING" => true } + expect(out).to_not include(warning) + end + end + + context "when silence_root_warning is passed as an option" do + it "skips the warning" do + bundle :install, :sudo => true, :silence_root_warning => true + expect(out).to_not include(warning) + end + end + + context "when silence_root_warning = false" do + it "warns against that" do + bundle :install, :sudo => true, :silence_root_warning => false + expect(out).to include(warning) + end end end end diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index 49d0d6f133..ae6df9e76d 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -64,7 +64,9 @@ module Spec def bundle(cmd, options = {}) expect_err = options.delete(:expect_err) - sudo = "sudo" if options.delete(:sudo) + with_sudo = options.delete(:sudo) + sudo = with_sudo == :preserve_env ? "sudo -E" : "sudo" if with_sudo + options["no-color"] = true unless options.key?("no-color") || %w(exec conf).include?(cmd.to_s[0..3]) bundle_bin = File.expand_path("../../../exe/bundle", __FILE__) |