diff --git a/piptools/_compat/__init__.py b/piptools/_compat/__init__.py index a26e53185..e835b1e5d 100644 --- a/piptools/_compat/__init__.py +++ b/piptools/_compat/__init__.py @@ -28,6 +28,7 @@ is_dir_url, is_file_url, is_vcs_url, + normalize_path, parse_requirements, path_to_url, stdlib_pkgs, diff --git a/piptools/_compat/pip_compat.py b/piptools/_compat/pip_compat.py index 1e45d3a9c..f465c598e 100644 --- a/piptools/_compat/pip_compat.py +++ b/piptools/_compat/pip_compat.py @@ -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): diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py index 2aa2a1df6..d90a5ffbf 100644 --- a/piptools/repositories/pypi.py +++ b/piptools/repositories/pypi.py @@ -24,6 +24,7 @@ is_dir_url, is_file_url, is_vcs_url, + normalize_path, path_to_url, url_to_path, ) @@ -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( @@ -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")) diff --git a/tests/test_repository_pypi.py b/tests/test_repository_pypi.py index 14ba1042c..5b12ef551 100644 --- a/tests/test_repository_pypi.py +++ b/tests/test_repository_pypi.py @@ -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 @@ -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)