Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/configuration/test/common/configurations.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { Event } from '../../../../base/common/event.js';
8
import { equals } from '../../../../base/common/objects.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
10
import { Extensions, IConfigurationNode, IConfigurationRegistry } from '../../common/configurationRegistry.js';
11
import { DefaultConfiguration } from '../../common/configurations.js';
12
import { NullLogService } from '../../../log/common/log.js';
13
import { Registry } from '../../../registry/common/platform.js';
14
15
suite('DefaultConfiguration', () => {
16
17
const disposables = ensureNoDisposablesAreLeakedInTestSuite();
18
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
19
20
setup(() => reset());
21
teardown(() => reset());
22
23
function reset() {
24
configurationRegistry.deregisterConfigurations(configurationRegistry.getConfigurations());
25
configurationRegistry.deregisterDefaultConfigurations(configurationRegistry.getRegisteredDefaultConfigurations());
26
}
27
28
test('Test registering a property before initialize', async () => {
29
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
30
configurationRegistry.registerConfiguration({
31
'id': 'a',
32
'order': 1,
33
'title': 'a',
34
'type': 'object',
35
'properties': {
36
'a': {
37
'description': 'a',
38
'type': 'boolean',
39
'default': false,
40
}
41
}
42
});
43
const actual = await testObject.initialize();
44
assert.strictEqual(actual.getValue('a'), false);
45
});
46
47
test('Test registering a property and do not initialize', async () => {
48
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
49
configurationRegistry.registerConfiguration({
50
'id': 'a',
51
'order': 1,
52
'title': 'a',
53
'type': 'object',
54
'properties': {
55
'a': {
56
'description': 'a',
57
'type': 'boolean',
58
'default': false,
59
}
60
}
61
});
62
assert.strictEqual(testObject.configurationModel.getValue('a'), undefined);
63
});
64
65
test('Test registering a property after initialize', async () => {
66
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
67
await testObject.initialize();
68
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
69
configurationRegistry.registerConfiguration({
70
'id': 'a',
71
'order': 1,
72
'title': 'a',
73
'type': 'object',
74
'properties': {
75
'defaultConfiguration.testSetting1': {
76
'description': 'a',
77
'type': 'boolean',
78
'default': false,
79
}
80
}
81
});
82
const { defaults: actual, properties } = await promise;
83
assert.strictEqual(actual.getValue('defaultConfiguration.testSetting1'), false);
84
assert.deepStrictEqual(properties, ['defaultConfiguration.testSetting1']);
85
});
86
87
test('Test registering nested properties', async () => {
88
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
89
configurationRegistry.registerConfiguration({
90
'id': 'a',
91
'order': 1,
92
'title': 'a',
93
'type': 'object',
94
'properties': {
95
'a.b': {
96
'description': '1',
97
'type': 'object',
98
'default': {},
99
},
100
'a.b.c': {
101
'description': '2',
102
'type': 'object',
103
'default': '2',
104
}
105
}
106
});
107
108
const actual = await testObject.initialize();
109
110
assert.ok(equals(actual.getValue('a'), { b: { c: '2' } }));
111
assert.ok(equals(actual.contents, { 'a': { b: { c: '2' } } }));
112
assert.deepStrictEqual(actual.keys.sort(), ['a.b', 'a.b.c']);
113
});
114
115
test('Test registering the same property again', async () => {
116
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
117
configurationRegistry.registerConfiguration({
118
'id': 'a',
119
'order': 1,
120
'title': 'a',
121
'type': 'object',
122
'properties': {
123
'a': {
124
'description': 'a',
125
'type': 'boolean',
126
'default': true,
127
}
128
}
129
});
130
configurationRegistry.registerConfiguration({
131
'id': 'a',
132
'order': 1,
133
'title': 'a',
134
'type': 'object',
135
'properties': {
136
'a': {
137
'description': 'a',
138
'type': 'boolean',
139
'default': false,
140
}
141
}
142
});
143
const actual = await testObject.initialize();
144
assert.strictEqual(true, actual.getValue('a'));
145
});
146
147
test('Test registering an override identifier', async () => {
148
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
149
configurationRegistry.registerDefaultConfigurations([{
150
overrides: {
151
'[a]': {
152
'b': true
153
}
154
}
155
}]);
156
const actual = await testObject.initialize();
157
assert.ok(equals(actual.getValue('[a]'), { 'b': true }));
158
assert.ok(equals(actual.contents, { '[a]': { 'b': true } }));
159
assert.ok(equals(actual.overrides, [{ contents: { 'b': true }, identifiers: ['a'], keys: ['b'] }]));
160
assert.deepStrictEqual(actual.keys.sort(), ['[a]']);
161
assert.strictEqual(actual.getOverrideValue('b', 'a'), true);
162
});
163
164
test('Test registering a normal property and override identifier', async () => {
165
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
166
configurationRegistry.registerConfiguration({
167
'id': 'a',
168
'order': 1,
169
'title': 'a',
170
'type': 'object',
171
'properties': {
172
'b': {
173
'description': 'b',
174
'type': 'boolean',
175
'default': false,
176
}
177
}
178
});
179
180
configurationRegistry.registerDefaultConfigurations([{
181
overrides: {
182
'[a]': {
183
'b': true
184
}
185
}
186
}]);
187
188
const actual = await testObject.initialize();
189
assert.deepStrictEqual(actual.getValue('b'), false);
190
assert.ok(equals(actual.getValue('[a]'), { 'b': true }));
191
assert.ok(equals(actual.contents, { 'b': false, '[a]': { 'b': true } }));
192
assert.ok(equals(actual.overrides, [{ contents: { 'b': true }, identifiers: ['a'], keys: ['b'] }]));
193
assert.deepStrictEqual(actual.keys.sort(), ['[a]', 'b']);
194
assert.strictEqual(actual.getOverrideValue('b', 'a'), true);
195
});
196
197
test('Test normal property is registered after override identifier', async () => {
198
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
199
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
200
configurationRegistry.registerDefaultConfigurations([{
201
overrides: {
202
'[a]': {
203
'b': true
204
}
205
}
206
}]);
207
208
await testObject.initialize();
209
210
configurationRegistry.registerConfiguration({
211
'id': 'a',
212
'order': 1,
213
'title': 'a',
214
'type': 'object',
215
'properties': {
216
'b': {
217
'description': 'b',
218
'type': 'boolean',
219
'default': false,
220
}
221
}
222
});
223
224
const { defaults: actual, properties } = await promise;
225
assert.deepStrictEqual(actual.getValue('b'), false);
226
assert.ok(equals(actual.getValue('[a]'), { 'b': true }));
227
assert.ok(equals(actual.contents, { 'b': false, '[a]': { 'b': true } }));
228
assert.ok(equals(actual.overrides, [{ contents: { 'b': true }, identifiers: ['a'], keys: ['b'] }]));
229
assert.deepStrictEqual(actual.keys.sort(), ['[a]', 'b']);
230
assert.strictEqual(actual.getOverrideValue('b', 'a'), true);
231
assert.deepStrictEqual(properties, ['b']);
232
});
233
234
test('Test override identifier is registered after property', async () => {
235
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
236
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
237
configurationRegistry.registerConfiguration({
238
'id': 'a',
239
'order': 1,
240
'title': 'a',
241
'type': 'object',
242
'properties': {
243
'b': {
244
'description': 'b',
245
'type': 'boolean',
246
'default': false,
247
}
248
}
249
});
250
await testObject.initialize();
251
252
configurationRegistry.registerDefaultConfigurations([{
253
overrides: {
254
'[a]': {
255
'b': true
256
}
257
}
258
}]);
259
260
const { defaults: actual, properties } = await promise;
261
assert.deepStrictEqual(actual.getValue('b'), false);
262
assert.ok(equals(actual.getValue('[a]'), { 'b': true }));
263
assert.ok(equals(actual.contents, { 'b': false, '[a]': { 'b': true } }));
264
assert.ok(equals(actual.overrides, [{ contents: { 'b': true }, identifiers: ['a'], keys: ['b'] }]));
265
assert.deepStrictEqual(actual.keys.sort(), ['[a]', 'b']);
266
assert.strictEqual(actual.getOverrideValue('b', 'a'), true);
267
assert.deepStrictEqual(properties, ['[a]']);
268
});
269
270
test('Test register override identifier and property after initialize', async () => {
271
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
272
273
await testObject.initialize();
274
275
configurationRegistry.registerConfiguration({
276
'id': 'a',
277
'order': 1,
278
'title': 'a',
279
'type': 'object',
280
'properties': {
281
'b': {
282
'description': 'b',
283
'type': 'boolean',
284
'default': false,
285
}
286
}
287
});
288
configurationRegistry.registerDefaultConfigurations([{
289
overrides: {
290
'[a]': {
291
'b': true
292
}
293
}
294
}]);
295
296
const actual = testObject.configurationModel;
297
assert.deepStrictEqual(actual.getValue('b'), false);
298
assert.ok(equals(actual.getValue('[a]'), { 'b': true }));
299
assert.ok(equals(actual.contents, { 'b': false, '[a]': { 'b': true } }));
300
assert.ok(equals(actual.overrides, [{ contents: { 'b': true }, identifiers: ['a'], keys: ['b'] }]));
301
assert.deepStrictEqual(actual.keys.sort(), ['[a]', 'b']);
302
assert.strictEqual(actual.getOverrideValue('b', 'a'), true);
303
});
304
305
test('Test deregistering a property', async () => {
306
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
307
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
308
const node: IConfigurationNode = {
309
'id': 'a',
310
'order': 1,
311
'title': 'a',
312
'type': 'object',
313
'properties': {
314
'a': {
315
'description': 'a',
316
'type': 'boolean',
317
'default': false,
318
}
319
}
320
};
321
configurationRegistry.registerConfiguration(node);
322
await testObject.initialize();
323
configurationRegistry.deregisterConfigurations([node]);
324
325
const { defaults: actual, properties } = await promise;
326
assert.strictEqual(actual.getValue('a'), undefined);
327
assert.ok(equals(actual.contents, {}));
328
assert.deepStrictEqual(actual.keys, []);
329
assert.deepStrictEqual(properties, ['a']);
330
});
331
332
test('Test deregistering an override identifier', async () => {
333
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
334
configurationRegistry.registerConfiguration({
335
'id': 'a',
336
'order': 1,
337
'title': 'a',
338
'type': 'object',
339
'properties': {
340
'b': {
341
'description': 'b',
342
'type': 'boolean',
343
'default': false,
344
}
345
}
346
});
347
const node = {
348
overrides: {
349
'[a]': {
350
'b': true
351
}
352
}
353
};
354
configurationRegistry.registerDefaultConfigurations([node]);
355
await testObject.initialize();
356
configurationRegistry.deregisterDefaultConfigurations([node]);
357
assert.deepStrictEqual(testObject.configurationModel.getValue('[a]'), undefined);
358
assert.ok(equals(testObject.configurationModel.contents, { 'b': false }));
359
assert.ok(equals(testObject.configurationModel.overrides, []));
360
assert.deepStrictEqual(testObject.configurationModel.keys, ['b']);
361
assert.strictEqual(testObject.configurationModel.getOverrideValue('b', 'a'), undefined);
362
});
363
364
test('Test deregistering a merged language object setting', async () => {
365
const testObject = disposables.add(new DefaultConfiguration(new NullLogService()));
366
configurationRegistry.registerConfiguration({
367
'id': 'b',
368
'order': 1,
369
'title': 'b',
370
'type': 'object',
371
'properties': {
372
'b': {
373
'description': 'b',
374
'type': 'object',
375
'default': {},
376
}
377
}
378
});
379
const node1 = {
380
overrides: {
381
'[a]': {
382
'b': {
383
'aa': '1',
384
'bb': '2'
385
}
386
}
387
},
388
source: { id: 'source1', displayName: 'source1' }
389
};
390
391
const node2 = {
392
overrides: {
393
'[a]': {
394
'b': {
395
'bb': '20',
396
'cc': '30'
397
}
398
}
399
},
400
source: { id: 'source2', displayName: 'source2' }
401
};
402
configurationRegistry.registerDefaultConfigurations([node1]);
403
configurationRegistry.registerDefaultConfigurations([node2]);
404
await testObject.initialize();
405
406
configurationRegistry.deregisterDefaultConfigurations([node1]);
407
assert.ok(equals(testObject.configurationModel.getValue('[a]'), { 'b': { 'bb': '20', 'cc': '30' } }));
408
assert.ok(equals(testObject.configurationModel.contents, { '[a]': { 'b': { 'bb': '20', 'cc': '30' } }, 'b': {} }));
409
assert.ok(equals(testObject.configurationModel.overrides, [{ contents: { 'b': { 'bb': '20', 'cc': '30' } }, identifiers: ['a'], keys: ['b'] }]));
410
assert.deepStrictEqual(testObject.configurationModel.keys.sort(), ['[a]', 'b']);
411
assert.ok(equals(testObject.configurationModel.getOverrideValue('b', 'a'), { 'bb': '20', 'cc': '30' }));
412
});
413
});
414
415