NetSnippets All articles
Developer Productivity

Docs That Don't Run Are Just Vibes: A CI Harness for README Snippets

NetSnippets
Docs That Don't Run Are Just Vibes: A CI Harness for README Snippets

Photo: README documentation code blocks continuous integration pipeline developer screen, via cdn.crank.se

Every README has a quickstart. Six months later, that quickstart has a dependency that no longer resolves, an environment variable that moved, and a command that silently does nothing. Nobody notices because nobody runs it. The README is a snippet library with no tests, and that is a productivity tax disguised as documentation.

The fix does not require a docs platform migration. You can treat documentation code blocks like first-class snippets: extract them, pin them, run them, and fail the build when they drift. Here is a practical harness you can put together in an afternoon.

Tag the snippets you actually mean to test

Not every code block should run. Output samples, pseudo-code, and intentionally broken examples are fine. Mark the runnable ones with a fence tag such as test and leave the rest alone.

```bash test
[npm](https://en.wikipedia.org/wiki/Npm) install
npm run build

A simple convention beats a perfect taxonomy. If a block is copied by users, it should probably carry the tag.

## Extract with a small script, not a heroic manual process

Write a script that walks `README.md` and `docs/**/*.md`, finds fenced blocks with your tag, and writes each one to a temp file with a stable name. Keep a manifest so failures point back to the source file and line number.

```py
import re, pathlib
fence = re.compile(r'```(\w+)? test\n(.*?)```', re.S)
for path in pathlib.Path('docs').rglob('*.md'):
    text = path.read_text()
    for i, (lang, code) in enumerate(fence.findall(text)):
        out = pathlib.Path('tmp/docs') / f'{path.stem}-{i}.{lang or "txt"}'
        out.parent.mkdir(parents=True, exist_ok=True)
        out.write_text(code)

Pin dependencies like the snippet is production code

A docs harness that installs latest is a flaky test generator. Keep a separate lockfile or container image for docs, and update it deliberately. If the README says npm install, your harness should install from a pinned lockfile. If it says pip install, pin the requirements. The point is not to freeze the docs forever; it is to make drift visible and reviewable.

Run setup and teardown around every snippet

Snippets fail for boring reasons: a missing temp directory, a port already in use, a required env var. Give each run a clean workspace, set fake credentials, and tear down containers and files afterward. A shell wrapper or a pytest fixture both work; the important part is isolation.

#!/usr/bin/env bash
set -euo pipefail
workdir=$(mktemp -d)
trap 'rm -rf "$workdir"' EXIT
cp -R fixtures/* "$workdir"
cd "$workdir"
export API_BASE_URL=http://localhost:4010
run_snippet "$1"

Wire it into CI and fail loudly

Run the harness on pull requests that touch docs, and on a schedule so external dependencies do not rot silently. Cache dependencies, but never cache the extracted snippets. When a block fails, print the source file, line number, and the exact command that failed. A vague 'docs test failed' message will get ignored by the second sprint.

name: docs-snippets
on:
  pull_request:
    paths: ['docs/**', 'README.md', '.github/workflows/docs.yml']
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: '22' }
      - run: npm ci --prefix docs-harness
      - run: node docs-harness/extract.mjs
      - run: bash docs-harness/run-all.sh

Decide what not to test, and say so

Some blocks are illustrative. Some require cloud credentials you should not put in CI. Mark those with no-test, and keep a short allowlist with an owner and a reason. The dangerous state is not 'we skipped one block'; it is 'we do not know which blocks run'. Visibility is the product here.

Treat docs like a product surface

Documentation snippets are copied by new hires, customers, and language models. That makes them high-leverage code. Give docs changes a reviewer, link failures to the owning team, and keep the harness fast enough that people run it locally. Once the README is tested, you stop discovering broken quickstarts in onboarding—and start discovering them in CI, where they are cheap to fix.

All Articles

Related Articles

We Put AI Code Generation Head-to-Head Against Hand-Rolled Snippets. Here's What We Actually Found.

We Put AI Code Generation Head-to-Head Against Hand-Rolled Snippets. Here's What We Actually Found.

When Your Snippet Library Becomes a Decision Trap: The Hidden Cost of Too Much Reusable Code

Your Snippet Library Has a Graveyard Problem — Here's How to Finally Fix It

Your Snippet Library Has a Graveyard Problem — Here's How to Finally Fix It