Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/configuration/test/common/configurationModels.test.ts
3297 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
import assert from 'assert';
6
import { Registry } from '../../../../../platform/registry/common/platform.js';
7
import { StandaloneConfigurationModelParser, Configuration } from '../../common/configurationModels.js';
8
import { ConfigurationModelParser, ConfigurationModel, ConfigurationParseOptions } from '../../../../../platform/configuration/common/configurationModels.js';
9
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from '../../../../../platform/configuration/common/configurationRegistry.js';
10
import { ResourceMap } from '../../../../../base/common/map.js';
11
import { WorkspaceFolder } from '../../../../../platform/workspace/common/workspace.js';
12
import { URI } from '../../../../../base/common/uri.js';
13
import { Workspace } from '../../../../../platform/workspace/test/common/testWorkspace.js';
14
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
15
import { NullLogService } from '../../../../../platform/log/common/log.js';
16
17
suite('FolderSettingsModelParser', () => {
18
19
ensureNoDisposablesAreLeakedInTestSuite();
20
21
suiteSetup(() => {
22
const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
23
configurationRegistry.registerConfiguration({
24
'id': 'FolderSettingsModelParser_1',
25
'type': 'object',
26
'properties': {
27
'FolderSettingsModelParser.window': {
28
'type': 'string',
29
'default': 'isSet'
30
},
31
'FolderSettingsModelParser.resource': {
32
'type': 'string',
33
'default': 'isSet',
34
scope: ConfigurationScope.RESOURCE,
35
},
36
'FolderSettingsModelParser.resourceLanguage': {
37
'type': 'string',
38
'default': 'isSet',
39
scope: ConfigurationScope.LANGUAGE_OVERRIDABLE,
40
},
41
'FolderSettingsModelParser.application': {
42
'type': 'string',
43
'default': 'isSet',
44
scope: ConfigurationScope.APPLICATION
45
},
46
'FolderSettingsModelParser.machine': {
47
'type': 'string',
48
'default': 'isSet',
49
scope: ConfigurationScope.MACHINE
50
}
51
}
52
});
53
});
54
55
test('parse all folder settings', () => {
56
const testObject = new ConfigurationModelParser('settings', new NullLogService());
57
58
testObject.parse(JSON.stringify({ 'FolderSettingsModelParser.window': 'window', 'FolderSettingsModelParser.resource': 'resource', 'FolderSettingsModelParser.application': 'application', 'FolderSettingsModelParser.machine': 'executable' }), { scopes: [ConfigurationScope.RESOURCE, ConfigurationScope.WINDOW] });
59
60
const expected = Object.create(null);
61
expected['FolderSettingsModelParser'] = Object.create(null);
62
expected['FolderSettingsModelParser']['window'] = 'window';
63
expected['FolderSettingsModelParser']['resource'] = 'resource';
64
assert.deepStrictEqual(testObject.configurationModel.contents, expected);
65
});
66
67
test('parse resource folder settings', () => {
68
const testObject = new ConfigurationModelParser('settings', new NullLogService());
69
70
testObject.parse(JSON.stringify({ 'FolderSettingsModelParser.window': 'window', 'FolderSettingsModelParser.resource': 'resource', 'FolderSettingsModelParser.application': 'application', 'FolderSettingsModelParser.machine': 'executable' }), { scopes: [ConfigurationScope.RESOURCE] });
71
72
const expected = Object.create(null);
73
expected['FolderSettingsModelParser'] = Object.create(null);
74
expected['FolderSettingsModelParser']['resource'] = 'resource';
75
assert.deepStrictEqual(testObject.configurationModel.contents, expected);
76
});
77
78
test('parse resource and resource language settings', () => {
79
const testObject = new ConfigurationModelParser('settings', new NullLogService());
80
81
testObject.parse(JSON.stringify({ '[json]': { 'FolderSettingsModelParser.window': 'window', 'FolderSettingsModelParser.resource': 'resource', 'FolderSettingsModelParser.resourceLanguage': 'resourceLanguage', 'FolderSettingsModelParser.application': 'application', 'FolderSettingsModelParser.machine': 'executable' } }), { scopes: [ConfigurationScope.RESOURCE, ConfigurationScope.LANGUAGE_OVERRIDABLE] });
82
83
const expected = Object.create(null);
84
expected['FolderSettingsModelParser'] = Object.create(null);
85
expected['FolderSettingsModelParser']['resource'] = 'resource';
86
expected['FolderSettingsModelParser']['resourceLanguage'] = 'resourceLanguage';
87
assert.deepStrictEqual(testObject.configurationModel.overrides, [{ 'contents': expected, 'identifiers': ['json'], 'keys': ['FolderSettingsModelParser.resource', 'FolderSettingsModelParser.resourceLanguage'] }]);
88
});
89
90
test('reparse folder settings excludes application and machine setting', () => {
91
const parseOptions: ConfigurationParseOptions = { scopes: [ConfigurationScope.RESOURCE, ConfigurationScope.WINDOW] };
92
const testObject = new ConfigurationModelParser('settings', new NullLogService());
93
94
testObject.parse(JSON.stringify({ 'FolderSettingsModelParser.resource': 'resource', 'FolderSettingsModelParser.anotherApplicationSetting': 'executable' }), parseOptions);
95
96
let expected = Object.create(null);
97
expected['FolderSettingsModelParser'] = Object.create(null);
98
expected['FolderSettingsModelParser']['resource'] = 'resource';
99
expected['FolderSettingsModelParser']['anotherApplicationSetting'] = 'executable';
100
assert.deepStrictEqual(testObject.configurationModel.contents, expected);
101
102
const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
103
configurationRegistry.registerConfiguration({
104
'id': 'FolderSettingsModelParser_2',
105
'type': 'object',
106
'properties': {
107
'FolderSettingsModelParser.anotherApplicationSetting': {
108
'type': 'string',
109
'default': 'isSet',
110
scope: ConfigurationScope.APPLICATION
111
},
112
'FolderSettingsModelParser.anotherMachineSetting': {
113
'type': 'string',
114
'default': 'isSet',
115
scope: ConfigurationScope.MACHINE
116
}
117
}
118
});
119
120
testObject.reparse(parseOptions);
121
122
expected = Object.create(null);
123
expected['FolderSettingsModelParser'] = Object.create(null);
124
expected['FolderSettingsModelParser']['resource'] = 'resource';
125
assert.deepStrictEqual(testObject.configurationModel.contents, expected);
126
});
127
128
});
129
130
suite('StandaloneConfigurationModelParser', () => {
131
132
ensureNoDisposablesAreLeakedInTestSuite();
133
134
test('parse tasks stand alone configuration model', () => {
135
const testObject = new StandaloneConfigurationModelParser('tasks', 'tasks', new NullLogService());
136
137
testObject.parse(JSON.stringify({ 'version': '1.1.1', 'tasks': [] }));
138
139
const expected = Object.create(null);
140
expected['tasks'] = Object.create(null);
141
expected['tasks']['version'] = '1.1.1';
142
expected['tasks']['tasks'] = [];
143
assert.deepStrictEqual(testObject.configurationModel.contents, expected);
144
});
145
146
});
147
148
suite('Workspace Configuration', () => {
149
150
ensureNoDisposablesAreLeakedInTestSuite();
151
152
const defaultConfigurationModel = toConfigurationModel({
153
'editor.lineNumbers': 'on',
154
'editor.fontSize': 12,
155
'window.zoomLevel': 1,
156
'[markdown]': {
157
'editor.wordWrap': 'off'
158
},
159
'window.title': 'custom',
160
'workbench.enableTabs': false,
161
'editor.insertSpaces': true
162
});
163
164
test('Test compare same configurations', () => {
165
const workspace = new Workspace('a', [new WorkspaceFolder({ index: 0, name: 'a', uri: URI.file('folder1') }), new WorkspaceFolder({ index: 1, name: 'b', uri: URI.file('folder2') }), new WorkspaceFolder({ index: 2, name: 'c', uri: URI.file('folder3') })]);
166
const configuration1 = new Configuration(ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), workspace, new NullLogService());
167
configuration1.updateDefaultConfiguration(defaultConfigurationModel);
168
configuration1.updateLocalUserConfiguration(toConfigurationModel({ 'window.title': 'native', '[typescript]': { 'editor.insertSpaces': false } }));
169
configuration1.updateWorkspaceConfiguration(toConfigurationModel({ 'editor.lineNumbers': 'on' }));
170
configuration1.updateFolderConfiguration(URI.file('folder1'), toConfigurationModel({ 'editor.fontSize': 14 }));
171
configuration1.updateFolderConfiguration(URI.file('folder2'), toConfigurationModel({ 'editor.wordWrap': 'on' }));
172
173
const configuration2 = new Configuration(ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), workspace, new NullLogService());
174
configuration2.updateDefaultConfiguration(defaultConfigurationModel);
175
configuration2.updateLocalUserConfiguration(toConfigurationModel({ 'window.title': 'native', '[typescript]': { 'editor.insertSpaces': false } }));
176
configuration2.updateWorkspaceConfiguration(toConfigurationModel({ 'editor.lineNumbers': 'on' }));
177
configuration2.updateFolderConfiguration(URI.file('folder1'), toConfigurationModel({ 'editor.fontSize': 14 }));
178
configuration2.updateFolderConfiguration(URI.file('folder2'), toConfigurationModel({ 'editor.wordWrap': 'on' }));
179
180
const actual = configuration2.compare(configuration1);
181
182
assert.deepStrictEqual(actual, { keys: [], overrides: [] });
183
});
184
185
test('Test compare different configurations', () => {
186
const workspace = new Workspace('a', [new WorkspaceFolder({ index: 0, name: 'a', uri: URI.file('folder1') }), new WorkspaceFolder({ index: 1, name: 'b', uri: URI.file('folder2') }), new WorkspaceFolder({ index: 2, name: 'c', uri: URI.file('folder3') })]);
187
const configuration1 = new Configuration(ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), workspace, new NullLogService());
188
configuration1.updateDefaultConfiguration(defaultConfigurationModel);
189
configuration1.updateLocalUserConfiguration(toConfigurationModel({ 'window.title': 'native', '[typescript]': { 'editor.insertSpaces': false } }));
190
configuration1.updateWorkspaceConfiguration(toConfigurationModel({ 'editor.lineNumbers': 'on' }));
191
configuration1.updateFolderConfiguration(URI.file('folder1'), toConfigurationModel({ 'editor.fontSize': 14 }));
192
configuration1.updateFolderConfiguration(URI.file('folder2'), toConfigurationModel({ 'editor.wordWrap': 'on' }));
193
194
const configuration2 = new Configuration(ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), ConfigurationModel.createEmptyModel(new NullLogService()), new ResourceMap<ConfigurationModel>(), workspace, new NullLogService());
195
configuration2.updateDefaultConfiguration(defaultConfigurationModel);
196
configuration2.updateLocalUserConfiguration(toConfigurationModel({ 'workbench.enableTabs': true, '[typescript]': { 'editor.insertSpaces': true } }));
197
configuration2.updateWorkspaceConfiguration(toConfigurationModel({ 'editor.fontSize': 11 }));
198
configuration2.updateFolderConfiguration(URI.file('folder1'), toConfigurationModel({ 'editor.insertSpaces': true }));
199
configuration2.updateFolderConfiguration(URI.file('folder2'), toConfigurationModel({
200
'[markdown]': {
201
'editor.wordWrap': 'on',
202
'editor.lineNumbers': 'relative'
203
},
204
}));
205
206
const actual = configuration2.compare(configuration1);
207
208
assert.deepStrictEqual(actual, { keys: ['editor.wordWrap', 'editor.fontSize', '[markdown]', 'window.title', 'workbench.enableTabs', '[typescript]'], overrides: [['markdown', ['editor.lineNumbers', 'editor.wordWrap']], ['typescript', ['editor.insertSpaces']]] });
209
});
210
211
212
});
213
214
function toConfigurationModel(obj: any): ConfigurationModel {
215
const parser = new ConfigurationModelParser('test', new NullLogService());
216
parser.parse(JSON.stringify(obj));
217
return parser.configurationModel;
218
}
219
220