[rubygems/rubygems] Warn when trying to remove a default source that's the only configured sources

https://github.com/rubygems/rubygems/commit/ef78de5b69
This commit is contained in:
David Rodríguez 2025-09-01 20:27:30 +02:00 committed by Hiroshi SHIBATA
parent cdb8c9e254
commit cc2a70da27
2 changed files with 43 additions and 1 deletions

View File

@ -208,7 +208,11 @@ To remove a source use the --remove argument:
Gem.sources.delete source
Gem.configuration.write
say "#{source_uri} removed from sources"
if default_sources.include?(source) && configured_sources.one?
alert_warning "Removing a default source when it is the only source has no effect. Add a different source to #{config_file_name} if you want to stop using it as a source."
else
say "#{source_uri} removed from sources"
end
elsif configured_sources
say "source #{source_uri} cannot be removed because it's not present in #{config_file_name}"
else
@ -239,6 +243,10 @@ To remove a source use the --remove argument:
private
def default_sources
Gem::SourceList.from(Gem.default_sources)
end
def configured_sources
return @configured_sources if defined?(@configured_sources)

View File

@ -455,6 +455,40 @@ beta-gems.example.com is not a URI
assert_equal "", @ui.error
end
def test_remove_default_also_present_in_configuration
Gem.configuration.sources = [@gem_repo]
@cmd.handle_options %W[--remove #{@gem_repo}]
use_ui @ui do
@cmd.execute
end
expected = "WARNING: Removing a default source when it is the only source has no effect. Add a different source to #{Gem.configuration.config_file_name} if you want to stop using it as a source.\n"
assert_equal "", @ui.output
assert_equal expected, @ui.error
ensure
Gem.configuration.sources = nil
end
def test_remove_default_also_present_in_configuration_when_there_are_more_configured_sources
Gem.configuration.sources = [@gem_repo, "https://other.repo"]
@cmd.handle_options %W[--remove #{@gem_repo}]
use_ui @ui do
@cmd.execute
end
expected = "#{@gem_repo} removed from sources\n"
assert_equal expected, @ui.output
assert_equal "", @ui.error
ensure
Gem.configuration.sources = nil
end
def test_execute_remove_redundant_source_trailing_slash
repo_with_slash = "http://sample.repo/"