Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache: Do not raise error when there are no files to remove #10459

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
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