Skip to content

Commit 449aea1

Browse files
committed
Add cache list command
This introduces a new cache sub-command that lists all available caches. Relates-to: #1162
1 parent c8d136b commit 449aea1

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

docs/docs/cli.md

+12
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,15 @@ The `env` command regroups sub commands to interact with the virtualenvs
452452
associated with a specific project.
453453

454454
See [Managing environments](./managing-environments.md) for more information about these commands.
455+
456+
## cache
457+
458+
The `cache` command regroups sub commands to interact with Poetry's cache.
459+
460+
### cache list
461+
462+
The `cache list` command lists Poetry's available caches.
463+
464+
```bash
465+
poetry cache list
466+
```

poetry/console/commands/cache/cache.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from poetry.console.commands.cache.list import CacheListCommand
2+
13
from ..command import Command
24
from .clear import CacheClearCommand
35

@@ -7,7 +9,7 @@ class CacheCommand(Command):
79
name = "cache"
810
description = "Interact with Poetry's cache"
911

10-
commands = [CacheClearCommand()]
12+
commands = [CacheClearCommand(), CacheListCommand()]
1113

1214
def handle(self):
1315
return self.call("help", self._config.name)

poetry/console/commands/cache/list.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
from ..command import Command
4+
5+
6+
class CacheListCommand(Command):
7+
8+
name = "list"
9+
description = "List Poetry's caches."
10+
11+
def handle(self):
12+
from poetry.locations import REPOSITORY_CACHE_DIR
13+
14+
if os.path.exists(str(REPOSITORY_CACHE_DIR)):
15+
caches = list(sorted(REPOSITORY_CACHE_DIR.iterdir()))
16+
if caches:
17+
for cache in caches:
18+
self.line("<info>{}</>".format(cache.name))
19+
return 0
20+
21+
self.line("<warning>No caches found</>")

tests/console/commands/test_cache.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import uuid
2+
3+
import pytest
4+
5+
from cleo.testers import CommandTester
6+
7+
8+
@pytest.fixture
9+
def repository_cache_dir(monkeypatch, tmpdir):
10+
import poetry.locations
11+
from poetry.utils._compat import Path
12+
13+
path = Path(tmpdir)
14+
monkeypatch.setattr(poetry.locations, "REPOSITORY_CACHE_DIR", path)
15+
return path
16+
17+
18+
@pytest.fixture
19+
def repository_one():
20+
return f"01_{uuid.uuid4()}"
21+
22+
23+
@pytest.fixture
24+
def repository_two():
25+
return f"02_{uuid.uuid4()}"
26+
27+
28+
@pytest.fixture
29+
def mock_caches(repository_cache_dir, repository_one, repository_two):
30+
(repository_cache_dir / repository_one).mkdir()
31+
(repository_cache_dir / repository_two).mkdir()
32+
33+
34+
def test_cache_list(app, mock_caches, repository_one, repository_two):
35+
command = app.find("cache list")
36+
tester = CommandTester(command)
37+
38+
tester.execute()
39+
40+
expected = f"""\
41+
{repository_one}
42+
{repository_two}
43+
"""
44+
45+
assert expected == tester.io.fetch_output()
46+
47+
48+
def test_cache_list_empty(app, repository_cache_dir):
49+
command = app.find("cache list")
50+
tester = CommandTester(command)
51+
52+
tester.execute()
53+
54+
expected = f"""\
55+
No caches found
56+
"""
57+
58+
assert expected == tester.io.fetch_output()

0 commit comments

Comments
 (0)