Skip to content

Commit

Permalink
feat: take virtualenvs.prefer-active-python into account on `poetry…
Browse files Browse the repository at this point in the history
… init`
  • Loading branch information
finswimmer authored and radoering committed Feb 4, 2023
1 parent f57cdc3 commit 8949bfd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import sys

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 [<comment>{default_python}</comment>]: ",
default=default_python,
Expand Down
46 changes: 46 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 8949bfd

Please sign in to comment.