Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): empty non top-level stack does not get deleted #21624

Merged
merged 8 commits into from
Aug 17, 2022
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class CdkToolkit {
} else {
warning('%s: stack has no resources, deleting existing stack.', chalk.bold(stack.displayName));
await this.destroy({
selector: { patterns: [stack.stackName] },
selector: { patterns: [stack.hierarchicalId] },
exclusively: true,
force: true,
roleArn: options.roleArn,
Expand Down
22 changes: 21 additions & 1 deletion packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jest.mock('../lib/logging', () => ({
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { Bootstrapper } from '../lib/api/bootstrap';
import { CloudFormationDeployments, DeployStackOptions } from '../lib/api/cloudformation-deployments';
import { CloudFormationDeployments, DeployStackOptions, DestroyStackOptions } from '../lib/api/cloudformation-deployments';
import { DeployStackResult } from '../lib/api/deploy-stack';
import { Template } from '../lib/api/util/cloudformation';
import { CdkToolkit, Tag } from '../lib/cdk-toolkit';
Expand Down Expand Up @@ -558,6 +558,21 @@ describe('deploy', () => {
});
});

describe('destroy', () => {
test('destroy correct stack', async () => {
const toolkit = defaultToolkitSetup();

await expect(() => {
return toolkit.destroy({
selector: { patterns: ['Test-Stack-A/Test-Stack-C'] },
exclusively: true,
force: true,
fromDeploy: true,
});
}).resolves;
});
});

describe('watch', () => {
test("fails when no 'watch' settings are found", async () => {
const toolkit = defaultToolkitSetup();
Expand Down Expand Up @@ -931,6 +946,11 @@ class FakeCloudFormation extends CloudFormationDeployments {
});
}

public destroyStack(options: DestroyStackOptions): Promise<void> {
expect(options.stack).toBeDefined();
return Promise.resolve();
}

public readCurrentTemplate(stack: cxapi.CloudFormationStackArtifact): Promise<Template> {
switch (stack.stackName) {
case MockStack.MOCK_STACK_A.stackName:
Expand Down