diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index 152b05ada..feb3561d5 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -17,6 +17,7 @@ import * as path from 'path'; import tmp from 'tmp'; import { loadDocument } from 'zenstack/cli/cli-util'; import prismaPlugin from 'zenstack/plugins/prisma'; +import os from 'os'; /** * Use it to represent multiple files in a single string like this @@ -30,7 +31,7 @@ import prismaPlugin from 'zenstack/plugins/prisma'; */ export const FILE_SPLITTER = '#FILE_SPLITTER#'; -tmp.setGracefulCleanup(); +const tempDirs: string[] = []; export type FullDbClientContract = CrudContract & { $on(eventType: any, callback: (event: any) => void): void; @@ -254,6 +255,7 @@ export function createProjectAndCompile(schema: string, options: SchemaLoadOptio let projectDir = opt.projectDir; if (!projectDir) { const r = tmp.dirSync({ unsafeCleanup: true }); + tempDirs.push(r.name); projectDir = r.name; } @@ -423,3 +425,18 @@ function loadModule(module: string, basePath: string): any { const modulePath = require.resolve(module, { paths: [basePath] }); return require(modulePath); } + +export function cleanUpTemps() { + process.chdir(os.tmpdir()); + tempDirs.forEach((d) => { + console.log('Cleaning up temp dir:', d); + if (fs.existsSync(d)) { + try { + fs.rmSync(d, { recursive: true, force: true }); + } catch { + // ignore + } + } + }); + tempDirs.splice(0, tempDirs.length); +} diff --git a/tests/integration/test-setup.ts b/tests/integration/test-setup.ts index b6147c287..f385a196e 100644 --- a/tests/integration/test-setup.ts +++ b/tests/integration/test-setup.ts @@ -1,3 +1,4 @@ +import { cleanUpTemps } from '@zenstackhq/testtools'; import { toBeRejectedByPolicy, toBeNotFound, @@ -15,3 +16,7 @@ expect.extend({ toResolveNull, toBeRejectedWithCode, }); + +afterAll(() => { + cleanUpTemps(); +}); diff --git a/tests/regression/test-setup.ts b/tests/regression/test-setup.ts index 89142216d..1ffbb15f4 100644 --- a/tests/regression/test-setup.ts +++ b/tests/regression/test-setup.ts @@ -1,3 +1,4 @@ +import { cleanUpTemps } from '@zenstackhq/testtools'; import { toBeNotFound, toBeRejectedByPolicy, @@ -15,3 +16,7 @@ expect.extend({ toResolveNull, toBeRejectedWithCode, }); + +afterAll(() => { + cleanUpTemps(); +});