test/stubgen: add conftest for stub autogen

This commit is contained in:
2026-07-09 19:09:23 +08:00
parent 7f711fda4a
commit 9a19ffb0ce
+58
View File
@@ -0,0 +1,58 @@
"""Regenerate .pyi stubs before running stub tests."""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
import pytest
SCHEMAS_DIR = Path(__file__).parent
TEST_SCHEMAS = [
"test_simple.capnp",
"test_nested.capnp",
"test_generics.capnp",
"test_interface.capnp",
"addressbook.capnp",
"dummy.capnp",
]
def _find_capnpc_py() -> str:
venv_bin = Path(sys.prefix) / "bin" / "capnpc-py"
if venv_bin.exists():
return str(venv_bin)
return "capnpc-py"
@pytest.fixture(scope="session", autouse=True)
def _regenerate_stubs() -> None: # pyright: ignore[reportUnusedFunction]
"""Delete old .pyi files and regenerate from .capnp schemas."""
capnpc = _find_capnpc_py()
# Clean + generate main schemas
for schema in TEST_SCHEMAS:
pyi = SCHEMAS_DIR / schema.replace(".capnp", "_capnp.pyi")
pyi.unlink(missing_ok=True)
result = subprocess.run(
["capnp", "compile", "-I.", f"-o{capnpc}", schema],
capture_output=True, text=True, cwd=str(SCHEMAS_DIR),
)
assert result.returncode == 0, (
f"capnp compile failed for {schema}:\n{result.stderr}"
)
# Multi-file schemas (consumer imports base)
multi_dir = SCHEMAS_DIR / "multi"
for pat in ("*_capnp.pyi",):
for f in multi_dir.glob(pat):
f.unlink(missing_ok=True)
for schema in ("base.capnp", "consumer.capnp"):
result = subprocess.run(
["capnp", "compile", f"-o{capnpc}", schema],
capture_output=True, text=True, cwd=str(multi_dir),
)
assert result.returncode == 0, (
f"capnp compile failed for multi/{schema}:\n{result.stderr}"
)