diff --git a/src/pip/_internal/commands/cache.py b/src/pip/_internal/commands/cache.py index 31e196ee3..b9d3ed410 100644 --- a/src/pip/_internal/commands/cache.py +++ b/src/pip/_internal/commands/cache.py @@ -37,18 +37,22 @@ class CacheCommand(Command): usage = """ %prog dir %prog info - %prog list [] [--abspath] + %prog list [] [--format=[human, abspath]] %prog remove %prog purge """ def add_options(self): # type: () -> None + self.cmd_opts.add_option( - '--abspath', - dest='abspath', - action='store_true', - help='List the absolute path of wheels') + '--format', + action='store', + dest='list_format', + default="human", + choices=('human', 'abspath'), + help="Select the output format among: human (default) or abspath" + ) self.parser.insert_option_group(0, self.cmd_opts) @@ -126,22 +130,34 @@ class CacheCommand(Command): pattern = '*' files = self._find_wheels(options, pattern) + if options.list_format == 'human': + self.format_for_human(files) + else: + self.format_for_abspath(files) + def format_for_human(self, files): + # type: (List[str]) -> None if not files: - if not options.abspath: - logger.info('Nothing cached.') + logger.info('Nothing cached.') return results = [] for filename in files: wheel = os.path.basename(filename) size = filesystem.format_file_size(filename) - if options.abspath: - results.append(filename) - else: - results.append(' - {} ({})'.format(wheel, size)) - if not options.abspath: - logger.info('Cache contents:\n') + results.append(' - {} ({})'.format(wheel, size)) + logger.info('Cache contents:\n') + logger.info('\n'.join(sorted(results))) + + def format_for_abspath(self, files): + # type: (List[str]) -> None + if not files: + return + + results = [] + for filename in files: + results.append(filename) + logger.info('\n'.join(sorted(results))) def remove_cache_items(self, options, args): diff --git a/tests/functional/test_cache.py b/tests/functional/test_cache.py index be165661a..603e11b5b 100644 --- a/tests/functional/test_cache.py +++ b/tests/functional/test_cache.py @@ -72,7 +72,7 @@ def list_matches_wheel(wheel_name, result): def list_matches_wheel_abspath(wheel_name, result): """Returns True if any line in `result`, which should be the output of - a `pip cache list --abspath` call, is a valid path and belongs to + a `pip cache list --format=abspath` call, is a valid path and belongs to `wheel_name`. E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with @@ -147,9 +147,9 @@ def test_cache_list(script): @pytest.mark.usefixtures("populate_wheel_cache") def test_cache_list_abspath(script): - """Running `pip cache list --abspath` should return full + """Running `pip cache list --format=abspath` should return full paths of exactly what the populate_wheel_cache fixture adds.""" - result = script.pip('cache', 'list', '--abspath') + result = script.pip('cache', 'list', '--format=abspath') assert list_matches_wheel_abspath('yyy-1.2.3', result) assert list_matches_wheel_abspath('zzz-4.5.6', result) @@ -167,9 +167,9 @@ def test_cache_list_with_empty_cache(script): @pytest.mark.usefixtures("empty_wheel_cache") def test_cache_list_with_empty_cache_abspath(script): - """Running `pip cache list --abspath` with an empty cache should not + """Running `pip cache list --format=abspath` with an empty cache should not print anything and exit.""" - result = script.pip('cache', 'list', '--abspath') + result = script.pip('cache', 'list', '--format=abspath') assert result.stdout.strip() == "" @@ -193,9 +193,10 @@ def test_cache_list_name_match(script): @pytest.mark.usefixtures("populate_wheel_cache") def test_cache_list_name_match_abspath(script): - """Running `pip cache list zzz --abspath` should list paths of + """Running `pip cache list zzz --format=abspath` should list paths of zzz-4.5.6, zzz-4.5.7, zzz-7.8.9, but nothing else.""" - result = script.pip('cache', 'list', 'zzz', '--abspath', '--verbose') + result = script.pip('cache', 'list', 'zzz', '--format=abspath', + '--verbose') assert not list_matches_wheel_abspath('yyy-1.2.3', result) assert list_matches_wheel_abspath('zzz-4.5.6', result) @@ -217,9 +218,10 @@ def test_cache_list_name_and_version_match(script): @pytest.mark.usefixtures("populate_wheel_cache") def test_cache_list_name_and_version_match_abspath(script): - """Running `pip cache list zzz-4.5.6 --abspath` should list path of + """Running `pip cache list zzz-4.5.6 --format=abspath` should list path of zzz-4.5.6, but nothing else.""" - result = script.pip('cache', 'list', 'zzz-4.5.6', '--abspath', '--verbose') + result = script.pip('cache', 'list', 'zzz-4.5.6', '--format=abspath', + '--verbose') assert not list_matches_wheel_abspath('yyy-1.2.3', result) assert list_matches_wheel_abspath('zzz-4.5.6', result)