Skip to content

Commit

Permalink
[rubygems/rubygems] Fix contradictory message about deletion of defau…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck authored and hsbt committed Jul 16, 2021
1 parent 29ed9d1 commit bbaebbf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
41 changes: 36 additions & 5 deletions lib/rubygems/uninstaller.rb
Expand Up @@ -70,6 +70,9 @@ def initialize(gem, options = {})
# only add user directory if install_dir is not set
@user_install = false
@user_install = options[:user_install] unless options[:install_dir]

# Optimization: populated during #uninstall
@default_specs_matching_uninstall_params = []
end

##
Expand Down Expand Up @@ -98,10 +101,8 @@ def uninstall
default_specs, list = list.partition do |spec|
spec.default_gem?
end

default_specs.each do |default_spec|
say "Gem #{default_spec.full_name} cannot be uninstalled because it is a default gem"
end
warn_cannot_uninstall_default_gems(default_specs - list)
@default_specs_matching_uninstall_params = default_specs

list, other_repo_specs = list.partition do |spec|
@gem_home == spec.base_dir or
Expand Down Expand Up @@ -273,7 +274,7 @@ def remove(spec)
end

safe_delete { FileUtils.rm_r gemspec }
say "Successfully uninstalled #{spec.full_name}"
announce_deletion_of(spec)

Gem::Specification.reset
end
Expand Down Expand Up @@ -376,4 +377,34 @@ def safe_delete(&block)

raise e
end

private

def announce_deletion_of(spec)
name = spec.full_name
say "Successfully uninstalled #{name}"
if default_spec_matches?(spec)
say(
"There was both a regular copy and a default copy of #{name}. The " \
"regular copy was successfully uninstalled, but the default copy " \
"was left around because default gems can't be removed."
)
end
end

# @return true if the specs of any default gems are `==` to the given `spec`.
def default_spec_matches?(spec)
!default_specs_that_match(spec).empty?
end

# @return [Array] specs of default gems that are `==` to the given `spec`.
def default_specs_that_match(spec)
@default_specs_matching_uninstall_params.select {|default_spec| spec == default_spec }
end

def warn_cannot_uninstall_default_gems(specs)
specs.each do |spec|
say "Gem #{spec.full_name} cannot be uninstalled because it is a default gem"
end
end
end
11 changes: 9 additions & 2 deletions test/rubygems/test_gem_uninstaller.rb
Expand Up @@ -295,8 +295,15 @@ def test_uninstall_default_gem_with_same_version

uninstaller = Gem::Uninstaller.new spec.name, :executables => true

uninstaller.uninstall

ui = Gem::MockGemUi.new "1\ny\n"
use_ui ui do
uninstaller.uninstall
end
expected = "Successfully uninstalled default-2\n" \
"There was both a regular copy and a default copy of default-2. The " \
"regular copy was successfully uninstalled, but the default copy " \
"was left around because default gems can't be removed.\n"
assert_equal expected, ui.output
assert_path_not_exist spec.gem_dir
end

Expand Down

0 comments on commit bbaebbf

Please sign in to comment.