diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 88d38bd03ee..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,7 +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 (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}, diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 7a59e8bcfde..ffcc59c8ef8 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -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) 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 diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 4b2cbc786d9..67ffca368b5 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -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]) -> ()