diff options
author | ximenasandoval <[email protected]> | 2021-12-29 14:24:04 -0700 |
---|---|---|
committer | git <[email protected]> | 2022-01-06 23:58:38 +0900 |
commit | cea4a81056311f13ca1c17f26c9d2c21b5a017a6 (patch) | |
tree | d8cdf026308c98bed4902a2a1a1ba7af12a0735d | |
parent | ee5e684bc1fda49bc28f0fcfc2f79abb26994f08 (diff) |
[rubygems/rubygems] Let fetch understand gem:version syntax
Fix version error message
Add tests to fetch error messages
Fix default version since is not necessary
https://github.com/rubygems/rubygems/commit/070620ebe4
-rw-r--r-- | lib/rubygems/commands/fetch_command.rb | 28 | ||||
-rw-r--r-- | test/rubygems/test_gem_commands_fetch_command.rb | 97 |
2 files changed, 118 insertions, 7 deletions
diff --git a/lib/rubygems/commands/fetch_command.rb b/lib/rubygems/commands/fetch_command.rb index 373851643d..dd45fe20fd 100644 --- a/lib/rubygems/commands/fetch_command.rb +++ b/lib/rubygems/commands/fetch_command.rb @@ -18,6 +18,10 @@ class Gem::Commands::FetchCommand < Gem::Command add_version_option add_platform_option add_prerelease_option + + add_option '--[no-]suggestions', 'Suggest alternates when gems are not found' do |value, options| + options[:suggest_alternate] = true + end end def arguments # :nodoc: @@ -42,15 +46,27 @@ then repackaging it. "#{program_name} GEMNAME [GEMNAME ...]" end + def check_version # :nodoc: + if options[:version] and options[:version] != Gem::Requirement.default and + get_all_gem_names.size > 1 + alert_error "Can't use --version with multiple gems. You can specify multiple gems with" \ + " version requirements using `gem fetch 'my_gem:1.0.0' 'my_other_gem:~>2.0.0'`" + terminate_interaction 1 + end + end + def execute - version = options[:version] || Gem::Requirement.default + check_version + version = options[:version] platform = Gem.platforms.last - gem_names = get_all_gem_names + gem_names = get_all_gem_names_and_versions - gem_names.each do |gem_name| - dep = Gem::Dependency.new gem_name, version + gem_names.each do |gem_name, gem_version| + gem_version ||= version + dep = Gem::Dependency.new gem_name, gem_version dep.prerelease = options[:prerelease] + suppress_suggestions = options[:suggest_alternate] specs_and_sources, errors = Gem::SpecFetcher.fetcher.spec_for_dependency dep @@ -63,12 +79,10 @@ then repackaging it. spec, source = specs_and_sources.max_by {|s,| s } if spec.nil? - show_lookup_failure gem_name, version, errors, options[:domain] + show_lookup_failure gem_name, gem_version, errors, suppress_suggestions, options[:domain] next end - source.download spec - say "Downloaded #{spec.full_name}" end end diff --git a/test/rubygems/test_gem_commands_fetch_command.rb b/test/rubygems/test_gem_commands_fetch_command.rb index c745648d56..550c05b998 100644 --- a/test/rubygems/test_gem_commands_fetch_command.rb +++ b/test/rubygems/test_gem_commands_fetch_command.rb @@ -157,4 +157,101 @@ class TestGemCommandsFetchCommand < Gem::TestCase assert_path_exist(File.join(@tempdir, a1.file_name), "#{a1.full_name} not fetched") end + + def test_execute_version_specified_by_colon + specs = spec_fetcher do |fetcher| + fetcher.gem 'a', 1 + end + + @cmd.options[:args] = %w[a:1] + + use_ui @ui do + Dir.chdir @tempdir do + @cmd.execute + end + end + + a1 = specs['a-1'] + + assert_path_exist(File.join(@tempdir, a1.file_name), + "#{a1.full_name} not fetched") + end + + def test_execute_two_version + @cmd.options[:args] = %w[a b] + @cmd.options[:version] = Gem::Requirement.new '1' + + use_ui @ui do + assert_raise Gem::MockGemUi::TermError, @ui.error do + @cmd.execute + end + end + + msg = "ERROR: Can't use --version with multiple gems. You can specify multiple gems with" \ + " version requirements using `gem fetch 'my_gem:1.0.0' 'my_other_gem:~>2.0.0'`" + + assert_empty @ui.output + assert_equal msg, @ui.error.chomp + end + + def test_execute_two_version_specified_by_colon + specs = spec_fetcher do |fetcher| + fetcher.gem 'a', 1 + fetcher.gem 'b', 1 + end + + @cmd.options[:args] = %w[a:1 b:1] + + use_ui @ui do + Dir.chdir @tempdir do + @cmd.execute + end + end + + a1 = specs['a-1'] + b1 = specs['b-1'] + + assert_path_exist(File.join(@tempdir, a1.file_name), + "#{a1.full_name} not fetched") + assert_path_exist(File.join(@tempdir, b1.file_name), + "#{b1.full_name} not fetched") + end + + def test_execute_version_nonexistent + spec_fetcher do |fetcher| + fetcher.spec 'foo', 1 + end + + @cmd.options[:args] = %w[foo:2] + + use_ui @ui do + @cmd.execute + end + + expected = <<-EXPECTED +ERROR: Could not find a valid gem 'foo' (2) in any repository +ERROR: Possible alternatives: foo + EXPECTED + + assert_equal expected, @ui.error + end + + def test_execute_nonexistent_hint_disabled + spec_fetcher do |fetcher| + fetcher.spec 'foo', 1 + end + + @cmd.options[:args] = %w[foo:2] + @cmd.options[:suggest_alternate] = true + + use_ui @ui do + @cmd.execute + end + + expected = <<-EXPECTED +ERROR: Could not find a valid gem 'foo' (2) in any repository + EXPECTED + + assert_equal expected, @ui.error + end end |