forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): glob-style key matching to context --reset (aws#19840)
Implementation of one of the solutions I proposed in aws#19797 using glob-style expressions to match keys. Uses minimatch which already existed as a dependency to match stack names in the synth and deploy commands. Makes the --reset command throw on no-ops i.e when trying to reset context defined in cdk.json or ~/.cdk.json Adds tests and prints messages to clarify previously undocumented behavior. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
1 parent
fd173d4
commit 79915f4
Showing
2 changed files
with
273 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 203 additions & 46 deletions
249
packages/aws-cdk/test/commands/context-command.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,221 @@ | ||
import { realHandler } from '../../lib/commands/context'; | ||
import { Configuration } from '../../lib/settings'; | ||
import { Configuration, Settings, Context } from '../../lib/settings'; | ||
|
||
test('context list', async() => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
describe('context --list', () => { | ||
test('runs', async() => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: {}, | ||
} as any); | ||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: {}, | ||
} as any); | ||
}); | ||
}); | ||
|
||
test('context reset can remove a context key', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('baz', 'quux'); | ||
describe('context --reset', () => { | ||
test('can remove a context key', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('baz', 'quux'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
}); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'foo' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
baz: 'quux', | ||
}); | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'foo' }, | ||
} as any); | ||
test('can remove a context key using number', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('baz', 'quux'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
}); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
baz: 'quux', | ||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: '1' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
}); | ||
|
||
test('context reset can remove a context key using number', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('baz', 'quux'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
test('can reset matched pattern', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('match-a', 'baz'); | ||
configuration.context.set('match-b', 'qux'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
'match-b': 'qux', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'match-*' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: '1' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
test('prefers an exact match', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('fo*', 'baz'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
'foo': 'bar', | ||
'fo*': 'baz', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'fo*' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
|
||
|
||
test('doesn\'t throw when at least one match is reset', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
const readOnlySettings = new Settings({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
}, true); | ||
configuration.context = new Context(readOnlySettings, new Settings()); | ||
configuration.context.set('match-b', 'quux'); | ||
|
||
// When | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: 'match-*' }, | ||
} as any)); | ||
|
||
// Then | ||
expect(configuration.context.all).toEqual({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
}); | ||
}); | ||
|
||
test('throws when key not found', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// THEN | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: 'baz' }, | ||
} as any)).rejects.toThrow(/No context value matching key/); | ||
}); | ||
|
||
|
||
test('throws when no key of index found', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// THEN | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: '2' }, | ||
} as any)).rejects.toThrow(/No context key with number/); | ||
}); | ||
|
||
|
||
test('throws when resetting read-only values', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
const readOnlySettings = new Settings({ | ||
foo: 'bar', | ||
}, true); | ||
configuration.context = new Context(readOnlySettings); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// THEN | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: 'foo' }, | ||
} as any)).rejects.toThrow(/Cannot reset readonly context value with key/); | ||
}); | ||
|
||
|
||
test('throws when no matches could be reset', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
const readOnlySettings = new Settings({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
'match-b': 'quux', | ||
}, true); | ||
configuration.context = new Context(readOnlySettings); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
'match-b': 'quux', | ||
}); | ||
|
||
// THEN | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: 'match-*' }, | ||
} as any)).rejects.toThrow(/None of the matched context values could be reset/); | ||
}); | ||
|
||
}); | ||
|