Skip to content

Commit c6d6ab9

Browse files
fix(cli): stabilize --fixture for CI format, types, and env leak
Format catalog helpers, fix pyright on the fixture generator, walk up for fixture roots, and keep integration scaffolds out of fixture mode. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 03f3ac7 commit c6d6ab9

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

packages/create-awesome-python-app/src/create_awesome_python_app/catalog.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def catalog_cache_path() -> Path:
363363
def resolve_fixture_root() -> Path | None:
364364
"""Resolve the repo root that contains ``fixtures/catalog/templates.json``.
365365
366-
Priority: ``CPA_FIXTURE_DIR`` → package-relative monorepo root → ``cwd``.
366+
Priority: ``CPA_FIXTURE_DIR`` → walk-up from package → ``cwd``.
367367
"""
368368
if _fixture_root_override is not _SENTINEL:
369369
return _fixture_root_override # type: ignore[return-value]
@@ -372,12 +372,19 @@ def resolve_fixture_root() -> Path | None:
372372
if env:
373373
return Path(env).expanduser().resolve()
374374

375-
auto = _AUTO_FIXTURE_DIR
376-
if (auto / "fixtures" / "catalog" / "templates.json").is_file():
377-
return auto
375+
def _has_fixture_catalog(root: Path) -> bool:
376+
return (root / "fixtures" / "catalog" / "templates.json").is_file()
377+
378+
if _has_fixture_catalog(_AUTO_FIXTURE_DIR):
379+
return _AUTO_FIXTURE_DIR
380+
381+
# Editable / site-packages layouts vary; walk up from this file.
382+
for parent in Path(__file__).resolve().parents:
383+
if _has_fixture_catalog(parent):
384+
return parent
378385

379386
cwd = Path.cwd()
380-
if (cwd / "fixtures" / "catalog" / "templates.json").is_file():
387+
if _has_fixture_catalog(cwd):
381388
return cwd
382389
return None
383390

@@ -505,9 +512,7 @@ def get_catalog_data(*, force_refresh: bool = False) -> dict[str, Any]:
505512
)
506513
data = fixture
507514
else:
508-
raise RuntimeError(
509-
f"Failed to load template catalog: {err}"
510-
) from err
515+
raise RuntimeError(f"Failed to load template catalog: {err}") from err
511516

512517
_memory_cache = data
513518
_memory_ts = time.time()

packages/create-awesome-python-app/tests/test_catalog_fetch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
@pytest.fixture(autouse=True)
27-
def _reset_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
27+
def _reset_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
2828
reset_catalog_cache_for_tests()
2929
reset_fixture_root_for_tests()
3030
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cache"))
@@ -111,9 +111,7 @@ def test_get_catalog_data_fixture_uses_custom_dir(
111111
"extensions": [],
112112
"categories": [],
113113
}
114-
(catalog_dir / "templates.json").write_text(
115-
json.dumps(payload), encoding="utf-8"
116-
)
114+
(catalog_dir / "templates.json").write_text(json.dumps(payload), encoding="utf-8")
117115
monkeypatch.setenv("CPA_CATALOG_FIXTURE", "1")
118116
monkeypatch.setenv("CPA_FIXTURE_DIR", str(tmp_path))
119117
data = get_catalog_data(force_refresh=True)

packages/create-awesome-python-app/tests/test_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ async def fake_check_for_latest_version(_package_name):
239239
def test_help_mentions_fixture() -> None:
240240
result = runner.invoke(app, ["--help"])
241241
assert result.exit_code == 0
242-
assert "--fixture" in result.stdout
242+
text = (result.stdout or "") + (result.stderr or "")
243+
assert "fixture" in text.lower()
243244

244245

245246
def test_preprocess_fixture_argv_bare_and_with_dir() -> None:

packages/create-awesome-python-app/tests/test_cpa_templates_integration.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ def _cpa_templates_available() -> bool:
2121
return (FASTAPI_TEMPLATE / "pyproject.toml").is_file()
2222

