[commands/cache] Raise errors if wrong number of args.

Also add tests for purge_cache, since I apparently forgot those before.
This commit is contained in:
Ellen Marie Dash 2019-10-09 19:58:23 -04:00
parent d57dcd934e
commit 50604be6c4
2 changed files with 44 additions and 3 deletions

View File

@ -88,7 +88,10 @@ class CacheCommand(Command):
def list_cache_items(self, options, args):
# type: (Values, List[Any]) -> None
if args and args[0]:
if len(args) > 1:
raise CommandError('Too many arguments')
if args:
pattern = args[0]
else:
pattern = '*'
@ -107,6 +110,9 @@ class CacheCommand(Command):
def remove_cache_items(self, options, args):
# type: (Values, List[Any]) -> None
if len(args) > 1:
raise CommandError('Too many arguments')
if not args:
raise CommandError('Please provide a pattern')
@ -121,6 +127,9 @@ class CacheCommand(Command):
def purge_cache(self, options, args):
# type: (Values, List[Any]) -> None
if args:
raise CommandError('Too many arguments')
return self.remove_cache_items(options, ['*'])
def _find_wheels(self, options, pattern):

View File

@ -3,12 +3,12 @@ import shutil
def _cache_dir(script):
results = script.run(
result = script.run(
'python', '-c',
'from pip._internal.locations import USER_CACHE_DIR;'
'print(USER_CACHE_DIR)'
)
return str(results.stdout).strip()
return result.stdout.strip()
def test_cache_info(script, monkeypatch):
@ -37,6 +37,11 @@ def test_cache_list(script, monkeypatch):
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))
def test_cache_list_too_many_args(script, monkeypatch):
script.pip('cache', 'list', 'aaa', 'bbb',
expect_error=True)
def test_cache_list_with_pattern(script, monkeypatch):
cache_dir = _cache_dir(script)
wheel_cache_dir = os.path.join(cache_dir, 'wheels')
@ -67,3 +72,30 @@ def test_cache_remove(script, monkeypatch):
assert 'yyy-1.2.3' not in result.stdout
assert 'zzz-4.5.6' in result.stdout
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))
def test_cache_remove_too_many_args(script, monkeypatch):
result = script.pip('cache', 'remove', 'aaa', 'bbb',
expect_error=True)
def test_cache_purge(script, monkeypatch):
cache_dir = _cache_dir(script)
wheel_cache_dir = os.path.join(cache_dir, 'wheels')
destination = os.path.join(wheel_cache_dir, 'arbitrary', 'pathname')
os.makedirs(destination)
with open(os.path.join(wheel_cache_dir, 'yyy-1.2.3.whl'), 'w'):
pass
with open(os.path.join(wheel_cache_dir, 'zzz-4.5.6.whl'), 'w'):
pass
result = script.pip('cache', 'purge', 'aaa', '--verbose',
expect_error=True)
assert 'yyy-1.2.3' not in result.stdout
assert 'zzz-4.5.6' not in result.stdout
result = script.pip('cache', 'purge', '--verbose')
assert 'yyy-1.2.3' in result.stdout
assert 'zzz-4.5.6' in result.stdout
shutil.rmtree(os.path.join(wheel_cache_dir, 'arbitrary'))