Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions tests/integration/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cleanUpTemps } from '@zenstackhq/testtools';
import {
toBeRejectedByPolicy,
toBeNotFound,
Expand All @@ -15,3 +16,7 @@ expect.extend({
toResolveNull,
toBeRejectedWithCode,
});

afterAll(() => {
cleanUpTemps();
});
5 changes: 5 additions & 0 deletions tests/regression/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cleanUpTemps } from '@zenstackhq/testtools';
import {
toBeNotFound,
toBeRejectedByPolicy,
Expand All @@ -15,3 +16,7 @@ expect.extend({
toResolveNull,
toBeRejectedWithCode,
});

afterAll(() => {
cleanUpTemps();
});
Loading