✨ feat(scripts): test runs fail when a suite leaves anything in its TMPDIR
Slice: #270
Test runs now fail when a suite leaves anything in its temp folder. Every package's suites, the scripts' suites and kavach's Python tests run under a fresh, empty TMPDIR (with TEMP and TMP set to it too). If anything is still in that folder when the suites end, the run fails and names each path. Alok chose a check at run time over a lint (#238, question 4). Once Cubic stops reviewing tests (#274), nothing else catches these leaks.
What changed
scripts/fresh-tmpdir.mjs(new, with its own tests at 100% coverage) runs a command under a newvt-*folder, created inside the caller's own temp folder.- When the command ends, it lists every entry still in the folder and fails with exit 1, even if every test passed.
- A command that failed keeps its own exit code, and the leftovers are still listed.
- If the folder was deleted or replaced by a file during the run, the run fails. If it cannot be read, the run is rejected.
- The folder is removed every time, in a
finally. SIGINT, SIGTERM, SIGHUP and SIGQUIT are passed on to the command. The signal handlers go on before the folder is made.
- The wiring: each package's
testscript, kavach'stest:pythonand the roottest:scriptsnow start with the wrapper. The only change to the rootpackage.jsonis inscripts. - CI: the "Script tests" step in
ci.ymlnow runspnpm test:scripts. Before, it rannode --test …directly, which skipped the wrapper. CI'spnpm -r checkalready runs each package'stest. - Docs:
docs/lessons/testing.mdhas a new "Temp folders" lesson on how to read the failure, andwhen-samples.tsvhas its sample.
Leaks the first run found, all fixed in the suites
| Package | Left behind | Fix |
|---|---|---|
| dvara | 172 folders: dvara-copy 67, dvara-run 47, dvara-records 27, and 16 other prefixes | The repo()/template() helper in records/fixture.test.ts and 8 other test files now record each folder they make and remove it in after() |
| pariksha | 187 folders: pariksha- 44, pariksha-authority 29, pariksha-cfg 25, pariksha-exports 17, pariksha-sel 16, pariksha-cov 13, and 15 other prefixes | 13 test files, through their repo/tree/temp helpers, now remove their folders in after() |
| sarathi | 20 folders: sarathi-cli 17, sarathi-config, sarathi-empty, sarathi-none | cli.test.ts removes them in after(); config.test.ts in a finally |
| kavach | node-compile-cache | npm's CLI turns on Node's compile cache, which it keeps in os.tmpdir(). The corpus row npx -c … runs the real npx. The corpus's sandbox env now sets NODE_DISABLE_COMPILE_CACHE=1 |
| scripts, agnipariksha, shuddhi, kavach's Python | nothing | none needed |
One test also failed during the first run: in pariksha's shell-gates.test.ts, afterEach hit ENOTEMPTY. This is not a leak. The fixture's git commit started git's detached auto-maintenance, which was still writing under .git while the folder was being removed (GIT_TRACE showed it). That file's fixture git now runs with -c maintenance.auto=false -c gc.auto=0. So does fixture git in the other pariksha files whose folders are now removed after commits, and in dvara's supervisor.test.ts. dvara's shared fixture already had them.
A later full run hit ENOTEMPTY again, this time in selection.test.ts's after(). That stopped its cleanup and left 7 folders. Its git ran under the machine's global config, whose trace2.eventtarget is a daemon's socket, and that daemon can write into a fixture's .git while it is being removed. This is the other cause docs/lessons/testing.md names under "Hermetic git". Four files now mask git's config and trace2 for the whole process, as main's cli.test.ts already does, and restore the environment in after(): pariksha's selection.test.ts, cli.config-file.test.ts and eslint-targets.test.ts, and dvara's supervisor.test.ts. Their commits already pass their own identity. dvara's other suites go through its shared fixture's hermeticEnv. pariksha's eslint-by-workspace and project-config only run git init, so they are left as they are.
Merged main to resolve a conflict with #297 in packages/pariksha/src/cli.test.ts. #297 rewrote that file around its own temp() helper, which removes every folder in after(). The file is main's version plus two changes: the fixture-git maintenance flags, and four folders main still made with a raw mkdtempSync (pariksha-none ×3, pariksha-norepo) now go through temp().
Notes
- Alok: niyam-guard refuses this PR, and its check is not required.
niyam-guard / judgefails because slice #270 does not name the guard files this PR changes. It is not one ofmain's required checks, so approving now would merge the PR with Guard red. The way through: - This waits on Alok's review.
.github/workflows/ci.ymlis code-owned. Without that change, CI's script suites would skip the check. Auto-merge is on, so the PR merges once he approves. - No hook changes. The check runs from the
testscripts, whichpnpm checkand CI already run, so nothing in.githooks/orscripts/guards.shchanged. - Stryker is untouched. #272's tap runner runs test files directly, not the
testscript, so it is not wrapped. agnipariksha still gives each Stryker run its ownTMPDIR. - Not wrapped: each package's
smoke, CI's Docker live-services step, and theneeds-youplugin tests (CI's Plugins job). - Ubuntu: the first CI run passed with nothing left in any TMPDIR.
Checks
pnpm checkis green with the check on, after the merge and the hermetic-git change: every package'scheck,test:scripts, lint and guards, with nofresh-tmpdir: the run leftline.- kavach's Python tests: 804 pass, nothing left.
🤖 Generated with Claude Code