diff --git a/lib/rubygems/install_update_options.rb b/lib/rubygems/install_update_options.rb index 5c7289426e..15b0860fc9 100644 --- a/lib/rubygems/install_update_options.rb +++ b/lib/rubygems/install_update_options.rb @@ -185,9 +185,24 @@ module Gem::InstallUpdateOptions # Default options for the gem install and update commands. def install_update_options - { + default_options = { :document => %w[ri], } + + # If Gem.paths.home exists, but we can't write to it, + # fall back to a user installation. + if File.exist?(Gem.paths.home) && !File.writable?(Gem.paths.home) + default_options[:user_install] = true + + alert_warning "The default GEM_HOME (#{Gem.paths.home}) is not" \ + " writable, so rubygems is falling back to installing" \ + " under your home folder. To get rid of this warning" \ + " permanently either fix your GEM_HOME folder permissions" \ + " or add the following to your ~/.gemrc file:\n" \ + " gem: --user-install" + end + + default_options end ## diff --git a/test/rubygems/test_gem_install_update_options.rb b/test/rubygems/test_gem_install_update_options.rb index f3bd1c6d5e..7342def5e9 100644 --- a/test/rubygems/test_gem_install_update_options.rb +++ b/test/rubygems/test_gem_install_update_options.rb @@ -159,6 +159,33 @@ class TestGemInstallUpdateOptions < Gem::InstallerTestCase FileUtils.chmod 0o755, @gemhome end + def test_auto_user_install_unless_gem_home_writable + if Process.uid.zero? + pend("test_auto_user_install_unless_gem_home_writable test skipped in root privilege") + return + end + + @spec = quick_gem "a" do |spec| + util_make_exec spec + end + + util_build_gem @spec + @gem = @spec.cache_file + + @cmd.handle_options %w[] + + refute @cmd.options[:user_install] + + FileUtils.chmod 0755, @userhome + FileUtils.chmod 0000, @gemhome + + Gem.use_paths @gemhome, @userhome + + @cmd.install_update_options.include?(:user_install) + ensure + FileUtils.chmod 0755, @gemhome + end + def test_vendor vendordir(File.join(@tempdir, "vendor")) do @cmd.handle_options %w[--vendor]