Skip to content

chore: improve test run speed #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ jobs:
run: pnpm install --frozen-lockfile

- name: Test
run: pnpm run test-ci
run: pnpm run test-scaffold && pnpm run test-ci
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
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ I want to think you first for considering contributing to ZenStack 🙏🏻. It'
pnpm build
```

1. Scaffold the project used for testing

```bash
pnpm test-scaffold
```

You only need to run this command once.

1. Run tests

```bash
Expand Down
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"scripts": {
"build": "pnpm -r build",
"lint": "pnpm -r lint",
"test": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit",
"test-ci": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit",
"test": "ZENSTACK_TEST=1 pnpm -r --parallel run test --silent --forceExit",
"test-ci": "ZENSTACK_TEST=1 pnpm -r --parallel run test --silent --forceExit",
"test-scaffold": "tsx script/test-scaffold.ts",
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
"publish-preview": "pnpm --filter \"./packages/**\" -r publish --force --registry https://preview.registry.zenstack.dev/",
"unpublish-preview": "pnpm --recursive --shell-mode exec -- npm unpublish -f --registry https://preview.registry.zenstack.dev/ \"\\$PNPM_PACKAGE_NAME\""
Expand All @@ -30,6 +31,7 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsup": "^8.0.1",
"tsx": "^4.7.1",
"typescript": "^5.3.2"
}
}
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.

71 changes: 44 additions & 27 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ 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;
}
}

export function installPackage(pkg: string, dev = false) {
run(`npm install ${dev ? '-D' : ''} --no-audit --no-fund ${pkg}`);

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). This string concatenation which depends on [library input](4) is later used in a [shell command](2).
}

function normalizePath(p: string) {
Expand Down Expand Up @@ -86,17 +95,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 +147,29 @@ 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 });

// install local deps
const localInstallDeps = [
'packages/schema/dist',
'packages/runtime/dist',
'packages/plugins/swr/dist',
'packages/plugins/trpc/dist',
'packages/plugins/openapi/dist',
];

run(`npm i --no-audit --no-fund ${localInstallDeps.map((d) => path.join(workspaceRoot, d)).join(' ')}`);

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

const files = schema.split(FILE_SPLITTER);
Expand Down Expand Up @@ -189,16 +206,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 --no-version-check --schema ${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 All @@ -209,10 +226,10 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
opt.extraDependencies?.push('@prisma/extension-pulse');
}

opt.extraDependencies?.forEach((dep) => {
console.log(`Installing dependency ${dep}`);
run(`npm install ${dep}`);
});
if (opt.extraDependencies) {
console.log(`Installing dependency ${opt.extraDependencies.join(' ')}`);
installPackage(opt.extraDependencies.join(' '));
}

opt.copyDependencies?.forEach((dep) => {
const pkgJson = JSON.parse(fs.readFileSync(path.join(dep, 'package.json'), { encoding: 'utf-8' }));
Expand Down
Loading