From 56324a3f388fdc2711e7479d50f55de93ca2afdd Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Tue, 30 Jul 2019 16:54:24 -0700 Subject: [PATCH] Fix "~" expansion in --find-links paths. --- news/6804.bugfix | 2 + src/pip/_internal/cli/cmdoptions.py | 8 ++-- tests/unit/test_cmdoptions.py | 57 ++++++++++++++++++++++++----- tests/unit/test_unit_outdated.py | 2 +- 4 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 news/6804.bugfix diff --git a/news/6804.bugfix b/news/6804.bugfix new file mode 100644 index 00000000000..f9599f9fda6 --- /dev/null +++ b/news/6804.bugfix @@ -0,0 +1,2 @@ +Fix a regression that caused ``~`` expansion not to occur in ``--find-links`` +paths. diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index ecf4d20bcd6..c5c6c22dcfd 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -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 diff --git a/tests/unit/test_cmdoptions.py b/tests/unit/test_cmdoptions.py index 8deb66a5f30..3cf2ba8b9ab 100644 --- a/tests/unit/test_cmdoptions.py +++ b/tests/unit/test_cmdoptions.py @@ -1,5 +1,8 @@ +import os + import pretend import pytest +from mock import patch from pip._internal.cli.cmdoptions import ( _convert_python_version, @@ -8,20 +11,24 @@ @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, @@ -29,10 +36,42 @@ def test_make_search_scope(no_index, suppress_no_index, expected_index_urls): 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)), diff --git a/tests/unit/test_unit_outdated.py b/tests/unit/test_unit_outdated.py index 5a8eb5c1e9f..a5d37f81868 100644 --- a/tests/unit/test_unit_outdated.py +++ b/tests/unit/test_unit_outdated.py @@ -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='', )