extHostConfiguration.test.ts 20.5 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as assert from 'assert';
9
import URI from 'vs/base/common/uri';
10
import { ExtHostWorkspace } from 'vs/workbench/api/node/extHostWorkspace';
J
Johannes Rieken 已提交
11
import { ExtHostConfiguration } from 'vs/workbench/api/node/extHostConfiguration';
S
Sandeep Somavarapu 已提交
12
import { MainThreadConfigurationShape, IConfigurationInitData } from 'vs/workbench/api/node/extHost.protocol';
J
Johannes Rieken 已提交
13
import { TPromise } from 'vs/base/common/winjs.base';
14
import { ConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
A
Alex Dima 已提交
15
import { TestRPCProtocol } from './testRPCProtocol';
16
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
17
import { IWorkspaceFolder, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
18
import { ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
19
import { NullLogService } from 'vs/platform/log/common/log';
S
Sandeep Somavarapu 已提交
20
import { assign } from 'vs/base/common/objects';
J
Johannes Rieken 已提交
21 22 23

suite('ExtHostConfiguration', function () {

24
	class RecordingShape extends mock<MainThreadConfigurationShape>() {
J
Johannes Rieken 已提交
25
		lastArgs: [ConfigurationTarget, string, any];
26
		$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): TPromise<void> {
J
Johannes Rieken 已提交
27
			this.lastArgs = [target, key, value];
28
			return TPromise.as(void 0);
J
Johannes Rieken 已提交
29
		}
30
	}
J
Johannes Rieken 已提交
31

32
	function createExtHostConfiguration(contents: any = Object.create(null), shape?: MainThreadConfigurationShape) {
33
		if (!shape) {
34
			shape = new class extends mock<MainThreadConfigurationShape>() { };
35
		}
36
		return new ExtHostConfiguration(shape, new ExtHostWorkspace(new TestRPCProtocol(), null, new NullLogService()), createConfigurationData(contents));
S
Sandeep Somavarapu 已提交
37 38
	}

S
Sandeep Somavarapu 已提交
39
	function createConfigurationData(contents: any): IConfigurationInitData {
S
Sandeep Somavarapu 已提交
40
		return {
41 42
			defaults: new ConfigurationModel(contents),
			user: new ConfigurationModel(contents),
43
			workspace: new ConfigurationModel(),
S
Sandeep Somavarapu 已提交
44
			folders: Object.create(null),
S
Sandeep Somavarapu 已提交
45
			configurationScopes: {}
S
Sandeep Somavarapu 已提交
46
		};
47 48
	}

49 50
	test('getConfiguration fails regression test 1.7.1 -> 1.8 #15552', function () {
		const extHostConfig = createExtHostConfiguration({
51 52 53 54 55
			'search': {
				'exclude': {
					'**/node_modules': true
				}
			}
56 57 58 59 60 61 62 63 64 65
		});

		assert.equal(extHostConfig.getConfiguration('search.exclude')['**/node_modules'], true);
		assert.equal(extHostConfig.getConfiguration('search.exclude').get('**/node_modules'), true);
		assert.equal(extHostConfig.getConfiguration('search').get('exclude')['**/node_modules'], true);

		assert.equal(extHostConfig.getConfiguration('search.exclude').has('**/node_modules'), true);
		assert.equal(extHostConfig.getConfiguration('search').has('exclude.**/node_modules'), true);
	});

66 67 68
	test('has/get', function () {

		const all = createExtHostConfiguration({
69 70 71 72 73 74 75 76
			'farboo': {
				'config0': true,
				'nested': {
					'config1': 42,
					'config2': 'Das Pferd frisst kein Reis.'
				},
				'config4': ''
			}
77 78 79 80 81 82 83 84 85
		});

		const config = all.getConfiguration('farboo');

		assert.ok(config.has('config0'));
		assert.equal(config.get('config0'), true);
		assert.equal(config.get('config4'), '');
		assert.equal(config['config0'], true);
		assert.equal(config['config4'], '');
86

87 88 89 90
		assert.ok(config.has('nested.config1'));
		assert.equal(config.get('nested.config1'), 42);
		assert.ok(config.has('nested.config2'));
		assert.equal(config.get('nested.config2'), 'Das Pferd frisst kein Reis.');
91 92 93

		assert.ok(config.has('nested'));
		assert.deepEqual(config.get('nested'), { config1: 42, config2: 'Das Pferd frisst kein Reis.' });
94 95
	});

S
Sandeep Somavarapu 已提交
96 97 98 99 100 101 102 103 104 105
	test('can modify the returned configuration', function () {

		const all = createExtHostConfiguration({
			'farboo': {
				'config0': true,
				'nested': {
					'config1': 42,
					'config2': 'Das Pferd frisst kein Reis.'
				},
				'config4': ''
S
Sandeep Somavarapu 已提交
106 107 108 109 110
			},
			'workbench': {
				'colorCustomizations': {
					'statusBar.foreground': 'somevalue'
				}
S
Sandeep Somavarapu 已提交
111 112 113
			}
		});

S
Sandeep Somavarapu 已提交
114
		let testObject = all.getConfiguration();
S
Sandeep Somavarapu 已提交
115
		let actual = testObject.get('farboo');
S
Sandeep Somavarapu 已提交
116 117
		actual['nested']['config1'] = 41;
		assert.equal(41, actual['nested']['config1']);
S
Sandeep Somavarapu 已提交
118 119 120
		actual['farboo1'] = 'newValue';
		assert.equal('newValue', actual['farboo1']);

S
Sandeep Somavarapu 已提交
121
		testObject = all.getConfiguration();
S
Sandeep Somavarapu 已提交
122 123 124
		actual = testObject.get('farboo');
		assert.equal(actual['nested']['config1'], 42);
		assert.equal(actual['farboo1'], undefined);
S
Sandeep Somavarapu 已提交
125 126

		testObject = all.getConfiguration();
S
Sandeep Somavarapu 已提交
127 128 129 130 131 132 133 134
		actual = testObject.get('farboo');
		assert.equal(actual['config0'], true);
		actual['config0'] = false;
		assert.equal(actual['config0'], false);

		testObject = all.getConfiguration();
		actual = testObject.get('farboo');
		assert.equal(actual['config0'], true);
S
Sandeep Somavarapu 已提交
135 136

		testObject = all.getConfiguration();
S
Sandeep Somavarapu 已提交
137 138 139
		actual = testObject.inspect('farboo');
		actual['value'] = 'effectiveValue';
		assert.equal('effectiveValue', actual['value']);
S
Sandeep Somavarapu 已提交
140 141 142 143 144 145 146 147

		testObject = all.getConfiguration('workbench');
		actual = testObject.get('colorCustomizations');
		delete actual['statusBar.foreground'];
		assert.equal(actual['statusBar.foreground'], undefined);
		testObject = all.getConfiguration('workbench');
		actual = testObject.get('colorCustomizations');
		assert.equal(actual['statusBar.foreground'], 'somevalue');
S
Sandeep Somavarapu 已提交
148
	});
S
Sandeep Somavarapu 已提交
149

S
Sandeep Somavarapu 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	test('Stringify returned configuration', function () {

		const all = createExtHostConfiguration({
			'farboo': {
				'config0': true,
				'nested': {
					'config1': 42,
					'config2': 'Das Pferd frisst kein Reis.'
				},
				'config4': ''
			},
			'workbench': {
				'colorCustomizations': {
					'statusBar.foreground': 'somevalue'
				},
				'emptyobjectkey': {
				}
			}
		});

		let testObject = all.getConfiguration();
		let actual = testObject.get('farboo');
		assert.deepEqual(JSON.stringify({
			'config0': true,
			'nested': {
				'config1': 42,
				'config2': 'Das Pferd frisst kein Reis.'
			},
			'config4': ''
		}), JSON.stringify(actual));

		assert.deepEqual(undefined, JSON.stringify(testObject.get('unknownkey')));

		actual = testObject.get('farboo');
		actual['config0'] = false;
		assert.deepEqual(JSON.stringify({
			'config0': false,
			'nested': {
				'config1': 42,
				'config2': 'Das Pferd frisst kein Reis.'
			},
			'config4': ''
		}), JSON.stringify(actual));

		actual = testObject.get('workbench')['colorCustomizations'];
		actual['statusBar.background'] = 'anothervalue';
		assert.deepEqual(JSON.stringify({
			'statusBar.foreground': 'somevalue',
			'statusBar.background': 'anothervalue'
		}), JSON.stringify(actual));

		actual = testObject.get('workbench');
		actual['unknownkey'] = 'somevalue';
		assert.deepEqual(JSON.stringify({
			'colorCustomizations': {
				'statusBar.foreground': 'somevalue'
			},
			'emptyobjectkey': {},
			'unknownkey': 'somevalue'
		}), JSON.stringify(actual));

		actual = all.getConfiguration('workbench').get('emptyobjectkey');
		actual = assign(actual || {}, {
			'statusBar.background': `#0ff`,
			'statusBar.foreground': `#ff0`,
		});
		assert.deepEqual(JSON.stringify({
			'statusBar.background': `#0ff`,
			'statusBar.foreground': `#ff0`,
		}), JSON.stringify(actual));

		actual = all.getConfiguration('workbench').get('unknownkey');
		actual = assign(actual || {}, {
			'statusBar.background': `#0ff`,
			'statusBar.foreground': `#ff0`,
		});
		assert.deepEqual(JSON.stringify({
			'statusBar.background': `#0ff`,
			'statusBar.foreground': `#ff0`,
		}), JSON.stringify(actual));
	});

S
Sandeep Somavarapu 已提交
232
	test('cannot modify returned configuration', function () {
S
Sandeep Somavarapu 已提交
233

S
Sandeep Somavarapu 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		const all = createExtHostConfiguration({
			'farboo': {
				'config0': true,
				'nested': {
					'config1': 42,
					'config2': 'Das Pferd frisst kein Reis.'
				},
				'config4': ''
			}
		});

		let testObject = all.getConfiguration();

		try {
			testObject['get'] = null;
			assert.fail('This should be readonly');
		} catch (e) {
		}

		try {
			testObject['farboo']['config0'] = false;
			assert.fail('This should be readonly');
		} catch (e) {
		}

		try {
			testObject['farboo']['farboo1'] = 'hello';
			assert.fail('This should be readonly');
		} catch (e) {
		}
S
Sandeep Somavarapu 已提交
264 265
	});

S
Sandeep Somavarapu 已提交
266 267
	test('inspect in no workspace context', function () {
		const testObject = new ExtHostConfiguration(
268
			new class extends mock<MainThreadConfigurationShape>() { },
269
			new ExtHostWorkspace(new TestRPCProtocol(), null, new NullLogService()),
S
Sandeep Somavarapu 已提交
270 271 272 273 274 275 276 277 278 279 280 281
			{
				defaults: new ConfigurationModel({
					'editor': {
						'wordWrap': 'off'
					}
				}, ['editor.wordWrap']),
				user: new ConfigurationModel({
					'editor': {
						'wordWrap': 'on'
					}
				}, ['editor.wordWrap']),
				workspace: new ConfigurationModel({}, []),
S
Sandeep Somavarapu 已提交
282
				folders: Object.create(null),
S
Sandeep Somavarapu 已提交
283
				configurationScopes: {}
S
Sandeep Somavarapu 已提交
284 285 286 287 288 289 290
			}
		);

		let actual = testObject.getConfiguration().inspect('editor.wordWrap');
		assert.equal(actual.defaultValue, 'off');
		assert.equal(actual.globalValue, 'on');
		assert.equal(actual.workspaceValue, undefined);
S
Sandeep Somavarapu 已提交
291
		assert.equal(actual.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
292 293 294 295 296

		actual = testObject.getConfiguration('editor').inspect('wordWrap');
		assert.equal(actual.defaultValue, 'off');
		assert.equal(actual.globalValue, 'on');
		assert.equal(actual.workspaceValue, undefined);
S
Sandeep Somavarapu 已提交
297
		assert.equal(actual.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
298 299 300
	});

	test('inspect in single root context', function () {
301 302
		const workspaceUri = URI.file('foo');
		const folders = Object.create(null);
303
		const workspace = new ConfigurationModel({
304 305 306 307
			'editor': {
				'wordWrap': 'bounded'
			}
		}, ['editor.wordWrap']);
308
		folders[workspaceUri.toString()] = workspace;
J
Johannes Rieken 已提交
309
		const testObject = new ExtHostConfiguration(
310
			new class extends mock<MainThreadConfigurationShape>() { },
A
Alex Dima 已提交
311
			new ExtHostWorkspace(new TestRPCProtocol(), {
J
Johannes Rieken 已提交
312
				'id': 'foo',
313
				'folders': [aWorkspaceFolder(URI.file('foo'), 0)],
J
Johannes Rieken 已提交
314
				'name': 'foo'
315
			}, new NullLogService()),
J
Johannes Rieken 已提交
316 317 318 319 320 321 322 323 324 325 326
			{
				defaults: new ConfigurationModel({
					'editor': {
						'wordWrap': 'off'
					}
				}, ['editor.wordWrap']),
				user: new ConfigurationModel({
					'editor': {
						'wordWrap': 'on'
					}
				}, ['editor.wordWrap']),
327
				workspace,
S
Sandeep Somavarapu 已提交
328
				folders,
S
Sandeep Somavarapu 已提交
329
				configurationScopes: {}
J
Johannes Rieken 已提交
330 331
			}
		);
332

S
Sandeep Somavarapu 已提交
333 334 335 336
		let actual1 = testObject.getConfiguration().inspect('editor.wordWrap');
		assert.equal(actual1.defaultValue, 'off');
		assert.equal(actual1.globalValue, 'on');
		assert.equal(actual1.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
337
		assert.equal(actual1.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
338 339 340 341 342

		actual1 = testObject.getConfiguration('editor').inspect('wordWrap');
		assert.equal(actual1.defaultValue, 'off');
		assert.equal(actual1.globalValue, 'on');
		assert.equal(actual1.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
343
		assert.equal(actual1.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
344

S
Sandeep Somavarapu 已提交
345
		let actual2 = testObject.getConfiguration(null, workspaceUri).inspect('editor.wordWrap');
S
Sandeep Somavarapu 已提交
346 347 348
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
349
		assert.equal(actual2.workspaceFolderValue, 'bounded');
S
Sandeep Somavarapu 已提交
350

S
Sandeep Somavarapu 已提交
351
		actual2 = testObject.getConfiguration('editor', workspaceUri).inspect('wordWrap');
S
Sandeep Somavarapu 已提交
352 353 354
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
355
		assert.equal(actual2.workspaceFolderValue, 'bounded');
S
Sandeep Somavarapu 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	});

	test('inspect in multi root context', function () {
		const workspace = new ConfigurationModel({
			'editor': {
				'wordWrap': 'bounded'
			}
		}, ['editor.wordWrap']);

		const firstRoot = URI.file('foo1');
		const secondRoot = URI.file('foo2');
		const thirdRoot = URI.file('foo3');
		const folders = Object.create(null);
		folders[firstRoot.toString()] = new ConfigurationModel({
			'editor': {
				'wordWrap': 'off',
				'lineNumbers': 'relative'
			}
		}, ['editor.wordWrap']);
		folders[secondRoot.toString()] = new ConfigurationModel({
			'editor': {
				'wordWrap': 'on'
			}
		}, ['editor.wordWrap']);
		folders[thirdRoot.toString()] = new ConfigurationModel({}, []);

		const testObject = new ExtHostConfiguration(
383
			new class extends mock<MainThreadConfigurationShape>() { },
A
Alex Dima 已提交
384
			new ExtHostWorkspace(new TestRPCProtocol(), {
S
Sandeep Somavarapu 已提交
385
				'id': 'foo',
386
				'folders': [aWorkspaceFolder(firstRoot, 0), aWorkspaceFolder(secondRoot, 1)],
S
Sandeep Somavarapu 已提交
387
				'name': 'foo'
388
			}, new NullLogService()),
S
Sandeep Somavarapu 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401
			{
				defaults: new ConfigurationModel({
					'editor': {
						'wordWrap': 'off',
						'lineNumbers': 'on'
					}
				}, ['editor.wordWrap']),
				user: new ConfigurationModel({
					'editor': {
						'wordWrap': 'on'
					}
				}, ['editor.wordWrap']),
				workspace,
S
Sandeep Somavarapu 已提交
402
				folders,
S
Sandeep Somavarapu 已提交
403
				configurationScopes: {}
S
Sandeep Somavarapu 已提交
404 405 406 407 408 409
			}
		);

		let actual1 = testObject.getConfiguration().inspect('editor.wordWrap');
		assert.equal(actual1.defaultValue, 'off');
		assert.equal(actual1.globalValue, 'on');
S
Sandeep Somavarapu 已提交
410
		assert.equal(actual1.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
411
		assert.equal(actual1.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
412 413 414 415

		actual1 = testObject.getConfiguration('editor').inspect('wordWrap');
		assert.equal(actual1.defaultValue, 'off');
		assert.equal(actual1.globalValue, 'on');
S
Sandeep Somavarapu 已提交
416
		assert.equal(actual1.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
417
		assert.equal(actual1.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
418 419 420 421

		actual1 = testObject.getConfiguration('editor').inspect('lineNumbers');
		assert.equal(actual1.defaultValue, 'on');
		assert.equal(actual1.globalValue, undefined);
S
Sandeep Somavarapu 已提交
422
		assert.equal(actual1.workspaceValue, undefined);
S
Sandeep Somavarapu 已提交
423
		assert.equal(actual1.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
424

S
Sandeep Somavarapu 已提交
425
		let actual2 = testObject.getConfiguration(null, firstRoot).inspect('editor.wordWrap');
S
Sandeep Somavarapu 已提交
426 427 428
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
429
		assert.equal(actual2.workspaceFolderValue, 'off');
S
Sandeep Somavarapu 已提交
430

S
Sandeep Somavarapu 已提交
431
		actual2 = testObject.getConfiguration('editor', firstRoot).inspect('wordWrap');
S
Sandeep Somavarapu 已提交
432 433 434
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
435
		assert.equal(actual2.workspaceFolderValue, 'off');
S
Sandeep Somavarapu 已提交
436

S
Sandeep Somavarapu 已提交
437
		actual2 = testObject.getConfiguration('editor', firstRoot).inspect('lineNumbers');
S
Sandeep Somavarapu 已提交
438 439 440
		assert.equal(actual2.defaultValue, 'on');
		assert.equal(actual2.globalValue, undefined);
		assert.equal(actual2.workspaceValue, undefined);
S
Sandeep Somavarapu 已提交
441
		assert.equal(actual2.workspaceFolderValue, 'relative');
S
Sandeep Somavarapu 已提交
442

S
Sandeep Somavarapu 已提交
443
		actual2 = testObject.getConfiguration(null, secondRoot).inspect('editor.wordWrap');
S
Sandeep Somavarapu 已提交
444 445 446
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
447
		assert.equal(actual2.workspaceFolderValue, 'on');
S
Sandeep Somavarapu 已提交
448

S
Sandeep Somavarapu 已提交
449
		actual2 = testObject.getConfiguration('editor', secondRoot).inspect('wordWrap');
S
Sandeep Somavarapu 已提交
450 451 452
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
453
		assert.equal(actual2.workspaceFolderValue, 'on');
S
Sandeep Somavarapu 已提交
454

S
Sandeep Somavarapu 已提交
455
		actual2 = testObject.getConfiguration(null, thirdRoot).inspect('editor.wordWrap');
S
Sandeep Somavarapu 已提交
456 457 458
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
459
		assert.ok(Object.keys(actual2).indexOf('workspaceFolderValue') !== -1);
S
Sandeep Somavarapu 已提交
460
		assert.equal(actual2.workspaceFolderValue, undefined);
S
Sandeep Somavarapu 已提交
461

S
Sandeep Somavarapu 已提交
462
		actual2 = testObject.getConfiguration('editor', thirdRoot).inspect('wordWrap');
S
Sandeep Somavarapu 已提交
463 464 465
		assert.equal(actual2.defaultValue, 'off');
		assert.equal(actual2.globalValue, 'on');
		assert.equal(actual2.workspaceValue, 'bounded');
S
Sandeep Somavarapu 已提交
466
		assert.ok(Object.keys(actual2).indexOf('workspaceFolderValue') !== -1);
S
Sandeep Somavarapu 已提交
467
		assert.equal(actual2.workspaceFolderValue, undefined);
468 469
	});

470 471 472
	test('getConfiguration vs get', function () {

		const all = createExtHostConfiguration({
473 474 475 476
			'farboo': {
				'config0': true,
				'config4': 38
			}
477 478 479 480 481 482 483 484 485
		});

		let config = all.getConfiguration('farboo.config0');
		assert.equal(config.get(''), undefined);
		assert.equal(config.has(''), false);

		config = all.getConfiguration('farboo');
		assert.equal(config.get('config0'), true);
		assert.equal(config.has('config0'), true);
486 487
	});

488 489 490
	test('getConfiguration vs get', function () {

		const all = createExtHostConfiguration({
491 492 493 494
			'farboo': {
				'config0': true,
				'config4': 38
			}
495 496 497 498 499 500 501 502 503 504 505
		});

		let config = all.getConfiguration('farboo.config0');
		assert.equal(config.get(''), undefined);
		assert.equal(config.has(''), false);

		config = all.getConfiguration('farboo');
		assert.equal(config.get('config0'), true);
		assert.equal(config.has('config0'), true);
	});

506 507
	test('name vs property', function () {
		const all = createExtHostConfiguration({
508 509 510
			'farboo': {
				'get': 'get-prop'
			}
511 512 513 514 515 516 517 518 519
		});
		const config = all.getConfiguration('farboo');

		assert.ok(config.has('get'));
		assert.equal(config.get('get'), 'get-prop');
		assert.deepEqual(config['get'], config.get);
		assert.throws(() => config['get'] = <any>'get-prop');
	});

S
Sandeep Somavarapu 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	test('update: no target passes null', function () {
		const shape = new RecordingShape();
		const allConfig = createExtHostConfiguration({
			'foo': {
				'bar': 1,
				'far': 1
			}
		}, shape);

		let config = allConfig.getConfiguration('foo');
		config.update('bar', 42);

		assert.equal(shape.lastArgs[0], null);
	});

J
Johannes Rieken 已提交
535
	test('update/section to key', function () {
J
Johannes Rieken 已提交
536

537
		const shape = new RecordingShape();
538
		const allConfig = createExtHostConfiguration({
539 540 541 542
			'foo': {
				'bar': 1,
				'far': 1
			}
543
		}, shape);
J
Johannes Rieken 已提交
544

545 546
		let config = allConfig.getConfiguration('foo');
		config.update('bar', 42, true);
J
Johannes Rieken 已提交
547

S
Sandeep Somavarapu 已提交
548
		assert.equal(shape.lastArgs[0], ConfigurationTarget.USER);
549 550
		assert.equal(shape.lastArgs[1], 'foo.bar');
		assert.equal(shape.lastArgs[2], 42);
J
Johannes Rieken 已提交
551

552 553 554
		config = allConfig.getConfiguration('');
		config.update('bar', 42, true);
		assert.equal(shape.lastArgs[1], 'bar');
J
Johannes Rieken 已提交
555

556 557 558
		config.update('foo.bar', 42, true);
		assert.equal(shape.lastArgs[1], 'foo.bar');
	});
J
Johannes Rieken 已提交
559

J
Johannes Rieken 已提交
560 561 562
	test('update, what is #15834', function () {
		const shape = new RecordingShape();
		const allConfig = createExtHostConfiguration({
563 564 565
			'editor': {
				'formatOnSave': true
			}
J
Johannes Rieken 已提交
566 567 568 569 570 571 572
		}, shape);

		allConfig.getConfiguration('editor').update('formatOnSave', { extensions: ['ts'] });
		assert.equal(shape.lastArgs[1], 'editor.formatOnSave');
		assert.deepEqual(shape.lastArgs[2], { extensions: ['ts'] });
	});

573
	test('update/error-state not OK', function () {
J
Johannes Rieken 已提交
574

575
		const shape = new class extends mock<MainThreadConfigurationShape>() {
576
			$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): TPromise<any> {
577
				return TPromise.wrapError(new Error('Unknown Key')); // something !== OK
578 579
			}
		};
J
Johannes Rieken 已提交
580

581 582 583
		return createExtHostConfiguration({}, shape)
			.getConfiguration('')
			.update('', true, false)
J
Johannes Rieken 已提交
584
			.then(() => assert.ok(false), err => { /* expecting rejection */ });
585
	});
586

S
Sandeep Somavarapu 已提交
587 588 589 590 591
	test('configuration change event', (done) => {

		const workspaceFolder = aWorkspaceFolder(URI.file('folder1'), 0);
		const testObject = new ExtHostConfiguration(
			new class extends mock<MainThreadConfigurationShape>() { },
A
Alex Dima 已提交
592
			new ExtHostWorkspace(new TestRPCProtocol(), {
S
Sandeep Somavarapu 已提交
593 594 595
				'id': 'foo',
				'folders': [workspaceFolder],
				'name': 'foo'
596
			}, new NullLogService()),
S
Sandeep Somavarapu 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
			createConfigurationData({
				'farboo': {
					'config': false,
					'updatedconfig': false
				}
			})
		);

		const newConfigData = createConfigurationData({
			'farboo': {
				'config': false,
				'updatedconfig': true,
				'newConfig': true,
			}
		});
		const changedConfigurationByResource = Object.create({});
		changedConfigurationByResource[workspaceFolder.uri.toString()] = new ConfigurationModel({
			'farboo': {
				'newConfig': true,
			}
		}, ['farboo.newConfig']);
		const configEventData = {
			changedConfiguration: new ConfigurationModel({
				'farboo': {
					'updatedConfig': true,
				}
			}, ['farboo.updatedConfig']),
			changedConfigurationByResource
		};
		testObject.onDidChangeConfiguration(e => {

			assert.deepEqual(testObject.getConfiguration().get('farboo'), {
				'config': false,
				'updatedconfig': true,
				'newConfig': true,
			});

			assert.ok(e.affectsConfiguration('farboo'));
			assert.ok(e.affectsConfiguration('farboo', workspaceFolder.uri));
			assert.ok(e.affectsConfiguration('farboo', URI.file('any')));

			assert.ok(e.affectsConfiguration('farboo.updatedConfig'));
			assert.ok(e.affectsConfiguration('farboo.updatedConfig', workspaceFolder.uri));
			assert.ok(e.affectsConfiguration('farboo.updatedConfig', URI.file('any')));

			assert.ok(e.affectsConfiguration('farboo.newConfig'));
			assert.ok(e.affectsConfiguration('farboo.newConfig', workspaceFolder.uri));
			assert.ok(!e.affectsConfiguration('farboo.newConfig', URI.file('any')));

			assert.ok(!e.affectsConfiguration('farboo.config'));
			assert.ok(!e.affectsConfiguration('farboo.config', workspaceFolder.uri));
			assert.ok(!e.affectsConfiguration('farboo.config', URI.file('any')));
			done();
		});

		testObject.$acceptConfigurationChanged(newConfigData, configEventData);
	});

655 656
	function aWorkspaceFolder(uri: URI, index: number, name: string = ''): IWorkspaceFolder {
		return new WorkspaceFolder({ uri, name, index });
657
	}
J
Johannes Rieken 已提交
658
});