Skip to content

Commit

Permalink
Merge pull request #1062 from kammala/fix/cache-dir-normalization
Browse files Browse the repository at this point in the history
Normalize cache_dir path to be compatible with pip v20
  • Loading branch information
atugushev authored Feb 14, 2020
2 parents d405bf9 + d61a28e commit 3180fad
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
is_dir_url,
is_file_url,
is_vcs_url,
normalize_path,
parse_requirements,
path_to_url,
stdlib_pkgs,
Expand Down
1 change: 1 addition & 0 deletions piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def do_import(module_path, subimport=None, old_path=None):
Session = do_import("_vendor.requests.sessions", "Session")
Resolver = do_import("legacy_resolve", "Resolver", old_path="resolve")
WheelCache = do_import("cache", "WheelCache", old_path="wheel")
normalize_path = do_import("utils.misc", "normalize_path", old_path="utils")

# pip 18.1 has refactored InstallRequirement constructors use by pip-tools.
if PIP_VERSION < (18, 1):
Expand Down
5 changes: 4 additions & 1 deletion piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
is_dir_url,
is_file_url,
is_vcs_url,
normalize_path,
path_to_url,
url_to_path,
)
Expand Down Expand Up @@ -62,6 +63,8 @@ def __init__(self, pip_args, cache_dir, build_isolation=False):
# and pre) are deferred to pip.
self.command = create_install_command()
self.options, _ = self.command.parse_args(pip_args)
if self.options.cache_dir:
self.options.cache_dir = normalize_path(self.options.cache_dir)

self.session = self.command._build_session(self.options)
self.finder = self.command._build_package_finder(
Expand All @@ -81,7 +84,7 @@ def __init__(self, pip_args, cache_dir, build_isolation=False):

# Setup file paths
self.freshen_build_caches()
self._cache_dir = cache_dir
self._cache_dir = normalize_path(cache_dir)
self._download_dir = fs_str(os.path.join(self._cache_dir, "pkgs"))
self._wheel_download_dir = fs_str(os.path.join(self._cache_dir, "wheels"))

Expand Down
29 changes: 29 additions & 0 deletions tests/test_repository_pypi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os

import mock
import pytest

from piptools._compat import PackageFinder
from piptools._compat.pip_compat import PIP_VERSION, Link, Session, path_to_url
from piptools.repositories import PyPIRepository
from piptools.repositories.pypi import open_local_or_remote_file


Expand Down Expand Up @@ -176,3 +179,29 @@ def test_wheel_cache_cleanup_called(
ireq = from_line("six==1.10.0")
pypi_repository.get_dependencies(ireq)
WheelCache.return_value.cleanup.assert_called_once_with()


def test_relative_path_cache_dir_is_normalized(from_line):
relative_cache_dir = "pypi-repo-cache"
pypi_repository = PyPIRepository([], cache_dir=relative_cache_dir)

assert os.path.isabs(pypi_repository._cache_dir)
assert pypi_repository._cache_dir.endswith(relative_cache_dir)


def test_relative_path_pip_cache_dir_is_normalized(from_line, tmpdir):
relative_cache_dir = "pip-cache"
pypi_repository = PyPIRepository(
["--cache-dir", relative_cache_dir], cache_dir=str(tmpdir / "pypi-repo-cache")
)

assert os.path.isabs(pypi_repository.options.cache_dir)
assert pypi_repository.options.cache_dir.endswith(relative_cache_dir)


def test_pip_cache_dir_is_empty(from_line, tmpdir):
pypi_repository = PyPIRepository(
["--no-cache-dir"], cache_dir=str(tmpdir / "pypi-repo-cache")
)

assert not pypi_repository.options.cache_dir

0 comments on commit 3180fad

Please sign in to comment.