Skip to content

Commit 7f4d47f

Browse files
committed
feat: take virtualenvs.prefer-active-python into account on poetry new
1 parent 159426d commit 7f4d47f

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/poetry/console/commands/new.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import sys
4-
53
from contextlib import suppress
64

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

3230
from poetry.core.vcs.git import GitConfig
3331

32+
from poetry.config.config import Config
3433
from poetry.layouts import layout
35-
from poetry.utils.env import SystemEnv
34+
from poetry.utils.env import EnvManager
3635

3736
if self.io.input.option("directory"):
3837
self.line_error(
@@ -71,8 +70,17 @@ def handle(self) -> int:
7170
if author_email:
7271
author += f" <{author_email}>"
7372

74-
current_env = SystemEnv(Path(sys.executable))
75-
default_python = "^" + ".".join(str(v) for v in current_env.version_info[:2])
73+
poetry_config = Config.create()
74+
default_python = (
75+
"^"
76+
+ EnvManager.get_python_version(
77+
precious=2,
78+
prefer_active_python=poetry_config.get(
79+
"virtualenvs.prefer-active-python"
80+
),
81+
io=self.io,
82+
).to_string()
83+
)
7684

7785
layout_ = layout_cls(
7886
name,

tests/console/commands/test_new.py

+50
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

3+
import subprocess
4+
import sys
5+
36
from pathlib import Path
47
from typing import TYPE_CHECKING
8+
from typing import Any
59

610
import pytest
711

@@ -10,7 +14,9 @@
1014

1115
if TYPE_CHECKING:
1216
from cleo.testers.command_tester import CommandTester
17+
from pytest_mock import MockerFixture
1318

19+
from poetry.config.config import Config
1420
from poetry.poetry import Poetry
1521
from tests.types import CommandTesterFactory
1622

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

171177
poetry = verify_project_directory(path, package, package, None)
172178
assert poetry.local_config.get("readme") == f"README.{fmt or 'md'}"
179+
180+
181+
@pytest.mark.parametrize(
182+
["prefer_active", "python"],
183+
[
184+
(True, "1.1"),
185+
(False, f"{sys.version_info[0]}.{sys.version_info[1]}"),
186+
],
187+
)
188+
def test_respect_prefer_active_on_new(
189+
prefer_active: bool,
190+
python: str,
191+
config: Config,
192+
mocker: MockerFixture,
193+
tester: CommandTester,
194+
tmp_dir: str,
195+
):
196+
from poetry.utils.env import GET_PYTHON_VERSION_ONELINER
197+
198+
orig_check_output = subprocess.check_output
199+
200+
def mock_check_output(cmd: str, *_: Any, **__: Any) -> str:
201+
if GET_PYTHON_VERSION_ONELINER in cmd:
202+
return "1.1.1"
203+
204+
return orig_check_output(cmd, *_, **__)
205+
206+
mocker.patch("subprocess.check_output", side_effect=mock_check_output)
207+
208+
config.config["virtualenvs"]["prefer-active-python"] = prefer_active
209+
210+
package = "package"
211+
path = Path(tmp_dir) / package
212+
options = [path.as_posix()]
213+
tester.execute(" ".join(options))
214+
215+
pyproject_file = path / "pyproject.toml"
216+
217+
expected = f"""\
218+
[tool.poetry.dependencies]
219+
python = "^{python}"
220+
"""
221+
222+
assert expected in pyproject_file.read_text()

0 commit comments

Comments
 (0)