Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the python patch version retrieval when passing an executable #1736

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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