Skip to content

Commit 3f54fdc

Browse files
branchvincentvalignatev
authored andcommitted
fix(config): expand ~ in virtualenvs.path
Co-authored-by: Valentin Ignatev <[email protected]>
1 parent 66328c7 commit 3f54fdc

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

src/poetry/config/config.py

+7
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ def _get_environment_repositories() -> dict[str, dict[str, str]]:
206206
def repository_cache_directory(self) -> Path:
207207
return Path(self.get("cache-dir")) / "cache" / "repositories"
208208

209+
@property
210+
def virtualenvs_path(self) -> Path:
211+
path = self.get("virtualenvs.path")
212+
if path is None:
213+
path = Path(self.get("cache-dir")) / "virtualenvs"
214+
return Path(path).expanduser()
215+
209216
def get(self, setting_name: str, default: Any = None) -> Any:
210217
"""
211218
Retrieve a setting value.

src/poetry/utils/env.py

+7-42
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,7 @@ def _detect_active_python(self, io: IO) -> str | None:
552552
return executable
553553

554554
def activate(self, python: str, io: IO) -> Env:
555-
venv_path = self._poetry.config.get("virtualenvs.path")
556-
if venv_path is None:
557-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
558-
else:
559-
venv_path = Path(venv_path)
560-
555+
venv_path = self._poetry.config.virtualenvs_path
561556
cwd = self._poetry.file.parent
562557

563558
envs_file = TOMLFile(venv_path / self.ENVS_FILE)
@@ -645,12 +640,7 @@ def activate(self, python: str, io: IO) -> Env:
645640
return self.get(reload=True)
646641

647642
def deactivate(self, io: IO) -> None:
648-
venv_path = self._poetry.config.get("virtualenvs.path")
649-
if venv_path is None:
650-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
651-
else:
652-
venv_path = Path(venv_path)
653-
643+
venv_path = self._poetry.config.virtualenvs_path
654644
name = self._poetry.package.name
655645
name = self.generate_env_name(name, str(self._poetry.file.parent))
656646

@@ -671,11 +661,7 @@ def get(self, reload: bool = False) -> Env:
671661

672662
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
673663

674-
venv_path = self._poetry.config.get("virtualenvs.path")
675-
if venv_path is None:
676-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
677-
else:
678-
venv_path = Path(venv_path)
664+
venv_path = self._poetry.config.virtualenvs_path
679665

680666
cwd = self._poetry.file.parent
681667
envs_file = TOMLFile(venv_path / self.ENVS_FILE)
@@ -712,11 +698,7 @@ def get(self, reload: bool = False) -> Env:
712698
if not create_venv:
713699
return self.get_system_env()
714700

715-
venv_path = self._poetry.config.get("virtualenvs.path")
716-
if venv_path is None:
717-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
718-
else:
719-
venv_path = Path(venv_path)
701+
venv_path = self._poetry.config.virtualenvs_path
720702

721703
name = f"{base_env_name}-py{python_minor.strip()}"
722704

@@ -741,13 +723,7 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
741723
name = self._poetry.package.name
742724

743725
venv_name = self.generate_env_name(name, str(self._poetry.file.parent))
744-
745-
venv_path = self._poetry.config.get("virtualenvs.path")
746-
if venv_path is None:
747-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
748-
else:
749-
venv_path = Path(venv_path)
750-
726+
venv_path = self._poetry.config.virtualenvs_path
751727
env_list = [
752728
VirtualEnv(Path(p)) for p in sorted(venv_path.glob(f"{venv_name}-py*"))
753729
]
@@ -762,11 +738,7 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
762738
return env_list
763739

764740
def remove(self, python: str) -> Env:
765-
venv_path = self._poetry.config.get("virtualenvs.path")
766-
if venv_path is None:
767-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
768-
else:
769-
venv_path = Path(venv_path)
741+
venv_path = self._poetry.config.virtualenvs_path
770742

771743
cwd = self._poetry.file.parent
772744
envs_file = TOMLFile(venv_path / self.ENVS_FILE)
@@ -875,7 +847,6 @@ def create_venv(
875847

876848
create_venv = self._poetry.config.get("virtualenvs.create")
877849
root_venv = self._poetry.config.get("virtualenvs.in-project")
878-
venv_path = self._poetry.config.get("virtualenvs.path")
879850
prefer_active_python = self._poetry.config.get(
880851
"virtualenvs.prefer-active-python"
881852
)
@@ -884,13 +855,7 @@ def create_venv(
884855
if not executable and prefer_active_python:
885856
executable = self._detect_active_python(io)
886857

887-
if root_venv:
888-
venv_path = cwd / ".venv"
889-
elif venv_path is None:
890-
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
891-
else:
892-
venv_path = Path(venv_path)
893-
858+
venv_path = cwd / ".venv" if root_venv else self._poetry.config.virtualenvs_path
894859
if not name:
895860
name = self._poetry.package.name
896861
assert name is not None

tests/config/test_config.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55

6+
from pathlib import Path
67
from typing import TYPE_CHECKING
78

89
import pytest
@@ -17,7 +18,6 @@
1718
if TYPE_CHECKING:
1819
from collections.abc import Callable
1920
from collections.abc import Iterator
20-
from pathlib import Path
2121

2222

2323
def get_options_based_on_normalizer(normalizer: Callable) -> str:
@@ -66,3 +66,14 @@ def test_config_get_from_environment_variable(
6666
):
6767
os.environ[env_var] = env_value
6868
assert config.get(name) is value
69+
70+
71+
@pytest.mark.parametrize(
72+
("path_config", "expected"),
73+
[("~/.venvs", Path.home() / ".venvs"), ("venv", Path("venv"))],
74+
)
75+
def test_config_expands_tilde_for_virtualenvs_path(
76+
config: Config, path_config: str, expected: Path
77+
):
78+
config.merge({"virtualenvs": {"path": path_config}})
79+
assert config.virtualenvs_path == expected

0 commit comments

Comments
 (0)