From 2d450936c6644a6f3d5d69953571046730c686ae Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sun, 26 May 2019 21:53:59 -0700 Subject: [PATCH] Move path_to_url() from download.py to misc.py. --- src/pip/_internal/download.py | 15 ++------------- src/pip/_internal/utils/misc.py | 12 ++++++++++++ tests/unit/test_download.py | 17 ----------------- tests/unit/test_utils.py | 21 +++++++++++++++++++-- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index 0dd22dd1e..38a67c3cc 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -37,8 +37,8 @@ from pip._internal.utils.glibc import libc_ver from pip._internal.utils.misc import ( ARCHIVE_EXTENSIONS, ask, ask_input, ask_password, ask_path_exists, backup_dir, consume, display_path, format_size, get_installed_version, - remove_auth_from_url, rmtree, split_auth_netloc_from_url, splitext, - unpack_file, + path_to_url, remove_auth_from_url, rmtree, split_auth_netloc_from_url, + splitext, unpack_file, ) from pip._internal.utils.temp_dir import TempDirectory from pip._internal.utils.typing import MYPY_CHECK_RUNNING @@ -693,17 +693,6 @@ def url_to_path(url): return path -def path_to_url(path): - # type: (Union[str, Text]) -> str - """ - Convert a path to a file: URL. The path will be made absolute and have - quoted path parts. - """ - path = os.path.normpath(os.path.abspath(path)) - url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path)) - return url - - def is_archive_file(name): # type: (str) -> bool """Return True if `name` is a considered as an archive file.""" diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index 024f4e750..3d7b8be86 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -25,6 +25,7 @@ from pip._vendor.retrying import retry # type: ignore from pip._vendor.six import PY2 from pip._vendor.six.moves import input, shlex_quote from pip._vendor.six.moves.urllib import parse as urllib_parse +from pip._vendor.six.moves.urllib import request as urllib_request from pip._vendor.six.moves.urllib.parse import unquote as urllib_unquote from pip._internal.exceptions import CommandError, InstallationError @@ -931,6 +932,17 @@ def enum(*sequential, **named): return type('Enum', (), enums) +def path_to_url(path): + # type: (Union[str, Text]) -> str + """ + Convert a path to a file: URL. The path will be made absolute and have + quoted path parts. + """ + path = os.path.normpath(os.path.abspath(path)) + url = urllib_parse.urljoin('file:', urllib_request.pathname2url(path)) + return url + + def split_auth_from_netloc(netloc): """ Parse out and remove the auth information from a netloc. diff --git a/tests/unit/test_download.py b/tests/unit/test_download.py index de86fcf03..17ee7c62c 100644 --- a/tests/unit/test_download.py +++ b/tests/unit/test_download.py @@ -8,7 +8,6 @@ from tempfile import mkdtemp import pytest from mock import Mock, patch -from pip._vendor.six.moves.urllib import request as urllib_request import pip from pip._internal.download import ( @@ -199,22 +198,6 @@ def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file): rmtree(download_dir) -@pytest.mark.skipif("sys.platform == 'win32'") -def test_path_to_url_unix(): - assert path_to_url('/tmp/file') == 'file:///tmp/file' - path = os.path.join(os.getcwd(), 'file') - assert path_to_url('file') == 'file://' + urllib_request.pathname2url(path) - - -@pytest.mark.skipif("sys.platform != 'win32'") -def test_path_to_url_win(): - assert path_to_url('c:/tmp/file') == 'file:///C:/tmp/file' - assert path_to_url('c:\\tmp\\file') == 'file:///C:/tmp/file' - assert path_to_url(r'\\unc\as\path') == 'file://unc/as/path' - path = os.path.join(os.getcwd(), 'file') - assert path_to_url('file') == 'file:' + urllib_request.pathname2url(path) - - @pytest.mark.parametrize("url,win_expected,non_win_expected", [ ('file:tmp', 'tmp', 'tmp'), ('file:c:/path/to/file', r'C:\path\to\file', 'c:/path/to/file'), diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 1d97e65a1..c42ce6fd9 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -18,6 +18,7 @@ from logging import DEBUG, ERROR, INFO, WARNING import pytest from mock import Mock, patch +from pip._vendor.six.moves.urllib import request as urllib_request from pip._internal.exceptions import ( HashMismatch, HashMissing, InstallationError, @@ -27,8 +28,8 @@ from pip._internal.utils.glibc import check_glibc_version from pip._internal.utils.hashes import Hashes, MissingHashes from pip._internal.utils.misc import ( call_subprocess, egg_link_path, ensure_dir, format_command_args, - get_installed_distributions, get_prog, normalize_path, redact_netloc, - redact_password_from_url, remove_auth_from_url, rmtree, + get_installed_distributions, get_prog, normalize_path, path_to_url, + redact_netloc, redact_password_from_url, remove_auth_from_url, rmtree, split_auth_from_netloc, split_auth_netloc_from_url, untar_file, unzip_file, ) from pip._internal.utils.temp_dir import AdjacentTempDirectory, TempDirectory @@ -964,6 +965,22 @@ class TestCallSubprocess(object): ) +@pytest.mark.skipif("sys.platform == 'win32'") +def test_path_to_url_unix(): + assert path_to_url('/tmp/file') == 'file:///tmp/file' + path = os.path.join(os.getcwd(), 'file') + assert path_to_url('file') == 'file://' + urllib_request.pathname2url(path) + + +@pytest.mark.skipif("sys.platform != 'win32'") +def test_path_to_url_win(): + assert path_to_url('c:/tmp/file') == 'file:///C:/tmp/file' + assert path_to_url('c:\\tmp\\file') == 'file:///C:/tmp/file' + assert path_to_url(r'\\unc\as\path') == 'file://unc/as/path' + path = os.path.join(os.getcwd(), 'file') + assert path_to_url('file') == 'file:' + urllib_request.pathname2url(path) + + @pytest.mark.parametrize('netloc, expected', [ # Test a basic case. ('example.com', ('example.com', (None, None))),