Skip to content

Commit 89ec597

Browse files
revert: "feat(cli): glob-style key matching to context --reset (#19840)" (#19888)
Reverts [glob-style context key matching](#19840) as it caused one of existing [integration tests](#19840) to fail. refs: edb4119. ---- ### All Submissions: * [ ] 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*
1 parent aa834be commit 89ec597

File tree

2 files changed

+56
-273
lines changed

2 files changed

+56
-273
lines changed

packages/aws-cdk/lib/commands/context.ts

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as chalk from 'chalk';
2-
import * as minimatch from 'minimatch';
32
import * as version from '../../lib/version';
43
import { CommandOptions } from '../command-api';
5-
import { print, error, warning } from '../logging';
6-
import { Context, PROJECT_CONFIG, PROJECT_CONTEXT, USER_DEFAULTS } from '../settings';
4+
import { print } from '../logging';
5+
import { Context, PROJECT_CONFIG } from '../settings';
76
import { renderTable } from '../util';
87

98
export async function realHandler(options: CommandOptions): Promise<number> {
109
const { configuration, args } = options;
10+
1111
if (args.clear) {
1212
configuration.context.clear();
1313
await configuration.saveContext();
@@ -48,8 +48,9 @@ function listContext(context: Context) {
4848
const jsonWithoutNewlines = JSON.stringify(context.all[key], undefined, 2).replace(/\s+/g, ' ');
4949
data.push([i, key, jsonWithoutNewlines]);
5050
}
51-
print('Context found in %s:', chalk.blue(PROJECT_CONFIG));
52-
print('');
51+
52+
print(`Context found in ${chalk.blue(PROJECT_CONFIG)}:\n`);
53+
5354
print(renderTable(data, process.stdout.columns));
5455

5556
// eslint-disable-next-line max-len
@@ -62,75 +63,14 @@ function invalidateContext(context: Context, key: string) {
6263
// was a number and we fully parsed it.
6364
key = keyByNumber(context, i);
6465
}
66+
6567
// Unset!
6668
if (context.has(key)) {
6769
context.unset(key);
68-
// check if the value was actually unset.
69-
if (!context.has(key)) {
70-
print('Context value %s reset. It will be refreshed on next synthesis', chalk.blue(key));
71-
return;
72-
}
73-
74-
// Value must be in readonly bag
75-
error('Only context values specified in %s can be reset through the CLI', chalk.blue(PROJECT_CONTEXT));
76-
throw new Error(`Cannot reset readonly context value with key: ${key}`);
77-
}
78-
79-
// check if value is expression matching keys
80-
const matches = keysByExpression(context, key);
81-
82-
if (matches.length > 0) {
83-
84-
matches.forEach((match) => {
85-
context.unset(match);
86-
});
87-
88-
const { unset, readonly } = getUnsetAndReadonly(context, matches);
89-
90-
// output the reset values
91-
printUnset(unset);
92-
93-
// warn about values not reset
94-
printReadonly(readonly);
95-
96-
// throw when none of the matches were reset
97-
if (unset.length === 0) {
98-
throw new Error('None of the matched context values could be reset');
99-
}
100-
return;
70+
print(`Context value ${chalk.blue(key)} reset. It will be refreshed on next synthesis`);
71+
} else {
72+
print(`No context value with key ${chalk.blue(key)}`);
10173
}
102-
103-
throw new Error(`No context value matching key: ${key}`);
104-
}
105-
function printUnset(unset: string[]) {
106-
if (unset.length === 0) return;
107-
print('The following matched context values reset. They will be refreshed on next synthesis');
108-
unset.forEach((match) => {
109-
print(' %s', match);
110-
});
111-
}
112-
function printReadonly(readonly: string[]) {
113-
if (readonly.length === 0) return;
114-
warning('The following matched context values could not be reset through the CLI');
115-
readonly.forEach((match) => {
116-
print(' %s', match);
117-
});
118-
print('');
119-
print('This usually means they are configured in %s or %s', chalk.blue(PROJECT_CONFIG), chalk.blue(USER_DEFAULTS));
120-
}
121-
function keysByExpression(context: Context, expression: string) {
122-
return context.keys.filter(minimatch.filter(expression));
123-
}
124-
125-
function getUnsetAndReadonly(context: Context, matches: string[]) {
126-
return matches.reduce<{ unset: string[], readonly: string[] }>((acc, match) => {
127-
if (context.has(match)) {
128-
acc.readonly.push(match);
129-
} else {
130-
acc.unset.push(match);
131-
}
132-
return acc;
133-
}, { unset: [], readonly: [] });
13474
}
13575

13676
function keyByNumber(context: Context, n: number) {
Lines changed: 46 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1,221 +1,64 @@
11
import { realHandler } from '../../lib/commands/context';
2-
import { Configuration, Settings, Context } from '../../lib/settings';
2+
import { Configuration } from '../../lib/settings';
33

4-
describe('context --list', () => {
5-
test('runs', async() => {
6-
// GIVEN
7-
const configuration = new Configuration();
8-
configuration.context.set('foo', 'bar');
4+
test('context list', async() => {
5+
// GIVEN
6+
const configuration = new Configuration();
7+
configuration.context.set('foo', 'bar');
98

10-
expect(configuration.context.all).toEqual({
11-
foo: 'bar',
12-
});
13-
14-
// WHEN
15-
await realHandler({
16-
configuration,
17-
args: {},
18-
} as any);
19-
});
20-
});
21-
22-
describe('context --reset', () => {
23-
test('can remove a context key', async () => {
24-
// GIVEN
25-
const configuration = new Configuration();
26-
configuration.context.set('foo', 'bar');
27-
configuration.context.set('baz', 'quux');
28-
29-
expect(configuration.context.all).toEqual({
30-
foo: 'bar',
31-
baz: 'quux',
32-
});
33-
34-
// WHEN
35-
await realHandler({
36-
configuration,
37-
args: { reset: 'foo' },
38-
} as any);
39-
40-
// THEN
41-
expect(configuration.context.all).toEqual({
42-
baz: 'quux',
43-
});
44-
});
45-
46-
test('can remove a context key using number', async () => {
47-
// GIVEN
48-
const configuration = new Configuration();
49-
configuration.context.set('foo', 'bar');
50-
configuration.context.set('baz', 'quux');
51-
52-
expect(configuration.context.all).toEqual({
53-
foo: 'bar',
54-
baz: 'quux',
55-
});
56-
57-
// WHEN
58-
await realHandler({
59-
configuration,
60-
args: { reset: '1' },
61-
} as any);
62-
63-
// THEN
64-
expect(configuration.context.all).toEqual({
65-
foo: 'bar',
66-
});
67-
});
68-
69-
70-
test('can reset matched pattern', async () => {
71-
// GIVEN
72-
const configuration = new Configuration();
73-
configuration.context.set('foo', 'bar');
74-
configuration.context.set('match-a', 'baz');
75-
configuration.context.set('match-b', 'qux');
76-
77-
expect(configuration.context.all).toEqual({
78-
'foo': 'bar',
79-
'match-a': 'baz',
80-
'match-b': 'qux',
81-
});
82-
83-
// WHEN
84-
await realHandler({
85-
configuration,
86-
args: { reset: 'match-*' },
87-
} as any);
88-
89-
// THEN
90-
expect(configuration.context.all).toEqual({
91-
foo: 'bar',
92-
});
9+
expect(configuration.context.all).toEqual({
10+
foo: 'bar',
9311
});
9412

13+
// WHEN
14+
await realHandler({
15+
configuration,
16+
args: {},
17+
} as any);
18+
});
9519

96-
test('prefers an exact match', async () => {
97-
// GIVEN
98-
const configuration = new Configuration();
99-
configuration.context.set('foo', 'bar');
100-
configuration.context.set('fo*', 'baz');
101-
102-
expect(configuration.context.all).toEqual({
103-
'foo': 'bar',
104-
'fo*': 'baz',
105-
});
106-
107-
// WHEN
108-
await realHandler({
109-
configuration,
110-
args: { reset: 'fo*' },
111-
} as any);
112-
113-
// THEN
114-
expect(configuration.context.all).toEqual({
115-
foo: 'bar',
116-
});
117-
});
118-
119-
120-
test('doesn\'t throw when at least one match is reset', async () => {
121-
// GIVEN
122-
const configuration = new Configuration();
123-
const readOnlySettings = new Settings({
124-
'foo': 'bar',
125-
'match-a': 'baz',
126-
}, true);
127-
configuration.context = new Context(readOnlySettings, new Settings());
128-
configuration.context.set('match-b', 'quux');
129-
130-
// When
131-
await expect(realHandler({
132-
configuration,
133-
args: { reset: 'match-*' },
134-
} as any));
20+
test('context reset can remove a context key', async () => {
21+
// GIVEN
22+
const configuration = new Configuration();
23+
configuration.context.set('foo', 'bar');
24+
configuration.context.set('baz', 'quux');
13525

136-
// Then
137-
expect(configuration.context.all).toEqual({
138-
'foo': 'bar',
139-
'match-a': 'baz',
140-
});
26+
expect(configuration.context.all).toEqual({
27+
foo: 'bar',
28+
baz: 'quux',
14129
});
14230

143-
test('throws when key not found', async () => {
144-
// GIVEN
145-
const configuration = new Configuration();
146-
configuration.context.set('foo', 'bar');
147-
148-
expect(configuration.context.all).toEqual({
149-
foo: 'bar',
150-
});
31+
// WHEN
32+
await realHandler({
33+
configuration,
34+
args: { reset: 'foo' },
35+
} as any);
15136

152-
// THEN
153-
await expect(realHandler({
154-
configuration,
155-
args: { reset: 'baz' },
156-
} as any)).rejects.toThrow(/No context value matching key/);
37+
// THEN
38+
expect(configuration.context.all).toEqual({
39+
baz: 'quux',
15740
});
41+
});
15842

43+
test('context reset can remove a context key using number', async () => {
44+
// GIVEN
45+
const configuration = new Configuration();
46+
configuration.context.set('foo', 'bar');
47+
configuration.context.set('baz', 'quux');
15948

160-
test('throws when no key of index found', async () => {
161-
// GIVEN
162-
const configuration = new Configuration();
163-
configuration.context.set('foo', 'bar');
164-
165-
expect(configuration.context.all).toEqual({
166-
foo: 'bar',
167-
});
168-
169-
// THEN
170-
await expect(realHandler({
171-
configuration,
172-
args: { reset: '2' },
173-
} as any)).rejects.toThrow(/No context key with number/);
174-
});
175-
176-
177-
test('throws when resetting read-only values', async () => {
178-
// GIVEN
179-
const configuration = new Configuration();
180-
const readOnlySettings = new Settings({
181-
foo: 'bar',
182-
}, true);
183-
configuration.context = new Context(readOnlySettings);
184-
185-
expect(configuration.context.all).toEqual({
186-
foo: 'bar',
187-
});
188-
189-
// THEN
190-
await expect(realHandler({
191-
configuration,
192-
args: { reset: 'foo' },
193-
} as any)).rejects.toThrow(/Cannot reset readonly context value with key/);
49+
expect(configuration.context.all).toEqual({
50+
foo: 'bar',
51+
baz: 'quux',
19452
});
19553

54+
// WHEN
55+
await realHandler({
56+
configuration,
57+
args: { reset: '1' },
58+
} as any);
19659

197-
test('throws when no matches could be reset', async () => {
198-
// GIVEN
199-
const configuration = new Configuration();
200-
const readOnlySettings = new Settings({
201-
'foo': 'bar',
202-
'match-a': 'baz',
203-
'match-b': 'quux',
204-
}, true);
205-
configuration.context = new Context(readOnlySettings);
206-
207-
expect(configuration.context.all).toEqual({
208-
'foo': 'bar',
209-
'match-a': 'baz',
210-
'match-b': 'quux',
211-
});
212-
213-
// THEN
214-
await expect(realHandler({
215-
configuration,
216-
args: { reset: 'match-*' },
217-
} as any)).rejects.toThrow(/None of the matched context values could be reset/);
60+
// THEN
61+
expect(configuration.context.all).toEqual({
62+
foo: 'bar',
21863
});
219-
22064
});
221-

0 commit comments

Comments
 (0)