forked from aws/aws-cdk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add validation for ubergen aws-cdk-lib README import re-writes (…
…aws#14302) Imports in READMEs are now rewritten from '@aws-cdk/...' to 'aws-cdk-lib'. This change was introduced in aws#14255, but no validation was included in the original PR. This change introduces a post-build validation step for aws-cdk-lib to ensure that the module rewrites were properly done. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
ce7f01a
commit fe9fb4c
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/aws-cdk-lib/scripts/verify-readme-import-rewrites.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* This script verifies the behavior of the `rewriteReadmeImports` method in `ubergen`, | ||
* which rewrites '@aws-cdk/...' imports to 'aws-cdk-lib' imports in the package READMEs. | ||
* Any verification based on expected state of the live READMEs is going to be somewhat fragile, | ||
* but this is the most certain way to be notified if either the tool is broken or the READMEs | ||
* have been explicitly changed to no longer reference their import statements. | ||
* | ||
* The script looks at a few modules with known different import formats and verifies that the | ||
* expected rewritten import is present in the generated .jsii manifest. | ||
*/ | ||
|
||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
|
||
const jsiiManifestPath = path.resolve(process.cwd(), '.jsii'); | ||
if (!fs.existsSync(jsiiManifestPath)) { | ||
throw new Error(`No .jsii manifest file found at: ${jsiiManifestPath}`); | ||
} | ||
|
||
const jsiiManifest = JSON.parse(fs.readFileSync(jsiiManifestPath, { encoding: 'utf-8' })); | ||
|
||
// Expected import statements chosen from the individual module READMEs to have a breadth of styles and syntax. | ||
// If this test fails because one of the below import statements is invalid, | ||
// please update to have a new, comparable example. | ||
// This is admittedly a bit fragile; if this test breaks a lot, we should reconsider validation methodology. | ||
// Count of times this test has been broken by README updates so far (please increment as necessary! :D): 0 | ||
const EXPECTED_SUBMODULE_IMPORTS = { | ||
// import * as origins from '@aws-cdk/aws-cloudfront-origins'; | ||
'aws-cdk-lib.aws_cloudfront_origins': "import { aws_cloudfront_origins as origins } from 'aws-cdk-lib';", | ||
// import * as cw from "@aws-cdk/aws-cloudwatch"; | ||
'aws-cdk-lib.aws_cloudwatch_actions': "import { aws_cloudwatch as cw } from 'aws-cdk-lib';", | ||
// import { PhysicalName } from '@aws-cdk/core'; | ||
'aws-cdk-lib.aws_codepipeline_actions': "import { PhysicalName } from 'aws-cdk-lib';", | ||
// import { Rule, Schedule } from '@aws-cdk/aws-events'; | ||
'aws-cdk-lib.aws_events': "import { Rule, Schedule } from 'aws-cdk-lib/aws-events';", | ||
// import * as cdk from '@aws-cdk/core'; | ||
'aws-cdk-lib.aws_stepfunctions': "import * as cdk from 'aws-cdk-lib';", | ||
}; | ||
|
||
Object.entries(EXPECTED_SUBMODULE_IMPORTS).forEach(([submodule, importStatement]) => { | ||
const submoduleReadme = jsiiManifest.submodules[submodule]?.readme?.markdown; | ||
if (!submoduleReadme) { | ||
throw new Error(`jsii manifest for submodule ${submodule} not found`); | ||
} else if (!submoduleReadme.includes(importStatement)) { | ||
const errorMessage = `Expected to find import statement in ${submodule} README: ${importStatement}\n` + | ||
'This may mean the README has changed and this test needs to be updated, or the uberGen rewriteReadmeImports method is broken.'; | ||
throw new Error(errorMessage); | ||
} | ||
}); |