Skip to content

Commit

Permalink
Retry with :prerelease when no suggestions are found
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalkr132 committed Aug 25, 2016
1 parent 0a99e91 commit 7c74e24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/rubygems/spec_fetcher.rb
Expand Up @@ -184,10 +184,10 @@ def spec_for_dependency(dependency, matching_platform=true)
# Suggests gems based on the supplied +gem_name+. Returns an array of
# alternative gem names.

def suggest_gems_from_name gem_name
def suggest_gems_from_name(gem_name, type: :latest)
gem_name = gem_name.downcase.tr('_-', '')
max = gem_name.size / 2
names = available_specs(:latest).first.values.flatten(1)
names = available_specs(type).first.values.flatten(1)

matches = names.map { |n|
next unless n.match_platform?
Expand All @@ -201,7 +201,11 @@ def suggest_gems_from_name gem_name
[n.name, distance]
}.compact

matches = matches.uniq.sort_by { |name, dist| dist }
matches = if matches.empty? && type != :prerelease
suggest_gems_from_name gem_name, type: :prerelease
else
matches.uniq.sort_by { |name, dist| dist }
end

matches.first(5).map { |name, dist| name }
end
Expand Down
20 changes: 20 additions & 0 deletions test/rubygems/test_gem_spec_fetcher.rb
Expand Up @@ -169,6 +169,26 @@ def src.fetch_spec(name)
assert_equal "bad news from the internet (#{@gem_repo})", sfp.error.message
end

def test_suggest_gems_from_name_latest
spec_fetcher do|fetcher|
fetcher.spec 'example', 1
fetcher.spec 'other-example', 1
end

suggestions = @sf.suggest_gems_from_name('examplw')
assert_equal ['example'], suggestions
end

def test_suggest_gems_from_name_prerelease
spec_fetcher do|fetcher|
fetcher.spec 'example', '1.a'
fetcher.spec 'other-example', 1
end

suggestions = @sf.suggest_gems_from_name('examplw')
assert_equal ['example'], suggestions
end

def test_available_specs_latest
spec_fetcher do |fetcher|
fetcher.spec 'a', 1
Expand Down

0 comments on commit 7c74e24

Please sign in to comment.