Skip to content

Commit

Permalink
fix(cli): context keys specified in cdk.json get moved to `cdk.cont…
Browse files Browse the repository at this point in the history
…ext.json

Removes migration logic that was introduced to move legacy context provider keys.

Rationale:
This logic was intended to be removed prior to v1.0
It looks for any key that contains a `:` and moves it to `cdk.context.json`. This is
not expected behavior and also prevents users from having keys that have `:`. Our
init templates also include feature flags which make use of the `:` character.

Closes #7399
  • Loading branch information
shivlaks committed Apr 23, 2020
1 parent 98429e0 commit 022eb66
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 48 deletions.
30 changes: 0 additions & 30 deletions packages/aws-cdk/lib/settings.ts
Expand Up @@ -68,8 +68,6 @@ export class Configuration {
this._projectConfig = await loadAndLog(PROJECT_CONFIG);
this._projectContext = await loadAndLog(PROJECT_CONTEXT);

await this.migrateLegacyContext();

this.context = new Context(
this.commandLineContext,
this.projectConfig.subSettings([CONTEXT_KEY]).makeReadOnly(),
Expand Down Expand Up @@ -99,34 +97,6 @@ export class Configuration {

return this;
}

/**
* Migrate context from the 'context' field in the projectConfig object to the dedicated object
*
* Only migrate context whose key contains a ':', to migrate only context generated
* by context providers.
*/
private async migrateLegacyContext() {
const legacyContext = this.projectConfig.get([CONTEXT_KEY]);
if (legacyContext === undefined) { return; }

const toMigrate = Object.keys(legacyContext).filter(k => k.indexOf(':') > -1);
if (toMigrate.length === 0) { return; }

for (const key of toMigrate) {
this.projectContext.set([key], legacyContext[key]);
this.projectConfig.unset([CONTEXT_KEY, key]);
}

// If the source object is empty now, completely remove it
if (Object.keys(this.projectConfig.get([CONTEXT_KEY])).length === 0) {
this.projectConfig.unset([CONTEXT_KEY]);
}

// Save back
await this.projectConfig.save(PROJECT_CONFIG);
await this.projectContext.save(PROJECT_CONTEXT);
}
}

async function loadAndLog(fileName: string): Promise<Settings> {
Expand Down
21 changes: 3 additions & 18 deletions packages/aws-cdk/test/context.test.ts
Expand Up @@ -39,21 +39,6 @@ test('load context from both files if available', async () => {
expect(config.context.get('boo')).toBe('far');
});

test('context with colons gets migrated to new file', async () => {
// GIVEN
await fs.writeJSON('cdk.context.json', { foo: 'bar' });
await fs.writeJSON('cdk.json', { context: { 'boo': 'far', 'boo:boo': 'far:far' } });
const config = await new Configuration().load();

// WHEN
config.context.set('baz', 'quux');
await config.saveContext();

// THEN
expect(await fs.readJSON('cdk.context.json')).toEqual({ 'foo': 'bar', 'boo:boo': 'far:far', 'baz': 'quux' });
expect(await fs.readJSON('cdk.json')).toEqual({ context: { boo: 'far'} });
});

test('deleted context disappears from new file', async () => {
// GIVEN
await fs.writeJSON('cdk.context.json', { foo: 'bar' });
Expand Down Expand Up @@ -84,7 +69,7 @@ test('clear deletes from new file', async () => {
expect(await fs.readJSON('cdk.json')).toEqual({ context: { boo: 'far' } });
});

test('surive missing new file', async () => {
test('context is preserved in the location from which it is read', async () => {
// GIVEN
await fs.writeJSON('cdk.json', { context: { 'boo:boo' : 'far' } });
const config = await new Configuration().load();
Expand All @@ -94,8 +79,8 @@ test('surive missing new file', async () => {
await config.saveContext();

// THEN
expect(await fs.readJSON('cdk.context.json')).toEqual({ 'boo:boo' : 'far' });
expect(await fs.readJSON('cdk.json')).toEqual({});
expect(await fs.readJSON('cdk.context.json')).toEqual({});
expect(await fs.readJSON('cdk.json')).toEqual({ context: { 'boo:boo' : 'far' } });
});

test('surive no context in old file', async () => {
Expand Down

0 comments on commit 022eb66

Please sign in to comment.