Skip to content

Commit 84b124d

Browse files
committed
feat: display warning on using gc with roleArn instead of throwing an error
1 parent 3808765 commit 84b124d

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

packages/aws-cdk/lib/cli/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
475475
// roleArn is defined for when cloudformation is invoked
476476
// This conflicts with direct sdk calls existing in the gc command to s3 and ecr
477477
if (args.roleArn) {
478-
throw new ToolkitError('The --role-arn option is not supported for the gc command');
478+
await ioHost.defaults.warn('The --role-arn option is not supported for the gc command and will be ignored.');
479479
}
480480
if (args.bootstrapStackName) {
481481
await ioHost.defaults.warn('--bootstrap-stack-name is deprecated and will be removed when gc is GA. Use --toolkit-stack-name.');

packages/aws-cdk/test/cli/cli.test.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -436,34 +436,45 @@ describe('notices configuration tests', () => {
436436
});
437437
});
438438

439-
describe('gc command validation tests', () => {
439+
describe('gc command warning tests', () => {
440440
beforeEach(() => {
441441
jest.clearAllMocks();
442442
});
443443

444-
/**
445-
* Tests that the gc does not support roleArn
446-
*/
447-
test('should throw error when --role-arn is provided with gc command', async () => {
448-
// Mock configuration to include 'gc' in unstable features
444+
test('should warn when --role-arn is used with gc command', async () => {
445+
const mockWarn = jest.fn();
449446
const mockConfig = {
447+
settings: { get: (key: string[]) => key[0] === 'unstable' ? ['gc'] : [] },
448+
context: { get: () => [] },
450449
loadConfigFiles: jest.fn().mockResolvedValue(undefined),
451-
settings: {
452-
get: jest.fn().mockImplementation((key: string[]) => {
453-
if (key[0] === 'unstable') return ['gc'];
454-
return undefined;
455-
}),
456-
},
457-
context: {
458-
get: jest.fn().mockReturnValue([]),
459-
},
460450
};
461-
451+
462452
(Configuration as any).mockImplementation(() => mockConfig);
463453
Configuration.fromArgsAndFiles = jest.fn().mockImplementation(() => mockConfig);
464-
// Expects an error to be thrown when roleArn is used with gc
465-
await expect(exec(['gc', '--role-arn', 'arn:aws:iam::123456789012:role/TestRole', '--unstable=gc']))
466-
.rejects
467-
.toThrow('The --role-arn option is not supported for the gc command');
454+
jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'garbageCollect').mockResolvedValue();
455+
jest.spyOn(CliIoHost, 'instance').mockReturnValue({
456+
defaults: {
457+
warn: mockWarn,
458+
debug: jest.fn(),
459+
info: jest.fn(),
460+
error: jest.fn(),
461+
trace: jest.fn()
462+
},
463+
noticesDestination: 'stderr',
464+
notify: jest.fn(),
465+
asIoHelper: () => ({
466+
defaults: {
467+
warn: mockWarn,
468+
debug: jest.fn(),
469+
info: jest.fn(),
470+
error: jest.fn(),
471+
trace: jest.fn()
472+
}
473+
}),
474+
startTelemetry: jest.fn().mockResolvedValue(undefined)
475+
} as any);
476+
477+
await exec(['gc', '--role-arn', 'test-role', '--unstable=gc']);
478+
expect(mockWarn).toHaveBeenCalledWith('The --role-arn option is not supported for the gc command and will be ignored.');
468479
});
469480
});

0 commit comments

Comments
 (0)