From 81182938e60b7db9e72ba5d3837018f4184743c0 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Wed, 28 Jul 2021 18:28:03 +0300 Subject: [PATCH] chore: introduce pragma:context to cdk-integ (#15779) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since context passed via command line overrides the App context, setting newStyleSynthesis in the pipeline integration tests will not work since in the v2 branch we set newStyleSynth to “false” explicitly in cdk-integ. Introduce a new cdk-integ pragma that can be used to override this context and use it for pipelines integration tests. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../@aws-cdk/pipelines/test/integ.newpipeline.ts | 2 +- .../pipelines/test/integ.pipeline-security.ts | 2 +- tools/cdk-integ-tools/lib/integ-helpers.ts | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/pipelines/test/integ.newpipeline.ts b/packages/@aws-cdk/pipelines/test/integ.newpipeline.ts index b777b61a23e09..b327745145505 100644 --- a/packages/@aws-cdk/pipelines/test/integ.newpipeline.ts +++ b/packages/@aws-cdk/pipelines/test/integ.newpipeline.ts @@ -1,5 +1,5 @@ // eslint-disable-next-line import/no-extraneous-dependencies -/// !cdk-integ PipelineStack +/// !cdk-integ PipelineStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true import * as sqs from '@aws-cdk/aws-sqs'; import { App, Stack, StackProps, Stage, StageProps } from '@aws-cdk/core'; import { Construct } from 'constructs'; diff --git a/packages/@aws-cdk/pipelines/test/integ.pipeline-security.ts b/packages/@aws-cdk/pipelines/test/integ.pipeline-security.ts index 8b43db6087a1c..a5d257b78523e 100644 --- a/packages/@aws-cdk/pipelines/test/integ.pipeline-security.ts +++ b/packages/@aws-cdk/pipelines/test/integ.pipeline-security.ts @@ -1,4 +1,4 @@ -/// !cdk-integ PipelineSecurityStack +/// !cdk-integ PipelineSecurityStack pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true import * as codepipeline from '@aws-cdk/aws-codepipeline'; import * as codepipeline_actions from '@aws-cdk/aws-codepipeline-actions'; import * as iam from '@aws-cdk/aws-iam'; diff --git a/tools/cdk-integ-tools/lib/integ-helpers.ts b/tools/cdk-integ-tools/lib/integ-helpers.ts index 5aabc99210663..bf52cbc4247cc 100644 --- a/tools/cdk-integ-tools/lib/integ-helpers.ts +++ b/tools/cdk-integ-tools/lib/integ-helpers.ts @@ -8,6 +8,7 @@ const CDK_OUTDIR = 'cdk-integ.out'; const CDK_INTEG_STACK_PRAGMA = '/// !cdk-integ'; const PRAGMA_PREFIX = 'pragma:'; +const SET_CONTEXT_PRAGMA_PREFIX = 'pragma:set-context:'; export class IntegrationTests { constructor(private readonly directory: string) { @@ -95,10 +96,23 @@ export class IntegrationTest { * Return the "main" template or a concatenation of all listed templates in the pragma */ public async cdkSynthFast(options: SynthOptions = {}): Promise { - const context = { + const context: Record = { ...options.context, }; + // apply context from set-context pragma + // usage: pragma:set-context:key=value + const ctxPragmas = (await this.pragmas()).filter(p => p.startsWith(SET_CONTEXT_PRAGMA_PREFIX)); + for (const p of ctxPragmas) { + const instruction = p.substring(SET_CONTEXT_PRAGMA_PREFIX.length); + const [key, value] = instruction.split('='); + if (key == null || value == null) { + throw new Error(`invalid "set-context" pragma syntax. example: "pragma:set-context:@aws-cdk/core:newStyleStackSynthesis=true" got: ${p}`); + } + + context[key] = value; + } + try { await exec(['node', `${this.name}`], { cwd: this.directory,