Skip to content

Commit 0b268d4

Browse files
Merge pull request #7645 from rubygems/deivid-rodriguez/user-install-symlinked-gem-home
Fix `gem uninstall --user-install` for symlinked HOME (cherry picked from commit 110cfd2)
1 parent 9aca2b5 commit 0b268d4

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/rubygems/commands/uninstall_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def uninstall_gem(gem_name)
184184
rescue Gem::GemNotInHomeException => e
185185
spec = e.spec
186186
alert("In order to remove #{spec.name}, please execute:\n" \
187-
"\tgem uninstall #{spec.name} --install-dir=#{spec.installation_path}")
187+
"\tgem uninstall #{spec.name} --install-dir=#{spec.base_dir}")
188188
rescue Gem::UninstallError => e
189189
spec = e.spec
190190
alert_error("Error: unable to successfully uninstall '#{spec.name}' which is " \

lib/rubygems/uninstaller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def initialize(gem, options = {})
5151
@version = options[:version] || Gem::Requirement.default
5252
@install_dir = options[:install_dir]
5353
@gem_home = File.realpath(@install_dir || Gem.dir)
54+
@user_dir = File.exist?(Gem.user_dir) ? File.realpath(Gem.user_dir) : Gem.user_dir
5455
@force_executables = options[:executables]
5556
@force_all = options[:all]
5657
@force_ignore = options[:ignore]
@@ -105,7 +106,7 @@ def uninstall
105106

106107
list, other_repo_specs = list.partition do |spec|
107108
@gem_home == spec.base_dir ||
108-
(@user_install && spec.base_dir == Gem.user_dir)
109+
(@user_install && spec.base_dir == @user_dir)
109110
end
110111

111112
list.sort!
@@ -239,7 +240,7 @@ def remove_all(list)
239240

240241
def remove(spec)
241242
unless path_ok?(@gem_home, spec) ||
242-
(@user_install && path_ok?(Gem.user_dir, spec))
243+
(@user_install && path_ok?(@user_dir, spec))
243244
e = Gem::GemNotInHomeException.new \
244245
"Gem '#{spec.full_name}' is not installed in directory #{@gem_home}"
245246
e.spec = spec

test/rubygems/test_gem_uninstaller.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def test_uninstall_not_ok
429429
end
430430

431431
def test_uninstall_user_install
432-
@user_spec = Gem::Specification.find_by_name "b"
432+
Gem::Specification.dirs = [Gem.user_dir]
433433

434434
uninstaller = Gem::Uninstaller.new(@user_spec.name,
435435
executables: true,
@@ -453,6 +453,32 @@ def test_uninstall_user_install
453453
assert_same uninstaller, @post_uninstall_hook_arg
454454
end
455455

456+
def test_uninstall_user_install_with_symlinked_home
457+
pend "Symlinks not supported or not enabled" unless symlink_supported?
458+
459+
Gem::Specification.dirs = [Gem.user_dir]
460+
461+
symlinked_home = File.join(@tempdir, "new-home")
462+
FileUtils.ln_s(Gem.user_home, symlinked_home)
463+
464+
ENV["HOME"] = symlinked_home
465+
Gem.instance_variable_set(:@user_home, nil)
466+
Gem.instance_variable_set(:@data_home, nil)
467+
468+
uninstaller = Gem::Uninstaller.new(@user_spec.name,
469+
executables: true,
470+
user_install: true,
471+
force: true)
472+
473+
gem_dir = File.join @user_spec.gem_dir
474+
475+
assert_path_exist gem_dir
476+
477+
uninstaller.uninstall
478+
479+
assert_path_not_exist gem_dir
480+
end
481+
456482
def test_uninstall_wrong_repo
457483
Dir.mkdir "#{@gemhome}2"
458484
Gem.use_paths "#{@gemhome}2", [@gemhome]

0 commit comments

Comments
 (0)