Skip to content

Commit cdb8c9e

Browse files
deivid-rodriguezhsbt
authored andcommitted
[rubygems/rubygems] Improve error output when removing a source through gem sources
"Not present in cache" felt a bit unclear, so I changed the reason to: "No configured sources" or "source not present in configured sources", also pointing explicitly to the configuration file where RubyGems is looking for the source to be removed. ruby/rubygems@2bae554eff
1 parent 30b344c commit cdb8c9e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/rubygems/commands/sources_command.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,15 @@ def execute
204204
def remove_source(source_uri) # :nodoc:
205205
source = Gem::Source.new source_uri
206206

207-
if Gem.sources.include? source
207+
if configured_sources&.include? source
208208
Gem.sources.delete source
209209
Gem.configuration.write
210210

211211
say "#{source_uri} removed from sources"
212+
elsif configured_sources
213+
say "source #{source_uri} cannot be removed because it's not present in #{config_file_name}"
212214
else
213-
say "source #{source_uri} not present in cache"
215+
say "source #{source_uri} cannot be removed because there are no configured sources in #{config_file_name}"
214216
end
215217
end
216218

@@ -238,6 +240,13 @@ def remove_cache_file(desc, path) # :nodoc:
238240
private
239241

240242
def configured_sources
241-
Gem.configuration.sources
243+
return @configured_sources if defined?(@configured_sources)
244+
245+
configuration_sources = Gem.configuration.sources
246+
@configured_sources = Gem::SourceList.from(configuration_sources) if configuration_sources
247+
end
248+
249+
def config_file_name
250+
Gem.configuration.config_file_name
242251
end
243252
end

test/rubygems/test_gem_commands_sources_command.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,23 @@ def test_execute_remove_no_network
424424
end
425425

426426
def test_execute_remove_not_present
427+
Gem.configuration.sources = ["https://other.repo"]
428+
429+
@cmd.handle_options %W[--remove #{@new_repo}]
430+
431+
use_ui @ui do
432+
@cmd.execute
433+
end
434+
435+
expected = "source #{@new_repo} cannot be removed because it's not present in #{Gem.configuration.config_file_name}\n"
436+
437+
assert_equal expected, @ui.output
438+
assert_equal "", @ui.error
439+
ensure
440+
Gem.configuration.sources = nil
441+
end
442+
443+
def test_execute_remove_nothing_configured
427444
spec_fetcher
428445

429446
@cmd.handle_options %W[--remove https://does.not.exist]
@@ -432,7 +449,7 @@ def test_execute_remove_not_present
432449
@cmd.execute
433450
end
434451

435-
expected = "source https://does.not.exist not present in cache\n"
452+
expected = "source https://does.not.exist cannot be removed because there are no configured sources in #{Gem.configuration.config_file_name}\n"
436453

437454
assert_equal expected, @ui.output
438455
assert_equal "", @ui.error

0 commit comments

Comments
 (0)