Skip to content

Commit

Permalink
[rubygems/rubygems] Make --build-root disable auto-user-install.
Browse files Browse the repository at this point in the history
  • Loading branch information
duckinator authored and hsbt committed Dec 7, 2023
1 parent 33bd956 commit 7d32830
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/rubygems/installer.rb
Expand Up @@ -675,14 +675,16 @@ def process_options # :nodoc:

@build_args = options[:build_args]

@gem_home = @install_dir
@gem_home ||= if options[:user_install]
Gem.user_dir
elsif !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.user_dir
else
Gem.dir
@gem_home = @install_dir || Gem.dir

# `--build-root` overrides `--user-install` and auto-user-install
if @build_root.nil? && @install_dir.nil?
if options[:user_install]
@gem_home = Gem.user_dir
elsif !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
end

# If the user has asked for the gem to be installed in a directory that is
Expand Down
20 changes: 20 additions & 0 deletions test/rubygems/test_gem_installer.rb
Expand Up @@ -1285,6 +1285,26 @@ def test_install_build_root
assert_equal @spec, installer.install
end

def test_install_build_root_when_gem_home_not_writable_does_not_fallback_to_user_install_inside_build_root
build_root = File.join(@tempdir, "build_root")

orig_gem_home = ENV.delete("GEM_HOME")

@gem = setup_base_gem

FileUtils.chmod "-w", @gemhome

installer = Gem::Installer.at @gem, :build_root => build_root

assert_equal @spec, installer.install

build_root_path = File.join(build_root, @gemhome.gsub(/^[a-zA-Z]:/, ""))
assert File.exist?(build_root_path), "gem not written to build_root"
ensure
FileUtils.chmod "+w", @gemhome
ENV["GEM_HOME"] = orig_gem_home
end

def test_install_missing_dirs
installer = setup_base_installer

Expand Down

0 comments on commit 7d32830

Please sign in to comment.