From 9a19ffb0ce2162489f78aacc5f9316be684a878c Mon Sep 17 00:00:00 2001 From: worldmozara Date: Thu, 9 Jul 2026 19:09:23 +0800 Subject: [PATCH] test/stubgen: add conftest for stub autogen --- tests/schemas/conftest.py | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/schemas/conftest.py diff --git a/tests/schemas/conftest.py b/tests/schemas/conftest.py new file mode 100644 index 0000000..b026c61 --- /dev/null +++ b/tests/schemas/conftest.py @@ -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}" + )