Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions packages/@aws-cdk-testing/cli-integ/lib/with-cdk-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"app": "node refactoring.js",
"versionReporting": false,
"context": {
"aws-cdk:enableDiffNoFail": "true"
}
}
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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, '');
}