diff --git a/lib/rubygems/spec_fetcher.rb b/lib/rubygems/spec_fetcher.rb index 7e5c1faa2ecf..ffb4a2be918e 100644 --- a/lib/rubygems/spec_fetcher.rb +++ b/lib/rubygems/spec_fetcher.rb @@ -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 diff --git a/test/rubygems/test_gem_commands_install_command.rb b/test/rubygems/test_gem_commands_install_command.rb index bf8bb92694d1..e6a9a9ce501f 100644 --- a/test/rubygems/test_gem_commands_install_command.rb +++ b/test/rubygems/test_gem_commands_install_command.rb @@ -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"