Skip to content

fix: optimize the way how generated packages are loaded in test environment #549

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
Jul 4, 2023
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "",
"scripts": {
"build": "pnpm -r build",
"test": "pnpm -r run test --silent",
"test-ci": "pnpm -r run test --silent",
"test": "ZENSTACK_TEST=1 pnpm -r run test --silent",
"test-ci": "ZENSTACK_TEST=1 pnpm -r run test --silent",
"lint": "pnpm -r lint",
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
"publish-preview": "pnpm --filter \"./packages/**\" -r publish --registry http://localhost:4873"
Expand Down
18 changes: 11 additions & 7 deletions packages/runtime/src/enhancements/model-meta.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { lowerCaseFirst } from 'lower-case-first';
import { ModelMeta } from './types';
import path from 'path';
import { ModelMeta } from './types';

/**
* Load model meta from standard location.
*/
export function getDefaultModelMeta(): ModelMeta {
try {
if (process.env.NODE_ENV === 'test') {
// handling the case when running as tests, resolve relative to CWD
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'model-meta')).default;
} else {
return require('.zenstack/model-meta').default;
}
// normal load
return require('.zenstack/model-meta').default;
} catch {
if (process.env.ZENSTACK_TEST === '1') {
try {
// special handling for running as tests, try resolving relative to CWD
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'model-meta')).default;
} catch {
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.');
}
}
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.');
}
}
Expand Down
21 changes: 20 additions & 1 deletion packages/runtime/src/enhancements/policy/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-explicit-any */

import path from 'path';
import { AuthUser, DbClientContract } from '../../types';
import { getDefaultModelMeta } from '../model-meta';
import { makeProxy } from '../proxy';
Expand Down Expand Up @@ -74,9 +76,18 @@ export function withPolicy<DbClient extends object>(

function getDefaultPolicy(): PolicyDef {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('.zenstack/policy').default;
} catch {
if (process.env.ZENSTACK_TEST === '1') {
try {
// special handling for running as tests, try resolving relative to CWD
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'policy')).default;
} catch {
throw new Error(
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
);
}
}
throw new Error(
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
);
Expand All @@ -88,6 +99,14 @@ function getDefaultZodSchemas(): ZodSchemas | undefined {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require('.zenstack/zod');
} catch {
if (process.env.ZENSTACK_TEST === '1') {
try {
// special handling for running as tests, try resolving relative to CWD
return require(path.join(process.cwd(), 'node_modules', '.zenstack', 'zod'));
} catch {
return undefined;
}
}
return undefined;
}
}