Skip to content

Commit

Permalink
Merge pull request #6827 from cjerdonek/issue-6804-find-links-expansion
Browse files Browse the repository at this point in the history
Fix "~" expansion in --find-links paths
  • Loading branch information
pradyunsg authored Aug 4, 2019
2 parents 82dbcda + 56324a3 commit 3e73edd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
2 changes: 2 additions & 0 deletions news/6804.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a regression that caused ``~`` expansion not to occur in ``--find-links``
paths.
8 changes: 5 additions & 3 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ def make_search_scope(options, suppress_no_index=False):
)
index_urls = []

search_scope = SearchScope(
find_links=options.find_links,
index_urls=index_urls,
# Make sure find_links is a list before passing to create().
find_links = options.find_links or []

search_scope = SearchScope.create(
find_links=find_links, index_urls=index_urls,
)

return search_scope
Expand Down
57 changes: 48 additions & 9 deletions tests/unit/test_cmdoptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

import pretend
import pytest
from mock import patch

from pip._internal.cli.cmdoptions import (
_convert_python_version,
Expand All @@ -8,31 +11,67 @@


@pytest.mark.parametrize(
'no_index, suppress_no_index, expected_index_urls', [
(False, False, ['default_url', 'url1', 'url2']),
(False, True, ['default_url', 'url1', 'url2']),
(True, False, []),
'find_links, no_index, suppress_no_index, expected', [
(['link1'], False, False,
(['link1'], ['default_url', 'url1', 'url2'])),
(['link1'], False, True, (['link1'], ['default_url', 'url1', 'url2'])),
(['link1'], True, False, (['link1'], [])),
# Passing suppress_no_index=True suppresses no_index=True.
(True, True, ['default_url', 'url1', 'url2']),
(['link1'], True, True, (['link1'], ['default_url', 'url1', 'url2'])),
# Test options.find_links=False.
(False, False, False, ([], ['default_url', 'url1', 'url2'])),
],
)
def test_make_search_scope(no_index, suppress_no_index, expected_index_urls):
def test_make_search_scope(find_links, no_index, suppress_no_index, expected):
"""
:param expected: the expected index_urls value.
:param expected: the expected (find_links, index_urls) values.
"""
expected_find_links, expected_index_urls = expected
options = pretend.stub(
find_links=['link1'],
find_links=find_links,
index_url='default_url',
extra_index_urls=['url1', 'url2'],
no_index=no_index,
)
search_scope = make_search_scope(
options, suppress_no_index=suppress_no_index,
)
assert search_scope.find_links == ['link1']
assert search_scope.find_links == expected_find_links
assert search_scope.index_urls == expected_index_urls


@patch('pip._internal.utils.misc.expanduser')
def test_make_search_scope__find_links_expansion(mock_expanduser, tmpdir):
"""
Test "~" expansion in --find-links paths.
"""
# This is a mock version of expanduser() that expands "~" to the tmpdir.
def expand_path(path):
if path.startswith('~/'):
path = os.path.join(tmpdir, path[2:])
return path

mock_expanduser.side_effect = expand_path

options = pretend.stub(
find_links=['~/temp1', '~/temp2'],
index_url='default_url',
extra_index_urls=[],
no_index=False,
)
# Only create temp2 and not temp1 to test that "~" expansion only occurs
# when the directory exists.
temp2_dir = os.path.join(tmpdir, 'temp2')
os.mkdir(temp2_dir)

search_scope = make_search_scope(options)

# Only ~/temp2 gets expanded. Also, the path is normalized when expanded.
expected_temp2_dir = os.path.normcase(temp2_dir)
assert search_scope.find_links == ['~/temp1', expected_temp2_dir]
assert search_scope.index_urls == ['default_url']


@pytest.mark.parametrize('value, expected', [
('', (None, None)),
('2', ((2,), None)),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_unit_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_metadata_lines(self, name):
def _options():
''' Some default options that we pass to outdated.pip_version_check '''
return pretend.stub(
find_links=False, index_url='default_url', extra_index_urls=[],
find_links=[], index_url='default_url', extra_index_urls=[],
no_index=False, pre=False, trusted_hosts=False, cache_dir='',
)

Expand Down

0 comments on commit 3e73edd

Please sign in to comment.