Skip to content

Commit bab1b48

Browse files
committed
test: clean up temp files
1 parent 415fee2 commit bab1b48

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

packages/testtools/src/schema.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import prismaPlugin from 'zenstack/plugins/prisma';
3030
*/
3131
export const FILE_SPLITTER = '#FILE_SPLITTER#';
3232

33+
const tempDirs: string[] = [];
3334
tmp.setGracefulCleanup();
3435

3536
export type FullDbClientContract = CrudContract & {
@@ -254,6 +255,7 @@ export function createProjectAndCompile(schema: string, options: SchemaLoadOptio
254255
let projectDir = opt.projectDir;
255256
if (!projectDir) {
256257
const r = tmp.dirSync({ unsafeCleanup: true });
258+
tempDirs.push(r.name);
257259
projectDir = r.name;
258260
}
259261

@@ -423,3 +425,17 @@ function loadModule(module: string, basePath: string): any {
423425
const modulePath = require.resolve(module, { paths: [basePath] });
424426
return require(modulePath);
425427
}
428+
429+
export function cleanUpTemps() {
430+
tempDirs.forEach((d) => {
431+
console.log('Cleaning up temp dir:', d);
432+
if (fs.existsSync(d)) {
433+
try {
434+
fs.rmSync(d, { recursive: true, force: true });
435+
} catch {
436+
// ignore
437+
}
438+
}
439+
});
440+
tempDirs.splice(0, tempDirs.length);
441+
}

tests/integration/test-setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cleanUpTemps } from '@zenstackhq/testtools';
12
import {
23
toBeRejectedByPolicy,
34
toBeNotFound,
@@ -15,3 +16,7 @@ expect.extend({
1516
toResolveNull,
1617
toBeRejectedWithCode,
1718
});
19+
20+
afterAll(() => {
21+
cleanUpTemps();
22+
});

tests/regression/test-setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cleanUpTemps } from '@zenstackhq/testtools';
12
import {
23
toBeNotFound,
34
toBeRejectedByPolicy,
@@ -15,3 +16,7 @@ expect.extend({
1516
toResolveNull,
1617
toBeRejectedWithCode,
1718
});
19+
20+
afterAll(() => {
21+
cleanUpTemps();
22+
});

0 commit comments

Comments
 (0)