Repository: pypa/pip

Issue:
pip search picks older version if returned list of versions are not ordered
* Pip version: 9.0.1
* Python version: 2.7
* Operating System: Ubuntu/CentOS

### Description:

For a list of versions returned by local pypi server that was ill-ordered like
```[{...'versions': ['1.0.249', '1.0.251', '1.0.250'], 'name':...}...]```

search picks the top element among all the versions returned to it.
```version = hit.get('versions', ['-'])[-1]```
 at https://github.com/pypa/pip/blob/9.0.1/pip/commands/search.py#L107 and https://github.com/pypa/pip/blob/9.0.1/pip/commands/search.py#L99

Rather it should do something like
```version = highest_version(hit.get('versions', ['-']))```




Discussion:

Resolving patch:
diff --git a/pip/commands/search.py b/pip/commands/search.py
--- a/pip/commands/search.py
+++ b/pip/commands/search.py
@@ -96,7 +96,7 @@ def print_results(hits, name_column_width=None, terminal_width=None):
         return
     if name_column_width is None:
         name_column_width = max([
-            len(hit['name']) + len(hit.get('versions', ['-'])[-1])
+            len(hit['name']) + len(highest_version(hit.get('versions', ['-'])))
             for hit in hits
         ]) + 4
 
@@ -104,7 +104,7 @@ def print_results(hits, name_column_width=None, terminal_width=None):
     for hit in hits:
         name = hit['name']
         summary = hit['summary'] or ''
-        version = hit.get('versions', ['-'])[-1]
+        latest = highest_version(hit.get('versions', ['-']))
         if terminal_width is not None:
             target_width = terminal_width - name_column_width - 5
             if target_width > 10:
@@ -113,13 +113,12 @@ def print_results(hits, name_column_width=None, terminal_width=None):
                 summary = ('\n' + ' ' * (name_column_width + 3)).join(summary)
 
         line = '%-*s - %s' % (name_column_width,
-                              '%s (%s)' % (name, version), summary)
+                              '%s (%s)' % (name, latest), summary)
         try:
             logger.info(line)
             if name in installed_packages:
                 dist = pkg_resources.get_distribution(name)
                 with indent_log():
-                    latest = highest_version(hit['versions'])
                     if dist.version == latest:
                         logger.info('INSTALLED: %s (latest)', dist.version)
                     else:


Test patch:
diff --git a/tests/functional/test_search.py b/tests/functional/test_search.py
--- a/tests/functional/test_search.py
+++ b/tests/functional/test_search.py
@@ -1,5 +1,6 @@
 import pytest
 from pip.commands.search import (highest_version,
+                                 print_results,
                                  transform_hits,
                                  SearchCommand)
 from pip.status_codes import NO_MATCHES_FOUND, SUCCESS
@@ -134,3 +135,25 @@ def test_search_exit_status_code_when_finds_no_package(script):
     """
     result = script.pip('search', 'nonexistentpackage', expect_error=True)
     assert result.returncode == NO_MATCHES_FOUND, result.returncode
+
+
+def test_search_print_results_should_contain_latest_versions(caplog):
+    """
+    Test that printed search results contain the latest package versions
+    """
+    hits = [
+        {
+            'name': 'testlib1',
+            'summary': 'Test library 1.',
+            'versions': ['1.0.5', '1.0.3']
+        },
+        {
+            'name': 'testlib2',
+            'summary': 'Test library 1.',
+            'versions': ['2.0.1', '2.0.3']
+        }
+    ]
+    print_results(hits)
+    log_messages = sorted([r.getMessage() for r in caplog.records()])
+    assert log_messages[0].startswith('testlib1 (1.0.5)')
+    assert log_messages[1].startswith('testlib2 (2.0.3)')