Skip to content

Commit 4a0c40f

Browse files
committed
update
1 parent 20cb34b commit 4a0c40f

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

packages/cli/test/db.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { execSync } from 'node:child_process';
21
import fs from 'node:fs';
32
import path from 'node:path';
43
import { describe, expect, it } from 'vitest';
5-
import { createProject } from './utils';
4+
import { createProject, runCli } from './utils';
65

76
const model = `
87
model User {
@@ -13,7 +12,7 @@ model User {
1312
describe('CLI db commands test', () => {
1413
it('should generate a database with db push', () => {
1514
const workDir = createProject(model);
16-
execSync('node node_modules/@zenstackhq/cli/bin/cli db push');
15+
runCli('db push', workDir);
1716
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
1817
});
1918
});

packages/cli/test/generate.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { execSync } from 'node:child_process';
21
import fs from 'node:fs';
32
import path from 'node:path';
43
import { describe, expect, it } from 'vitest';
5-
import { createProject } from './utils';
4+
import { createProject, runCli } from './utils';
65

76
const model = `
87
model User {
@@ -13,33 +12,33 @@ model User {
1312
describe('CLI generate command test', () => {
1413
it('should generate a TypeScript schema', () => {
1514
const workDir = createProject(model);
16-
execSync('node node_modules/@zenstackhq/cli/bin/cli generate');
15+
runCli('generate', workDir);
1716
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
1817
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(false);
1918
});
2019

2120
it('should respect custom output directory', () => {
2221
const workDir = createProject(model);
23-
execSync('node node_modules/@zenstackhq/cli/bin/cli generate --output ./zen');
22+
runCli('generate --output ./zen', workDir);
2423
expect(fs.existsSync(path.join(workDir, 'zen/schema.ts'))).toBe(true);
2524
});
2625

2726
it('should respect custom schema location', () => {
2827
const workDir = createProject(model);
2928
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'zenstack/foo.zmodel'));
30-
execSync('node node_modules/@zenstackhq/cli/bin/cli generate --schema ./zenstack/foo.zmodel');
29+
runCli('generate --schema ./zenstack/foo.zmodel', workDir);
3130
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
3231
});
3332

3433
it('should respect save prisma schema option', () => {
3534
const workDir = createProject(model);
36-
execSync('node node_modules/@zenstackhq/cli/bin/cli generate --save-prisma-schema');
35+
runCli('generate --save-prisma-schema', workDir);
3736
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(true);
3837
});
3938

4039
it('should respect save prisma schema custom path option', () => {
4140
const workDir = createProject(model);
42-
execSync('node node_modules/@zenstackhq/cli/bin/cli generate --save-prisma-schema "../prisma/schema.prisma"');
41+
runCli('generate --save-prisma-schema "../prisma/schema.prisma"', workDir);
4342
expect(fs.existsSync(path.join(workDir, 'prisma/schema.prisma'))).toBe(true);
4443
});
4544
});

packages/cli/test/init.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { execSync } from 'node:child_process';
21
import fs from 'node:fs';
32
import path from 'node:path';
43
import tmp from 'tmp';
54
import { describe, expect, it } from 'vitest';
5+
import { runCli } from './utils';
66

77
describe('Cli init command tests', () => {
88
it('should create a new project', () => {
99
const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });
10-
process.chdir(workDir);
11-
execSync('npm init -y');
12-
const cli = path.join(__dirname, '../dist/index.js');
13-
execSync(`node ${cli} init`);
14-
expect(fs.existsSync('zenstack/schema.zmodel')).toBe(true);
10+
runCli('init', workDir);
11+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.zmodel'))).toBe(true);
1512
});
1613
});

packages/cli/test/migrate.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { execSync } from 'node:child_process';
21
import fs from 'node:fs';
32
import path from 'node:path';
43
import { describe, expect, it } from 'vitest';
5-
import { createProject } from './utils';
4+
import { createProject, runCli } from './utils';
65

76
const model = `
87
model User {
@@ -13,30 +12,30 @@ model User {
1312
describe('CLI migrate commands test', () => {
1413
it('should generate a database with migrate dev', () => {
1514
const workDir = createProject(model);
16-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate dev --name init');
15+
runCli('migrate dev --name init', workDir);
1716
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
1817
expect(fs.existsSync(path.join(workDir, 'zenstack/migrations'))).toBe(true);
1918
});
2019

2120
it('should reset the database with migrate reset', () => {
2221
const workDir = createProject(model);
23-
execSync('node node_modules/@zenstackhq/cli/bin/cli db push');
22+
runCli('db push', workDir);
2423
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
25-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate reset --force');
24+
runCli('migrate reset --force', workDir);
2625
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
2726
});
2827

2928
it('should reset the database with migrate deploy', () => {
3029
const workDir = createProject(model);
31-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate dev --name init');
30+
runCli('migrate dev --name init', workDir);
3231
fs.rmSync(path.join(workDir, 'zenstack/dev.db'));
33-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate deploy');
32+
runCli('migrate deploy', workDir);
3433
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
3534
});
3635

3736
it('supports migrate status', () => {
38-
createProject(model);
39-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate dev --name init');
40-
execSync('node node_modules/@zenstackhq/cli/bin/cli migrate status');
37+
const workDir = createProject(model);
38+
runCli('migrate dev --name init', workDir);
39+
runCli('migrate status', workDir);
4140
});
4241
});

packages/cli/test/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createTestProject } from '@zenstackhq/testtools';
2+
import { execSync } from 'node:child_process';
23
import fs from 'node:fs';
34
import path from 'node:path';
45

@@ -13,6 +14,9 @@ export function createProject(zmodel: string, addPrelude = true) {
1314
fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true });
1415
const schemaPath = path.join(workDir, 'zenstack/schema.zmodel');
1516
fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n\n${zmodel}` : zmodel);
16-
process.chdir(workDir);
1717
return workDir;
1818
}
19+
20+
export function runCli(command: string, cwd: string) {
21+
execSync(`node ${__dirname}/../bin/cli ${command}`, { cwd });
22+
}

0 commit comments

Comments
 (0)