Skip to content

Commit 607e70f

Browse files
author
Frost Ming
authored
list .venv when it exists (#1762)
* list .venv when it exists * only list when in-project is true * missing config * move logic to manager.list * Add .venv when it exists
1 parent 9306cd2 commit 607e70f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

poetry/utils/env.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,20 @@ def list(self, name=None): # type: (Optional[str]) -> List[VirtualEnv]
372372
else:
373373
venv_path = Path(venv_path)
374374

375-
return [
375+
env_list = [
376376
VirtualEnv(Path(p))
377377
for p in sorted(venv_path.glob("{}-py*".format(venv_name)))
378378
]
379379

380+
venv = self._poetry.file.parent / ".venv"
381+
if (
382+
self._poetry.config.get("virtualenvs.in-project")
383+
and venv.exists()
384+
and venv.is_dir()
385+
):
386+
env_list.insert(0, VirtualEnv(venv))
387+
return env_list
388+
380389
def remove(self, python): # type: (str) -> Env
381390
venv_path = self._poetry.config.get("virtualenvs.path")
382391
if venv_path is None:

tests/console/commands/env/test_list.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import tomlkit
24

35
from cleo.testers import CommandTester
@@ -58,3 +60,19 @@ def test_activated(app, tmp_dir):
5860
)
5961

6062
assert expected == tester.io.fetch_output()
63+
64+
65+
def test_in_project_venv(app, tmpdir):
66+
os.environ.pop("VIRTUAL_ENV", None)
67+
app.poetry.config.merge({"virtualenvs": {"in-project": True}})
68+
69+
(app.poetry.file.parent / ".venv").mkdir(exist_ok=True)
70+
71+
command = app.find("env list")
72+
tester = CommandTester(command)
73+
tester.execute()
74+
75+
expected = ".venv (Activated)\n"
76+
77+
assert expected == tester.io.fetch_output()
78+
(app.poetry.file.parent / ".venv").rmdir()

0 commit comments

Comments
 (0)