2323

24+
def _clean_fixture_env(monkeypatch: pytest.MonkeyPatch) -> None:
25+
"""Ensure subprocess scaffolds are not forced into fixture mode."""
26+
monkeypatch.delenv("CPA_CATALOG_FIXTURE", raising=False)
27+
monkeypatch.delenv("CPA_FIXTURE_DIR", raising=False)
28+
29+
30+
def _subprocess_env() -> dict[str, str]:
31+
env = os.environ.copy()
32+
env.pop("CPA_CATALOG_FIXTURE", None)
33+
env.pop("CPA_FIXTURE_DIR", None)
34+
return env
35+
36+
2437
@pytest.mark.skipif(
2538
not _cpa_templates_available(),
2639
reason="cpa-templates checkout not available (set CPA_TEMPLATES_ROOT)",
2740
)
2841
def test_scaffold_fastapi_starter_from_cpa_templates(
2942
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
3043
) -> None:
44+
_clean_fixture_env(monkeypatch)
3145
monkeypatch.setenv("CI", "1")
3246
monkeypatch.setenv("CPA_SKIP_GIT", "1")
3347
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cpa-cache"))
@@ -48,6 +62,7 @@ def test_scaffold_fastapi_starter_from_cpa_templates(
4862
cwd=_REPO_ROOT,
4963
capture_output=True,
5064
text=True,
65+
env=_subprocess_env(),
5166
check=False,
5267
)
5368
assert result.returncode == 0, result.stdout + result.stderr
@@ -78,6 +93,7 @@ def test_scaffold_fastapi_starter_via_catalog_slug(
7893
"""Scaffold using --template fastapi-starter slug (issue #160 / #161)."""
7994
import json
8095

96+
_clean_fixture_env(monkeypatch)
8197
monkeypatch.setenv("CI", "1")
8298
monkeypatch.setenv("CPA_SKIP_GIT", "1")
8399
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cpa-cache"))
@@ -114,6 +130,7 @@ def test_scaffold_fastapi_starter_via_catalog_slug(
114130
cwd=_REPO_ROOT,
115131
capture_output=True,
116132
text=True,
133+
env=_subprocess_env(),
117134
check=False,
118135
)
119136
assert result.returncode == 0, result.stdout + result.stderr
@@ -129,6 +146,7 @@ def test_scaffold_via_catalog_addon_slug(
129146
) -> None:
130147
import json
131148

149+
_clean_fixture_env(monkeypatch)
132150
monkeypatch.setenv("CI", "1")
133151
monkeypatch.setenv("CPA_SKIP_GIT", "1")
134152
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cpa-cache"))
@@ -170,6 +188,7 @@ def test_scaffold_via_catalog_addon_slug(
170188
cwd=_REPO_ROOT,
171189
capture_output=True,
172190
text=True,
191+
env=_subprocess_env(),
173192
check=False,
174193
)
175194
assert result.returncode == 0, result.stdout + result.stderr
@@ -183,6 +202,7 @@ def test_scaffold_via_catalog_addon_slug(
183202
def test_scaffold_fastapi_with_github_setup_extension(
184203
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
185204
) -> None:
205+
_clean_fixture_env(monkeypatch)
186206
monkeypatch.setenv("CI", "1")
187207
monkeypatch.setenv("CPA_SKIP_GIT", "1")
188208
monkeypatch.setenv("CPA_CACHE_DIR", str(tmp_path / "cpa-cache"))
@@ -204,6 +224,7 @@ def test_scaffold_fastapi_with_github_setup_extension(
204224
cwd=_REPO_ROOT,
205225
capture_output=True,
206226
text=True,
227+
env=_subprocess_env(),
207228
check=False,
208229
)
209230
assert result.returncode == 0, result.stdout + result.stderr

0 commit comments

Comments
 (0)