Skip to content
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

fix(cloudformation-diff): cdk diff not picking up differences if old/new value is in format n.n.n #16050

Merged
merged 6 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,16 @@ export function unionOf(lv: string[] | Set<string>, rv: string[] | Set<string>):
*/
function safeParseFloat(str: string): number {
const ret = parseFloat(str);
const nonNumericRegex = /\d*\.\d+\./;
if (ret === 0) {
// if the str is exactly '0', that's OK;
// but parseFloat() also returns 0 for things like '0.0';
// in this case, return NaN, so we'll fall back to string comparison
return str === '0' ? ret : NaN;
} else if (nonNumericRegex.test(str)) {
// if the str contains non-numeric characters,
// return NaN, so we'll fall back to string comparison
return NaN;
} else {
return ret;
}
Expand Down
91 changes: 91 additions & 0 deletions packages/@aws-cdk/cloudformation-diff/test/diff-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,94 @@ test('when a property changes including equivalent DependsOn', () => {
differences = diffTemplate(newTemplate, currentTemplate);
expect(differences.resources.differenceCount).toBe(1);
});

test('when a property with a number-like format changes', () => {
const bucketName = 'ShineyBucketName';
const tagChanges = {
'0.31.1-prod': '0.31.2-prod',
'8.0.5.5.4-identifier': '8.0.5.5.5-identifier',
'1.1.1.1': '1.1.2.2',
'1.2.3': '1.2.4',
'2.2.2.2': '2.2.3.2',
'3.3.3.3': '3.4.3.3',
};
const oldTags = Object.keys(tagChanges);
const newTags = Object.values(tagChanges);
const currentTemplate = {
Resources: {
QueueResource: {
Type: 'AWS::SQS::Queue',
},
BucketResource: {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: bucketName,
Tags: oldTags,
},
},
},
};
const newTemplate = {
Resources: {
QueueResource: {
Type: 'AWS::SQS::Queue',
},
BucketResource: {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: bucketName,
Tags: newTags,
},
},
},
};

const differences = diffTemplate(currentTemplate, newTemplate);
expect(differences.differenceCount).toBe(1);
expect(differences.resources.differenceCount).toBe(1);
const difference = differences.resources.changes.BucketResource;
expect(difference).not.toBeUndefined();
expect(difference?.oldResourceType).toEqual('AWS::S3::Bucket');
expect(difference?.propertyUpdates).toEqual({
Tags: { oldValue: oldTags, newValue: newTags, changeImpact: ResourceImpact.WILL_UPDATE, isDifferent: true },
});
});

test('when a property with a number-like format doesn\'t change', () => {
const bucketName = 'ShineyBucketName';
const tags = ['0.31.1-prod', '8.0.5.5.4-identifier', '1.1.1.1', '1.2.3'];
const currentTemplate = {
Resources: {
QueueResource: {
Type: 'AWS::SQS::Queue',
},
BucketResource: {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: bucketName,
Tags: tags,
},
},
},
};
const newTemplate = {
Resources: {
QueueResource: {
Type: 'AWS::SQS::Queue',
},
BucketResource: {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: bucketName,
Tags: tags,
},
},
},
};

const differences = diffTemplate(currentTemplate, newTemplate);
expect(differences.differenceCount).toBe(0);
expect(differences.resources.differenceCount).toBe(0);
const difference = differences.resources.changes.BucketResource;
expect(difference).toBeUndefined();
});