|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'node:test'; |
| 6 | +import { findLeakedRunDirectories } from './check-tmpdir-leaks-model.ts'; |
| 7 | + |
| 8 | +function withScratchRoot(fn: (root: string) => void): void { |
| 9 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'check-tmpdir-leaks-model-test-')); |
| 10 | + try { |
| 11 | + fn(root); |
| 12 | + } finally { |
| 13 | + fs.rmSync(root, { recursive: true, force: true }); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +test('a directory owned by a still-running process is not reported as a leak', () => { |
| 18 | + withScratchRoot((root) => { |
| 19 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-4242-abcdef')); |
| 20 | + const leaks = findLeakedRunDirectories(root, (pid) => pid === 4242); |
| 21 | + assert.deepEqual(leaks, []); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +test('a directory whose owning process has exited is reported as a leak', () => { |
| 26 | + withScratchRoot((root) => { |
| 27 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-4242-abcdef')); |
| 28 | + const leaks = findLeakedRunDirectories(root, () => false); |
| 29 | + assert.deepEqual(leaks, ['agent-device-test-run-4242-abcdef']); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +test('a concurrent run from another worktree does not fail the check for this one', () => { |
| 34 | + withScratchRoot((root) => { |
| 35 | + // Simulates two worktrees running vitest at once: pid 1111 (this |
| 36 | + // invocation, still alive) and pid 2222 (a different worktree's |
| 37 | + // in-progress run, also alive). |
| 38 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-1111-aaaaaa')); |
| 39 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-2222-bbbbbb')); |
| 40 | + const alivePids = new Set([1111, 2222]); |
| 41 | + const leaks = findLeakedRunDirectories(root, (pid) => alivePids.has(pid)); |
| 42 | + assert.deepEqual(leaks, []); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +test('a mix of active and abandoned directories reports only the abandoned one', () => { |
| 47 | + withScratchRoot((root) => { |
| 48 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-1111-aaaaaa')); // alive |
| 49 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-3333-cccccc')); // exited |
| 50 | + const leaks = findLeakedRunDirectories(root, (pid) => pid === 1111); |
| 51 | + assert.deepEqual(leaks, ['agent-device-test-run-3333-cccccc']); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('a directory with no parseable pid is conservatively reported as a leak', () => { |
| 56 | + withScratchRoot((root) => { |
| 57 | + fs.mkdirSync(path.join(root, 'agent-device-test-run-not-a-pid')); |
| 58 | + const leaks = findLeakedRunDirectories(root, () => true); |
| 59 | + assert.deepEqual(leaks, ['agent-device-test-run-not-a-pid']); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('non-matching directories and files are ignored', () => { |
| 64 | + withScratchRoot((root) => { |
| 65 | + fs.mkdirSync(path.join(root, 'unrelated-directory')); |
| 66 | + fs.writeFileSync(path.join(root, 'agent-device-test-run-4242-loose-file'), ''); |
| 67 | + const leaks = findLeakedRunDirectories(root, () => false); |
| 68 | + assert.deepEqual(leaks, []); |
| 69 | + }); |
| 70 | +}); |
0 commit comments