test(server): restore write access before removing the vitest home

Runtime-context bundles are published read-only (0o555 directories), so
the worker-exit rmSync of the per-worker PAPERCLIP_HOME failed with
EACCES. Vitest reports that as an unhandled error and fails the shard
even when every test passes (server (11/12) on #15962, twice).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PaperclipandClaude Opus 5.5 committed 2026-10-12 18:02:51 +00:00
1 parent d37c03c236
commit fea261262e
1 file changed
+12 -1
+12 -1
View File
@@ -27,9 +27,20 @@ process.env.PAPERCLIP_TEST_HOST_HOME ??= process.env.PAPERCLIP_HOME?.trim() || p
// Receipt-spool recovery must never scan a developer's instance during tests.
const paperclipTestHome = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-vitest-home-"));
process.env.PAPERCLIP_HOME = paperclipTestHome;
// Runtime-context bundles are published read-only (0o555 directories), so
// restore owner write access on directories before removing the tree.
function restoreDirectoryWriteAccess(target: string): void {
const stat = fs.lstatSync(target, { throwIfNoEntry: false });
if (!stat?.isDirectory()) return;
fs.chmodSync(target, 0o700);
for (const entry of fs.readdirSync(target)) restoreDirectoryWriteAccess(path.join(target, entry));
}
// Setup-file afterAll hooks run before suites drain asynchronous heartbeats.
// Cleanup at worker exit, after all suite teardown, avoids deleting live state.
process.once("exit", () => fs.rmSync(paperclipTestHome, { recursive: true, force: true, maxRetries: 8, retryDelay: 50 }));
process.once("exit", () => {
restoreDirectoryWriteAccess(paperclipTestHome);
fs.rmSync(paperclipTestHome, { recursive: true, force: true, maxRetries: 8, retryDelay: 50 });
});
const require = createRequire(import.meta.url);
const SupertestTest = require("supertest/lib/test.js") as SupertestTestConstructor;