From bab1b48016fa195cb14e763ca342f117c77aeeb3 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Mon, 21 Jul 2025 13:56:12 +0800 Subject: [PATCH 1/2] test: clean up temp files --- packages/testtools/src/schema.ts | 16 ++++++++++++++++ tests/integration/test-setup.ts | 5 +++++ tests/regression/test-setup.ts | 5 +++++ 3 files changed, 26 insertions(+) diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index 152b05ada..96fc5edea 100644 --- a/packages/testtools/src/schema.ts +++ b/packages/testtools/src/schema.ts @@ -30,6 +30,7 @@ import prismaPlugin from 'zenstack/plugins/prisma'; */ export const FILE_SPLITTER = '#FILE_SPLITTER#'; +const tempDirs: string[] = []; tmp.setGracefulCleanup(); export type FullDbClientContract = CrudContract & { @@ -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,17 @@ function loadModule(module: string, basePath: string): any { const modulePath = require.resolve(module, { paths: [basePath] }); return require(modulePath); } + +export function cleanUpTemps() { + 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(); +}); From ff3642d6c2dd05e8af770bfe38e579e2c3dc5ec8 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Mon, 21 Jul 2025 14:44:14 +0800 Subject: [PATCH 2/2] update --- packages/testtools/src/schema.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/testtools/src/schema.ts b/packages/testtools/src/schema.ts index 96fc5edea..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 @@ -31,7 +32,6 @@ import prismaPlugin from 'zenstack/plugins/prisma'; export const FILE_SPLITTER = '#FILE_SPLITTER#'; const tempDirs: string[] = []; -tmp.setGracefulCleanup(); export type FullDbClientContract = CrudContract & { $on(eventType: any, callback: (event: any) => void): void; @@ -427,6 +427,7 @@ function loadModule(module: string, basePath: string): any { } export function cleanUpTemps() { + process.chdir(os.tmpdir()); tempDirs.forEach((d) => { console.log('Cleaning up temp dir:', d); if (fs.existsSync(d)) {