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
… new`
  • Loading branch information
finswimmer committed Feb 4, 2023
1 parent 24e69fa commit 526cde9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def handle(self) -> int:
default_python = (
"^"
+ EnvManager.get_python_version(
precious=2,
precision=2,
prefer_active_python=config.get("virtualenvs.prefer-active-python"),
io=self.io,
).to_string()
Expand Down
18 changes: 13 additions & 5 deletions src/poetry/console/commands/new.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import sys

from contextlib import suppress

from cleo.helpers import argument
Expand Down Expand Up @@ -31,8 +29,9 @@ def handle(self) -> int:

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

if self.io.input.option("directory"):
self.line_error(
Expand Down Expand Up @@ -71,8 +70,17 @@ def handle(self) -> int:
if author_email:
author += f" <{author_email}>"

current_env = SystemEnv(Path(sys.executable))
default_python = "^" + ".".join(str(v) for v in current_env.version_info[:2])
poetry_config = Config.create()
default_python = (
"^"
+ EnvManager.get_python_version(
precision=2,
prefer_active_python=poetry_config.get(
"virtualenvs.prefer-active-python"
),
io=self.io,
).to_string()
)

layout_ = layout_cls(
name,
Expand Down
8 changes: 4 additions & 4 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ def _detect_active_python(io: None | IO = None) -> str | None:

@staticmethod
def get_python_version(
precious: int = 3,
precision: int = 3,
prefer_active_python: bool = False,
io: None | IO = None,
) -> Version:
version = ".".join(str(v) for v in sys.version_info[:precious])
version = ".".join(str(v) for v in sys.version_info[:precision])

if prefer_active_python:
executable = EnvManager._detect_active_python(io)
Expand All @@ -582,7 +582,7 @@ def get_python_version(
).strip()
)

version = ".".join(str(v) for v in python_patch.split(".")[:precious])
version = ".".join(str(v) for v in python_patch.split(".")[:precision])

return Version.parse(version)

Expand Down Expand Up @@ -701,7 +701,7 @@ def get(self, reload: bool = False) -> Env:
"virtualenvs.prefer-active-python"
)
python_minor = self.get_python_version(
precious=2, prefer_active_python=prefer_active_python, io=self._io
precision=2, prefer_active_python=prefer_active_python, io=self._io
).to_string()

venv_path = self._poetry.config.virtualenvs_path
Expand Down
50 changes: 50 additions & 0 deletions tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

import subprocess
import sys

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

import pytest

Expand All @@ -10,7 +14,9 @@

if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture

from poetry.config.config import Config
from poetry.poetry import Poetry
from tests.types import CommandTesterFactory

Expand Down Expand Up @@ -170,3 +176,47 @@ def test_command_new_with_readme(fmt: str | None, tester: CommandTester, tmp_dir

poetry = verify_project_directory(path, package, package, None)
assert poetry.local_config.get("readme") == f"README.{fmt or 'md'}"


@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_new(
prefer_active: bool,
python: str,
config: Config,
mocker: MockerFixture,
tester: CommandTester,
tmp_dir: str,
):
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

package = "package"
path = Path(tmp_dir) / package
options = [path.as_posix()]
tester.execute(" ".join(options))

pyproject_file = path / "pyproject.toml"

expected = f"""\
[tool.poetry.dependencies]
python = "^{python}"
"""

assert expected in pyproject_file.read_text()

0 comments on commit 526cde9

Please sign in to comment.