Skip to content

Make runnable examples drop the databases they create - #10

Merged
bashhack merged 1 commit into
mainfrom
example-cleanup
Oct 22, 2026
Merged

bashhack merged 1 commit into
mainfrom
example-cleanup

Conversation

@bashhack

Copy link
Copy Markdown
Owner

Go examples have no *testing.T, so the runnable Example_* functions used t := &testing.T{}. A zero testing.T accepts t.Cleanup(...) and never runs it (nothing drives that T), so every database an example created through postgres.Setup/New stayed on the server: 12 per go test, and 219 had accumulated on the dev server.

The fix is exampleT, a small stand-in embedding testing.TB that implements only what the library calls (Helper, Cleanup, Context, Failed, Logf, Errorf, Fatalf) and a done() that runs the registered cleanups in reverse order, as testing does. Runnable examples become t := newExampleT(); defer t.done(). Cleanup failures print to stdout, which breaks the example's // Output: and fails it visibly. Compile-only examples keep their zero T, since they never execute.

Verified: the Postgres server holds the same number of databases before and after running every example.

Examples have no *testing.T, so the runnable ones used a zero
testing.T. Its Cleanup accepts functions and never runs them, so every
database an example created through Setup or New stayed on the server:
twelve per go test run, over two hundred on a developer's machine.

exampleT, a small stand-in embedding testing.TB, implements what the
library calls and runs the registered cleanups when the example is done,
in reverse order as testing does. Cleanup failures print to stdout, so
they break the example's expected output and fail it visibly.
Compile-only examples keep their zero T; they never execute.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@bashhack
bashhack merged commit b2dc49a into main Oct 22, 2026
1 check passed
@bashhack
bashhack deleted the example-cleanup branch October 22, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants