Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove underscores on suggest - updated for master #21

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/rubygems/spec_fetcher.rb
Expand Up @@ -148,20 +148,18 @@ def spec_for_dependency(dependency, matching_platform=true)
end

##
# Suggests a gem based on the supplied +gem_name+. Returns a string
# of the gem name if an approximate match can be found or nil
# otherwise. NOTE: for performance reasons only gems which exactly
# match the first character of +gem_name+ are considered.
# Suggests gems based on the supplied +gem_name+. Returns an array of
# alternative gem names.

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

matches = names.map { |n|
next unless n.match_platform?

distance = levenshtein_distance gem_name, n.name.downcase
distance = levenshtein_distance gem_name, n.name.downcase.tr('_-', '')

next if distance >= max

Expand Down
24 changes: 24 additions & 0 deletions test/rubygems/test_gem_commands_install_command.rb
Expand Up @@ -205,6 +205,30 @@ def test_execute_nonexistent_with_hint
assert_equal expected, @ui.error
end

def test_execute_nonexistent_with_dashes
misspelled = "non-existent_with-hint"
correctly_spelled = "nonexistent-with_hint"

util_setup_fake_fetcher
util_setup_spec_fetcher quick_spec(correctly_spelled, '2')

@cmd.options[:args] = [misspelled]

use_ui @ui do
e = assert_raises Gem::SystemExitException do
@cmd.execute
end

assert_equal 2, e.exit_code
end

expected = ["ERROR: Could not find a valid gem 'non-existent_with-hint' (>= 0) in any repository", "ERROR: Possible alternatives: nonexistent-with_hint"]

output = @ui.error.split "\n"

assert_equal expected, output
end

def test_execute_conflicting_install_options
@cmd.options[:user_install] = true
@cmd.options[:install_dir] = "whatever"
Expand Down