Skip to content

Commit 2465b51

Browse files
authored
chore(cdk-integ-tools): move canonicalizeTemplates internal (#18647)
We're about to deprecate `assert-internal` and `cdk-integ-tools` only uses `canonicalizeTemplates`. There is an argument for canonicalizing assets by default in `assertions` as well, but I am putting that on the backlog for now. For now, just copying over the relevant function which will remove the dependency to `assert-internal`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 291963b commit 2465b51

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

tools/@aws-cdk/cdk-integ-tools/bin/cdk-integ-assert.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
// Verify that all integration tests still match their expected output
3-
import { canonicalizeTemplate } from '@aws-cdk/assert-internal';
43
import { diffTemplate, formatDifferences } from '@aws-cdk/cloudformation-diff';
4+
import { canonicalizeTemplate } from '../lib/canonicalize-assets';
55
import { DEFAULT_SYNTH_OPTIONS, IntegrationTests } from '../lib/integ-helpers';
66

77
/* eslint-disable no-console */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Reduce template to a normal form where asset references have been normalized
3+
*
4+
* This makes it possible to compare templates if all that's different between
5+
* them is the hashes of the asset values.
6+
*
7+
* Currently only handles parameterized assets, but can (and should)
8+
* be adapted to handle convention-mode assets as well when we start using
9+
* more of those.
10+
*/
11+
export function canonicalizeTemplate(template: any): any {
12+
// For the weird case where we have an array of templates...
13+
if (Array.isArray(template)) {
14+
return template.map(canonicalizeTemplate);
15+
}
16+
17+
// Find assets via parameters
18+
const stringSubstitutions = new Array<[RegExp, string]>();
19+
const paramRe = /^AssetParameters([a-zA-Z0-9]{64})(S3Bucket|S3VersionKey|ArtifactHash)([a-zA-Z0-9]{8})$/;
20+
21+
const assetsSeen = new Set<string>();
22+
for (const paramName of Object.keys(template?.Parameters || {})) {
23+
const m = paramRe.exec(paramName);
24+
if (!m) { continue; }
25+
if (assetsSeen.has(m[1])) { continue; }
26+
27+
assetsSeen.add(m[1]);
28+
const ix = assetsSeen.size;
29+
30+
// Full parameter reference
31+
stringSubstitutions.push([
32+
new RegExp(`AssetParameters${m[1]}(S3Bucket|S3VersionKey|ArtifactHash)([a-zA-Z0-9]{8})`),
33+
`Asset${ix}$1`,
34+
]);
35+
// Substring asset hash reference
36+
stringSubstitutions.push([
37+
new RegExp(`${m[1]}`),
38+
`Asset${ix}Hash`,
39+
]);
40+
}
41+
42+
// Substitute them out
43+
return substitute(template);
44+
45+
function substitute(what: any): any {
46+
if (Array.isArray(what)) {
47+
return what.map(substitute);
48+
}
49+
50+
if (typeof what === 'object' && what !== null) {
51+
const ret: any = {};
52+
for (const [k, v] of Object.entries(what)) {
53+
ret[stringSub(k)] = substitute(v);
54+
}
55+
return ret;
56+
}
57+
58+
if (typeof what === 'string') {
59+
return stringSub(what);
60+
}
61+
62+
return what;
63+
}
64+
65+
function stringSub(x: string) {
66+
for (const [re, replacement] of stringSubstitutions) {
67+
x = x.replace(re, replacement);
68+
}
69+
return x;
70+
}
71+
}

tools/@aws-cdk/cdk-integ-tools/package.json

-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"dependencies": {
4040
"@aws-cdk/cloudformation-diff": "0.0.0",
4141
"@aws-cdk/cx-api": "0.0.0",
42-
"@aws-cdk/assert-internal": "0.0.0",
4342
"aws-cdk": "0.0.0",
4443
"fs-extra": "^9.1.0",
4544
"yargs": "^16.2.0"
@@ -52,9 +51,6 @@
5251
"engines": {
5352
"node": ">= 10.13.0 <13 || >=13.7.0"
5453
},
55-
"peerDependencies": {
56-
"@aws-cdk/assert-internal": "0.0.0"
57-
},
5854
"ubergen": {
5955
"exclude": true
6056
}

0 commit comments

Comments
 (0)