Skip to content

Commit

Permalink
chore(cdk-lib): cloud-assembly-schema tests are flaky, becaue they ar…
Browse files Browse the repository at this point in the history
…e not free of side-effects (#26568)

The attempted fix in #26551 did not work.

This is another attempt with a different approach.
Based on the fact that the failure occurs in `manifest.ts`, there appear to be circumstances for this file to be load in a context that is not fully initialized. Thus the `Cannot find name 'require'` error.
However runtime code like `require` is not needed to generate the schemas.
In fact the `manifest.ts` file isn't needed at all for this.

This change ensures that only the required files are loaded when generating each schema.
Hopefully this will bypass the erroneous code.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain authored Jul 30, 2023
1 parent c610c97 commit 0e2c169
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,32 @@ function log(message: string) {
*/
const SCHEMA_DIR = path.resolve(__dirname, '../schema');

const SCHEMA_DEFINITIONS: { [schemaName: string]: { rootTypeName: string } } = {
'assets': { rootTypeName: 'AssetManifest' },
'cloud-assembly': { rootTypeName: 'AssemblyManifest' },
'integ': { rootTypeName: 'IntegManifest' },
const SCHEMA_DEFINITIONS: {
[schemaName: string]: {
/**
* The name of the root type.
*/
rootTypeName: string;
/**
* Files loaded to generate the schema.
* Should be relative to `cloud-assembly-schema/lib`.
* Usually this is just the file containing the root type.
*/
files: string[];
}
} = {
'assets': {
rootTypeName: 'AssetManifest',
files: [path.join('assets', 'schema.ts')],
},
'cloud-assembly': {
rootTypeName: 'AssemblyManifest',
files: [path.join('cloud-assembly', 'schema.ts')],
},
'integ': {
rootTypeName: 'IntegManifest',
files: [path.join('integ-tests', 'schema.ts')],
},
};

export const SCHEMAS = Object.keys(SCHEMA_DEFINITIONS);
Expand Down Expand Up @@ -59,14 +81,13 @@ export function generateSchema(schemaName: string, saveToFile: boolean = true) {
topRef: true,
noExtraProps: false,
out,
skipLibCheck: true,
};

const compilerOptions = {
strictNullChecks: true,
};

const program = tjs.getProgramFromFiles([path.join(__dirname, '../lib/index.d.ts')], compilerOptions);
const program = tjs.getProgramFromFiles(spec.files.map(file =>path.join(__dirname, '..', 'lib', file)), compilerOptions);
const schema = tjs.generateSchema(program, spec.rootTypeName, settings);

augmentDescription(schema);
Expand Down Expand Up @@ -126,5 +147,5 @@ function augmentDescription(schema: any) {
* compatibility checks.
*/
function addAnyMetadataEntry(schema: any) {
schema.definitions.MetadataEntry?.properties.data.anyOf.push({ description: 'Free form data.' });
schema?.definitions?.MetadataEntry?.properties.data.anyOf.push({ description: 'Free form data.' });
}

0 comments on commit 0e2c169

Please sign in to comment.