Skip to content

Commit efb161f

Browse files
committed
test(cli): clean up migration-show temp dirs in afterEach
Tests in migration-show.test.ts used createTempDir() to mkdir under tmpdir() but never removed the directories, leaving real state behind between runs (violating the CLI guideline that tests must clean up their own ephemeral directories). Track every returned dir in a module-level list and remove them in a suite-wide afterEach with rm({ recursive: true, force: true }) so cleanup runs even on failures. Signed-off-by: Will Madden <madden@prisma.io>
1 parent 4147a79 commit efb161f

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

packages/1-framework/3-tooling/cli/test/commands/migration-show.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdir } from 'node:fs/promises';
1+
import { mkdir, rm } from 'node:fs/promises';
22
import { tmpdir } from 'node:os';
33
import { join } from 'node:path';
44
import { createContract, createSqlContract } from '@prisma-next/contract/testing';
@@ -17,7 +17,7 @@ import {
1717
import type { MigrationMetadata } from '@prisma-next/migration-tools/metadata';
1818
import type { OnDiskMigrationPackage } from '@prisma-next/migration-tools/package';
1919
import stripAnsi from 'strip-ansi';
20-
import { describe, expect, it } from 'vitest';
20+
import { afterEach, describe, expect, it } from 'vitest';
2121
import type { MigrationShowSpaceResult } from '../../src/commands/migration-show';
2222
import {
2323
resolveAppTargetPath,
@@ -27,15 +27,26 @@ import {
2727
import { formatMigrationShowOutput } from '../../src/utils/formatters/migrations';
2828
import { parseGlobalFlags } from '../../src/utils/global-flags';
2929

30+
// Track every temp dir handed out by `createTempDir` so the suite-wide
31+
// `afterEach` can remove them — even when an assertion fails — keeping
32+
// ephemeral state out of the shared `tmpdir()` between runs.
33+
const createdTempDirs: string[] = [];
34+
3035
async function createTempDir(prefix: string): Promise<string> {
3136
const dir = join(
3237
tmpdir(),
3338
`test-migration-show-${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`,
3439
);
3540
await mkdir(dir, { recursive: true });
41+
createdTempDirs.push(dir);
3642
return dir;
3743
}
3844

45+
afterEach(async () => {
46+
const dirs = createdTempDirs.splice(0);
47+
await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true })));
48+
});
49+
3950
function createOp(
4051
id: string,
4152
label: string,

0 commit comments

Comments
 (0)