From 092a48de7ef905e393abe3369a37e558333a5039 Mon Sep 17 00:00:00 2001 From: Ellen Marie Dash Date: Thu, 31 Oct 2024 19:56:55 -0400 Subject: [PATCH] [rubygems/rubygems] [SpecFetcher] If candidates include {name}-ruby or ruby-{name}, recommend those. https://github.com/rubygems/rubygems/commit/d7d33172c1 --- lib/rubygems/spec_fetcher.rb | 21 +++++++++++++++----- test/rubygems/test_gem_spec_fetcher.rb | 27 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/rubygems/spec_fetcher.rb b/lib/rubygems/spec_fetcher.rb index cf92dd73ee..285f117b39 100644 --- a/lib/rubygems/spec_fetcher.rb +++ b/lib/rubygems/spec_fetcher.rb @@ -182,20 +182,31 @@ class Gem::SpecFetcher min_length = gem_name.length - max max_length = gem_name.length + max + gem_name_with_postfix = "#{gem_name}ruby" + gem_name_with_prefix = "ruby#{gem_name}" + matches = names.filter_map do |n| len = n.name.length - # If the length is min_length or shorter, we've done `max` deletions. - # If the length is max_length or longer, we've done `max` insertions. - # These would both be rejected later, so we skip early for performance. - next if len <= min_length || len >= max_length - # If the gem doesn't support the current platform, bail early. next unless n.match_platform? + # If the length is min_length or shorter, we've done `max` deletions. + # This would be rejected later, so we skip it for performance. + next if len <= min_length + # The candidate name, normalized the same as gem_name. normalized_name = n.name.downcase normalized_name.tr!("_-", "") + # If the gem is "{NAME}-ruby" and "ruby-{NAME}", we want to return it. + # But we already removed hyphens, so we check "{NAME}ruby" and "ruby{NAME}". + next [n.name, 0] if normalized_name == gem_name_with_postfix + next [n.name, 0] if normalized_name == gem_name_with_prefix + + # If the length is max_length or longer, we've done `max` insertions. + # This would be rejected later, so we skip it for performance. + next if len >= max_length + # If we found an exact match (after stripping underscores and hyphens), # that's our most likely candidate. # Return it immediately, and skip the rest of the loop. diff --git a/test/rubygems/test_gem_spec_fetcher.rb b/test/rubygems/test_gem_spec_fetcher.rb index 9608d93c7b..1f7b5984c3 100644 --- a/test/rubygems/test_gem_spec_fetcher.rb +++ b/test/rubygems/test_gem_spec_fetcher.rb @@ -199,6 +199,33 @@ class TestGemSpecFetcher < Gem::TestCase assert_equal ["example"], suggestions end + def test_suggest_gems_from_name_prefix_or_suffix + spec_fetcher do|fetcher| + fetcher.spec "example-one-ruby", 1 + fetcher.spec "example-one-rrrr", 1 + fetcher.spec "ruby-example-two", 1 + fetcher.spec "rrrr-example-two", 1 + end + + suggestions = @sf.suggest_gems_from_name("example-one") + assert_equal ["example-one-ruby"], suggestions + + suggestions = @sf.suggest_gems_from_name("example-two") + assert_equal ["ruby-example-two"], suggestions + + suggestions = @sf.suggest_gems_from_name("exampleone") + assert_equal ["example-one-ruby"], suggestions + + suggestions = @sf.suggest_gems_from_name("exampletwo") + assert_equal ["ruby-example-two"], suggestions + + suggestions = @sf.suggest_gems_from_name("example---one") + assert_equal ["example-one-ruby"], suggestions + + suggestions = @sf.suggest_gems_from_name("example---two") + assert_equal ["ruby-example-two"], suggestions + end + def test_available_specs_latest spec_fetcher do |fetcher| fetcher.spec "a", 1