[rubygems/rubygems] [SpecFetcher] If candidates include {name}-ruby or ruby-{name}, recommend those.

https://github.com/rubygems/rubygems/commit/d7d33172c1
This commit is contained in:
Ellen Marie Dash 2024-10-31 19:56:55 -04:00 committed by git
parent 8f9b9aecd0
commit 092a48de7e
2 changed files with 43 additions and 5 deletions

View File

@ -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.

View File

@ -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