Skip to content

Commit

Permalink
Normalize cache_dir path to be compatible with pip v20. (jazzband#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
kammala committed Feb 11, 2020
1 parent d405bf9 commit 92b11e0
Show file tree
Hide file tree
Showing 4 changed files with 24 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
18 changes: 18 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,18 @@ 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()


@mock.patch("piptools.repositories.pypi.PyPIRepository.resolve_reqs") # to run offline
@mock.patch("piptools.repositories.pypi.WheelCache")
def test_relative_path_cache_dir(WheelCache, resolve_reqs, from_line):
relative_cache_dir = "pypi-repo-cache"
pypi_repository = PyPIRepository(
["--index-url", PyPIRepository.DEFAULT_INDEX_URL], cache_dir=relative_cache_dir
)
ireq = from_line("six==1.10.0")
pypi_repository.get_dependencies(ireq)
WheelCache.assert_called_once()
cache_dir = WheelCache.call_args[0][0]
assert os.path.isabs(cache_dir)
assert cache_dir.endswith(relative_cache_dir)

0 comments on commit 92b11e0

Please sign in to comment.