Skip to content

Commit

Permalink
Fix gem uninstall --user-install for symlinked HOME's
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed May 13, 2024
1 parent c3f827f commit 2f3b98b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rubygems/uninstaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def initialize(gem, options = {})
@gem = gem
@version = options[:version] || Gem::Requirement.default
@gem_home = File.realpath(options[:install_dir] || Gem.dir)
@user_dir = File.exist?(Gem.user_dir) ? File.realpath(Gem.user_dir) : Gem.user_dir
@force_executables = options[:executables]
@force_all = options[:all]
@force_ignore = options[:ignore]
Expand Down Expand Up @@ -104,7 +105,7 @@ def uninstall

list, other_repo_specs = list.partition do |spec|
@gem_home == spec.base_dir ||
(@user_install && spec.base_dir == Gem.user_dir)
(@user_install && spec.base_dir == @user_dir)
end

list.sort!
Expand Down Expand Up @@ -238,7 +239,7 @@ def remove_all(list)

def remove(spec)
unless path_ok?(@gem_home, spec) ||
(@user_install && path_ok?(Gem.user_dir, spec))
(@user_install && path_ok?(@user_dir, spec))
e = Gem::GemNotInHomeException.new \
"Gem '#{spec.full_name}' is not installed in directory #{@gem_home}"
e.spec = spec
Expand Down
26 changes: 26 additions & 0 deletions test/rubygems/test_gem_uninstaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,32 @@ def test_uninstall_user_install
assert_same uninstaller, @post_uninstall_hook_arg
end

def test_uninstall_user_install_with_symlinked_home
pend "Symlinks not supported or not enabled" unless symlink_supported?

Gem::Specification.dirs = [Gem.user_dir]

symlinked_home = File.join(@tempdir, "new-home")
FileUtils.ln_s(Gem.user_home, symlinked_home)

ENV["HOME"] = symlinked_home
Gem.instance_variable_set(:@user_home, nil)
Gem.instance_variable_set(:@data_home, nil)

uninstaller = Gem::Uninstaller.new(@user_spec.name,
executables: true,
user_install: true,
force: true)

gem_dir = File.join @user_spec.gem_dir

assert_path_exist gem_dir

uninstaller.uninstall

assert_path_not_exist gem_dir
end

def test_uninstall_wrong_repo
Dir.mkdir "#{@gemhome}2"
Gem.use_paths "#{@gemhome}2", [@gemhome]
Expand Down

0 comments on commit 2f3b98b

Please sign in to comment.