diff --git a/lib/bundler/cli/install.rb b/lib/bundler/cli/install.rb index 1f5337d46ee..256ae0a7d28 100644 --- a/lib/bundler/cli/install.rb +++ b/lib/bundler/cli/install.rb @@ -157,7 +157,7 @@ def run 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 f5286c9a221..c859a7677be 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -2,7 +2,7 @@ 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 640572ed6d7..ed5ac405e04 100644 --- a/spec/install/gems/sudo_spec.rb +++ b/spec/install/gems/sudo_spec.rb @@ -143,10 +143,36 @@ 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 49d0d6f1330..ae6df9e76dd 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -64,7 +64,9 @@ def 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__)