|
21 | 21 | from poetry.utils.env import EnvManager |
22 | 22 | from poetry.utils.env import IncorrectEnvError |
23 | 23 | from poetry.utils.env.env_manager import EnvsFile |
| 24 | +from poetry.utils.env.mock_env import MockEnv |
24 | 25 | from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError |
25 | 26 | from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError |
26 | 27 | from poetry.utils.env.python.exceptions import PythonVersionNotFoundError |
|
32 | 33 | from collections.abc import Iterator |
33 | 34 | from unittest.mock import MagicMock |
34 | 35 |
|
| 36 | + from _pytest.monkeypatch import MonkeyPatch |
35 | 37 | from cleo.io.buffered_io import BufferedIO |
36 | 38 | from pytest import LogCaptureFixture |
37 | 39 | from pytest_mock import MockerFixture |
@@ -1069,6 +1071,50 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found( |
1069 | 1071 | assert m.call_count == 0 |
1070 | 1072 |
|
1071 | 1073 |
|
| 1074 | +@pytest.mark.parametrize("use_poetry_python", [True, False]) |
| 1075 | +def test_create_venv_fails_if_current_python_is_not_supported_without_creating_venv( |
| 1076 | + manager: EnvManager, |
| 1077 | + poetry: Poetry, |
| 1078 | + config: Config, |
| 1079 | + mocker: MockerFixture, |
| 1080 | + mocked_python_register: MockedPythonRegister, |
| 1081 | + monkeypatch: MonkeyPatch, |
| 1082 | + use_poetry_python: bool, |
| 1083 | +) -> None: |
| 1084 | + config.config["virtualenvs"]["create"] = False |
| 1085 | + config.config["virtualenvs"]["use-poetry-python"] = use_poetry_python |
| 1086 | + monkeypatch.delenv("VIRTUAL_ENV", raising=False) |
| 1087 | + |
| 1088 | + poetry.package.python_versions = "^3.10" |
| 1089 | + |
| 1090 | + current_python = mocked_python_register("3.9.0") |
| 1091 | + compatible_python = mocked_python_register("3.10.0") |
| 1092 | + mocker.patch.object(manager, "get", return_value=MockEnv(version_info=(3, 9, 0))) |
| 1093 | + mocker.patch( |
| 1094 | + "poetry.utils.env.env_manager.Python.get_preferred_python", |
| 1095 | + return_value=current_python if use_poetry_python else compatible_python, |
| 1096 | + ) |
| 1097 | + get_compatible_python = mocker.patch( |
| 1098 | + "poetry.utils.env.env_manager.Python.get_compatible_python", |
| 1099 | + return_value=compatible_python, |
| 1100 | + ) |
| 1101 | + build_venv = mocker.patch("poetry.utils.env.EnvManager.build_venv") |
| 1102 | + |
| 1103 | + with pytest.raises(InvalidCurrentPythonVersionError) as e: |
| 1104 | + manager.create_venv() |
| 1105 | + |
| 1106 | + expected_message = ( |
| 1107 | + "Current Python version (3.9.0) is not allowed by the project (^3.10).\n" |
| 1108 | + 'Please change python executable via the "env use" command.\n' |
| 1109 | + "Poetry cannot switch to a compatible Python version because virtualenv " |
| 1110 | + "creation is disabled." |
| 1111 | + ) |
| 1112 | + |
| 1113 | + assert str(e.value) == expected_message |
| 1114 | + get_compatible_python.assert_not_called() |
| 1115 | + build_venv.assert_not_called() |
| 1116 | + |
| 1117 | + |
1072 | 1118 | @pytest.mark.parametrize("use_poetry_python", [True, False]) |
1073 | 1119 | def test_create_venv_does_not_try_to_find_compatible_versions_with_executable( |
1074 | 1120 | manager: EnvManager, |
|
0 commit comments