ruby-changes:70759
From: ximenasandoval <ko1@a...>
Date: Thu, 6 Jan 2022 23:58:51 +0900 (JST)
Subject: [ruby-changes:70759] cea4a81056 (master): [rubygems/rubygems] Let fetch understand gem:version syntax
https://git.ruby-lang.org/ruby.git/commit/?id=cea4a81056 From cea4a81056311f13ca1c17f26c9d2c21b5a017a6 Mon Sep 17 00:00:00 2001 From: ximenasandoval <ximena.sandoval.dh@g...> Date: Wed, 29 Dec 2021 14:24:04 -0700 Subject: [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 --- lib/rubygems/commands/fetch_command.rb | 28 +++++-- 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 373851643d4..dd45fe20fdf 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 https://github.com/ruby/ruby/blob/trunk/lib/rubygems/commands/fetch_command.rb#L18 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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/commands/fetch_command.rb#L46 "#{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. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/commands/fetch_command.rb#L79 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 c745648d569..550c05b998f 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 https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_commands_fetch_command.rb#L157 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 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/