Skip to content

Commit f22b829

Browse files
committed
test: validate gc command rejects --role-arn flag
Add unit test to ensure gc command throws error when used with --role-arn. Add documentation to explain reasoning behind this feature
1 parent 95023c0 commit f22b829

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
477477
if (!configuration.settings.get(['unstable']).includes('gc')) {
478478
throw new ToolkitError('Unstable feature use: \'gc\' is unstable. It must be opted in via \'--unstable\', e.g. \'cdk gc --unstable=gc\'');
479479
}
480+
// roleArn is defined for when cloudformation is invoked
481+
// This conflicts with direct sdk calls existing in the gc command to s3 and ecr
480482
if (args.roleArn) {
481483
throw new ToolkitError('The --role-arn option is not supported for the gc command');
482484
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ jest.mock('../../lib/cli/parse-command-line-arguments', () => ({
5151
if (args.includes('ts')) {
5252
result = { ...result, language: 'typescript' };
5353
}
54+
} else if (args.includes('gc')) {
55+
result = { ...result, _: ['gc'] };
56+
57+
// Handle role-arn flag for gc command validation testing
58+
// This simulates parser behavior to test that the CLI properly rejects roleArn
59+
if (args.includes('--role-arn')) {
60+
result = { ...result, roleArn: 'arn:aws:iam::123456789012:role/TestRole' };
61+
}
5462
}
5563

5664
// Handle notices flags
@@ -433,6 +441,9 @@ describe('gc command validation tests', () => {
433441
jest.clearAllMocks();
434442
});
435443

444+
/**
445+
* Tests that the gc does not support roleArn
446+
*/
436447
test('should throw error when --role-arn is provided with gc command', async () => {
437448
// Mock configuration to include 'gc' in unstable features
438449
const mockConfig = {
@@ -450,7 +461,7 @@ describe('gc command validation tests', () => {
450461

451462
(Configuration as any).mockImplementation(() => mockConfig);
452463
Configuration.fromArgsAndFiles = jest.fn().mockImplementation(() => mockConfig);
453-
464+
// Expects an error to be thrown when roleArn is used with gc
454465
await expect(exec(['gc', '--role-arn', 'arn:aws:iam::123456789012:role/TestRole', '--unstable=gc']))
455466
.rejects
456467
.toThrow('The --role-arn option is not supported for the gc command');

0 commit comments

Comments
 (0)