Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { MappingGroup } from '..';
import type { SdkProvider } from '../../../api/aws-auth/sdk-provider';
import { groupStacks, RefactoringContext } from '../../../api/refactoring';
import { ToolkitError } from '../../../toolkit/toolkit-error';
import { isObject } from '../../../util';

export function parseMappingGroups(s: string) {
const mappingGroups = doParse();
Expand All @@ -27,11 +28,11 @@ export function parseMappingGroups(s: string) {

function doParse(): MappingGroup[] {
const content = JSON.parse(s);
if (content.environments || !Array.isArray(content.environments)) {
return content.environments;
} else {
throw new ToolkitError('MissingEnvironmentsArray', "Expected an 'environments' array");
const environments = content?.environments;
if (!Array.isArray(environments) || !environments.every((group) => isObject(group?.resources))) {
throw new ToolkitError('MissingEnvironmentsArray', "Expected an 'environments' array whose entries each have a 'resources' object");
Comment on lines +32 to +33

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Optional — One error message, Expected an 'environments' array whose entries each have a 'resources' object, now covers every kind of bad input: a missing key, a value that isn't an array, and a bad entry at any position. It never says what was actually found. A user with several environment entries can't tell which one failed. The repo's error-handling guidance asks messages to include the wrong value.

Suggested change: Split the check into two branches. If environments is not an array, say what was found instead, such as its type or the file's top-level keys. If an entry has no resources object, find it with findIndex and report its index plus its account/region. Start both messages with Expected an 'environments' array so the new tests still match, then regenerate the three new .ndjson snapshots.

}
return environments;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ with the following structure:

```json
{
"mappings": [
"environments": [
{
"account": "123456789012",
"region": "us-east-1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"seq":0,"type":"notify","action":"refactor","level":"error","code":null,"message":"Expected an 'environments' array whose entries each have a 'resources' object"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"seq":0,"type":"notify","action":"refactor","level":"error","code":null,"message":"Expected an 'environments' array whose entries each have a 'resources' object"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"seq":0,"type":"notify","action":"refactor","level":"error","code":null,"message":"Expected an 'environments' array whose entries each have a 'resources' object"}
13 changes: 13 additions & 0 deletions packages/aws-cdk/test/commands/refactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ describe('refusals', () => {
);
});

test.each([
['uses the old mappings key', { mappings: [{ account: ACCOUNT, region: REGION, resources: { 'Stack1.OldName': 'Stack1.NewName' } }] }],
['has an empty environments value', { environments: '' }],
['has an entry without resources', { environments: [{ account: ACCOUNT, region: REGION }] }],
])('an override file that %s is rejected before doing anything', async (_, content) => {
const toolkit = await makeToolkit([{ stackName: 'Stack1', env: ENV, template: { Resources: {} } }]);
const overrideFile = writeOverrideFile(content);

await expect(toolkit.refactor({ dryRun: true, overrideFile })).rejects.toThrow(
"Expected an 'environments' array",
);
});

test('a refactor that adds or removes resources is rejected', async () => {
givenDeployedStacks({ Stack1: { Resources: { MyBucket: bucket('Stack1/MyBucket/Resource') } } });

Expand Down
Loading