Skip to content

Commit

Permalink
[rubygems/rubygems] Make sure --no-user-install is respected for au…
Browse files Browse the repository at this point in the history
…to 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 rubygems/rubygems#7237

rubygems/rubygems@9281545474
  • Loading branch information
voxik authored and hsbt committed Dec 13, 2023
1 parent 844759c commit 402fd96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubygems/installer.rb
Expand Up @@ -682,7 +682,7 @@ def process_options # :nodoc:
if @build_root.nil?
if options[:user_install]
@gem_home = Gem.user_dir
elsif !ENV.key?("GEM_HOME") && (File.exist?(Gem.dir) && !File.writable?(Gem.dir))
elsif options[:user_install].nil? && !ENV.key?("GEM_HOME") && (File.exist?(Gem.dir) && !File.writable?(Gem.dir))
say "Defaulting to user installation because default installation directory (#{Gem.dir}) is not writable."
@gem_home = Gem.user_dir
end
Expand Down
21 changes: 21 additions & 0 deletions test/rubygems/test_gem_installer.rb
Expand Up @@ -1995,6 +1995,27 @@ def test_process_options_fallback_to_user_install_when_gem_home_not_writable
ENV["GEM_HOME"] = orig_gem_home
end

def test_process_options_does_not_fallback_to_user_install_when_gem_home_not_writable_and_no_user_install
if Process.uid.zero?
pend("skipped in root privilege")
return
end

orig_gem_home = ENV.delete("GEM_HOME")

@gem = setup_base_gem

FileUtils.chmod 0o000, @gemhome

installer = use_ui(@ui) { Gem::Installer.at @gem, user_install: false }

assert_equal @gemhome, installer.gem_home
assert_empty @ui.output.strip
ensure
FileUtils.chmod 0o755, @gemhome
ENV["GEM_HOME"] = orig_gem_home
end

def test_shebang_arguments
load_relative "no" do
installer = setup_base_installer
Expand Down

0 comments on commit 402fd96

Please sign in to comment.