From 40d74a60a5aa99ac3778e6d3c0af0947c816599e Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Fri, 25 Oct 2019 14:49:12 +0700 Subject: [PATCH] Cleanup wheel cache properly Attribute `self.wheel_cache` has been removed in f6de247b3f7a9e776325057a1e6cf6e48d34e7e7, thus `self.wheel_cache.cleanup()` will allways throw the AttributeError and `wheel_cache.cleanup()` will never be called as supposed to be. WheelCache.cleanup() has been introduced in `pip==10.0.0`. It's better to check pip version explicitly (LBYL) rather than ask for forgiveness (EAFP). --- piptools/repositories/pypi.py | 8 ++++---- tests/test_repository_pypi.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py index 50bcc8739..b6b7b8a08 100644 --- a/piptools/repositories/pypi.py +++ b/piptools/repositories/pypi.py @@ -274,10 +274,10 @@ def get_dependencies(self, ireq): os.environ["PIP_REQ_TRACKER"] = prev_tracker else: del os.environ["PIP_REQ_TRACKER"] - try: - self.wheel_cache.cleanup() - except AttributeError: - pass + + # WheelCache.cleanup() introduced in pip==10.0.0 + if PIP_VERSION >= (10,): + wheel_cache.cleanup() return self._dependencies_cache[ireq] def get_hashes(self, ireq): diff --git a/tests/test_repository_pypi.py b/tests/test_repository_pypi.py index 8eca3ec37..c39b9a51e 100644 --- a/tests/test_repository_pypi.py +++ b/tests/test_repository_pypi.py @@ -174,3 +174,19 @@ def test_pypirepo_calls_reqset_with_str_paths(pypi_repository, from_line): assert isinstance(called_with_finder, PackageFinder) assert called_with_ireq == ireq assert not pf_call_kwargs + + +@pytest.mark.skipif( + PIP_VERSION < (10,), reason="WheelCache.cleanup() introduced in pip==10.0.0" +) +@mock.patch("piptools.repositories.pypi.PyPIRepository.resolve_reqs") # to run offline +@mock.patch("piptools.repositories.pypi.WheelCache") +def test_wheel_cache_cleanup_called( + WheelCache, resolve_reqs, pypi_repository, from_line +): + """ + Test WheelCache.cleanup() called once after dependency resolution. + """ + ireq = from_line("six==1.10.0") + pypi_repository.get_dependencies(ireq) + WheelCache.return_value.cleanup.assert_called_once_with()