Skip to content

Commit

Permalink
chore: fix test directory cleanup (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Apr 18, 2023
1 parent ed99ea4 commit 3be9ade
Show file tree
Hide file tree
Showing 18 changed files with 716 additions and 3,108 deletions.
8 changes: 4 additions & 4 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
"tmp": "^0.2.1"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/jest": "^29.5.0",
"@types/react": "^18.0.26",
"@types/supertest": "^2.0.12",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
"jest": "^29.4.3",
"jest": "^29.5.0",
"react": "^17.0.2 || ^18",
"rimraf": "^3.0.2",
"superjson": "^1.11.0",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4",
"@zenstackhq/testtools": "workspace:*"
"typescript": "^4.9.4"
}
}
11 changes: 9 additions & 2 deletions packages/next/tests/request-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { loadSchema } from '@zenstackhq/testtools';
import fs from 'fs';
import { createServer, RequestListener } from 'http';
import { apiResolver } from 'next/dist/server/api-utils/node';
import superjson from 'superjson';
Expand Down Expand Up @@ -36,13 +37,17 @@ function makeTestClient(apiPath: string, options: RequestHandlerOptions, queryAr

describe('request handler tests', () => {
let origDir: string;
let projDir: string;

beforeEach(() => {
origDir = process.cwd();
});

afterEach(() => {
process.chdir(origDir);
if (projDir) {
fs.rmSync(projDir, { recursive: true, force: true });
}
});

it('simple crud', async () => {
Expand All @@ -53,7 +58,8 @@ model M {
}
`;

const { prisma } = await loadSchema(model);
const { prisma, projectDir } = await loadSchema(model);
projDir = projectDir;

await makeTestClient('/m/create', { getPrisma: () => prisma })
.post('/')
Expand Down Expand Up @@ -174,7 +180,8 @@ model M {
}
`;

const { withPresets } = await loadSchema(model);
const { withPresets, projectDir } = await loadSchema(model);
projDir = projectDir;

await makeTestClient('/m/create', { getPrisma: () => withPresets() })
.post('/m/create')
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@
"openapi-types": "^12.1.0",
"tiny-invariant": "^1.3.1",
"yaml": "^2.2.1",
"zod": "^3.19.1",
"zod": "3.21.1",
"zod-validation-error": "^0.2.1"
},
"devDependencies": {
"@prisma/internals": "^4.7.1",
"@readme/openapi-parser": "^2.4.0",
"@types/jest": "^29.4.0",
"@types/jest": "^29.5.0",
"@types/tmp": "^0.2.3",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
"eslint": "^8.35.0",
"jest": "^29.4.3",
"jest": "^29.5.0",
"rimraf": "^3.0.2",
"tmp": "^0.2.1",
"ts-jest": "^29.0.5",
Expand Down
11 changes: 9 additions & 2 deletions packages/plugins/react/tests/react-hooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/// <reference types="@types/jest" />

import { loadSchema } from '@zenstackhq/testtools';
import fs from 'fs';

describe('React Hooks Plugin Tests', () => {
let origDir: string;
let projDir: string;

beforeAll(() => {
origDir = process.cwd();
});

afterEach(() => {
process.chdir(origDir);
if (projDir) {
fs.rmSync(projDir, { recursive: true, force: true });
}
});

const sharedModel = `
Expand Down Expand Up @@ -41,7 +46,7 @@ model Foo {
`;

it('swr generator', async () => {
await loadSchema(
const { projectDir } = await loadSchema(
`
plugin react {
provider = '${process.cwd()}/dist'
Expand All @@ -55,10 +60,11 @@ ${sharedModel}
[`${origDir}/dist`, 'react', '@types/react', 'swr'],
true
);
projDir = projectDir;
});

it('react-query generator', async () => {
await loadSchema(
const { projectDir } = await loadSchema(
`
plugin react {
provider = '${process.cwd()}/dist'
Expand All @@ -73,5 +79,6 @@ ${sharedModel}
[`${origDir}/dist`, 'react', '@types/react', '@tanstack/react-query'],
true
);
projDir = projectDir;
});
});
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prettier": "^2.8.3",
"ts-morph": "^16.0.0",
"tslib": "^2.4.1",
"zod": "^3.19.1"
"zod": "3.21.1"
},
"devDependencies": {
"@types/jest": "^29.5.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/plugins/trpc/tests/trpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
/// <reference types="@types/jest" />

import { loadSchema } from '@zenstackhq/testtools';
import fs from 'fs';

describe('tRPC Plugin Tests', () => {
let origDir: string;
let projDir: string;

beforeAll(() => {
origDir = process.cwd();
});

afterEach(() => {
process.chdir(origDir);
if (projDir) {
fs.rmSync(projDir, { recursive: true, force: true });
}
});

it('run plugin', async () => {
await loadSchema(
const { projectDir } = await loadSchema(
`
plugin trpc {
provider = '${process.cwd()}/dist'
Expand Down Expand Up @@ -51,5 +56,6 @@ model Foo {
[`${origDir}/dist`, '@trpc/client', '@trpc/server'],
true
);
projDir = projectDir;
});
});
4 changes: 2 additions & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"pluralize": "^8.0.0",
"superjson": "^1.11.0",
"tslib": "^2.4.1",
"zod": "^3.19.1",
"zod": "3.21.1",
"zod-validation-error": "^0.2.1"
},
"peerDependencies": {
Expand All @@ -44,7 +44,7 @@
"license": "MIT",
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/jest": "^29.0.3",
"@types/jest": "^29.5.0",
"@types/node": "^14.18.29",
"@types/pluralize": "^0.0.29",
"copyfiles": "^2.4.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@
"vscode-languageserver": "^8.0.2",
"vscode-languageserver-textdocument": "^1.0.7",
"vscode-uri": "^3.0.6",
"zod": "^3.19.1",
"zod": "3.21.1",
"zod-validation-error": "^0.2.1"
},
"devDependencies": {
"@types/async-exit-hook": "^2.0.0",
"@types/jest": "^29.2.0",
"@types/jest": "^29.5.0",
"@types/node": "^14.18.32",
"@types/pluralize": "^0.0.29",
"@types/semver": "^7.3.13",
Expand All @@ -130,7 +130,7 @@
"esbuild": "^0.15.12",
"eslint": "^8.27.0",
"eslint-plugin-jest": "^27.1.7",
"jest": "^29.2.1",
"jest": "^29.5.0",
"prisma": "^4.0.0",
"renamer": "^4.0.0",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/tests/cli/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('CLI Plugins Tests', () => {
});

afterEach(() => {
// fs.rmSync(projDir, { recursive: true, force: true });
fs.rmSync(projDir, { recursive: true, force: true });
process.chdir(origDir);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.17",
"@types/jest": "^29.4.0",
"@types/jest": "^29.5.0",
"@types/supertest": "^2.0.12",
"@zenstackhq/testtools": "workspace:*",
"body-parser": "^1.20.2",
"copyfiles": "^2.4.1",
"express": "^4.18.2",
"fastify": "^4.14.1",
"fastify-plugin": "^4.5.0",
"jest": "^29.4.3",
"jest": "^29.5.0",
"rimraf": "^3.0.2",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/src/package.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"prisma": "^4.0.0",
"typescript": "^4.9.3",
"zenstack": "file:<root>/packages/schema/dist",
"zod": "^3.19.1"
"zod": "3.21.1"
}
}
91 changes: 48 additions & 43 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,61 +96,66 @@ export async function loadSchema(
) {
const { name: projectRoot } = tmp.dirSync();

const root = getWorkspaceRoot(__dirname);
try {
const root = getWorkspaceRoot(__dirname);

if (!root) {
throw new Error('Could not find workspace root');
}
if (!root) {
throw new Error('Could not find workspace root');
}

console.log('Workspace root:', root);
console.log('Workspace root:', root);

const pkgContent = fs.readFileSync(path.join(__dirname, 'package.template.json'), { encoding: 'utf-8' });
fs.writeFileSync(path.join(projectRoot, 'package.json'), pkgContent.replaceAll('<root>', root));
const pkgContent = fs.readFileSync(path.join(__dirname, 'package.template.json'), { encoding: 'utf-8' });
fs.writeFileSync(path.join(projectRoot, 'package.json'), pkgContent.replaceAll('<root>', root));

const npmrcContent = fs.readFileSync(path.join(__dirname, '.npmrc.template'), { encoding: 'utf-8' });
fs.writeFileSync(path.join(projectRoot, '.npmrc'), npmrcContent.replaceAll('<root>', root));
const npmrcContent = fs.readFileSync(path.join(__dirname, '.npmrc.template'), { encoding: 'utf-8' });
fs.writeFileSync(path.join(projectRoot, '.npmrc'), npmrcContent.replaceAll('<root>', root));

console.log('Workdir:', projectRoot);
process.chdir(projectRoot);
console.log('Workdir:', projectRoot);
process.chdir(projectRoot);

schema = schema.replaceAll('$projectRoot', projectRoot);
schema = schema.replaceAll('$projectRoot', projectRoot);

const content = addPrelude ? `${MODEL_PRELUDE}\n${schema}` : schema;
fs.writeFileSync('schema.zmodel', content);
run('npm install');
run('npx zenstack generate --no-dependency-check', { NODE_PATH: './node_modules' });
const content = addPrelude ? `${MODEL_PRELUDE}\n${schema}` : schema;
fs.writeFileSync('schema.zmodel', content);
run('npm install');
run('npx zenstack generate --no-dependency-check', { NODE_PATH: './node_modules' });

if (pushDb) {
run('npx prisma db push');
}
if (pushDb) {
run('npx prisma db push');
}

const PrismaClient = require(path.join(projectRoot, 'node_modules/.prisma/client')).PrismaClient;
const prisma = new PrismaClient({ log: ['info', 'warn', 'error'] });
const PrismaClient = require(path.join(projectRoot, 'node_modules/.prisma/client')).PrismaClient;
const prisma = new PrismaClient({ log: ['info', 'warn', 'error'] });

extraDependencies.forEach((dep) => {
console.log(`Installing dependency ${dep}`);
run(`npm install ${dep}`);
});
extraDependencies.forEach((dep) => {
console.log(`Installing dependency ${dep}`);
run(`npm install ${dep}`);
});

if (compile) {
console.log('Compiling...');
run('npx tsc --init');
run('npx tsc --project tsconfig.json');
}
if (compile) {
console.log('Compiling...');
run('npx tsc --init');
run('npx tsc --project tsconfig.json');
}

const policy = require(path.join(projectRoot, '.zenstack/policy')).default;
const modelMeta = require(path.join(projectRoot, '.zenstack/model-meta')).default;
const zodSchemas = require(path.join(projectRoot, '.zenstack/zod')).default;

return {
projectDir: projectRoot,
prisma,
withPolicy: (user?: AuthUser) => withPolicy<WeakDbClientContract>(prisma, { user }, policy, modelMeta),
withOmit: () => withOmit<WeakDbClientContract>(prisma, modelMeta),
withPassword: () => withPassword<WeakDbClientContract>(prisma, modelMeta),
withPresets: (user?: AuthUser) => withPresets<WeakDbClientContract>(prisma, { user }, policy, modelMeta),
zodSchemas,
};
const policy = require(path.join(projectRoot, '.zenstack/policy')).default;
const modelMeta = require(path.join(projectRoot, '.zenstack/model-meta')).default;
const zodSchemas = require(path.join(projectRoot, '.zenstack/zod')).default;

return {
projectDir: projectRoot,
prisma,
withPolicy: (user?: AuthUser) => withPolicy<WeakDbClientContract>(prisma, { user }, policy, modelMeta),
withOmit: () => withOmit<WeakDbClientContract>(prisma, modelMeta),
withPassword: () => withPassword<WeakDbClientContract>(prisma, modelMeta),
withPresets: (user?: AuthUser) => withPresets<WeakDbClientContract>(prisma, { user }, policy, modelMeta),
zodSchemas,
};
} catch (err) {
fs.rmSync(projectRoot, { recursive: true, force: true });
throw err;
}
}

/**
Expand Down
Loading

0 comments on commit 3be9ade

Please sign in to comment.