From f61eff96ecfbbf175d8229923a0f589bc3708dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Fri, 6 Dec 2019 15:14:24 +0100 Subject: [PATCH] Fix an error in env use if the virtualenvs.in-project setting is activated (#1682) --- poetry/utils/env.py | 18 ++++++++++++++++++ tests/utils/test_env.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 79dd557f439..922333433c8 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -208,6 +208,24 @@ def activate(self, python, io): # type: (str, IO) -> Env patch = python_version.text create = False + is_root_venv = self._poetry.config.get("virtualenvs.in-project") + # If we are required to create the virtual environment in the root folder, + # create or recreate it if needed + if is_root_venv: + create = False + venv = self._poetry.file.parent / ".venv" + if venv.exists(): + # We need to check if the patch version is correct + _venv = VirtualEnv(venv) + current_patch = ".".join(str(v) for v in _venv.version_info[:3]) + + if patch != current_patch: + create = True + + self.create_venv(io, executable=python, force=create) + + return self.get(reload=True) + envs = tomlkit.document() base_env_name = self.generate_env_name(self._poetry.package.name, str(cwd)) if envs_file.exists(): diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 6c9433477a8..5aa5d62ac56 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -661,3 +661,38 @@ def test_create_venv_does_not_try_to_find_compatible_versions_with_executable( assert expected_message == str(e.value) assert 0 == m.call_count + + +def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir( + manager, poetry, config, tmp_dir, mocker +): + if "VIRTUAL_ENV" in os.environ: + del os.environ["VIRTUAL_ENV"] + + config.merge( + { + "virtualenvs": { + "path": str(Path(tmp_dir) / "virtualenvs"), + "in-project": True, + } + } + ) + + mocker.patch( + "poetry.utils._compat.subprocess.check_output", + side_effect=check_output_wrapper(), + ) + mocker.patch( + "poetry.utils._compat.subprocess.Popen.communicate", + side_effect=[("/prefix", None), ("/prefix", None)], + ) + m = mocker.patch("poetry.utils.env.EnvManager.build_venv") + + manager.activate("python3.7", NullIO()) + + m.assert_called_with( + os.path.join(str(poetry.file.parent), ".venv"), executable="python3.7" + ) + + envs_file = TomlFile(Path(tmp_dir) / "virtualenvs" / "envs.toml") + assert not envs_file.exists()