|
| 1 | +"""Integration tests against the cpa-templates bank.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +_REPO_ROOT = Path(__file__).resolve().parents[4] |
| 12 | +_DEFAULT_CPA_TEMPLATES = (_REPO_ROOT.parent / "cpa-templates").resolve() |
| 13 | +CPA_TEMPLATES_ROOT = Path( |
| 14 | + os.environ.get("CPA_TEMPLATES_ROOT", str(_DEFAULT_CPA_TEMPLATES)) |
| 15 | +).resolve() |
| 16 | +FASTAPI_TEMPLATE = CPA_TEMPLATES_ROOT / "templates" / "fastapi-starter" |
| 17 | +GITHUB_SETUP = CPA_TEMPLATES_ROOT / "extensions" / "github-setup" |
| 18 | + |
| 19 | + |
| 20 | +def _cpa_templates_available() -> bool: |
| 21 | + return (FASTAPI_TEMPLATE / "pyproject.toml").is_file() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.skipif( |
| 25 | + not _cpa_templates_available(), |
| 26 | + reason="cpa-templates checkout not available (set CPA_TEMPLATES_ROOT)", |
| 27 | +) |
| 28 | +def test_scaffold_fastapi_starter_from_cpa_templates( |
| 29 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 30 | +) -> None: |
| 31 | + monkeypatch.setenv("CI", "1") |
| 32 | + monkeypatch.setenv("CPA_SKIP_GIT", "1") |
| 33 | + dest = tmp_path / "api" |
| 34 | + template_url = f"file://{CPA_TEMPLATES_ROOT}?subdir=templates/fastapi-starter" |
| 35 | + |
| 36 | + result = subprocess.run( |
| 37 | + [ |
| 38 | + "uv", |
| 39 | + "run", |
| 40 | + "create-awesome-python-app", |
| 41 | + "--template", |
| 42 | + template_url, |
| 43 | + "--no-interactive", |
| 44 | + "--no-install", |
| 45 | + str(dest), |
| 46 | + ], |
| 47 | + cwd=_REPO_ROOT, |
| 48 | + capture_output=True, |
| 49 | + text=True, |
| 50 | + check=False, |
| 51 | + ) |
| 52 | + assert result.returncode == 0, result.stdout + result.stderr |
| 53 | + assert (dest / "app" / "main.py").is_file() |
| 54 | + assert (dest / "pyproject.toml").is_file() |
| 55 | + |
| 56 | + sync = subprocess.run(["uv", "sync"], cwd=dest, capture_output=True, text=True) |
| 57 | + assert sync.returncode == 0, sync.stderr |
| 58 | + |
| 59 | + lint = subprocess.run(["uv", "run", "ruff", "check", "."], cwd=dest, capture_output=True, text=True) |
| 60 | + assert lint.returncode == 0, lint.stderr |
| 61 | + |
| 62 | + tests = subprocess.run(["uv", "run", "pytest", "-q"], cwd=dest, capture_output=True, text=True) |
| 63 | + assert tests.returncode == 0, tests.stdout + tests.stderr |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.skipif( |
| 67 | + not (_cpa_templates_available() and GITHUB_SETUP.is_dir()), |
| 68 | + reason="cpa-templates extensions not available", |
| 69 | +) |
| 70 | +def test_scaffold_fastapi_with_github_setup_extension( |
| 71 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 72 | +) -> None: |
| 73 | + monkeypatch.setenv("CI", "1") |
| 74 | + monkeypatch.setenv("CPA_SKIP_GIT", "1") |
| 75 | + dest = tmp_path / "api-ext" |
| 76 | + repo = CPA_TEMPLATES_ROOT |
| 77 | + result = subprocess.run( |
| 78 | + [ |
| 79 | + "uv", |
| 80 | + "run", |
| 81 | + "create-awesome-python-app", |
| 82 | + "--template", |
| 83 | + f"file://{repo}?subdir=templates/fastapi-starter", |
| 84 | + "--addons", |
| 85 | + f"file://{repo}?subdir=extensions/github-setup", |
| 86 | + "--no-interactive", |
| 87 | + "--no-install", |
| 88 | + str(dest), |
| 89 | + ], |
| 90 | + cwd=_REPO_ROOT, |
| 91 | + capture_output=True, |
| 92 | + text=True, |
| 93 | + check=False, |
| 94 | + ) |
| 95 | + assert result.returncode == 0, result.stdout + result.stderr |
| 96 | + assert (dest / ".github" / "workflows" / "ci.yml").is_file() |
0 commit comments