Commit 8949bfdc by finswimmer Committed by Randy Döring

feat: take `virtualenvs.prefer-active-python` into account on `poetry init`

parent f57cdc31
from __future__ import annotations
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
......@@ -78,8 +76,9 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
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 @@ The <c1>init</c1> command creates a basic <comment>pyproject.toml</> file in the
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 [<comment>{default_python}</comment>]: ",
default=default_python,
......
......@@ -2,10 +2,12 @@ from __future__ import annotations
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 @@ if TYPE_CHECKING:
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 <you@example.com>' --name 'my-package'",
interactive=False,
)
expected = f"""\
[tool.poetry.dependencies]
python = "^{python}"
"""
assert expected in pyproject_file.read_text()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment