Skip to content

Commit

Permalink
fix(config): expand ~ in virtualenvs.path
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Ignatev <[email protected]>
  • Loading branch information
branchvincent and valignatev committed Jun 15, 2022
1 parent bed427d commit 3eb547d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
7 changes: 7 additions & 0 deletions src/poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ def _get_environment_repositories() -> dict[str, dict[str, str]]:
def repository_cache_directory(self) -> Path:
return Path(self.get("cache-dir")) / "cache" / "repositories"

@property
def virtualenvs_path(self) -> Path:
path = self.get("virtualenvs.path")
if path is None:
path = Path(self.get("cache-dir")) / "virtualenvs"
return Path(path).expanduser()

def get(self, setting_name: str, default: Any = None) -> Any:
"""
Retrieve a setting value.
Expand Down
49 changes: 7 additions & 42 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,7 @@ def _detect_active_python(self, io: IO) -> str | None:
return executable

def activate(self, python: str, io: IO) -> Env:
venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)

venv_path = self._poetry.config.virtualenvs_path
cwd = self._poetry.file.parent

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

def deactivate(self, io: IO) -> None:
venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)

venv_path = self._poetry.config.virtualenvs_path
name = self._poetry.package.name
name = self.generate_env_name(name, str(self._poetry.file.parent))

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

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

venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)
venv_path = self._poetry.config.virtualenvs_path

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

venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)
venv_path = self._poetry.config.virtualenvs_path

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

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

venv_name = self.generate_env_name(name, str(self._poetry.file.parent))

venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)

venv_path = self._poetry.config.virtualenvs_path
env_list = [
VirtualEnv(Path(p)) for p in sorted(venv_path.glob(f"{venv_name}-py*"))
]
Expand All @@ -762,11 +738,7 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
return env_list

def remove(self, python: str) -> Env:
venv_path = self._poetry.config.get("virtualenvs.path")
if venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)
venv_path = self._poetry.config.virtualenvs_path

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

create_venv = self._poetry.config.get("virtualenvs.create")
root_venv = self._poetry.config.get("virtualenvs.in-project")
venv_path = self._poetry.config.get("virtualenvs.path")
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
Expand All @@ -884,13 +855,7 @@ def create_venv(
if not executable and prefer_active_python:
executable = self._detect_active_python(io)

if root_venv:
venv_path = cwd / ".venv"
elif venv_path is None:
venv_path = self._poetry.config.get("cache-dir") / "virtualenvs"
else:
venv_path = Path(venv_path)

venv_path = cwd / ".venv" if root_venv else self._poetry.config.virtualenvs_path
if not name:
name = self._poetry.package.name
assert name is not None
Expand Down
14 changes: 13 additions & 1 deletion tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -17,7 +18,6 @@
if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Iterator
from pathlib import Path


def get_options_based_on_normalizer(normalizer: Callable) -> str:
Expand Down Expand Up @@ -66,3 +66,15 @@ def test_config_get_from_environment_variable(
):
os.environ[env_var] = env_value
assert config.get(name) is value


@pytest.mark.parametrize(
("path_config", "expected"),
[("~/.venvs", Path.home() / ".venvs"), ("venv", Path("venv"))],
)
def test_config_expands_tilde_for_virtualenvs_path(
config: Config, path_config: str, expected: Path
):

config.merge({"virtualenvs": {"path": path_config}})
assert config.virtualenvs_path == expected

0 comments on commit 3eb547d

Please sign in to comment.