Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into chore/merge-dev-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Feb 20, 2024
2 parents 10cb00a + da53753 commit 16df179
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,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 @@ -33,6 +34,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.

67 changes: 42 additions & 25 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 = CrudContract & {
};

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, {
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}`);
}

function normalizePath(p: string) {
Expand Down Expand Up @@ -90,7 +99,7 @@ plugin enhancer {
plugin zod {
provider = '@core/zod'
preserveTsFiles = true
// preserveTsFiles = true
modelOnly = ${!options.fullZod}
}
`;
Expand Down Expand Up @@ -134,21 +143,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 @@ -185,16 +202,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}`, {
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}`, {
NODE_PATH: './node_modules',
});
}

if (opt.pushDb) {
Expand All @@ -205,10 +222,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

0 comments on commit 16df179

Please sign in to comment.