diff --git a/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts b/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts index 39a9027..87a4239 100644 --- a/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts +++ b/packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts @@ -370,6 +370,13 @@ export class TestFixture extends ShellHelper { ], options); } + public async cdkRefactor(options: CdkCliOptions = {}) { + return this.cdk([ + 'refactor', + ...(options.options ?? []), + ], options); + } + public async cdkDestroy(stackNames: string | string[], options: CdkDestroyCliOptions = {}) { stackNames = typeof stackNames === 'string' ? [stackNames] : stackNames; diff --git a/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/cdk.json b/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/cdk.json new file mode 100644 index 0000000..3d27118 --- /dev/null +++ b/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/cdk.json @@ -0,0 +1,7 @@ +{ + "app": "node refactoring.js", + "versionReporting": false, + "context": { + "aws-cdk:enableDiffNoFail": "true" + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/refactoring.js b/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/refactoring.js new file mode 100644 index 0000000..8e957ff --- /dev/null +++ b/packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/refactoring.js @@ -0,0 +1,18 @@ +const cdk = require('aws-cdk-lib'); +const sqs = require('aws-cdk-lib/aws-sqs'); + +class BasicStack extends cdk.Stack { + constructor(parent, id, props) { + super(parent, id, props); + new sqs.Queue(this, props.queueName); + } +} + +const stackPrefix = process.env.STACK_NAME_PREFIX; +const app = new cdk.App(); + +new BasicStack(app, `${stackPrefix}-basic`, { + queueName: process.env.BASIC_QUEUE_LOGICAL_ID ?? 'BasicQueue', +}); + +app.synth(); \ No newline at end of file diff --git a/packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cdk-refactor-dry-run.integtest.ts b/packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cdk-refactor-dry-run.integtest.ts new file mode 100644 index 0000000..03391cc --- /dev/null +++ b/packages/@aws-cdk-testing/cli-integ/tests/cli-integ-tests/cdk-refactor-dry-run.integtest.ts @@ -0,0 +1,54 @@ +import { integTest, withSpecificFixture } from '../../lib'; + +integTest( + 'detects refactoring changes and prints the result', + withSpecificFixture('refactoring', async (fixture) => { + // First, deploy a stack + await fixture.cdkDeploy('basic', { + modEnv: { + BASIC_QUEUE_LOGICAL_ID: 'OldName' + } + }); + + // Then see if the refactoring tool detects the change + const stdErr = await fixture.cdkRefactor({ + options: ['--dry-run', '--unstable=refactor'], + allowErrExit: true, + // Making sure the synthesized stack has the new name + // so that a refactor is detected + modEnv: { + BASIC_QUEUE_LOGICAL_ID: 'NewName' + } + }); + + expect(stdErr).toContain('The following resources were moved or renamed:'); + expect(removeColor(stdErr)).toMatch(/│ AWS::SQS::Queue │ .*\/OldName\/Resource │ .*\/NewName\/Resource │/); + }), +); + +integTest( + 'no refactoring changes detected', + withSpecificFixture('refactoring', async (fixture) => { + const modEnv = { + BASIC_QUEUE_LOGICAL_ID: 'OldName' + }; + + // First, deploy a stack + await fixture.cdkDeploy('basic', { modEnv }); + + // Then see if the refactoring tool detects the change + const stdErr = await fixture.cdkRefactor({ + options: ['--dry-run', '--unstable=refactor'], + allowErrExit: true, + modEnv, + }); + + expect(stdErr).toContain('Nothing to refactor'); + }), +); + +function removeColor(str: string): string { + return str.replace(/\x1B[[(?);]{0,2}(;?\d)*./g, ''); +} + +