From 449aea1403ce639898795416ebf2582f71fdf0df Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Mon, 2 Mar 2020 15:35:25 +0100 Subject: [PATCH] Add cache list command This introduces a new cache sub-command that lists all available caches. Relates-to: #1162 --- docs/docs/cli.md | 12 ++++++ poetry/console/commands/cache/cache.py | 4 +- poetry/console/commands/cache/list.py | 21 ++++++++++ tests/console/commands/test_cache.py | 58 ++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 poetry/console/commands/cache/list.py create mode 100644 tests/console/commands/test_cache.py diff --git a/docs/docs/cli.md b/docs/docs/cli.md index 89d19a8cc18..63d63fbc990 100644 --- a/docs/docs/cli.md +++ b/docs/docs/cli.md @@ -452,3 +452,15 @@ The `env` command regroups sub commands to interact with the virtualenvs associated with a specific project. See [Managing environments](./managing-environments.md) for more information about these commands. + +## cache + +The `cache` command regroups sub commands to interact with Poetry's cache. + +### cache list + +The `cache list` command lists Poetry's available caches. + +```bash +poetry cache list +``` diff --git a/poetry/console/commands/cache/cache.py b/poetry/console/commands/cache/cache.py index 469eb56be05..695e27e0af7 100644 --- a/poetry/console/commands/cache/cache.py +++ b/poetry/console/commands/cache/cache.py @@ -1,3 +1,5 @@ +from poetry.console.commands.cache.list import CacheListCommand + from ..command import Command from .clear import CacheClearCommand @@ -7,7 +9,7 @@ class CacheCommand(Command): name = "cache" description = "Interact with Poetry's cache" - commands = [CacheClearCommand()] + commands = [CacheClearCommand(), CacheListCommand()] def handle(self): return self.call("help", self._config.name) diff --git a/poetry/console/commands/cache/list.py b/poetry/console/commands/cache/list.py new file mode 100644 index 00000000000..6a030fa2eba --- /dev/null +++ b/poetry/console/commands/cache/list.py @@ -0,0 +1,21 @@ +import os + +from ..command import Command + + +class CacheListCommand(Command): + + name = "list" + description = "List Poetry's caches." + + def handle(self): + from poetry.locations import REPOSITORY_CACHE_DIR + + if os.path.exists(str(REPOSITORY_CACHE_DIR)): + caches = list(sorted(REPOSITORY_CACHE_DIR.iterdir())) + if caches: + for cache in caches: + self.line("{}".format(cache.name)) + return 0 + + self.line("No caches found") diff --git a/tests/console/commands/test_cache.py b/tests/console/commands/test_cache.py new file mode 100644 index 00000000000..f66fdb0c496 --- /dev/null +++ b/tests/console/commands/test_cache.py @@ -0,0 +1,58 @@ +import uuid + +import pytest + +from cleo.testers import CommandTester + + +@pytest.fixture +def repository_cache_dir(monkeypatch, tmpdir): + import poetry.locations + from poetry.utils._compat import Path + + path = Path(tmpdir) + monkeypatch.setattr(poetry.locations, "REPOSITORY_CACHE_DIR", path) + return path + + +@pytest.fixture +def repository_one(): + return f"01_{uuid.uuid4()}" + + +@pytest.fixture +def repository_two(): + return f"02_{uuid.uuid4()}" + + +@pytest.fixture +def mock_caches(repository_cache_dir, repository_one, repository_two): + (repository_cache_dir / repository_one).mkdir() + (repository_cache_dir / repository_two).mkdir() + + +def test_cache_list(app, mock_caches, repository_one, repository_two): + command = app.find("cache list") + tester = CommandTester(command) + + tester.execute() + + expected = f"""\ +{repository_one} +{repository_two} +""" + + assert expected == tester.io.fetch_output() + + +def test_cache_list_empty(app, repository_cache_dir): + command = app.find("cache list") + tester = CommandTester(command) + + tester.execute() + + expected = f"""\ +No caches found +""" + + assert expected == tester.io.fetch_output()