Skip to content

Commit daf13d1

Browse files
martinemdedeivid-rodriguez
authored andcommitted
Merge pull request #8083 from duckinator/duckinator/simplify-suggest-gems-from-name
Remove code that makes suggest_gems_from_name give worse results. (cherry picked from commit 74a8893)
1 parent e8880f1 commit daf13d1

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

lib/rubygems/spec_fetcher.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,12 @@ def suggest_gems_from_name(gem_name, type = :latest, num_results = 5)
176176

177177
matches = names.map do |n|
178178
next unless n.match_platform?
179-
[n.name, 0] if n.name.downcase.tr("_-", "").include?(gem_name)
179+
distance = levenshtein_distance gem_name, n.name.downcase.tr("_-", "")
180+
next if distance >= max
181+
return [n.name] if distance == 0
182+
[n.name, distance]
180183
end.compact
181184

182-
if matches.length < num_results
183-
matches += names.map do |n|
184-
next unless n.match_platform?
185-
distance = levenshtein_distance gem_name, n.name.downcase.tr("_-", "")
186-
next if distance >= max
187-
return [n.name] if distance == 0
188-
[n.name, distance]
189-
end.compact
190-
end
191-
192185
matches = if matches.empty? && type != :prerelease
193186
suggest_gems_from_name gem_name, :prerelease
194187
else

test/rubygems/test_gem_commands_exec_command.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,6 @@ def test_version_mismatch
493493
assert_equal 2, e.exit_code
494494
assert_equal <<~ERR, @ui.error
495495
ERROR: Could not find a valid gem 'a' (= 2) in any repository
496-
ERROR: Possible alternatives: a
497496
ERR
498497
end
499498
end
@@ -574,7 +573,6 @@ def test_conservative_missing_gem
574573
assert_include @ui.output, "a (= 2) not available locally"
575574
assert_equal <<~ERROR, @ui.error
576575
ERROR: Could not find a valid gem 'a' (= 2) in any repository
577-
ERROR: Possible alternatives: a
578576
ERROR
579577
end
580578
end
@@ -769,8 +767,7 @@ def test_only_prerelease_available
769767
assert_raise Gem::MockGemUi::TermError do
770768
invoke "a"
771769
end
772-
assert_equal "ERROR: Could not find a valid gem 'a' (>= 0) in any repository\n" \
773-
"ERROR: Possible alternatives: a\n", @ui.error
770+
assert_equal "ERROR: Could not find a valid gem 'a' (>= 0) in any repository\n", @ui.error
774771
assert_empty @ui.output
775772
assert_empty @installed_specs
776773
end

test/rubygems/test_gem_spec_fetcher.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,22 @@ def src.fetch_spec(name)
168168
def test_suggest_gems_from_name_latest
169169
spec_fetcher do|fetcher|
170170
fetcher.spec "example", 1
171-
fetcher.spec "other-example", 1
171+
fetcher.spec "an-example", 1
172172
fetcher.spec "examp", 1
173+
fetcher.spec "other-example", 1
173174
end
174175

175176
suggestions = @sf.suggest_gems_from_name("examplw", :latest, 1)
176177
assert_equal ["example"], suggestions
177178

178-
suggestions = @sf.suggest_gems_from_name("other")
179-
assert_equal ["other-example"], suggestions
179+
suggestions = @sf.suggest_gems_from_name("anexample")
180+
assert_equal ["an-example"], suggestions
180181

181-
suggestions = @sf.suggest_gems_from_name("exam")
182-
assert suggestions.any? { ["examp"] }
183-
assert suggestions.any? { ["example"] }
184-
assert suggestions.any? { ["other-example"] }
182+
suggestions = @sf.suggest_gems_from_name("xample")
183+
assert_equal ["example"], suggestions
184+
185+
suggestions = @sf.suggest_gems_from_name("other-apple")
186+
assert_equal ["other-example"], suggestions
185187
end
186188

187189
def test_suggest_gems_from_name_prerelease

0 commit comments

Comments
 (0)