Skip to content

Commit

Permalink
Merge pull request #10459 from NoahGorny/do-not-throw-error-on-cache-…
Browse files Browse the repository at this point in the history
…purge
  • Loading branch information
pradyunsg authored Sep 28, 2021
2 parents d91fecd + 2d3f17c commit 8ea2651
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions news/10459.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Do not raise error when there are no files to remove with ``pip cache purge/remove``.
Instead log a warning and continue (to log that we removed 0 files).
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,16 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:

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

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

if not files:
raise CommandError("No matching packages")
logger.warning(no_matching_msg)

for filename in files:
os.unlink(filename)
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,24 @@ def test_cache_list_with_empty_cache_abspath(script):
assert result.stdout.strip() == ""


@pytest.mark.usefixtures("empty_wheel_cache")
def test_cache_purge_with_empty_cache(script):
"""Running `pip cache purge` with an empty cache should print a warning
and exit without an error code."""
result = script.pip("cache", "purge", allow_stderr_warning=True)
assert result.stderr == "WARNING: No matching packages\n"
assert result.stdout == "Files removed: 0\n"


@pytest.mark.usefixtures("populate_wheel_cache")
def test_cache_remove_with_bad_pattern(script):
"""Running `pip cache remove` with a bad pattern should print a warning
and exit without an error code."""
result = script.pip("cache", "remove", "aaa", allow_stderr_warning=True)
assert result.stderr == 'WARNING: No matching packages for pattern "aaa"\n'
assert result.stdout == "Files removed: 0\n"


def test_cache_list_too_many_args(script):
"""Passing `pip cache list` too many arguments should cause an error."""
script.pip("cache", "list", "aaa", "bbb", expect_error=True)
Expand Down

0 comments on commit 8ea2651

Please sign in to comment.