From 8949bfdccb15383b50b33979f53c15ad0d593671 Mon Sep 17 00:00:00 2001 From: finswimmer Date: Sat, 26 Nov 2022 09:39:44 +0100 Subject: [PATCH] feat: take `virtualenvs.prefer-active-python` into account on `poetry init` --- src/poetry/console/commands/init.py | 17 +++++++---- tests/console/commands/test_init.py | 46 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index 92cd249f084..ee068e9ca31 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -1,7 +1,5 @@ from __future__ import annotations -import sys - from pathlib import Path from typing import TYPE_CHECKING from typing import Any @@ -78,8 +76,9 @@ def handle(self) -> int: from poetry.core.pyproject.toml import PyProjectTOML from poetry.core.vcs.git import GitConfig + from poetry.config.config import Config from poetry.layouts import layout - from poetry.utils.env import SystemEnv + from poetry.utils.env import EnvManager project_path = Path.cwd() @@ -161,10 +160,16 @@ def handle(self) -> int: python = self.option("python") if not python: - current_env = SystemEnv(Path(sys.executable)) - default_python = "^" + ".".join( - str(v) for v in current_env.version_info[:2] + config = Config.create() + default_python = ( + "^" + + EnvManager.get_python_version( + precious=2, + prefer_active_python=config.get("virtualenvs.prefer-active-python"), + io=self.io, + ).to_string() ) + question = self.create_question( f"Compatible Python versions [{default_python}]: ", default=default_python, diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index f78e9a64d17..c4d0171f43a 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -2,10 +2,12 @@ import os import shutil +import subprocess import sys from pathlib import Path from typing import TYPE_CHECKING +from typing import Any import pytest @@ -26,6 +28,7 @@ from poetry.core.packages.package import Package from pytest_mock import MockerFixture + from poetry.config.config import Config from poetry.poetry import Poetry from tests.helpers import TestRepository from tests.types import FixtureDirGetter @@ -1061,3 +1064,46 @@ def test_package_include( f'python = "^3.10"\n' ) assert expected in tester.io.fetch_output() + + +@pytest.mark.parametrize( + ["prefer_active", "python"], + [ + (True, "1.1"), + (False, f"{sys.version_info[0]}.{sys.version_info[1]}"), + ], +) +def test_respect_prefer_active_on_init( + prefer_active: bool, + python: str, + config: Config, + mocker: MockerFixture, + tester: CommandTester, + source_dir: Path, +): + from poetry.utils.env import GET_PYTHON_VERSION_ONELINER + + orig_check_output = subprocess.check_output + + def mock_check_output(cmd: str, *_: Any, **__: Any) -> str: + if GET_PYTHON_VERSION_ONELINER in cmd: + return "1.1.1" + + return orig_check_output(cmd, *_, **__) + + mocker.patch("subprocess.check_output", side_effect=mock_check_output) + + config.config["virtualenvs"]["prefer-active-python"] = prefer_active + pyproject_file = source_dir / "pyproject.toml" + + tester.execute( + "--author 'Your Name ' --name 'my-package'", + interactive=False, + ) + + expected = f"""\ +[tool.poetry.dependencies] +python = "^{python}" +""" + + assert expected in pyproject_file.read_text()