Skip to content

Commit 9281545

Browse files
voxikdeivid-rodriguez
authored andcommitted
Make sure --no-user-install is respected for auto user installation
The `options[:user_install]` might have three states: * `true`: `--user-install` * `false`: `--no-user-install` and * `nil`: option was not specified However, this had not been respected previously and the `false` and `nil` were treated the same. This could lead to auto user installation despite `--no-user-install` being specified on the command line. Fixes #7237
1 parent 57d458c commit 9281545

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/rubygems/installer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def process_options # :nodoc:
682682
if @build_root.nil?
683683
if options[:user_install]
684684
@gem_home = Gem.user_dir
685-
elsif !ENV.key?("GEM_HOME") && (File.exist?(Gem.dir) && !File.writable?(Gem.dir))
685+
elsif options[:user_install].nil? && !ENV.key?("GEM_HOME") && (File.exist?(Gem.dir) && !File.writable?(Gem.dir))
686686
say "Defaulting to user installation because default installation directory (#{Gem.dir}) is not writable."
687687
@gem_home = Gem.user_dir
688688
end

test/rubygems/test_gem_installer.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,27 @@ def test_process_options_fallback_to_user_install_when_gem_home_not_writable
19951995
ENV["GEM_HOME"] = orig_gem_home
19961996
end
19971997

1998+
def test_process_options_does_not_fallback_to_user_install_when_gem_home_not_writable_and_no_user_install
1999+
if Process.uid.zero?
2000+
pend("skipped in root privilege")
2001+
return
2002+
end
2003+
2004+
orig_gem_home = ENV.delete("GEM_HOME")
2005+
2006+
@gem = setup_base_gem
2007+
2008+
FileUtils.chmod 0o000, @gemhome
2009+
2010+
installer = use_ui(@ui) { Gem::Installer.at @gem, user_install: false }
2011+
2012+
assert_equal @gemhome, installer.gem_home
2013+
assert_empty @ui.output.strip
2014+
ensure
2015+
FileUtils.chmod 0o755, @gemhome
2016+
ENV["GEM_HOME"] = orig_gem_home
2017+
end
2018+
19982019
def test_shebang_arguments
19992020
load_relative "no" do
20002021
installer = setup_base_installer

0 commit comments

Comments
 (0)