Skip to content

Commit

Permalink
Fix an error in env use if the virtualenvs.in-project setting is acti…
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored and shenek committed Dec 31, 2019
1 parent 27abb49 commit f61eff9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
18 changes: 18 additions & 0 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
35 changes: 35 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit f61eff9

Please sign in to comment.