-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "feat(cli): glob-style key matching to context --reset (#19840)"
This reverts commit edb4119.
- Loading branch information
1 parent
697fdbe
commit 70b8454
Showing
2 changed files
with
56 additions
and
273 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: 46 additions & 203 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,221 +1,64 @@ | ||
import { realHandler } from '../../lib/commands/context'; | ||
import { Configuration, Settings, Context } from '../../lib/settings'; | ||
import { Configuration } from '../../lib/settings'; | ||
|
||
describe('context --list', () => { | ||
test('runs', async() => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
test('context list', async() => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: {}, | ||
} as any); | ||
}); | ||
}); | ||
|
||
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', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'foo' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
baz: 'quux', | ||
}); | ||
}); | ||
|
||
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', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: '1' }, | ||
} as any); | ||
|
||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
}); | ||
|
||
|
||
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', | ||
}); | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: {}, | ||
} as any); | ||
}); | ||
|
||
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)); | ||
test('context reset can remove a context key', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
configuration.context.set('baz', 'quux'); | ||
|
||
// Then | ||
expect(configuration.context.all).toEqual({ | ||
'foo': 'bar', | ||
'match-a': 'baz', | ||
}); | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
}); | ||
|
||
test('throws when key not found', async () => { | ||
// GIVEN | ||
const configuration = new Configuration(); | ||
configuration.context.set('foo', 'bar'); | ||
|
||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: 'foo' }, | ||
} as any); | ||
|
||
// THEN | ||
await expect(realHandler({ | ||
configuration, | ||
args: { reset: 'baz' }, | ||
} as any)).rejects.toThrow(/No context value matching key/); | ||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
baz: 'quux', | ||
}); | ||
}); | ||
|
||
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'); | ||
|
||
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/); | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
baz: 'quux', | ||
}); | ||
|
||
// WHEN | ||
await realHandler({ | ||
configuration, | ||
args: { reset: '1' }, | ||
} as any); | ||
|
||
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/); | ||
// THEN | ||
expect(configuration.context.all).toEqual({ | ||
foo: 'bar', | ||
}); | ||
|
||
}); | ||
|