Skip to content

Commit 5af1c35

Browse files
committed
test: speed up prisma-generator tests by pnpm-installing to a test-specific store once, then offline-installing from that store for each test going forward
1 parent 37f4a3b commit 5af1c35

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ dist
88
coverage
99
.build
1010
.test
11+
12+
13+
/.pnpm-test-store

packages/schema/tests/generator/prisma-generator.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@ import path from 'path';
66
import tmp from 'tmp';
77
import { loadDocument } from '../../src/cli/cli-util';
88
import { PrismaSchemaGenerator } from '../../src/plugins/prisma/schema-generator';
9-
import { execSync } from '../../src/utils/exec-utils';
109
import { loadModel } from '../utils';
10+
import { preparePackageJson, initProjectDir } from '../../../../script/test-utils';
1111

1212
tmp.setGracefulCleanup();
1313

1414
describe('Prisma generator test', () => {
1515
let origDir: string;
1616

17+
let packageJsonContents: string;
18+
19+
beforeAll(async () => {
20+
packageJsonContents = preparePackageJson({
21+
"prisma": "^5.0.0"
22+
});
23+
console.log(`Got my packageJsonContents`);
24+
})
25+
1726
beforeEach(() => {
1827
origDir = process.cwd();
1928
const r = tmp.dirSync({ unsafeCleanup: true });
2029
console.log(`Project dir: ${r.name}`);
2130
process.chdir(r.name);
2231

23-
execSync('npm init -y', { stdio: 'ignore' });
24-
execSync('npm install prisma');
32+
initProjectDir(r.name, packageJsonContents);
2533
});
2634

2735
afterEach(() => {

script/test-utils.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import path from 'node:path';
2+
import fs from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import { execSync } from 'node:child_process';
5+
6+
export const PNPM_STORE_PATH = path.resolve(__dirname, '../.pnpm-test-store');
7+
export const NPM_RC_FILE = '.npmrc';
8+
export const NPM_RC_CONTENTS = `store-dir = ${PNPM_STORE_PATH}`;
9+
export const PACKAGE_JSON_FILE = 'package.json';
10+
export const PACKAGE_JSON_CONTENTS = '{"name":"test-project","version":"1.0.0"}';
11+
12+
export function preparePackageJson(dependencies: {[key: string]: string} = {}, devDependencies: {[key: string]: string} = {}): string {
13+
const tmpDir = fs.mkdtempSync(path.join(tmpdir(), 'zenstack-test-'));
14+
try {
15+
const packageJsonContents =
16+
`{
17+
"name":"test-project",
18+
"version":"1.0.0",
19+
"dependencies": {
20+
${Object.entries(dependencies).map(([k, v]) => `"${k}": "${v}"`).join(',\n')}
21+
},
22+
"devDependencies": {
23+
${Object.entries(devDependencies).map(([k, v]) => `"${k}": "${v}"`).join(',\n')}
24+
}
25+
}`;
26+
27+
initProjectDir(tmpDir, packageJsonContents, false);
28+
29+
return packageJsonContents;
30+
} finally {
31+
fs.rmSync(tmpDir, {recursive: true, force: true});
32+
console.log(`Loaded dependencies into store via temp dir ${tmpDir}`);
33+
}
34+
}
35+
36+
function execCmdSync(cmd: string, path: string) {
37+
console.log(`Running: ${cmd}, in ${path}`);
38+
try {
39+
execSync(cmd, { cwd: path, stdio: 'ignore' });
40+
} catch (err) {
41+
console.error(`Test project scaffolding cmd error: ${err}`);
42+
throw err;
43+
}
44+
}
45+
46+
export function initProjectDir(projectDir: string, packageJsonContents: string, offline: boolean = true) {
47+
if (!fs.existsSync(projectDir)) {
48+
fs.mkdirSync(projectDir, { recursive: true });
49+
}
50+
fs.writeFileSync(path.join(projectDir, PACKAGE_JSON_FILE), packageJsonContents, { flag: 'w+' });
51+
fs.writeFileSync(path.join(projectDir, NPM_RC_FILE), NPM_RC_CONTENTS, { flag: 'w+' });
52+
execCmdSync(`pnpm install ${offline ? '--offline ' : ''}--ignore-workspace`, projectDir);
53+
}

0 commit comments

Comments
 (0)