From 29e9ca2b7d69fc60108cdef93a689e991f683008 Mon Sep 17 00:00:00 2001
From: Niranjan Jayakar <nija@amazon.com>
Date: Tue, 16 Feb 2021 17:33:11 +0000
Subject: [PATCH] chore(cdk): remove expired feature flags from init templates

As we expire feature flags in CDKv2, remove the expired feature flags
from the init templates.
---
 packages/@aws-cdk/cx-api/lib/features.ts | 2 +-
 packages/aws-cdk/lib/init.ts             | 7 ++++++-
 packages/aws-cdk/test/init.test.ts       | 9 +++++++--
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/packages/@aws-cdk/cx-api/lib/features.ts b/packages/@aws-cdk/cx-api/lib/features.ts
index 57c9a5738ee96..24ba471882644 100644
--- a/packages/@aws-cdk/cx-api/lib/features.ts
+++ b/packages/@aws-cdk/cx-api/lib/features.ts
@@ -102,7 +102,7 @@ export const S3_GRANT_WRITE_WITHOUT_ACL = '@aws-cdk/aws-s3:grantWriteWithoutAcl'
  *
  * Tests must cover the default (disabled) case and the future (enabled) case.
  */
-export const FUTURE_FLAGS = {
+export const FUTURE_FLAGS: { [key: string]: any } = {
   [ENABLE_STACK_NAME_DUPLICATES_CONTEXT]: 'true',
   [ENABLE_DIFF_NO_FAIL_CONTEXT]: 'true',
   [STACK_RELATIVE_EXPORTS_CONTEXT]: 'true',
diff --git a/packages/aws-cdk/lib/init.ts b/packages/aws-cdk/lib/init.ts
index 6166736f3e8da..78c844b0a6550 100644
--- a/packages/aws-cdk/lib/init.ts
+++ b/packages/aws-cdk/lib/init.ts
@@ -179,10 +179,15 @@ export class InitTemplate {
       return;
     }
 
+    const futureFlags: {[key: string]: any} = {};
+    Object.entries(cxapi.FUTURE_FLAGS)
+      .filter(([k, _]) => !cxapi.FUTURE_FLAGS_EXPIRED.includes(k))
+      .forEach(([k, v]) => futureFlags[k] = v);
+
     const config = await fs.readJson(cdkJson);
     config.context = {
       ...config.context,
-      ...cxapi.FUTURE_FLAGS,
+      ...futureFlags,
     };
 
     await fs.writeJson(cdkJson, config, { spaces: 2 });
diff --git a/packages/aws-cdk/test/init.test.ts b/packages/aws-cdk/test/init.test.ts
index db042d76fc25d..e659161fbb38a 100644
--- a/packages/aws-cdk/test/init.test.ts
+++ b/packages/aws-cdk/test/init.test.ts
@@ -83,10 +83,15 @@ describe.each(['1', '2'])('v%s tests', (majorVersion) => {
 
           const config = await fs.readJson(path.join(tmpDir, 'cdk.json'));
           const context = config.context || {};
-          for (const [key, expected] of Object.entries(cxapi.FUTURE_FLAGS)) {
-            const actual = context[key];
+          for (const [key, expected] of Object.entries(context)) {
+            const actual = cxapi.FUTURE_FLAGS[key];
             expect(actual).toEqual(expected);
           }
+
+          // assert that expired future flags are not part of the cdk.json
+          Object.keys(context).forEach(k => {
+            expect(cxapi.FUTURE_FLAGS_EXPIRED.includes(k)).toEqual(false);
+          });
         });
       }
     }