From 6987aaa83e127a4cf62942da0a630cad14585298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Sat, 14 Dec 2019 23:06:19 +0100 Subject: [PATCH] Fix the python patch version retrieval when passing an executable (#1736) Fixes #1735 --- poetry/utils/env.py | 7 ++++--- tests/utils/test_env.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/poetry/utils/env.py b/poetry/utils/env.py index 99be86b9488..de7ba662bc7 100644 --- a/poetry/utils/env.py +++ b/poetry/utils/env.py @@ -494,18 +494,19 @@ def create_venv( python_patch = ".".join([str(v) for v in sys.version_info[:3]]) python_minor = ".".join([str(v) for v in sys.version_info[:2]]) if executable: - python_minor = decode( + python_patch = decode( subprocess.check_output( " ".join( [ executable, "-c", - "\"import sys; print('.'.join([str(s) for s in sys.version_info[:2]]))\"", + "\"import sys; print('.'.join([str(s) for s in sys.version_info[:3]]))\"", ] ), shell=True, ).strip() ) + python_minor = ".".join(python_patch.split(".")[:2]) supported_python = self._poetry.package.python_constraint if not supported_python.allows(Version.parse(python_patch)): @@ -517,7 +518,7 @@ def create_venv( # Otherwise, we try to find a compatible Python version. if executable: raise NoCompatiblePythonVersionFound( - self._poetry.package.python_versions, python_minor + self._poetry.package.python_versions, python_patch ) io.write_line( diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 5bff3df0b9b..b8b299ae37b 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -700,6 +700,45 @@ def test_create_venv_uses_patch_version_to_detect_compatibility( ) +def test_create_venv_uses_patch_version_to_detect_compatibility_with_executable( + manager, poetry, config, mocker +): + if "VIRTUAL_ENV" in os.environ: + del os.environ["VIRTUAL_ENV"] + + version = Version.parse(".".join(str(c) for c in sys.version_info[:3])) + poetry.package.python_versions = "~{}".format( + ".".join(str(c) for c in (version.major, version.minor - 1, 0)) + ) + venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent)) + + check_output = mocker.patch( + "poetry.utils._compat.subprocess.check_output", + side_effect=check_output_wrapper( + Version.parse("{}.{}.0".format(version.major, version.minor - 1)) + ), + ) + m = mocker.patch( + "poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: "" + ) + + manager.create_venv( + NullIO(), executable="python{}.{}".format(version.major, version.minor - 1) + ) + + assert check_output.called + m.assert_called_with( + str( + Path( + "/foo/virtualenvs/{}-py{}.{}".format( + venv_name, version.major, version.minor - 1 + ) + ) + ), + executable="python{}.{}".format(version.major, version.minor - 1), + ) + + def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir( manager, poetry, config, tmp_dir, mocker ):