|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
3 | 6 | from pathlib import Path
|
4 | 7 | from typing import TYPE_CHECKING
|
| 8 | +from typing import Any |
5 | 9 |
|
6 | 10 | import pytest
|
7 | 11 |
|
|
10 | 14 |
|
11 | 15 | if TYPE_CHECKING:
|
12 | 16 | from cleo.testers.command_tester import CommandTester
|
| 17 | + from pytest_mock import MockerFixture |
13 | 18 |
|
| 19 | + from poetry.config.config import Config |
14 | 20 | from poetry.poetry import Poetry
|
15 | 21 | from tests.types import CommandTesterFactory
|
16 | 22 |
|
@@ -170,3 +176,47 @@ def test_command_new_with_readme(fmt: str | None, tester: CommandTester, tmp_dir
|
170 | 176 |
|
171 | 177 | poetry = verify_project_directory(path, package, package, None)
|
172 | 178 | 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