diff --git a/src/poetry/utils/env/env_manager.py b/src/poetry/utils/env/env_manager.py index 01ee342ffa9..1061ff772ce 100644 --- a/src/poetry/utils/env/env_manager.py +++ b/src/poetry/utils/env/env_manager.py @@ -395,7 +395,6 @@ def create_venv( create_venv = self._poetry.config.get("virtualenvs.create") in_project_venv = self.use_in_project_venv() - use_poetry_python = self._poetry.config.get("virtualenvs.use-poetry-python") venv_prompt = self._poetry.config.get("virtualenvs.prompt") specific_python_requested = python is not None @@ -420,7 +419,7 @@ def create_venv( # If an executable has been specified, we stop there # and notify the user of the incompatibility. # Otherwise, we try to find a compatible Python version. - if specific_python_requested and use_poetry_python: + if specific_python_requested: raise NoCompatiblePythonVersionFoundError( self._poetry.package.python_versions, python.patch_version.to_string(), diff --git a/tests/console/commands/env/conftest.py b/tests/console/commands/env/conftest.py index 7fb3b1502a3..7adbdcdbd92 100644 --- a/tests/console/commands/env/conftest.py +++ b/tests/console/commands/env/conftest.py @@ -26,7 +26,9 @@ def venv_name(app: PoetryTestApplication) -> str: @pytest.fixture def venv_cache(tmp_path: Path) -> Path: - return tmp_path + path = tmp_path / "venv_cache" + path.mkdir() + return path @pytest.fixture(scope="module") diff --git a/tests/console/commands/env/test_use.py b/tests/console/commands/env/test_use.py index f27b1ac0437..e24dd5afedb 100644 --- a/tests/console/commands/env/test_use.py +++ b/tests/console/commands/env/test_use.py @@ -11,8 +11,10 @@ from poetry.core.constraints.version import Version +from poetry.console.commands.env.use import EnvUseCommand from poetry.toml.file import TOMLFile from poetry.utils.env import MockEnv +from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError from tests.console.commands.env.helpers import build_venv from tests.console.commands.env.helpers import check_output_wrapper @@ -66,6 +68,10 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( mock_build_env = mocker.patch( "poetry.utils.env.EnvManager.build_venv", side_effect=build_venv ) + envs_file = TOMLFile(venv_cache / "envs.toml") + + assert not envs_file.exists() + assert not list(venv_cache.iterdir()) tester.execute("3.7") @@ -81,7 +87,6 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( prompt="simple-project-py3.7", ) - envs_file = TOMLFile(venv_cache / "envs.toml") assert envs_file.exists() envs: dict[str, Any] = envs_file.read() assert envs[venv_name]["minor"] == "3.7" @@ -94,6 +99,33 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( assert tester.io.fetch_output() == f"Using virtualenv: {venv_py37}\n" +@pytest.mark.parametrize("use_poetry_python", [True, False]) +def test_activate_does_not_activate_non_existing_virtualenv_with_unsupported_version( + tester: CommandTester, + venv_cache: Path, + venv_name: str, + venvs_in_cache_config: None, + mocked_python_register: MockedPythonRegister, + with_no_active_python: MagicMock, + use_poetry_python: bool, +) -> None: + mocked_python_register("3.7.1") + mocked_python_register("3.8.2") + command = tester.command + assert isinstance(command, EnvUseCommand) + command.poetry.package.python_versions = "~3.8" + command.poetry.config.merge( + {"virtualenvs": {"use-poetry-python": use_poetry_python}} + ) + + assert not list(venv_cache.iterdir()) + + with pytest.raises(NoCompatiblePythonVersionFoundError): + tester.execute("3.7") + + assert not list(venv_cache.iterdir()) + + def test_get_prefers_explicitly_activated_virtualenvs_over_env_var( tester: CommandTester, current_python: PythonVersion, diff --git a/tests/utils/env/test_env_manager.py b/tests/utils/env/test_env_manager.py index 134a420225f..df5cbf1a0af 100644 --- a/tests/utils/env/test_env_manager.py +++ b/tests/utils/env/test_env_manager.py @@ -991,14 +991,16 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found( assert m.call_count == 0 +@pytest.mark.parametrize("use_poetry_python", [True, False]) def test_create_venv_does_not_try_to_find_compatible_versions_with_executable( manager: EnvManager, poetry: Poetry, config: Config, mocker: MockerFixture, mocked_python_register: MockedPythonRegister, + use_poetry_python: bool, ) -> None: - config.config["virtualenvs"]["use-poetry-python"] = True + config.config["virtualenvs"]["use-poetry-python"] = use_poetry_python if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"]