From e212f6657502376fcc8927b5d82a8efd12252691 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 24 Apr 2020 12:05:19 +0300 Subject: [PATCH 1/2] commands: cache: Abort early if cache is disabled --- news/8124.bugfix | 1 + src/pip/_internal/commands/cache.py | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 news/8124.bugfix diff --git a/news/8124.bugfix b/news/8124.bugfix new file mode 100644 index 00000000000..a859381e0d8 --- /dev/null +++ b/news/8124.bugfix @@ -0,0 +1 @@ +Abort pip cache commands early when cache is disabled. diff --git a/src/pip/_internal/commands/cache.py b/src/pip/_internal/commands/cache.py index 7e3f72e080b..3c345dfa0d1 100644 --- a/src/pip/_internal/commands/cache.py +++ b/src/pip/_internal/commands/cache.py @@ -48,6 +48,11 @@ def run(self, options, args): "purge": self.purge_cache, } + if not options.cache_dir: + logger.error("pip cache commands can not " + "function since cache is disabled.") + return ERROR + # Determine action if not args or args[0] not in handlers: logger.error("Need an action ({}) to perform.".format( From 8c28b8173a29da65a2820f9e45f5c5eb955f4614 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Fri, 24 Apr 2020 12:32:24 +0300 Subject: [PATCH 2/2] tests: commands: cache: Add no-cache-dir test case --- tests/functional/test_cache.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/functional/test_cache.py b/tests/functional/test_cache.py index a464ece7945..7fec4dd56c4 100644 --- a/tests/functional/test_cache.py +++ b/tests/functional/test_cache.py @@ -216,3 +216,15 @@ def test_cache_purge_too_many_args(script, wheel_cache_files): # Make sure nothing was deleted. for filename in wheel_cache_files: assert os.path.exists(filename) + + +@pytest.mark.parametrize("command", ["info", "list", "remove", "purge"]) +def test_cache_abort_when_no_cache_dir(script, command): + """Running any pip cache command when cache is disabled should + abort and log an informative error""" + result = script.pip('cache', command, '--no-cache-dir', + expect_error=True) + assert result.stdout == '' + + assert ('ERROR: pip cache commands can not function' + ' since cache is disabled.' in result.stderr.splitlines())