From 1796a8e21eebd5a9bf5d54007ccacbf785f77b12 Mon Sep 17 00:00:00 2001 From: Marcel Laverdet Date: Thu, 9 Mar 2023 15:18:27 -0600 Subject: [PATCH 1/3] fix(cli): hide diffs of mangled unicode strings CloudFormation's `GetStackTemplate` irrecoverably mangles any character not in the 7-bit ASCII range. This causes noisy output from `cdk diff` when a template contains non-English languages or emoji. We can detect this case and consider these strings equal. *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts b/packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts index 1cbd4b1a111d7..b1e6146cf8afd 100644 --- a/packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts +++ b/packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts @@ -27,6 +27,13 @@ export function deepEqual(lvalue: any, rvalue: any): boolean { safeParseFloat(lvalue) === safeParseFloat(rvalue)) { return true; } + // GetStackTemplate flattens any codepoint greater than "\u7f" to "?". This + // is true even for codepoints in the supplemental planes which are + // represented in JS as surrogate pairs, all the way up to "\u{10ffff}". + if (typeof lvalue === 'string' && typeof rvalue === 'string' && + lvalue === rvalue.replace(/[\u{80}-\u{10ffff}]/gu, '?')) { + return true; + } if (typeof lvalue !== typeof rvalue) { return false; } if (Array.isArray(lvalue) !== Array.isArray(rvalue)) { return false; } if (Array.isArray(lvalue) /* && Array.isArray(rvalue) */) { From 8f20a233faf06a4449f64c6f6e21d1541593f2b9 Mon Sep 17 00:00:00 2001 From: Marcel Laverdet Date: Thu, 9 Mar 2023 15:45:49 -0600 Subject: [PATCH 2/3] Add test cases --- .../cloudformation-diff/test/deep-equal.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/@aws-cdk/cloudformation-diff/test/deep-equal.test.ts diff --git a/packages/@aws-cdk/cloudformation-diff/test/deep-equal.test.ts b/packages/@aws-cdk/cloudformation-diff/test/deep-equal.test.ts new file mode 100644 index 0000000000000..59df74fa37552 --- /dev/null +++ b/packages/@aws-cdk/cloudformation-diff/test/deep-equal.test.ts @@ -0,0 +1,11 @@ +import { deepEqual } from '../lib/diff/util'; + +test('mangled strings', () => { + expect(deepEqual('foo', 'foo')).toBeTruthy(); + expect(deepEqual('????', '文字化け')).toBeTruthy(); + expect(deepEqual('?????', '🤦🏻‍♂️')).toBeTruthy(); + expect(deepEqual('?', '\u{10ffff}')).toBeTruthy(); + expect(deepEqual('\u007f', '\u007f')).toBeTruthy(); + expect(deepEqual('?', '\u007f')).toBeFalsy(); + expect(deepEqual('?', '\u0080')).toBeTruthy(); +}); From b0e5ef0fd6db320844fbb463cb1fbe6837029e6e Mon Sep 17 00:00:00 2001 From: Marcel Laverdet Date: Mon, 1 May 2023 19:45:23 -0500 Subject: [PATCH 3/3] Literally nothing