From 144555c4208f78912f0c90813c35930f35258f34 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 5 Aug 2020 14:59:58 +0200 Subject: [PATCH 1/7] * detect .venv folder in case `in-project` is `true` or `None` * update docs * add tests --- docs/docs/configuration.md | 4 ++++ poetry/utils/env.py | 7 ++++--- tests/utils/test_env.py | 26 ++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 88d38bd03ee..5962c1b3ed3 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -112,6 +112,10 @@ Defaults to `true`. Create the virtualenv inside the project's root directory. Defaults to `false`. +If set to `true`, the virtualenv wil be created and expected in a folder named `.venv` within the root directory of the project. + +If set to `None`, `poetry` will use the virtualenv from the `.venv` directory only if available. + ### `virtualenvs.path`: string Directory where virtual environments will be created. diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 7a59e8bcfde..00a50c5de22 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -347,9 +347,10 @@ 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: + venv = cwd / ".venv" - return VirtualEnv(venv) + return VirtualEnv(venv) create_venv = self._poetry.config.get("virtualenvs.create", True) @@ -1016,8 +1017,8 @@ def get_paths(self): # type: () -> Dict[str, str] # on some distributions it does not return the proper paths # (those used by pip for instance). We go through distutils # to get the proper ones. - from distutils.command.install import SCHEME_KEYS # noqa from distutils.core import Distribution + from distutils.command.install import SCHEME_KEYS # noqa d = Distribution() d.parse_config_files() diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 4b2cbc786d9..641b43b0bc5 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -74,17 +74,35 @@ 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): +@pytest.mark.parametrize("in_project", [True, None]) +def test_env_get_in_project_venv_with_venv_folder_present(manager, poetry, in_project): if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"] - (poetry.file.parent / ".venv").mkdir() + venv_folder = Path(poetry.file.parent) / ".venv" + venv_folder.mkdir() + poetry.config.config["virtualenvs"]["in-project"] = in_project venv = manager.get() - assert venv.path == poetry.file.parent / ".venv" + assert venv.path == venv_folder - shutil.rmtree(str(venv.path)) + shutil.rmtree(str(venv_folder)) + + +def test_env_get_venv_with_venv_folder_present(manager, poetry): + if "VIRTUAL_ENV" in os.environ: + del os.environ["VIRTUAL_ENV"] + + venv_folder = Path(poetry.file.parent) / ".venv" + venv_folder.mkdir() + poetry.config.config["virtualenvs"]["in-project"] = False + + venv = manager.get() + + assert venv.path != venv_folder + + shutil.rmtree(str(venv_folder)) def build_venv(path, executable=None): # type: (Union[Path,str], Optional[str]) -> () From 72b88e393d64964388496319fcd555912e6cf489 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 5 Aug 2020 18:27:18 +0200 Subject: [PATCH 2/7] run `isort` via pre-commit hook --- docs/docs/configuration.md | 2 +- poetry/installation/chooser.py | 1 - poetry/utils/env.py | 7 ++++--- tests/installation/test_chef.py | 1 - tests/installation/test_chooser.py | 1 - tests/repositories/test_installed_repository.py | 3 +-- 6 files changed, 6 insertions(+), 9 deletions(-) diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 5962c1b3ed3..884ed3d22ef 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -114,7 +114,7 @@ Defaults to `false`. If set to `true`, the virtualenv wil be created and expected in a folder named `.venv` within the root directory of the project. -If set to `None`, `poetry` will use the virtualenv from the `.venv` directory only if available. +If set to `None`, `poetry` will use the virtualenv from the `.venv` directory only if available. ### `virtualenvs.path`: string diff --git a/poetry/installation/chooser.py b/poetry/installation/chooser.py index db9e0a9c357..ee825bc9740 100644 --- a/poetry/installation/chooser.py +++ b/poetry/installation/chooser.py @@ -4,7 +4,6 @@ from typing import Tuple from packaging.tags import Tag - from poetry.core.packages.package import Package from poetry.core.packages.utils.link import Link from poetry.repositories.pool import Pool diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 00a50c5de22..c703e2c8eb8 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -17,16 +17,17 @@ from typing import Tuple from typing import Union -import packaging.tags import tomlkit -import virtualenv from clikit.api.io import IO + +import packaging.tags +import virtualenv + from packaging.tags import Tag from packaging.tags import interpreter_name from packaging.tags import interpreter_version from packaging.tags import sys_tags - from poetry.core.semver import parse_constraint from poetry.core.semver.version import Version from poetry.core.version.markers import BaseMarker diff --git a/tests/installation/test_chef.py b/tests/installation/test_chef.py index 9fcbbea184c..93e1823fdb8 100644 --- a/tests/installation/test_chef.py +++ b/tests/installation/test_chef.py @@ -1,5 +1,4 @@ from packaging.tags import Tag - from poetry.core.packages.utils.link import Link from poetry.installation.chef import Chef from poetry.utils._compat import Path diff --git a/tests/installation/test_chooser.py b/tests/installation/test_chooser.py index cf3f931b942..acaf4d5f620 100644 --- a/tests/installation/test_chooser.py +++ b/tests/installation/test_chooser.py @@ -3,7 +3,6 @@ import pytest from packaging.tags import Tag - from poetry.core.packages.package import Package from poetry.installation.chooser import Chooser from poetry.repositories.legacy_repository import LegacyRepository diff --git a/tests/repositories/test_installed_repository.py b/tests/repositories/test_installed_repository.py index 79ac262671c..7b535852084 100644 --- a/tests/repositories/test_installed_repository.py +++ b/tests/repositories/test_installed_repository.py @@ -2,8 +2,6 @@ import pytest -from pytest_mock.plugin import MockFixture - from poetry.core.packages import Package from poetry.repositories.installed_repository import InstalledRepository from poetry.utils._compat import PY36 @@ -11,6 +9,7 @@ from poetry.utils._compat import metadata from poetry.utils._compat import zipp from poetry.utils.env import MockEnv as BaseMockEnv +from pytest_mock.plugin import MockFixture FIXTURES_DIR = Path(__file__).parent / "fixtures" From 13c615b4305da95dfd348676e4859c5c2a90eb7f Mon Sep 17 00:00:00 2001 From: finswimmer Date: Mon, 21 Sep 2020 20:46:19 +0200 Subject: [PATCH 3/7] run isort once again --- poetry/installation/chooser.py | 1 + poetry/utils/env.py | 9 ++++----- tests/installation/test_chef.py | 1 + tests/installation/test_chooser.py | 1 + tests/repositories/test_installed_repository.py | 3 ++- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/poetry/installation/chooser.py b/poetry/installation/chooser.py index ee825bc9740..db9e0a9c357 100644 --- a/poetry/installation/chooser.py +++ b/poetry/installation/chooser.py @@ -4,6 +4,7 @@ from typing import Tuple from packaging.tags import Tag + from poetry.core.packages.package import Package from poetry.core.packages.utils.link import Link from poetry.repositories.pool import Pool diff --git a/poetry/utils/env.py b/poetry/utils/env.py index c703e2c8eb8..c361f76d434 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -17,17 +17,16 @@ from typing import Tuple from typing import Union -import tomlkit - -from clikit.api.io import IO - import packaging.tags +import tomlkit import virtualenv +from clikit.api.io import IO from packaging.tags import Tag from packaging.tags import interpreter_name from packaging.tags import interpreter_version from packaging.tags import sys_tags + from poetry.core.semver import parse_constraint from poetry.core.semver.version import Version from poetry.core.version.markers import BaseMarker @@ -1018,8 +1017,8 @@ def get_paths(self): # type: () -> Dict[str, str] # on some distributions it does not return the proper paths # (those used by pip for instance). We go through distutils # to get the proper ones. - from distutils.core import Distribution from distutils.command.install import SCHEME_KEYS # noqa + from distutils.core import Distribution d = Distribution() d.parse_config_files() diff --git a/tests/installation/test_chef.py b/tests/installation/test_chef.py index 93e1823fdb8..9fcbbea184c 100644 --- a/tests/installation/test_chef.py +++ b/tests/installation/test_chef.py @@ -1,4 +1,5 @@ from packaging.tags import Tag + from poetry.core.packages.utils.link import Link from poetry.installation.chef import Chef from poetry.utils._compat import Path diff --git a/tests/installation/test_chooser.py b/tests/installation/test_chooser.py index acaf4d5f620..cf3f931b942 100644 --- a/tests/installation/test_chooser.py +++ b/tests/installation/test_chooser.py @@ -3,6 +3,7 @@ import pytest from packaging.tags import Tag + from poetry.core.packages.package import Package from poetry.installation.chooser import Chooser from poetry.repositories.legacy_repository import LegacyRepository diff --git a/tests/repositories/test_installed_repository.py b/tests/repositories/test_installed_repository.py index 7b535852084..79ac262671c 100644 --- a/tests/repositories/test_installed_repository.py +++ b/tests/repositories/test_installed_repository.py @@ -2,6 +2,8 @@ import pytest +from pytest_mock.plugin import MockFixture + from poetry.core.packages import Package from poetry.repositories.installed_repository import InstalledRepository from poetry.utils._compat import PY36 @@ -9,7 +11,6 @@ from poetry.utils._compat import metadata from poetry.utils._compat import zipp from poetry.utils.env import MockEnv as BaseMockEnv -from pytest_mock.plugin import MockFixture FIXTURES_DIR = Path(__file__).parent / "fixtures" From 32da3d921e53acc2019b7eb289afe7e52b56a24b Mon Sep 17 00:00:00 2001 From: finswimmer Date: Mon, 21 Sep 2020 21:10:07 +0200 Subject: [PATCH 4/7] Update docs/docs/configuration.md Co-authored-by: Arun Babu Neelicattu --- docs/docs/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 884ed3d22ef..26978f3be2a 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -114,7 +114,7 @@ Defaults to `false`. If set to `true`, the virtualenv wil be created and expected in a folder named `.venv` within the root directory of the project. -If set to `None`, `poetry` will use the virtualenv from the `.venv` directory only if available. +If not set explicitly, `poetry` will use the virtualenv from the `.venv` directory only if available. ### `virtualenvs.path`: string From da7886f997906ee3305b12c299d1cdcdb480b484 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Tue, 22 Sep 2020 19:43:41 +0200 Subject: [PATCH 5/7] change (utils.env): check if virtualenvs.in-project is not false before checking if .venv folder exists --- poetry/utils/env.py | 4 ++-- tests/utils/test_env.py | 46 +++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/poetry/utils/env.py b/poetry/utils/env.py index c361f76d434..ffcc59c8ef8 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -346,8 +346,8 @@ 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(): - if self._poetry.config.get("virtualenvs.in-project") is not False: + 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) diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 641b43b0bc5..67ffca368b5 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -74,35 +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") -@pytest.mark.parametrize("in_project", [True, None]) -def test_env_get_in_project_venv_with_venv_folder_present(manager, poetry, in_project): - if "VIRTUAL_ENV" in os.environ: - del os.environ["VIRTUAL_ENV"] - - venv_folder = Path(poetry.file.parent) / ".venv" - venv_folder.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 == venv_folder - - shutil.rmtree(str(venv_folder)) - - -def test_env_get_venv_with_venv_folder_present(manager, poetry): - if "VIRTUAL_ENV" in os.environ: - del os.environ["VIRTUAL_ENV"] - - venv_folder = Path(poetry.file.parent) / ".venv" - venv_folder.mkdir() - poetry.config.config["virtualenvs"]["in-project"] = False - venv = manager.get() - - assert venv.path != venv_folder - - shutil.rmtree(str(venv_folder)) + 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]) -> () From a19d1411a1de0e3a5ef4821f67332ce839bbe584 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 23 Sep 2020 19:04:49 +0200 Subject: [PATCH 6/7] change (config.config): setting default value for in-project to None --- docs/docs/configuration.md | 7 ++++--- poetry/config/config.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 26978f3be2a..bedac73c796 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -33,7 +33,7 @@ which will give you something similar to this: ```toml cache-dir = "/path/to/cache/directory" virtualenvs.create = true -virtualenvs.in-project = false +virtualenvs.in-project = null virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs ``` @@ -110,11 +110,12 @@ Defaults to `true`. ### `virtualenvs.in-project`: boolean Create the virtualenv inside the project's root directory. -Defaults to `false`. +Defaults to `None`. 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. +If not set explicitly (default), `poetry` will use the virtualenv from the `.venv` directory when one is available. If set to `false`, `poetry` will ignore any existing `.venv` directory. + ### `virtualenvs.path`: string diff --git a/poetry/config/config.py b/poetry/config/config.py index dbece199237..be585575c05 100644 --- a/poetry/config/config.py +++ b/poetry/config/config.py @@ -34,7 +34,7 @@ class Config(object): "cache-dir": str(CACHE_DIR), "virtualenvs": { "create": True, - "in-project": False, + "in-project": None, "path": os.path.join("{cache-dir}", "virtualenvs"), }, "experimental": {"new-installer": True}, From c972fd57e18e04db85f181c1bb6df395e8ccecbd Mon Sep 17 00:00:00 2001 From: finswimmer Date: Wed, 23 Sep 2020 19:08:48 +0200 Subject: [PATCH 7/7] fix: fix tests for config to respect new default value for in-project --- tests/console/commands/test_config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/console/commands/test_config.py b/tests/console/commands/test_config.py index 14048090146..534e4de2fc6 100644 --- a/tests/console/commands/test_config.py +++ b/tests/console/commands/test_config.py @@ -15,7 +15,7 @@ def test_list_displays_default_value_if_not_set(app, config): expected = """cache-dir = "/foo" experimental.new-installer = true virtualenvs.create = true -virtualenvs.in-project = false +virtualenvs.in-project = null virtualenvs.path = {path} # /foo{sep}virtualenvs """.format( path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep @@ -35,7 +35,7 @@ def test_list_displays_set_get_setting(app, config): expected = """cache-dir = "/foo" experimental.new-installer = true virtualenvs.create = false -virtualenvs.in-project = false +virtualenvs.in-project = null virtualenvs.path = {path} # /foo{sep}virtualenvs """.format( path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep @@ -83,7 +83,7 @@ def test_list_displays_set_get_local_setting(app, config): expected = """cache-dir = "/foo" experimental.new-installer = true virtualenvs.create = false -virtualenvs.in-project = false +virtualenvs.in-project = null virtualenvs.path = {path} # /foo{sep}virtualenvs """.format( path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep