|
| 1 | +"""Test multiple variants of python version.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from ansible_dev_environment.cli import main |
| 12 | + |
| 13 | + |
| 14 | +def generate_pythons_uv() -> list[str]: |
| 15 | + """Generate a list of python versions. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + List of python versions |
| 19 | + """ |
| 20 | + pythons = ["python3"] |
| 21 | + version = sys.version.split(" ", maxsplit=1)[0] |
| 22 | + pythons.append(version) |
| 23 | + pythons.append(f"python{version}") |
| 24 | + major_minor = version.rsplit(".", 1)[0] |
| 25 | + pythons.append(major_minor) |
| 26 | + major, minor = major_minor.split(".") |
| 27 | + one_less = f"{major}.{int(minor) - 1}" |
| 28 | + pythons.append(one_less) |
| 29 | + sys_path = str(Path("/usr/bin/python3").resolve()) |
| 30 | + pythons.append(sys_path) |
| 31 | + return pythons |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("python", generate_pythons_uv()) |
| 35 | +def test_specified_python_version_uv( |
| 36 | + python: str, |
| 37 | + capsys: pytest.CaptureFixture[str], |
| 38 | + tmp_path: Path, |
| 39 | + monkeypatch: pytest.MonkeyPatch, |
| 40 | +) -> None: |
| 41 | + """Build the venv with a user specified python version. |
| 42 | +
|
| 43 | + Args: |
| 44 | + python: Python version |
| 45 | + capsys: Capture stdout and stderr |
| 46 | + tmp_path: Temporary directory |
| 47 | + monkeypatch: Pytest monkeypatch |
| 48 | + """ |
| 49 | + venv_path = tmp_path / ".venv" |
| 50 | + monkeypatch.setattr( |
| 51 | + "sys.argv", |
| 52 | + [ |
| 53 | + "ade", |
| 54 | + "install", |
| 55 | + f"--venv={venv_path}", |
| 56 | + f"--python={python}", |
| 57 | + ], |
| 58 | + ) |
| 59 | + with pytest.raises(SystemExit): |
| 60 | + main() |
| 61 | + |
| 62 | + captured = capsys.readouterr() |
| 63 | + venv_line = [ |
| 64 | + line for line in captured.out.splitlines() if "Created virtual environment:" in line |
| 65 | + ] |
| 66 | + assert venv_line[0].endswith(python) |
| 67 | + |
| 68 | + |
| 69 | +def generate_pythons_pip() -> list[str]: |
| 70 | + """Generate a list of python versions. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + List of python versions |
| 74 | + """ |
| 75 | + pythons = ["python3"] |
| 76 | + version = sys.version.split(" ", maxsplit=1)[0] |
| 77 | + major_minor = version.rsplit(".", 1)[0] |
| 78 | + pythons.append(major_minor) |
| 79 | + sys_path = str(Path("/usr/bin/python3").resolve()) |
| 80 | + pythons.append(sys_path) |
| 81 | + return pythons |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("python", generate_pythons_pip()) |
| 85 | +def test_specified_python_version_pip( |
| 86 | + python: str, |
| 87 | + capsys: pytest.CaptureFixture[str], |
| 88 | + tmp_path: Path, |
| 89 | + monkeypatch: pytest.MonkeyPatch, |
| 90 | +) -> None: |
| 91 | + """Build the venv with a user specified python version. |
| 92 | +
|
| 93 | + Args: |
| 94 | + python: Python version |
| 95 | + capsys: Capture stdout and stderr |
| 96 | + tmp_path: Temporary directory |
| 97 | + monkeypatch: Pytest monkeypatch |
| 98 | + """ |
| 99 | + venv_path = tmp_path / ".venv" |
| 100 | + monkeypatch.setattr( |
| 101 | + "sys.argv", |
| 102 | + ["ade", "install", f"--venv={venv_path}", f"--python={python}", "--no-uv"], |
| 103 | + ) |
| 104 | + with pytest.raises(SystemExit): |
| 105 | + main() |
| 106 | + |
| 107 | + captured = capsys.readouterr() |
| 108 | + venv_line = [ |
| 109 | + line for line in captured.out.splitlines() if "Created virtual environment:" in line |
| 110 | + ] |
| 111 | + assert venv_line[0].endswith(python) |
0 commit comments