Skip to content

Commit

Permalink
Fix the python patch version retrieval when passing an executable (#1736
Browse files Browse the repository at this point in the history
)

Fixes #1735
  • Loading branch information
sdispater committed Dec 14, 2019
1 parent a7792f6 commit 6987aaa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand All @@ -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(
Expand Down
39 changes: 39 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down

0 comments on commit 6987aaa

Please sign in to comment.