Skip to content

Commit

Permalink
Use existing .venv even without explicit in-project
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 18, 2023
1 parent ec35b83 commit 1a35032
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def activate(self, python: str) -> Env:
patch = python_version.text

create = False
is_root_venv = self._poetry.config.get("virtualenvs.in-project")
is_root_venv = self.use_root_env()
# If we are required to create the virtual environment in the root folder,
# create or recreate it if needed
if is_root_venv:
Expand Down Expand Up @@ -730,11 +730,7 @@ def get(self, reload: bool = False) -> Env:

if not in_venv or env is not None:
# Checking if a local virtualenv exists
if (
self._poetry.config.get("virtualenvs.in-project") is not False
and (cwd / ".venv").exists()
and (cwd / ".venv").is_dir()
):
if self.use_root_env():
venv = cwd / ".venv"

return VirtualEnv(venv)
Expand Down Expand Up @@ -773,11 +769,7 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
env_list = [VirtualEnv(p) for p in sorted(venv_path.glob(f"{venv_name}-py*"))]

venv = self._poetry.file.parent / ".venv"
if (
self._poetry.config.get("virtualenvs.in-project") is not False
and venv.exists()
and venv.is_dir()
):
if self.use_root_env():
env_list.insert(0, VirtualEnv(venv))
return env_list

Expand Down Expand Up @@ -891,6 +883,14 @@ def remove(self, python: str) -> Env:

return VirtualEnv(venv_path, venv_path)

def use_root_env(self) -> bool:
in_project: bool | None = self._poetry.config.get("virtualenvs.in-project")
if in_project is not None:
return in_project

root_venv: Path = self._poetry.file.parent / ".venv"
return root_venv.exists() and root_venv.is_dir()

def create_venv(
self,
name: str | None = None,
Expand Down Expand Up @@ -918,7 +918,7 @@ def create_venv(
return env

create_venv = self._poetry.config.get("virtualenvs.create")
root_venv = self._poetry.config.get("virtualenvs.in-project")
root_venv = self.use_root_env()
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
Expand Down
48 changes: 47 additions & 1 deletion tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,52 @@ def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str:
return check_output


def test_activate_in_project_venv_no_explicit_config(
tmp_path: Path,
manager: EnvManager,
poetry: Poetry,
mocker: MockerFixture,
venv_name: str,
in_project_venv_dir: Path,
) -> None:
mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}")
mocker.patch(
"subprocess.check_output",
side_effect=check_output_wrapper(),
)
mocker.patch(
"subprocess.Popen.communicate",
side_effect=[
("/prefix", None),
('{"version_info": [3, 7, 0]}', None),
("/prefix", None),
("/prefix", None),
("/prefix", None),
],
)
m = mocker.patch("poetry.utils.env.EnvManager.build_venv", side_effect=build_venv)

env = manager.activate("python3.7")

assert env.path == tmp_path / "poetry-fixture-simple" / ".venv"
assert env.base == Path("/prefix")

m.assert_called_with(
tmp_path / "poetry-fixture-simple" / ".venv",
executable=Path("/usr/bin/python3.7"),
flags={
"always-copy": False,
"system-site-packages": False,
"no-pip": False,
"no-setuptools": False,
},
prompt="simple-project-py3.7",
)

envs_file = TOMLFile(tmp_path / "envs.toml")
assert not envs_file.exists()


def test_activate_activates_non_existing_virtualenv_no_envs_file(
tmp_path: Path,
manager: EnvManager,
Expand Down Expand Up @@ -1378,7 +1424,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
)
mocker.patch(
"subprocess.Popen.communicate",
side_effect=[("/prefix", None), ("/prefix", None)],
side_effect=[("/prefix", None), ("/prefix", None), ("/prefix", None)],
)
m = mocker.patch("poetry.utils.env.EnvManager.build_venv")

Expand Down

0 comments on commit 1a35032

Please sign in to comment.