[rubygems/rubygems] Simplify logic for Gem::PathSupport#home, and make GEM_HOME always overide it.

https://github.com/rubygems/rubygems/commit/64273fd7e3
This commit is contained in:
Ellen Marie Dash 2023-09-29 23:50:30 -04:00 committed by git
parent e84b73398b
commit c83f8ad867
3 changed files with 25 additions and 17 deletions

View File

@ -24,19 +24,24 @@ class Gem::PathSupport
# hashtable, or defaults to ENV, the system environment. # hashtable, or defaults to ENV, the system environment.
# #
def initialize(env) def initialize(env)
@home = default_home_dir(env) # Current implementation of @home, which is exposed as `Gem.paths.home`:
# 1. If `env["GEM_HOME"]` is defined in the environment: `env["GEM_HOME"]`.
# 2. If `Gem.default_dir` is writable OR it does not exist and it's parent
# directory is writable: `Gem.default_dir`.
# 3. Otherwise: `Gem.user_dir`.
# If @home (aka Gem.paths.home) exists, but we can't write to it, if env.key?("GEM_HOME")
# fall back to Gem.user_dir (the directory used for user installs). @home = normalize_home_dir(env["GEM_HOME"])
if File.exist?(@home) && !File.writable?(@home) elsif File.writable?(Gem.default_dir) || \
warn "The default GEM_HOME (#{@home}) is not" \ (!File.exist?(Gem.default_dir) && File.writable?(File.expand_path("..", Gem.default_dir)))
" 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: --install-dir #{Gem.user_dir}"
@home = Gem.user_dir @home = normalize_home_dir(Gem.default_dir)
else
# If `GEM_HOME` is not set AND we can't use `Gem.default_dir`,
# default to a user installation and print a message about this.
puts "Defaulting to user installation because default GEM_HOME (#{Gem.default_dir}) is not writable."
@home = normalize_home_dir(Gem.user_dir)
end end
@path = split_gem_path env["GEM_PATH"], @home @path = split_gem_path env["GEM_PATH"], @home
@ -52,9 +57,7 @@ class Gem::PathSupport
# The default home directory. # The default home directory.
# This function was broken out to accommodate tests in `bundler/spec/commands/doctor_spec.rb`. # This function was broken out to accommodate tests in `bundler/spec/commands/doctor_spec.rb`.
def default_home_dir(env) def normalize_home_dir(home)
home = env["GEM_HOME"] || Gem.default_dir
if File::ALT_SEPARATOR if File::ALT_SEPARATOR
home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR) home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
end end

View File

@ -40,8 +40,9 @@ RSpec.describe "bundle doctor" do
allow(File).to receive(:readable?).with(unwritable_file) { true } allow(File).to receive(:readable?).with(unwritable_file) { true }
# The following 2 lines are for `Gem::PathSupport#initialize`. # The following 2 lines are for `Gem::PathSupport#initialize`.
allow(File).to receive(:exist?).with(Gem.paths.send(:default_home_dir)) allow(File).to receive(:exist?).with(Gem.default_dir)
allow(File).to receive(:writable?).with(Gem.paths.send(:default_home_dir)) allow(File).to receive(:writable?).with(Gem.default_dir)
allow(File).to receive(:writable?).with(File.expand_path("..", Gem.default_dir))
end end
it "exits with no message if the installed gem has no C extensions" do it "exits with no message if the installed gem has no C extensions" do

View File

@ -165,6 +165,9 @@ class TestGemInstallUpdateOptions < Gem::InstallerTestCase
return return
end end
orig_gem_home = ENV["GEM_HOME"]
ENV.delete("GEM_HOME")
@spec = quick_gem "a" do |spec| @spec = quick_gem "a" do |spec|
util_make_exec spec util_make_exec spec
end end
@ -179,11 +182,12 @@ class TestGemInstallUpdateOptions < Gem::InstallerTestCase
FileUtils.chmod 0o755, @userhome FileUtils.chmod 0o755, @userhome
FileUtils.chmod 0o000, @gemhome FileUtils.chmod 0o000, @gemhome
Gem.use_paths @gemhome, @userhome Gem.use_paths nil, @userhome
assert_equal Gem.paths.home, Gem.user_dir assert_equal Gem.paths.home, Gem.user_dir
ensure ensure
FileUtils.chmod 0o755, @gemhome FileUtils.chmod 0o755, @gemhome
ENV["GEM_HOME"] = orig_gem_home if orig_gem_home
end end
def test_vendor def test_vendor