Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/configuration/test/browser/configuration.test.ts
5262 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 { joinPath } from '../../../../../base/common/resources.js';
9
import { URI } from '../../../../../base/common/uri.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
11
import { Extensions, IConfigurationRegistry } from '../../../../../platform/configuration/common/configurationRegistry.js';
12
import { NullLogService } from '../../../../../platform/log/common/log.js';
13
import { Registry } from '../../../../../platform/registry/common/platform.js';
14
import { DefaultConfiguration } from '../../browser/configuration.js';
15
import { ConfigurationKey, IConfigurationCache } from '../../common/configuration.js';
16
import { BrowserWorkbenchEnvironmentService } from '../../../environment/browser/environmentService.js';
17
import { TestEnvironmentService } from '../../../../test/browser/workbenchTestServices.js';
18
import { TestProductService } from '../../../../test/common/workbenchTestServices.js';
19
20
class ConfigurationCache implements IConfigurationCache {
21
private readonly cache = new Map<string, string>();
22
needsCaching(resource: URI): boolean { return false; }
23
async read({ type, key }: ConfigurationKey): Promise<string> { return this.cache.get(`${type}:${key}`) || ''; }
24
async write({ type, key }: ConfigurationKey, content: string): Promise<void> { this.cache.set(`${type}:${key}`, content); }
25
async remove({ type, key }: ConfigurationKey): Promise<void> { this.cache.delete(`${type}:${key}`); }
26
}
27
28
suite('DefaultConfiguration', () => {
29
30
const disposables = ensureNoDisposablesAreLeakedInTestSuite();
31
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
32
const cacheKey: ConfigurationKey = { type: 'defaults', key: 'default-configurationDefaultsOverrides' };
33
let configurationCache: ConfigurationCache;
34
35
setup(() => {
36
configurationCache = new ConfigurationCache();
37
configurationRegistry.registerConfiguration({
38
'id': 'test.configurationDefaultsOverride',
39
'type': 'object',
40
'properties': {
41
'test.configurationDefaultsOverride': {
42
'type': 'string',
43
'default': 'defaultValue',
44
}
45
}
46
});
47
});
48
49
teardown(() => {
50
configurationRegistry.deregisterConfigurations(configurationRegistry.getConfigurations());
51
configurationRegistry.deregisterDefaultConfigurations(configurationRegistry.getRegisteredDefaultConfigurations());
52
});
53
54
test('configuration default overrides are read from environment', async () => {
55
const environmentService = new BrowserWorkbenchEnvironmentService('', joinPath(URI.file('tests').with({ scheme: 'vscode-tests' }), 'logs'), { configurationDefaults: { 'test.configurationDefaultsOverride': 'envOverrideValue' } }, TestProductService);
56
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, environmentService, new NullLogService()));
57
await testObject.initialize();
58
assert.deepStrictEqual(testObject.configurationModel.getValue('test.configurationDefaultsOverride'), 'envOverrideValue');
59
});
60
61
test('configuration default overrides are read from cache', async () => {
62
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
63
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
64
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
65
66
const actual = await testObject.initialize();
67
68
assert.deepStrictEqual(actual.getValue('test.configurationDefaultsOverride'), 'overrideValue');
69
assert.deepStrictEqual(testObject.configurationModel.getValue('test.configurationDefaultsOverride'), 'overrideValue');
70
});
71
72
test('configuration default overrides are not read from cache when model is read before initialize', async () => {
73
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
74
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
75
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
76
assert.deepStrictEqual(testObject.configurationModel.getValue('test.configurationDefaultsOverride'), undefined);
77
});
78
79
test('configuration default overrides read from cache override environment', async () => {
80
const environmentService = new BrowserWorkbenchEnvironmentService('', joinPath(URI.file('tests').with({ scheme: 'vscode-tests' }), 'logs'), { configurationDefaults: { 'test.configurationDefaultsOverride': 'envOverrideValue' } }, TestProductService);
81
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
82
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
83
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, environmentService, new NullLogService()));
84
85
const actual = await testObject.initialize();
86
87
assert.deepStrictEqual(actual.getValue('test.configurationDefaultsOverride'), 'overrideValue');
88
});
89
90
test('configuration default overrides are read from cache when default configuration changed', async () => {
91
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
92
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
93
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
94
await testObject.initialize();
95
96
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
97
configurationRegistry.registerConfiguration({
98
'id': 'test.configurationDefaultsOverride',
99
'type': 'object',
100
'properties': {
101
'test.configurationDefaultsOverride1': {
102
'type': 'string',
103
'default': 'defaultValue',
104
}
105
}
106
});
107
108
const { defaults: actual } = await promise;
109
assert.deepStrictEqual(actual.getValue('test.configurationDefaultsOverride'), 'overrideValue');
110
});
111
112
test('configuration default overrides are not read from cache after reload', async () => {
113
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
114
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
115
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
116
117
await testObject.initialize();
118
const actual = testObject.reload();
119
120
assert.deepStrictEqual(actual.getValue('test.configurationDefaultsOverride'), 'defaultValue');
121
});
122
123
test('cache is reset after reload', async () => {
124
localStorage.setItem(DefaultConfiguration.DEFAULT_OVERRIDES_CACHE_EXISTS_KEY, 'yes');
125
await configurationCache.write(cacheKey, JSON.stringify({ 'test.configurationDefaultsOverride': 'overrideValue' }));
126
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
127
128
await testObject.initialize();
129
testObject.reload();
130
131
assert.deepStrictEqual(await configurationCache.read(cacheKey), '');
132
});
133
134
test('configuration default overrides are written in cache', async () => {
135
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
136
await testObject.initialize();
137
testObject.reload();
138
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
139
configurationRegistry.registerDefaultConfigurations([{ overrides: { 'test.configurationDefaultsOverride': 'newoverrideValue' } }]);
140
await promise;
141
142
const actual = JSON.parse(await configurationCache.read(cacheKey));
143
assert.deepStrictEqual(actual, { 'test.configurationDefaultsOverride': 'newoverrideValue' });
144
});
145
146
test('configuration default overrides are removed from cache if there are no overrides', async () => {
147
const testObject = disposables.add(new DefaultConfiguration('default', configurationCache, TestEnvironmentService, new NullLogService()));
148
await testObject.initialize();
149
const promise = Event.toPromise(testObject.onDidChangeConfiguration);
150
configurationRegistry.registerConfiguration({
151
'id': 'test.configurationDefaultsOverride',
152
'type': 'object',
153
'properties': {
154
'test.configurationDefaultsOverride1': {
155
'type': 'string',
156
'default': 'defaultValue',
157
}
158
}
159
});
160
await promise;
161
162
assert.deepStrictEqual(await configurationCache.read(cacheKey), '');
163
});
164
165
});
166
167