@@ -141,85 +141,81 @@ Badges are few and load-bearing.
141141## Documented examples that run
142142
143143Examples in this repository are tests. This section is the contract for
144- writing one the test suite can actually see, and it is this repo's real
145- mechanism — read from ` pyproject.toml ` 's ` [tool.pytest.ini_options] ` and
146- the root ` conftest.py ` , not assumed.
147-
148- ** A fence tag is cosmetic. Only a ` >>> ` prompt executes.** A block
149- written as
150-
151- ```python
152- term = Terminal(rows=2, cols=20)
153- ```
154-
155- is prose that looks like a test. Nothing collects it, nothing runs it,
156- and it can be wrong for years. The same block written with prompts is a
157- test:
158-
159- ```python
160- >>> term = Terminal(rows=2, cols=20)
161- ```
162-
163- This is the single most expensive mistake available when editing
164- documentation, because removing the prompts leaves a green test suite
165- and a silently deleted test. When editing a file that contains examples,
166- count the prompts before and after.
167-
168- ** The fence tag is ` python ` ** for a doctest session and ` console ` for a
169- shell command at a ` $ ` prompt.
170-
171- ** Where examples run.** ` testpaths ` lists ` src/libvt ` , ` tests ` ,
172- ` docs ` , and ` README.md ` . ` addopts ` sets ` --doctest-docutils-modules ` and
173- ` -p no:doctest ` : the stdlib ` doctest ` pytest plugin is disabled, and
174- ` gp-libs ` ' docutils-based collector (` pytest_doctest_docutils ` , loaded
175- under the ` sphinx ` entry-point name) runs in its place. That collector
176- reads doctests out of ` .py ` docstrings * and* out of any Markdown or
177- reStructuredText page docutils parses — which is why a ` >>> ` block in
178- ` README.md ` or under ` docs/ ` is executed exactly like one in a
179- docstring. ` ELLIPSIS ` and ` NORMALIZE_WHITESPACE ` are enabled globally
180- (` doctest_optionflags ` ), so ` ... ` elides variable output and whitespace
181- differences do not fail a comparison. Reach for an inline
182- ` # doctest: +FLAG ` only for the block that needs something more.
183-
184- ** ` # doctest: +SKIP ` is not permitted.** It is a workaround that tests
185- nothing. Use the fixtures.
186-
187- ** Do not downgrade a doctest to a non-executed block to make it pass.**
188- A ` .. code-block:: ` or an unprompted fence does not run. If an example
189- cannot pass, fix the example or fix the code.
190-
191- ** The ` doctest_namespace ` fixtures.** The root ` conftest.py ` 's
192- ` _seed_doctest_namespace ` autouse fixture populates ` doctest_namespace `
193- before every doctest in the suite runs, making common entry points
194- available without boilerplate: ` libvt ` , ` Terminal ` , ` AnsiColor ` ,
195- ` assert_screen ` , and ` assert_screen_box ` . Standalone documentation blocks
196- should still show explicit imports when doing so makes the first example
197- on a page clear to a reader copying it.
198-
199- ** Cross-platform doctests.** ` pytest ` executes ` README.md ` and ` docs/ `
200- doctests on Linux, macOS, and Windows in CI. Keep documentation doctests
201- portable across all three platforms (for example, drive
202- ` AsyncTerminalStream ` with an in-memory ` asyncio.StreamReader ` rather
203- than calling POSIX-only ` open_pty() ` inside an unguarded doctest).
204-
205- ** ` # doctest: +HIDE ` .** A ` gp-libs ` extension flag: it is a no-op for
206- execution and checking — the line still runs and its output, if any, is
207- still verified — but it tells the Sphinx-rendered docs to drop that line
208- from the page. Use it to set up state a docstring's * Examples* section
209- does not need to show:
144+ writing one the test suite executes, matching ` pyproject.toml ` 's
145+ ` [tool.pytest.ini_options] ` , root ` conftest.py ` , and
146+ ` tests/_docs_codeblocks.py ` .
147+
148+ ** Three Markdown block forms execute under ` pytest ` :**
149+
150+ 1 . ** Doctest ` python ` fences** (` >>> ` prompts) in docstrings, ` README.md ` ,
151+ and ` docs/**/*.md ` run through ` gp-libs ` ' docutils collector
152+ (` --doctest-docutils-modules ` , ` -p no:doctest ` ) with ` ELLIPSIS ` and
153+ ` NORMALIZE_WHITESPACE ` enabled globally.
154+ 2 . ** Plain ` python ` fences** (no ` >>> ` prompts) in ` README.md ` and
155+ ` docs/**/*.md ` run through ` conftest.py ` 's ` pytest_collect_file ` hook
156+ (` tests._docs_codeblocks ` , marked ` documentation ` ). Each block runs in
157+ isolation with no implicit globals, so every plain ` python ` block must
158+ include explicit imports. Blocks that define top-level ` def test_* ` or
159+ ` async def test_* ` functions without calling them execute inside a
160+ temporary ` pytest ` run against the real ` libvt.pytest_plugin ` fixtures.
161+ 3 . ** ` console ` fences and ` {literalinclude} ` directives** in ` README.md `
162+ and ` docs/**/*.md ` run through the same ` documentation ` collector.
163+ Runnable commands (` python -m libvt ` , ` printf ... | python -m libvt ` ,
164+ ` examples/ ` , ` scripts/ ` , and documented ` pytest ` commands) execute in a
165+ temporary sandbox isolated from ` $HOME ` and the network; expected output
166+ lines beneath a ` $ ` command are compared against stdout. Non-runnable
167+ blocks (package installs, ` docs/project/releasing.md ` git templates,
168+ interactive TUI loops, and optional external-tool invocations) are
169+ classified explicitly and checked statically. Any unclassified shell or
170+ ` console ` fence fails ` pytest ` .
171+
172+ ** The fence tag is ` python ` ** for executable Python snippets or doctests
173+ and ` console ` for shell commands at a ` $ ` prompt. Use
174+ ` uv run pytest -m documentation ` to run only the Markdown code-block and
175+ ` {literalinclude} ` checks, or target a single page such as
176+ ` uv run pytest README.md ` .
177+
178+ ** Where examples run.** ` testpaths ` lists ` src/libvt ` , ` tests ` , ` docs ` ,
179+ and ` README.md ` . The docs code-block collector lives in
180+ ` tests/_docs_codeblocks.py ` (included in sdists, excluded from built wheels
181+ and ` pytest11 ` entry points). Every ` python ` fence in ` MIGRATION ` is
182+ classified as a historical ` # Before ` / ` # After ` block and checked in
183+ ` tests/test_docs_codeblocks.py ` .
184+
185+ ** ` # doctest: +SKIP ` is not permitted.** Use fixtures or explicit platform
186+ guards instead.
187+
188+ ** Do not downgrade an example to an unexecuted block to make it pass.**
189+ If an example fails, fix the example or fix the code.
190+
191+ ** The ` doctest_namespace ` fixtures.** Root ` conftest.py ` 's
192+ ` _seed_doctest_namespace ` autouse fixture populates ` doctest_namespace ` for
193+ ` >>> ` doctests only (` libvt ` , ` Terminal ` , ` AnsiColor ` , ` assert_screen ` ,
194+ ` assert_screen_box ` , and ` vt_* ` fixtures). Plain ` python ` blocks never
195+ receive ` doctest_namespace ` ; standalone doctest blocks on ` README.md ` ,
196+ ` docs/quickstart.md ` , and testing/snapshot/API pages also verify explicit
197+ imports in ` tests/test_libvt.py ` .
198+
199+ ** Cross-platform examples.** ` pytest ` executes ` README.md ` and ` docs/ `
200+ blocks on Linux, macOS, Windows, and PyPy in CI. Keep documentation
201+ examples portable across those targets (for example, guard POSIX-only
202+ ` open_pty() ` calls or drive ` AsyncTerminalStream ` with an in-memory
203+ ` asyncio.StreamReader ` ).
204+
205+ ** ` # doctest: +HIDE ` .** A ` gp-libs ` extension flag for ` >>> ` doctests: the
206+ line still runs and its output is verified, while Sphinx omits that line
207+ from rendered HTML. Use plain ` python ` fences instead of ` # doctest: +HIDE `
208+ shims for copy-pasteable ` pytest ` examples.
210209
211210``` python
212211>> > term = Terminal(rows = 2 , cols = 20 ) # doctest: +HIDE
213212>> > term.feed(b " ready" ) # doctest: +HIDE
214213```
215214
216- ** Every code block on a ` docs/ ` page is an independent doctest.** Blocks
217- do not share a session: each one gets a fresh copy of
218- ` doctest_namespace ` . A later block cannot use a ` term ` an earlier block
219- created — the name is not defined there. Write every block
220- self-contained. Order is load-bearing for the reader's narrative, not
221- for state — reorder, add, or drop a block as the prose demands, and keep
222- the story coherent.
215+ ** Every code block on a ` docs/ ` page is independent.** Blocks do not share
216+ state: doctests receive a fresh copy of ` doctest_namespace ` , and plain
217+ ` python ` blocks run in a fresh empty namespace. Write every block
218+ self-contained.
223219
224220** Docstring examples** use the NumPy ` Examples ` section:
225221
@@ -229,15 +225,6 @@ the story coherent.
229225 >>> add(2, 2)
230226 4
231227
232- ** Room to grow.** The docutils collector reads ` .md ` and ` .rst `
233- wherever it is loaded, which is everywhere in this repository. A
234- prompted block added to a documentation page is executed from that
235- moment with no configuration change. The MyST ` {doctest} ` directive and
236- the reStructuredText ` .. doctest:: ` directive are also registered and
237- available for a case that ever needs an explicitly marked block, rather
238- than a plain prompted fence. This section is where any additional
239- executed-block format is documented when one is adopted.
240-
241228## Documentation pages
242229
243230Warm the framing, never the facts. Resolution-order lists, value
0 commit comments