Commit 81b27746 by finswimmer Committed by GitHub

Respect in-project config even when .venv exists

Before this change, when .venv directory exists  within the project 
root, poetry will use it as the path to the venv regardless whether 
virtualenvs.in-project is set to true or not. This leads to confusion
as described in #1770 and #2756.

With this change poetry will check if virtualenvs.in-project is set to
true if it finds a .venv folder.

This commit also changes the default state of this configuration to be
unset (null), in order to not break any current environments.

Resolves: #1770 #2756
parent 32f11c3e
...@@ -33,7 +33,7 @@ which will give you something similar to this: ...@@ -33,7 +33,7 @@ which will give you something similar to this:
```toml ```toml
cache-dir = "/path/to/cache/directory" cache-dir = "/path/to/cache/directory"
virtualenvs.create = true virtualenvs.create = true
virtualenvs.in-project = false virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs
``` ```
...@@ -110,7 +110,12 @@ Defaults to `true`. ...@@ -110,7 +110,12 @@ Defaults to `true`.
### `virtualenvs.in-project`: boolean ### `virtualenvs.in-project`: boolean
Create the virtualenv inside the project's root directory. 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 ### `virtualenvs.path`: string
......
...@@ -34,7 +34,7 @@ class Config(object): ...@@ -34,7 +34,7 @@ class Config(object):
"cache-dir": str(CACHE_DIR), "cache-dir": str(CACHE_DIR),
"virtualenvs": { "virtualenvs": {
"create": True, "create": True,
"in-project": False, "in-project": None,
"path": os.path.join("{cache-dir}", "virtualenvs"), "path": os.path.join("{cache-dir}", "virtualenvs"),
}, },
"experimental": {"new-installer": True}, "experimental": {"new-installer": True},
......
...@@ -346,6 +346,7 @@ class EnvManager(object): ...@@ -346,6 +346,7 @@ class EnvManager(object):
if not in_venv or env is not None: if not in_venv or env is not None:
# Checking if a local virtualenv exists # Checking if a local virtualenv exists
if self._poetry.config.get("virtualenvs.in-project") is not False:
if (cwd / ".venv").exists() and (cwd / ".venv").is_dir(): if (cwd / ".venv").exists() and (cwd / ".venv").is_dir():
venv = cwd / ".venv" venv = cwd / ".venv"
......
...@@ -15,7 +15,7 @@ def test_list_displays_default_value_if_not_set(app, config): ...@@ -15,7 +15,7 @@ def test_list_displays_default_value_if_not_set(app, config):
expected = """cache-dir = "/foo" expected = """cache-dir = "/foo"
experimental.new-installer = true experimental.new-installer = true
virtualenvs.create = true virtualenvs.create = true
virtualenvs.in-project = false virtualenvs.in-project = null
virtualenvs.path = {path} # /foo{sep}virtualenvs virtualenvs.path = {path} # /foo{sep}virtualenvs
""".format( """.format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep 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): ...@@ -35,7 +35,7 @@ def test_list_displays_set_get_setting(app, config):
expected = """cache-dir = "/foo" expected = """cache-dir = "/foo"
experimental.new-installer = true experimental.new-installer = true
virtualenvs.create = false virtualenvs.create = false
virtualenvs.in-project = false virtualenvs.in-project = null
virtualenvs.path = {path} # /foo{sep}virtualenvs virtualenvs.path = {path} # /foo{sep}virtualenvs
""".format( """.format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep 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): ...@@ -83,7 +83,7 @@ def test_list_displays_set_get_local_setting(app, config):
expected = """cache-dir = "/foo" expected = """cache-dir = "/foo"
experimental.new-installer = true experimental.new-installer = true
virtualenvs.create = false virtualenvs.create = false
virtualenvs.in-project = false virtualenvs.in-project = null
virtualenvs.path = {path} # /foo{sep}virtualenvs virtualenvs.path = {path} # /foo{sep}virtualenvs
""".format( """.format(
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), sep=os.path.sep
......
...@@ -74,17 +74,27 @@ def test_virtualenvs_with_spaces_in_their_path_work_as_expected(tmp_dir, manager ...@@ -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") assert venv.run("python", "-V", shell=True).startswith("Python")
def test_env_get_in_project_venv(manager, poetry): @pytest.fixture
if "VIRTUAL_ENV" in os.environ: def in_project_venv_dir(poetry):
del os.environ["VIRTUAL_ENV"] os.environ.pop("VIRTUAL_ENV", None)
venv_dir = poetry.file.parent.joinpath(".venv")
(poetry.file.parent / ".venv").mkdir() 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() venv = manager.get()
if in_project is False:
assert venv.path == poetry.file.parent / ".venv" assert venv.path != in_project_venv_dir
else:
shutil.rmtree(str(venv.path)) assert venv.path == in_project_venv_dir
def build_venv(path, executable=None): # type: (Union[Path,str], Optional[str]) -> () def build_venv(path, executable=None): # type: (Union[Path,str], Optional[str]) -> ()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment