I wrote a 24-second test to prove a one-line config fix still worked
This is a postmortem on a test I wrote, and on the tests that later grew around it. The test wasn’t broken. Its problem is that it should never have been written. It cost about 30% of our Python test suite’s runtime to check something our CI already checks on every commit.
The original bug
Some tools that manage git worktrees put them inside the repository’s own .git directory, at a path like .git/<tool>/worktrees/<id>/. That means every file in such a checkout has .git somewhere in its path.
Our cspell configuration had **/.git/** in ignorePaths. cspell matches that pattern against the whole path, so in one of these worktrees it ignored every file. With nothing to check, it printed its usage text instead of a file count, and the spelling gate quietly checked nothing.
The fix, which was complete
The fix had two parts:
- Anchor the pattern.
**/.git/**became/.git/**, so it only matches the.gitdirectory at the root of the checkout. We already had a comment in the markdownlint config explaining the same anchoring for that tool. Explicitly re-including the worktree path would also have worked. - Make the gate fail loudly. The cspell and markdownlint gates now fail when the tool reports zero files checked, or no count at all, and the error message names the likely cause. That check has its own unit tests, which run on plain output strings in milliseconds.
The gate runs as a commit hook and in every pipeline run. If the problem ever comes back, the next commit made in the affected checkout fails with a message that explains why.
That should have been the end of it.
What I added anyway
Next to the fix, I wrote a test module that:
- copies every file in the workspace into a temporary directory,
- runs
git initand commits the copy, - adds a worktree under the copy’s
.git, - symlinks the npm install into both checkouts,
- runs the real cspell gate, the real markdownlint gate, cspell’s own file listing, several hooks and the web app’s test runner in both checkouts,
- and asserts that the counts match.
One test even puts the old, broken pattern back into a scratch copy of the config and checks that the gate fails. In other words, it tests that a guard which runs on every commit still works, by triggering it on purpose in a slower environment.
The module took about 24 seconds. Each lint test cost around 5 seconds, and each hook test 2 to 3 seconds. It ran whenever anyone touched the gate scripts, the lint configs, the npm manifests, the web app or .gitignore, which is often.
How it grew
Once a fixture existed that could build a worktree under .git, it became the obvious place for related tests. Later changes added:
- a test that the web app’s dev server file restrictions behave under a
.gitpath, - a test of where the npm tools are found,
- a test that a test-runner cache directory doesn’t count as an install.
Each one followed the same pattern: fix something once in code or config, then add a test that re-runs the real tool to show the fix holds. Reviewers approved each one, because each looked like a reasonable addition to a module that already existed.
Why it was wrong
When someone pointed it out, they asked: “Isn’t this literally just what CI already does?”
Yes, it is. They also gave an analogy I keep thinking about. If someone asks you for a cup of coffee and adds “don’t use ketchup,” that doesn’t mean you should write test_coffee_was_not_made_with_ketchup(). Tests should check behavior that exists. The only tests that should check for the absence of a behavior are regression tests. There’s no limit to the things a system shouldn’t do, so negative tests belong where a fault has actually come back, not where one might.
My module failed that standard in four ways:
- It guarded one bug three times. The anchored pattern prevents the bug. The zero-count check catches it wherever the gate runs. My test re-ran both to confirm they still worked. The first two were the fix. The third just repeated them.
- It tested an absence, ahead of time. “The gate fails on the old pattern” and “the tool lists the same files in both layouts” both assert that a fixed bug isn’t there. The bug had happened once and had never come back.
- It tested the tool, not our code. When a test needs a copy of the entire workspace, a git repo, a worktree and the real linters, what it’s really testing is the tool’s behavior. The way to observe a tool’s behavior is to run it, and the gate already runs it.
- It went against lessons we already had. We had agreed that pure logic gets unit-tested through a seam, which the zero-count check already was. We had agreed to ask “what would have to be wrong for this test to fail?” Here the answer is “someone un-anchored the pattern,” and the gate would report that at the next commit anyway. Those lessons didn’t stop this module because it arrived as part of a fix they’d approve of. The test looked like proof that the fix worked, so nobody questioned it.
What I should have done
Fixed the config, added a comment explaining the anchoring, and stopped there. The guard that fails on zero files already existed and was already unit-tested. Nothing else was needed.
What I’ll do differently
- A config fix is done when the config is fixed. A test that re-runs the tool to show the fix holds is the same fix written a second time, paid for on every run.
- Negative tests are regression tests or nothing. I’ll only write a “this must not happen” test after the fault has actually come back. I won’t write one in anticipation.
- The gate’s own run is the check for a tool’s behavior. If a lint gate fails on zero files on every commit and every pipeline run, a test that reproduces the fault to watch the gate fail adds nothing.
- A fixture that copies the workspace is a warning sign. If I find myself writing one so I can run the project’s own linters inside it, I’ll stop and ask what CI isn’t already doing.
What reviewers can look for
- Fixtures that copy the whole workspace or run
git worktree addin order to run the project’s own gates. - Tests that edit a config file in a scratch copy to bring back a fixed bug, then assert that the gate fails.
- Tests asserting that a tool lists the same files in two different checkouts.
- Test modules where each test takes seconds because it starts a linter or test runner as a subprocess.
- A test named for something that must not happen, added in the same change as the fix that stops it from happening.
We’re removing the module. The config fix and the zero-count guard stay, and so do the guard’s fast unit tests. I’m sorry for the CI time this cost everyone, and for the extra tests it attracted.