From 07622415fcaab9048446724dccf7229547a72d22 Mon Sep 17 00:00:00 2001 From: rajashidattapy Date: Sat, 1 Aug 2026 08:32:35 +0530 Subject: [PATCH] chore: add tests for documented code snippets in README files --- README-PYPI.md | 2 ++ README.md | 2 ++ tests/test_docs_snippets.py | 44 +++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 tests/test_docs_snippets.py diff --git a/README-PYPI.md b/README-PYPI.md index 833bb3c3..d6398da8 100644 --- a/README-PYPI.md +++ b/README-PYPI.md @@ -306,6 +306,7 @@ def main(): api_key=os.getenv("OPENROUTER_API_KEY", ""), ) as open_router: # Rest of application here... + pass # Or when using async: @@ -318,6 +319,7 @@ async def amain(): api_key=os.getenv("OPENROUTER_API_KEY", ""), ) as open_router: # Rest of application here... + pass ``` diff --git a/README.md b/README.md index 12cbcbdd..72087c81 100644 --- a/README.md +++ b/README.md @@ -306,6 +306,7 @@ def main(): api_key=os.getenv("OPENROUTER_API_KEY", ""), ) as open_router: # Rest of application here... + pass # Or when using async: @@ -318,6 +319,7 @@ async def amain(): api_key=os.getenv("OPENROUTER_API_KEY", ""), ) as open_router: # Rest of application here... + pass ``` diff --git a/tests/test_docs_snippets.py b/tests/test_docs_snippets.py new file mode 100644 index 00000000..45724d37 --- /dev/null +++ b/tests/test_docs_snippets.py @@ -0,0 +1,44 @@ +import ast +import re +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +SNIPPET = re.compile(r"```python\n(.*?)```", re.S) + + +def _snippets(): + """Every documented snippet that actually uses the SDK. + + Blocks under docs/components/ and docs/operations/ are type signatures rather + than programs, so only blocks that import from `openrouter` are collected. + """ + files = [ROOT / "README.md", ROOT / "README-PYPI.md", ROOT / "USAGE.md"] + files += sorted(ROOT.glob("docs/**/*.mdx")) + + found = [] + for path in files: + if not path.exists(): + continue + text = path.read_text(encoding="utf-8") + for match in SNIPPET.finditer(text): + code = match.group(1) + if "from openrouter" in code or "import openrouter" in code: + line = text[: match.start()].count("\n") + 1 + found.append( + pytest.param(code, id=f"{path.relative_to(ROOT).as_posix()}:{line}") + ) + return found + + +@pytest.mark.parametrize("code", _snippets()) +def test_documented_snippet_is_valid_python(code): + # The README's Resource Management block is generated between Speakeasy + # section markers, so a regeneration can silently reintroduce the empty + # `with` bodies this guards against. + ast.parse(code) + + +def test_snippet_collection_is_not_empty(): + assert len(_snippets()) > 100