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(env): get full python path for appropriate executable #7221

Merged
merged 2 commits into from
Jan 21, 2023
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
4 changes: 2 additions & 2 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def _detect_active_python(self) -> str | None:
self._io.write_error_line(
f"Found: {executable}", verbosity=Verbosity.VERBOSE
)
except CalledProcessError:
except EnvCommandError:
self._io.write_error_line(
(
"Unable to detect the current active python executable. Falling"
Expand Down Expand Up @@ -995,7 +995,7 @@ def create_venv(
self._io.write_error_line(
f"Using <c1>{python}</c1> ({python_patch})"
)
executable = python
executable = self._full_python_path(python)
python_minor = ".".join(python_patch.split(".")[:2])
break

Expand Down
38 changes: 30 additions & 8 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils._compat import WINDOWS
from poetry.utils.env import GET_BASE_PREFIX
from poetry.utils.env import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env import EnvCommandError
from poetry.utils.env import EnvManager
from poetry.utils.env import GenericEnv
Expand Down Expand Up @@ -1002,7 +1003,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_generic_

m.assert_called_with(
config_virtualenvs_path / f"{venv_name}-py3.7",
executable="python3",
executable="/usr/bin/python3",
flags={
"always-copy": False,
"system-site-packages": False,
Expand All @@ -1027,7 +1028,9 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific
poetry.package.python_versions = "^3.6"

mocker.patch("sys.version_info", (2, 7, 16))
mocker.patch("subprocess.check_output", side_effect=["3.5.3", "3.9.0"])
mocker.patch(
"subprocess.check_output", side_effect=["3.5.3", "3.9.0", "/usr/bin/python3.9"]
)
m = mocker.patch(
"poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
)
Expand All @@ -1036,7 +1039,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific

m.assert_called_with(
config_virtualenvs_path / f"{venv_name}-py3.9",
executable="python3.9",
executable="/usr/bin/python3.9",
flags={
"always-copy": False,
"system-site-packages": False,
Expand Down Expand Up @@ -1459,11 +1462,18 @@ def test_create_venv_accepts_fallback_version_w_nonzero_patchlevel(

poetry.package.python_versions = "~3.5.1"

def mock_check_output(cmd: str, *args: Any, **kwargs: Any) -> str:
if GET_PYTHON_VERSION_ONELINER in cmd:
if "python3.5" in cmd:
return "3.5.12"
else:
return "3.7.1"
else:
return "/usr/bin/python3.5"

check_output = mocker.patch(
"subprocess.check_output",
side_effect=lambda cmd, *args, **kwargs: str(
"3.5.12" if "python3.5" in cmd else "3.7.1"
),
side_effect=mock_check_output,
)
m = mocker.patch(
"poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
Expand All @@ -1474,7 +1484,7 @@ def test_create_venv_accepts_fallback_version_w_nonzero_patchlevel(
assert check_output.called
m.assert_called_with(
config_virtualenvs_path / f"{venv_name}-py3.5",
executable="python3.5",
executable="/usr/bin/python3.5",
flags={
"always-copy": False,
"system-site-packages": False,
Expand Down Expand Up @@ -1582,7 +1592,7 @@ def test_create_venv_project_name_empty_sets_correct_prompt(

m.assert_called_with(
config_virtualenvs_path / f"{venv_name}-py3.7",
executable="python3",
executable="/usr/bin/python3",
flags={
"always-copy": False,
"system-site-packages": False,
Expand All @@ -1591,3 +1601,15 @@ def test_create_venv_project_name_empty_sets_correct_prompt(
},
prompt="virtualenv-py3.7",
)


def test_fallback_on_detect_active_python(poetry: Poetry, mocker: MockerFixture):
m = mocker.patch(
"subprocess.check_output",
side_effect=subprocess.CalledProcessError(1, "some command"),
)
env_manager = EnvManager(poetry)
active_python = env_manager._detect_active_python()

assert active_python is None
assert m.call_count == 1