Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
.npmcache
coverage
.build
.test
6 changes: 5 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
* https://jestjs.io/docs/configuration
*/

import path from 'path';

export default {
// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,

globalSetup: path.join(__dirname, './test-setup.ts'),

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// The directory where Jest should output its coverage files
coverageDirectory: 'tests/coverage',
coverageDirectory: path.join(__dirname, '.test/coverage'),

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['/node_modules/', '/tests/'],
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src --ext ts",
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && copyfiles -u 1 src/package.template.json src/.npmrc.template dist && pnpm pack dist --pack-destination '../../../.build'",
"build": "pnpm lint && pnpm clean && tsc && copyfiles ./package.json ./LICENSE ./README.md dist && pnpm pack dist --pack-destination '../../../.build'",
"watch": "tsc --watch",
"prepublishOnly": "pnpm build"
},
Expand Down
1 change: 0 additions & 1 deletion packages/testtools/src/.npmrc.template

This file was deleted.

21 changes: 0 additions & 21 deletions packages/testtools/src/package.template.json

This file was deleted.

48 changes: 25 additions & 23 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ export type FullDbClientContract = Record<string, DbOperations> & {
};

export function run(cmd: string, env?: Record<string, string>, cwd?: string) {
const start = Date.now();
execSync(cmd, {
stdio: 'pipe',
encoding: 'utf-8',
env: { ...process.env, DO_NOT_TRACK: '1', ...env },
cwd,
});
console.log('Execution took', Date.now() - start, 'ms', '-', cmd);
try {
const start = Date.now();
execSync(cmd, {

Check warning

Code scanning / CodeQL

Shell command built from environment values

This shell command depends on an uncontrolled [absolute path](1). This shell command depends on an uncontrolled [absolute path](2). This shell command depends on an uncontrolled [absolute path](3). This shell command depends on an uncontrolled [absolute path](4). This shell command depends on an uncontrolled [absolute path](5). This shell command depends on an uncontrolled [absolute path](6). This shell command depends on an uncontrolled [absolute path](7). This shell command depends on an uncontrolled [absolute path](8). This shell command depends on an uncontrolled [absolute path](9). This shell command depends on an uncontrolled [absolute path](10). This shell command depends on an uncontrolled [absolute path](11). This shell command depends on an uncontrolled [absolute path](12). This shell command depends on an uncontrolled [absolute path](13). This shell command depends on an uncontrolled [absolute path](14). This shell command depends on an uncontrolled [absolute path](15). This shell command depends on an uncontrolled [absolute path](16). This shell command depends on an uncontrolled [absolute path](17).
stdio: 'pipe',
encoding: 'utf-8',
env: { ...process.env, DO_NOT_TRACK: '1', ...env },
cwd,
});
console.log('Execution took', Date.now() - start, 'ms', '-', cmd);
} catch (err) {
console.error('Command failed:', cmd, err);
throw err;
}
}

function normalizePath(p: string) {
Expand Down Expand Up @@ -86,17 +91,17 @@ generator js {

plugin meta {
provider = '@core/model-meta'
preserveTsFiles = true
// preserveTsFiles = true
}

plugin policy {
provider = '@core/access-policy'
preserveTsFiles = true
// preserveTsFiles = true
}

plugin zod {
provider = '@core/zod'
preserveTsFiles = true
// preserveTsFiles = true
modelOnly = ${!options.fullZod}
}
`;
Expand Down Expand Up @@ -138,21 +143,18 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {

const { name: projectRoot } = tmp.dirSync({ unsafeCleanup: true });

const root = getWorkspaceRoot(__dirname);
const workspaceRoot = getWorkspaceRoot(__dirname);

if (!root) {
if (!workspaceRoot) {
throw new Error('Could not find workspace 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));

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

// copy project structure from scaffold (prepared by test-setup.ts)
fs.cpSync(path.join(workspaceRoot, '.test/scaffold'), projectRoot, { recursive: true, force: true });

let zmodelPath = path.join(projectRoot, 'schema.zmodel');

const files = schema.split(FILE_SPLITTER);
Expand Down Expand Up @@ -189,16 +191,16 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
}
}

run('npm install');

const outputArg = opt.output ? ` --output ${opt.output}` : '';

if (opt.customSchemaFilePath) {
run(`npx zenstack generate --schema ${zmodelPath} --no-dependency-check${outputArg}`, {
run(`npx zenstack generate --schema --no-version-check ${zmodelPath} --no-dependency-check${outputArg}`, {

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input

This string concatenation which depends on [library input](1) is later used in a [shell command](2). This string concatenation which depends on [library input](1) is later used in a [shell command](2). This string concatenation which depends on [library input](3) is later used in a [shell command](2). This string concatenation which depends on [library input](4) is later used in a [shell command](2). This string concatenation which depends on [library input](4) is later used in a [shell command](2).
NODE_PATH: './node_modules',
});
} else {
run(`npx zenstack generate --no-dependency-check${outputArg}`, { NODE_PATH: './node_modules' });
run(`npx zenstack generate --no-version-check --no-dependency-check${outputArg}`, {

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input

This string concatenation which depends on [library input](1) is later used in a [shell command](2). This string concatenation which depends on [library input](3) is later used in a [shell command](2).
NODE_PATH: './node_modules',
});
}

if (opt.pushDb) {
Expand Down
40 changes: 40 additions & 0 deletions test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';

export default function globalSetup() {
console.log('Setting up test project scaffold...');

const scaffoldPath = path.join(__dirname, '.test/scaffold');
if (fs.existsSync(scaffoldPath)) {
fs.rmSync(scaffoldPath, { recursive: true, force: true });
}
fs.mkdirSync(scaffoldPath, { recursive: true });

function run(cmd: string) {
console.log(`Running: ${cmd}, in ${scaffoldPath}`);
try {
execSync(cmd, { cwd: scaffoldPath, stdio: 'ignore' });
} catch (err) {
console.error(`Test project scaffolding cmd error: ${err}`);
throw err;
}
}

run('npm init -y');
run('npm i -D prisma typescript');
run('npm i @prisma/client zod decimal.js');

const localDeps = [
'packages/schema/dist',
'packages/runtime/dist',
'packages/plugins/swr/dist',
'packages/plugins/tanstack-query/dist',
'packages/plugins/trpc/dist',
'packages/plugins/openapi/dist',
];

run(`npm i ${localDeps.map((d) => path.join('../../', d)).join(' ')}`);

console.log('Test scaffold setup complete.');
}
10 changes: 0 additions & 10 deletions tests/integration/global-setup.js

This file was deleted.

26 changes: 3 additions & 23 deletions tests/integration/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import baseConfig from '../../jest.config';

/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/
export default {
// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,

// A map from regular expressions to paths to transformers
transform: { '^.+\\.tsx?$': 'ts-jest' },

testTimeout: 300000,

...baseConfig,
setupFilesAfterEnv: ['./test-setup.ts'],

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// The directory where Jest should output its coverage files
coverageDirectory: 'tests/coverage',

// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ['/node_modules/', '/tests/'],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',

// A list of reporter names that Jest uses when writing coverage reports
coverageReporters: ['json', 'text', 'lcov', 'clover'],
};
2 changes: 1 addition & 1 deletion tests/integration/tests/schema/todo.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ generator js {

plugin zod {
provider = '@core/zod'
preserveTsFiles = true
// preserveTsFiles = true
}

/*
Expand Down