Repository navigation
Description
See django-components/django-components#1404
(Below is AI generated)
Goal
Add executable documentation validation later as a generic, opt-in docs-site guard. Do not restore page-specific pytest assertions. The guard should report the Markdown source path and line so content authors can fix the page without finding a hidden test.
This is intentionally follow-up work. The current Getting started content can continue through editorial review and the existing guards first.
What the previous test actually did
The old docs_site/tests/test_getting_started_content.py mixed executable checks with editorial assertions. Only the executable behavior is relevant to this issue:
- One self-contained fence: it read the sole
citryfence frominstallation.md, passed the fence body topython -c, and checked the rendered HTML. - Several files in one temporary project: it took selected fences from
parametrising-components.md,components-in-templates.md, andadding-slots.md; wrote them asreading_list.py,page.py,check_page.py, andreading_panel.py; then executed the files from the same temporary directory. The snippets stayed as separate Python modules and imported one another. They were not concatenated, and no common header or footer was added. - Complete FastAPI source files plus one shared probe: for each server-side step, it copied
citry_setup.py,app.py, and one complete component snippet fromdocs_site/snippets/getting_started/into a temporary directory. The selected component file was renamed tocomponents.py. It then usedpython -conly for a shared FastAPITestClientprobe that requested/and/citry/citry.jsand checked page-specific text.
The same file also locked headings, prose, navigation labels/order, page titles, and exact snippet membership. Those are deliberately out of scope: user-facing content must not require pytest updates.
Desired shape
- Authors explicitly opt a fence or a named set of files into execution.
- A multi-file example can declare filenames and a shared working directory without hard-coded page names in Python tests.
- A complete-project example can declare a command or reusable probe.
- Failures point to the declaration in Markdown, including its line.
- The guard implementation is tested with synthetic Markdown and fixtures.
- Normal prose, headings, page order, link wording, and example output copy remain outside pytest.
Previous test file
"""Executable and editorial contracts for the Getting started journey."""
from __future__ import annotations
import re
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
from docs_site._internal.guards.fence_validator import scan_fences
from docs_site._internal.nav import load_nav
from docs_site._internal.pipeline import render_page
REPO_ROOT = Path(__file__).resolve().parents[2]
DOCS_SITE_ROOT = REPO_ROOT / "docs_site"
CONTENT_ROOT = DOCS_SITE_ROOT / "content"
CONTENT_NAV = CONTENT_ROOT / "_nav.yml"
GETTING_STARTED_ROOT = CONTENT_ROOT / "getting-started"
SNIPPET_ROOT = DOCS_SITE_ROOT / "snippets" / "getting_started"
INSTALLATION_PAGE = GETTING_STARTED_ROOT / "installation.md"
JOURNEY = [
("Install Citry", "/getting-started/installation/"),
("Your first component", "/getting-started/your-first-component/"),
("Use data in components", "/getting-started/parametrising-components/"),
("Build a page from components", "/getting-started/components-in-templates/"),
("Add flexible content", "/getting-started/adding-slots/"),
("Add browser behavior", "/getting-started/browser-interactivity/"),
("Connect components in the browser", "/getting-started/client-props-and-handlers/"),
("Serve the page with FastAPI", "/getting-started/fastapi/"),
("Call Python from a click", "/getting-started/call-python/"),
("Keep a value between calls", "/getting-started/state/"),
("Handle and validate a form", "/getting-started/forms/"),
("Replace part of the page from Python", "/getting-started/server-rendered-updates/"),
]
def _installation_source() -> str:
return INSTALLATION_PAGE.read_text(encoding="utf-8")
def _first_citry_fence(page_name: str) -> str:
source = (GETTING_STARTED_ROOT / page_name).read_text(encoding="utf-8")
return next(fence.body for fence in scan_fences(source) if fence.lang == "citry")
def test_installation_render_proof_executes() -> None:
fences = [fence for fence in scan_fences(_installation_source()) if fence.lang == "citry"]
assert len(fences) == 1
result = subprocess.run(
[sys.executable, "-c", fences[0].body],
cwd=REPO_ROOT,
capture_output=True,
text=True,
check=False,
timeout=30,
)
assert result.returncode == 0, result.stdout + result.stderr
assert re.search(r"<p(?:\s[^>]*)?>Hello from Citry!</p>", result.stdout)
def test_installation_page_stays_narrow_and_hands_off_to_the_card() -> None:
source = _installation_source()
result = render_page(
source,
current_path="getting-started/installation/",
source_path=INSTALLATION_PAGE,
wrap_in_layout=False,
)
assert "# Install Citry" in result.markdown_body
assert "## Before you start" in result.markdown_body
assert "## Install the package" in result.markdown_body
assert "## Check the installation" in result.markdown_body
assert "## Build your first component" in result.markdown_body
assert "## Troubleshoot" in result.markdown_body
assert "uv add citry" in result.markdown_body
assert "inside an existing `uv` project" in source
assert "substitute `python3` or `py`" in source
assert 'href="/getting-started/your-first-component/"' in result.html
assert 'href="/about/compatibility/"' in result.html
assert load_nav(CONTENT_NAV).find_title("/getting-started/installation/") == "Install Citry"
for displaced_topic in ("template_data", "CitryElement", "citry --version", "citry list", "mount(app"):
assert displaced_topic not in source
def test_getting_started_nav_is_the_twelve_page_journey() -> None:
nav = load_nav(CONTENT_NAV)
section = next(section for section in nav.sections if section.label == "Getting started")
assert [(item.title, item.path) for item in section.items] == JOURNEY
def test_local_component_steps_execute_as_one_project(tmp_path: Path) -> None:
page_fences = scan_fences((GETTING_STARTED_ROOT / "components-in-templates.md").read_text(encoding="utf-8"))
sources = {
"reading_list.py": _first_citry_fence("parametrising-components.md"),
"page.py": _first_citry_fence("components-in-templates.md"),
"check_page.py": next(fence.body for fence in page_fences if "The page looks right." in fence.body),
"reading_panel.py": _first_citry_fence("adding-slots.md"),
}
for name, source in sources.items():
(tmp_path / name).write_text(source, encoding="utf-8")
page = subprocess.run(
[sys.executable, "page.py"],
cwd=tmp_path,
capture_output=True,
text=True,
check=False,
timeout=30,
)
panel = subprocess.run(
[sys.executable, "reading_panel.py"],
cwd=tmp_path,
capture_output=True,
text=True,
check=False,
timeout=30,
)
check = subprocess.run(
[sys.executable, "check_page.py"],
cwd=tmp_path,
capture_output=True,
text=True,
check=False,
timeout=30,
)
assert page.returncode == 0, page.stdout + page.stderr
assert "My reading shelf" in page.stdout
assert "A Wizard of Earthsea" in page.stdout
assert "Choose your next book." in page.stdout
assert check.returncode == 0, check.stdout + check.stderr
assert "The page looks right." in check.stdout
assert panel.returncode == 0, panel.stdout + panel.stderr
assert "No action needed." in panel.stdout
assert "Start reading" in panel.stdout
@pytest.mark.parametrize(("title", "url"), JOURNEY)
def test_every_journey_page_renders_with_its_title(title: str, url: str) -> None:
source_path = GETTING_STARTED_ROOT / f"{url.removeprefix('/getting-started/').removesuffix('/')}.md"
source = source_path.read_text(encoding="utf-8")
result = render_page(
source,
current_path=url.strip("/"),
source_path=source_path,
wrap_in_layout=False,
)
assert f"# {title}" in result.markdown_body
assert "<c-include-file" not in result.html
def test_server_pages_reuse_one_complete_source_per_step() -> None:
consumers = {
"fastapi.md": ("citry_setup.py", "components_step8.py", "app.py"),
"call-python.md": ("components_step9.py",),
"state.md": ("components_step10.py",),
"forms.md": ("components_step11.py",),
"server-rendered-updates.md": ("components.py",),
}
for page_name, snippet_names in consumers.items():
source = (GETTING_STARTED_ROOT / page_name).read_text(encoding="utf-8")
for snippet_name in snippet_names:
assert f"docs_site/snippets/getting_started/{snippet_name}" in source
for snippet in SNIPPET_ROOT.glob("*.py"):
uses = [
page.name
for page in GETTING_STARTED_ROOT.glob("*.md")
if snippet.as_posix().removeprefix(f"{REPO_ROOT.as_posix()}/") in page.read_text(encoding="utf-8")
]
assert uses, f"{snippet.name} has no Getting started consumer"
@pytest.mark.parametrize(
("snippet_name", "expected_text"),
[
("components_step8.py", "Current choice"),
("components_step9.py", "Waiting for Python."),
("components_step10.py", "Checks completed"),
("components_step11.py", "Work email"),
("components.py", "Your confirmation will appear here."),
],
)
def test_each_fastapi_step_serves_the_page_and_runtime(
tmp_path: Path,
snippet_name: str,
expected_text: str,
) -> None:
for source_name, target_name in (
("citry_setup.py", "citry_setup.py"),
("app.py", "app.py"),
(snippet_name, "components.py"),
):
shutil.copy2(SNIPPET_ROOT / source_name, tmp_path / target_name)
code = """
from fastapi.testclient import TestClient
from app import app
with TestClient(app) as client:
page = client.get("/")
runtime = client.get("/citry/citry.js")
assert page.status_code == 200
assert "Reading room" in page.text
assert expected_text in page.text
assert runtime.status_code == 200
assert len(runtime.content) > 1000
"""
env = {
"CITRY_SECRET": "docs-getting-started-test-secret",
"PYTHONPATH": str(tmp_path),
}
result = subprocess.run(
[sys.executable, "-c", f"expected_text = {expected_text!r}\n{code}"],
cwd=tmp_path,
env=env,
capture_output=True,
text=True,
check=False,
timeout=30,
)
assert result.returncode == 0, result.stdout + result.stderr
Activity