Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect virtualenvs.in-project setting, if .venv is found in project folder #2771

Merged
merged 7 commits into from
Sep 23, 2020
4 changes: 4 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ Defaults to `true`.
Create the virtualenv inside the project's root directory.
Defaults to `false`.
abn marked this conversation as resolved.
Show resolved Hide resolved

If set to `true`, the virtualenv wil be created and expected in a folder named `.venv` within the root directory of the project.

If not set explicitly, `poetry` will use the virtualenv from the `.venv` directory only if available.
abn marked this conversation as resolved.
Show resolved Hide resolved

### `virtualenvs.path`: string

Directory where virtual environments will be created.
Expand Down
7 changes: 4 additions & 3 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,11 @@ def get(self, reload=False): # type: (bool) -> Env

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

return VirtualEnv(venv)
return VirtualEnv(venv)

create_venv = self._poetry.config.get("virtualenvs.create", True)

Expand Down
30 changes: 20 additions & 10 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,27 @@ def test_virtualenvs_with_spaces_in_their_path_work_as_expected(tmp_dir, manager
assert venv.run("python", "-V", shell=True).startswith("Python")


def test_env_get_in_project_venv(manager, poetry):
if "VIRTUAL_ENV" in os.environ:
del os.environ["VIRTUAL_ENV"]

(poetry.file.parent / ".venv").mkdir()

@pytest.fixture
def in_project_venv_dir(poetry):
os.environ.pop("VIRTUAL_ENV", None)
venv_dir = poetry.file.parent.joinpath(".venv")
venv_dir.mkdir()
try:
yield venv_dir
finally:
venv_dir.rmdir()


@pytest.mark.parametrize("in_project", [True, False, None])
def test_env_get_venv_with_venv_folder_present(
manager, poetry, in_project_venv_dir, in_project
):
poetry.config.config["virtualenvs"]["in-project"] = in_project
venv = manager.get()

assert venv.path == poetry.file.parent / ".venv"

shutil.rmtree(str(venv.path))
if in_project is False:
assert venv.path != in_project_venv_dir
else:
assert venv.path == in_project_venv_dir


def build_venv(path, executable=None): # type: (Union[Path,str], Optional[str]) -> ()
Expand Down