Skip to content

Commit 8ea2651

Browse files
authored
Merge pull request #10459 from NoahGorny/do-not-throw-error-on-cache-purge
2 parents d91fecd + 2d3f17c commit 8ea2651

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

news/10459.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Do not raise error when there are no files to remove with ``pip cache purge/remove``.
2+
Instead log a warning and continue (to log that we removed 0 files).

src/pip/_internal/commands/cache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,16 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
170170

171171
files = self._find_wheels(options, args[0])
172172

173-
# Only fetch http files if no specific pattern given
173+
no_matching_msg = "No matching packages"
174174
if args[0] == "*":
175+
# Only fetch http files if no specific pattern given
175176
files += self._find_http_files(options)
177+
else:
178+
# Add the pattern to the log message
179+
no_matching_msg += ' for pattern "{}"'.format(args[0])
176180

177181
if not files:
178-
raise CommandError("No matching packages")
182+
logger.warning(no_matching_msg)
179183

180184
for filename in files:
181185
os.unlink(filename)

tests/functional/test_cache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ def test_cache_list_with_empty_cache_abspath(script):
244244
assert result.stdout.strip() == ""
245245

246246

247+
@pytest.mark.usefixtures("empty_wheel_cache")
248+
def test_cache_purge_with_empty_cache(script):
249+
"""Running `pip cache purge` with an empty cache should print a warning
250+
and exit without an error code."""
251+
result = script.pip("cache", "purge", allow_stderr_warning=True)
252+
assert result.stderr == "WARNING: No matching packages\n"
253+
assert result.stdout == "Files removed: 0\n"
254+
255+
256+
@pytest.mark.usefixtures("populate_wheel_cache")
257+
def test_cache_remove_with_bad_pattern(script):
258+
"""Running `pip cache remove` with a bad pattern should print a warning
259+
and exit without an error code."""
260+
result = script.pip("cache", "remove", "aaa", allow_stderr_warning=True)
261+
assert result.stderr == 'WARNING: No matching packages for pattern "aaa"\n'
262+
assert result.stdout == "Files removed: 0\n"
263+
264+
247265
def test_cache_list_too_many_args(script):
248266
"""Passing `pip cache list` too many arguments should cause an error."""
249267
script.pip("cache", "list", "aaa", "bbb", expect_error=True)

0 commit comments

Comments
 (0)