configurationService.test.ts 30.3 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

S
Sandeep Somavarapu 已提交
8
import * as assert from 'assert';
S
Sandeep Somavarapu 已提交
9
import * as sinon from 'sinon';
S
Sandeep Somavarapu 已提交
10 11 12
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
S
Sandeep Somavarapu 已提交
13
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
14
import { TPromise } from 'vs/base/common/winjs.base';
15
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
16
import { ParsedArgs, IEnvironmentService } from 'vs/platform/environment/common/environment';
J
Johannes Rieken 已提交
17 18
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { parseArgs } from 'vs/platform/environment/node/argv';
19 20
import extfs = require('vs/base/node/extfs');
import uuid = require('vs/base/common/uuid');
S
Sandeep Somavarapu 已提交
21
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
22
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configurationService';
S
Sandeep Somavarapu 已提交
23
import { ConfigurationEditingErrorCode } from 'vs/workbench/services/configuration/node/configurationEditingService';
S
Sandeep Somavarapu 已提交
24
import { FileChangeType, FileChangesEvent, IFileService } from 'vs/platform/files/common/files';
S
Sandeep Somavarapu 已提交
25
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFoldersChangeEvent } from 'vs/platform/workspace/common/workspace';
S
Sandeep Somavarapu 已提交
26 27 28 29 30 31 32
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { workbenchInstantiationService, TestTextResourceConfigurationService, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
import { FileService } from 'vs/workbench/services/files/node/fileService';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { TextModelResolverService } from 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
S
Sandeep Somavarapu 已提交
33 34
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
import { JSONEditingService } from 'vs/workbench/services/configuration/node/jsonEditingService';
35 36 37 38 39 40 41 42 43 44

class SettingsTestEnvironmentService extends EnvironmentService {

	constructor(args: ParsedArgs, _execPath: string, private customAppSettingsHome) {
		super(args, _execPath);
	}

	get appSettingsPath(): string { return this.customAppSettingsHome; }
}

45
function setUpFolderWorkspace(folderName: string): TPromise<{ parentDir: string, folderDir: string }> {
46 47
	const id = uuid.generateUuid();
	const parentDir = path.join(os.tmpdir(), 'vsctests', id);
48
	return setUpFolder(folderName, parentDir).then(folderDir => ({ parentDir, folderDir }));
S
Sandeep Somavarapu 已提交
49
}
50

S
Sandeep Somavarapu 已提交
51 52 53
function setUpFolder(folderName: string, parentDir: string): TPromise<string> {
	const folderDir = path.join(parentDir, folderName);
	const workspaceSettingsDir = path.join(folderDir, '.vscode');
54
	return new TPromise((c, e) => {
55
		extfs.mkdirp(workspaceSettingsDir, 493, (error) => {
56 57 58 59
			if (error) {
				e(error);
				return null;
			}
S
Sandeep Somavarapu 已提交
60 61 62 63 64
			c(folderDir);
		});
	});
}

65
function setUpWorkspace(folders: string[]): TPromise<{ parentDir: string, configPath: string }> {
S
Sandeep Somavarapu 已提交
66 67 68 69 70 71 72 73 74 75 76

	const id = uuid.generateUuid();
	const parentDir = path.join(os.tmpdir(), 'vsctests', id);

	return createDir(parentDir)
		.then(() => {
			const configPath = path.join(parentDir, 'vsctests.code-workspace');
			const workspace = { folders: folders.map(path => ({ path })) };
			fs.writeFileSync(configPath, JSON.stringify(workspace, null, '\t'));

			return TPromise.join(folders.map(folder => setUpFolder(folder, parentDir)))
77
				.then(() => ({ parentDir, configPath }));
S
Sandeep Somavarapu 已提交
78
		});
79

S
Sandeep Somavarapu 已提交
80 81 82 83 84 85 86 87 88 89
}

function createDir(dir: string): TPromise<void> {
	return new TPromise((c, e) => {
		extfs.mkdirp(dir, 493, (error) => {
			if (error) {
				e(error);
				return null;
			}
			c(null);
90
		});
91 92
	});
}
93

94 95
suite('WorkspaceContextService - Folder', () => {

S
Sandeep Somavarapu 已提交
96
	let workspaceName = `testWorkspace${uuid.generateUuid()}`, parentResource: string, workspaceResource: string, workspaceContextService: IWorkspaceContextService;
97 98

	setup(() => {
S
Sandeep Somavarapu 已提交
99
		return setUpFolderWorkspace(workspaceName)
100
			.then(({ parentDir, folderDir }) => {
101
				parentResource = parentDir;
S
Sandeep Somavarapu 已提交
102
				workspaceResource = folderDir;
103 104 105 106
				const globalSettingsFile = path.join(parentDir, 'settings.json');
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
				workspaceContextService = new WorkspaceService(environmentService, null);
				return (<WorkspaceService>workspaceContextService).initialize(folderDir);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
			});
	});

	teardown(done => {
		if (workspaceContextService) {
			(<WorkspaceService>workspaceContextService).dispose();
		}
		if (parentResource) {
			extfs.del(parentResource, os.tmpdir(), () => { }, done);
		}
	});

	test('getWorkspace()', () => {
		const actual = workspaceContextService.getWorkspace();

		assert.equal(actual.folders.length, 1);
S
Sandeep Somavarapu 已提交
123
		assert.equal(actual.folders[0].uri.fsPath, URI.file(workspaceResource).fsPath);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		assert.equal(actual.folders[0].name, workspaceName);
		assert.equal(actual.folders[0].index, 0);
		assert.ok(!actual.configuration);
	});

	test('getWorkbenchState()', () => {
		const actual = workspaceContextService.getWorkbenchState();

		assert.equal(actual, WorkbenchState.FOLDER);
	});

	test('getWorkspaceFolder()', () => {
		const actual = workspaceContextService.getWorkspaceFolder(URI.file(path.join(workspaceResource, 'a')));

		assert.equal(actual, workspaceContextService.getWorkspace().folders[0]);
	});

	test('isCurrentWorkspace() => true', () => {
		assert.ok(workspaceContextService.isCurrentWorkspace(workspaceResource));
	});

	test('isCurrentWorkspace() => false', () => {
		assert.ok(!workspaceContextService.isCurrentWorkspace(workspaceResource + 'abc'));
	});
});

S
Sandeep Somavarapu 已提交
150 151
suite('WorkspaceContextService - Workspace', () => {

152
	let parentResource: string, testObject: WorkspaceService;
S
Sandeep Somavarapu 已提交
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

	setup(() => {
		return setUpWorkspace(['a', 'b'])
			.then(({ parentDir, configPath }) => {

				parentResource = parentDir;

				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
				const workspaceService = new WorkspaceService(environmentService, null);

				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);

				return workspaceService.initialize({ id: configPath, configPath }).then(() => {

					instantiationService.stub(IFileService, new FileService(<IWorkspaceContextService>workspaceService, new TestTextResourceConfigurationService(), workspaceService, { disableWatcher: true }));
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
					workspaceService.setInstantiationService(instantiationService);

					testObject = workspaceService;
				});
			});
	});

	teardown(done => {
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
			extfs.del(parentResource, os.tmpdir(), () => { }, done);
		}
	});

	test('workspace folders', () => {
		const actual = testObject.getWorkspace().folders;

		assert.equal(actual.length, 2);
		assert.equal(path.basename(actual[0].uri.fsPath), 'a');
		assert.equal(path.basename(actual[1].uri.fsPath), 'b');
	});

	test('add folders', () => {
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
199
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }])
S
Sandeep Somavarapu 已提交
200 201 202 203 204 205 206 207 208 209 210
			.then(() => {
				const actual = testObject.getWorkspace().folders;

				assert.equal(actual.length, 4);
				assert.equal(path.basename(actual[0].uri.fsPath), 'a');
				assert.equal(path.basename(actual[1].uri.fsPath), 'b');
				assert.equal(path.basename(actual[2].uri.fsPath), 'd');
				assert.equal(path.basename(actual[3].uri.fsPath), 'c');
			});
	});

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	test('add folders (with name)', () => {
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')), name: 'DDD' }, { uri: URI.file(path.join(workspaceDir, 'c')), name: 'CCC' }])
			.then(() => {
				const actual = testObject.getWorkspace().folders;

				assert.equal(actual.length, 4);
				assert.equal(path.basename(actual[0].uri.fsPath), 'a');
				assert.equal(path.basename(actual[1].uri.fsPath), 'b');
				assert.equal(path.basename(actual[2].uri.fsPath), 'd');
				assert.equal(path.basename(actual[3].uri.fsPath), 'c');
				assert.equal(actual[2].name, 'DDD');
				assert.equal(actual[3].name, 'CCC');
			});
	});

S
Sandeep Somavarapu 已提交
227 228 229 230
	test('add folders triggers change event', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
S
Sandeep Somavarapu 已提交
231 232 233 234 235 236 237 238 239
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }];
		return testObject.addFolders(addedFolders)
			.then(() => {
				assert.ok(target.calledOnce);
				const actual = <IWorkspaceFoldersChangeEvent>target.args[0][0];
				assert.deepEqual(actual.added.map(r => r.uri.toString()), addedFolders.map(a => a.uri.toString()));
				assert.deepEqual(actual.removed, []);
				assert.deepEqual(actual.changed, []);
			});
S
Sandeep Somavarapu 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
	});

	test('remove folders', () => {
		return testObject.removeFolders([testObject.getWorkspace().folders[0].uri])
			.then(() => {
				const actual = testObject.getWorkspace().folders;
				assert.equal(actual.length, 1);
				assert.equal(path.basename(actual[0].uri.fsPath), 'b');
			});
	});

	test('remove folders triggers change event', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
S
Sandeep Somavarapu 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		const removedFolder = testObject.getWorkspace().folders[0];
		return testObject.removeFolders([removedFolder.uri])
			.then(() => {
				assert.ok(target.calledOnce);
				const actual = <IWorkspaceFoldersChangeEvent>target.args[0][0];
				assert.deepEqual(actual.added, []);
				assert.deepEqual(actual.removed.map(r => r.uri.toString()), [removedFolder.uri.toString()]);
				assert.deepEqual(actual.changed.map(c => c.uri.toString()), [testObject.getWorkspace().folders[0].uri.toString()]);
			});
	});

	test('reorder folders trigger change event', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspace = { folders: [{ path: testObject.getWorkspace().folders[1].uri.fsPath }, { path: testObject.getWorkspace().folders[0].uri.fsPath }] };
		fs.writeFileSync(testObject.getWorkspace().configuration.fsPath, JSON.stringify(workspace, null, '\t'));
		return testObject.reloadConfiguration()
			.then(() => {
				assert.ok(target.calledOnce);
				const actual = <IWorkspaceFoldersChangeEvent>target.args[0][0];
				assert.deepEqual(actual.added, []);
				assert.deepEqual(actual.removed, []);
				assert.deepEqual(actual.changed.map(c => c.uri.toString()), testObject.getWorkspace().folders.map(f => f.uri.toString()).reverse());
			});
	});

	test('rename folders trigger change event', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspace = { folders: [{ path: testObject.getWorkspace().folders[0].uri.fsPath, name: '1' }, { path: testObject.getWorkspace().folders[1].uri.fsPath }] };
		fs.writeFileSync(testObject.getWorkspace().configuration.fsPath, JSON.stringify(workspace, null, '\t'));
		return testObject.reloadConfiguration()
			.then(() => {
				assert.ok(target.calledOnce);
				const actual = <IWorkspaceFoldersChangeEvent>target.args[0][0];
				assert.deepEqual(actual.added, []);
				assert.deepEqual(actual.removed, []);
				assert.deepEqual(actual.changed.map(c => c.uri.toString()), [testObject.getWorkspace().folders[0].uri.toString()]);
			});
S
Sandeep Somavarapu 已提交
293 294 295 296
	});

});

297
suite('WorkspaceConfigurationService - Folder', () => {
298

299
	let workspaceName = `testWorkspace${uuid.generateUuid()}`, parentResource: string, workspaceDir: string, testObject: IConfigurationService, globalSettingsFile: string;
300

301
	suiteSetup(() => {
302
		const configurationRegistry = <IConfigurationRegistry>Registry.as(ConfigurationExtensions.Configuration);
B
Benjamin Pasero 已提交
303 304 305 306
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
307
				'configurationService.folder.testSetting': {
B
Benjamin Pasero 已提交
308 309
					'type': 'string',
					'default': 'isSet'
310
				},
B
Benjamin Pasero 已提交
311 312
			}
		});
313 314
	});

315 316 317
	setup(() => {
		return setUpFolderWorkspace(workspaceName)
			.then(({ parentDir, folderDir }) => {
318

319 320 321
				parentResource = parentDir;
				workspaceDir = folderDir;
				globalSettingsFile = path.join(parentDir, 'settings.json');
322

323 324 325 326 327 328
				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
				const workspaceService = new WorkspaceService(environmentService, null);
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);
329

330 331 332 333 334 335
				return workspaceService.initialize(folderDir).then(() => {
					instantiationService.stub(IFileService, new FileService(<IWorkspaceContextService>workspaceService, new TestTextResourceConfigurationService(), workspaceService, { disableWatcher: true }));
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
					workspaceService.setInstantiationService(instantiationService);
					testObject = workspaceService;
336 337 338 339
				});
			});
	});

340 341 342 343 344 345
	teardown(done => {
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
			extfs.del(parentResource, os.tmpdir(), () => { }, done);
346 347 348
		}
	});

349 350
	test('defaults', () => {
		assert.deepEqual(testObject.getValue('configurationService'), { 'folder': { 'testSetting': 'isSet' } });
351 352
	});

353 354 355 356
	test('globals override defaults', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'userValue'));
357 358
	});

359 360 361 362
	test('globals', () => {
		fs.writeFileSync(globalSettingsFile, '{ "testworkbench.editor.tabs": true }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('testworkbench.editor.tabs'), true));
363
	});
B
Benjamin Pasero 已提交
364

365 366 367 368
	test('workspace settings', () => {
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "testworkbench.editor.icons": true }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('testworkbench.editor.icons'), true));
S
Sandeep Somavarapu 已提交
369 370
	});

371 372 373 374 375
	test('workspace settings override user settings', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'workspaceValue'));
S
Sandeep Somavarapu 已提交
376 377
	});

378 379 380 381 382 383 384 385 386 387
	test('workspace change triggers event', () => {
		const settingsFile = path.join(workspaceDir, '.vscode', 'settings.json');
		fs.writeFileSync(settingsFile, '{ "configurationService.folder.testSetting": "workspaceValue" }');
		const event = new FileChangesEvent([{ resource: URI.file(settingsFile), type: FileChangeType.ADDED }]);
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return (<WorkspaceService>testObject).handleWorkspaceFileEvents(event)
			.then(() => {
				assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'workspaceValue');
				assert.ok(target.called);
S
Sandeep Somavarapu 已提交
388 389 390
			});
	});

391 392 393 394 395
	test('reload configuration emits events after global configuraiton changes', () => {
		fs.writeFileSync(globalSettingsFile, '{ "testworkbench.editor.tabs": true }');
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.reloadConfiguration().then(() => assert.ok(target.called));
B
Benjamin Pasero 已提交
396
	});
B
Benjamin Pasero 已提交
397

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	test('reload configuration emits events after workspace configuraiton changes', () => {
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue" }');
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.reloadConfiguration().then(() => assert.ok(target.called));
	});

	test('reload configuration should not emit event if no changes', () => {
		fs.writeFileSync(globalSettingsFile, '{ "testworkbench.editor.tabs": true }');
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue" }');
		return testObject.reloadConfiguration()
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeConfiguration(() => { target(); });
				return testObject.reloadConfiguration()
					.then(() => assert.ok(!target.called));
B
Benjamin Pasero 已提交
414 415
			});
	});
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	test('inspect', () => {
		let actual = testObject.inspect('something.missing');
		assert.equal(actual.default, void 0);
		assert.equal(actual.user, void 0);
		assert.equal(actual.workspace, void 0);
		assert.equal(actual.workspaceFolder, void 0);
		assert.equal(actual.value, void 0);

		actual = testObject.inspect('configurationService.folder.testSetting');
		assert.equal(actual.default, 'isSet');
		assert.equal(actual.user, void 0);
		assert.equal(actual.workspace, void 0);
		assert.equal(actual.workspaceFolder, void 0);
		assert.equal(actual.value, 'isSet');

		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => {
				actual = testObject.inspect('configurationService.folder.testSetting');
				assert.equal(actual.default, 'isSet');
				assert.equal(actual.user, 'userValue');
				assert.equal(actual.workspace, void 0);
				assert.equal(actual.workspaceFolder, void 0);
				assert.equal(actual.value, 'userValue');

				fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue" }');

				return testObject.reloadConfiguration()
					.then(() => {
						actual = testObject.inspect('configurationService.folder.testSetting');
						assert.equal(actual.default, 'isSet');
						assert.equal(actual.user, 'userValue');
						assert.equal(actual.workspace, 'workspaceValue');
						assert.equal(actual.workspaceFolder, void 0);
						assert.equal(actual.value, 'workspaceValue');
452 453
					});
			});
S
Sandeep Somavarapu 已提交
454 455
	});

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	test('keys', () => {
		let actual = testObject.keys();
		assert.ok(actual.default.indexOf('configurationService.folder.testSetting') !== -1);
		assert.deepEqual(actual.user, []);
		assert.deepEqual(actual.workspace, []);
		assert.deepEqual(actual.workspaceFolder, []);

		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => {
				actual = testObject.keys();
				assert.ok(actual.default.indexOf('configurationService.folder.testSetting') !== -1);
				assert.deepEqual(actual.user, ['configurationService.folder.testSetting']);
				assert.deepEqual(actual.workspace, []);
				assert.deepEqual(actual.workspaceFolder, []);

				fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue" }');

				return testObject.reloadConfiguration()
					.then(() => {
						actual = testObject.keys();
						assert.ok(actual.default.indexOf('configurationService.folder.testSetting') !== -1);
						assert.deepEqual(actual.user, ['configurationService.folder.testSetting']);
						assert.deepEqual(actual.workspace, ['configurationService.folder.testSetting']);
						assert.deepEqual(actual.workspaceFolder, []);
					});
S
Sandeep Somavarapu 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
			});
	});

	test('update user configuration', () => {
		return testObject.updateValue('configurationService.folder.testSetting', 'value', ConfigurationTarget.USER)
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'value'));
	});

	test('update workspace configuration', () => {
		return testObject.updateValue('tasks.service.testSetting', 'value', ConfigurationTarget.WORKSPACE)
			.then(() => assert.equal(testObject.getValue('tasks.service.testSetting'), 'value'));
	});

	test('update tasks configuration', () => {
		return testObject.updateValue('tasks', { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }, ConfigurationTarget.WORKSPACE)
			.then(() => assert.deepEqual(testObject.getValue('tasks'), { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }));
	});

	test('update user configuration should trigger change event before promise is resolve', () => {
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('configurationService.folder.testSetting', 'value', ConfigurationTarget.USER)
			.then(() => assert.ok(target.called));
	});

	test('update workspace configuration should trigger change event before promise is resolve', () => {
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('configurationService.folder.testSetting', 'value', ConfigurationTarget.WORKSPACE)
			.then(() => assert.ok(target.called));
	});

	test('update task configuration should trigger change event before promise is resolve', () => {
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('tasks', { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }, ConfigurationTarget.WORKSPACE)
			.then(() => assert.ok(target.called));
	});

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	test('initialize with different folder triggers configuration event if there are changes', () => {
		return setUpFolderWorkspace(`testWorkspace${uuid.generateUuid()}`)
			.then(({ folderDir }) => {
				const target = sinon.spy();
				testObject.onDidChangeConfiguration(target);

				fs.writeFileSync(path.join(folderDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue2" }');
				return (<WorkspaceService>testObject).initialize(folderDir)
					.then(() => {
						assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'workspaceValue2');
						assert.ok(target.called);
					});
			});
	});

	test('initialize with different folder triggers configuration event if there are no changes', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "workspaceValue2" }');
		return testObject.reloadConfiguration()
			.then(() => setUpFolderWorkspace(`testWorkspace${uuid.generateUuid()}`))
			.then(({ folderDir }) => {
				const target = sinon.spy();
				testObject.onDidChangeConfiguration(() => target());
				fs.writeFileSync(path.join(folderDir, '.vscode', 'settings.json'), '{ "configurationService.folder.testSetting": "workspaceValue2" }');
				return (<WorkspaceService>testObject).initialize(folderDir)
					.then(() => {
						assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'workspaceValue2');
						assert.ok(!target.called);
					});
			});
	});
S
Sandeep Somavarapu 已提交
551 552 553 554
});

suite('WorkspaceConfigurationService - Update (Multiroot)', () => {

S
Sandeep Somavarapu 已提交
555
	let parentResource: string, workspaceContextService: IWorkspaceContextService, jsonEditingServce: IJSONEditingService, testObject: IConfigurationService;
S
Sandeep Somavarapu 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

	suiteSetup(() => {
		const configurationRegistry = <IConfigurationRegistry>Registry.as(ConfigurationExtensions.Configuration);
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
				'configurationService.workspace.testSetting': {
					'type': 'string',
					'default': 'isSet'
				},
				'configurationService.workspace.testResourceSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
				}
			}
		});
	});

	setup(() => {
		return setUpWorkspace(['1', '2'])
578
			.then(({ parentDir, configPath }) => {
S
Sandeep Somavarapu 已提交
579 580

				parentResource = parentDir;
581 582 583

				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
				const workspaceService = new WorkspaceService(environmentService, null);
S
Sandeep Somavarapu 已提交
584 585 586 587 588

				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);
589 590 591 592 593 594 595 596 597

				return workspaceService.initialize({ id: configPath, configPath }).then(() => {

					instantiationService.stub(IFileService, new FileService(<IWorkspaceContextService>workspaceService, new TestTextResourceConfigurationService(), workspaceService, { disableWatcher: true }));
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
					workspaceService.setInstantiationService(instantiationService);

					workspaceContextService = workspaceService;
S
Sandeep Somavarapu 已提交
598
					jsonEditingServce = instantiationService.createInstance(JSONEditingService);
599 600
					testObject = workspaceService;
				});
S
Sandeep Somavarapu 已提交
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
			});
	});

	teardown(done => {
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
			extfs.del(parentResource, os.tmpdir(), () => { }, done);
		}
	});

	test('update user configuration', () => {
		return testObject.updateValue('configurationService.workspace.testSetting', 'userValue', ConfigurationTarget.USER)
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.testSetting'), 'userValue'));
	});

	test('update user configuration should trigger change event before promise is resolve', () => {
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('configurationService.workspace.testSetting', 'userValue', ConfigurationTarget.USER)
			.then(() => assert.ok(target.called));
	});

	test('update workspace configuration', () => {
		return testObject.updateValue('configurationService.workspace.testSetting', 'workspaceValue', ConfigurationTarget.WORKSPACE)
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.testSetting'), 'workspaceValue'));
	});

	test('update workspace configuration should trigger change event before promise is resolve', () => {
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('configurationService.workspace.testSetting', 'workspaceValue', ConfigurationTarget.WORKSPACE)
			.then(() => assert.ok(target.called));
	});

	test('update workspace folder configuration', () => {
638
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
639 640 641 642 643
		return testObject.updateValue('configurationService.workspace.testResourceSetting', 'workspaceFolderValue', { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE_FOLDER)
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.testResourceSetting', { resource: workspace.folders[0].uri }), 'workspaceFolderValue'));
	});

	test('update workspace folder configuration should trigger change event before promise is resolve', () => {
644
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
645 646 647 648 649 650
		const target = sinon.spy();
		testObject.onDidChangeConfiguration(target);
		return testObject.updateValue('configurationService.workspace.testResourceSetting', 'workspaceFolderValue', { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE_FOLDER)
			.then(() => assert.ok(target.called));
	});

S
Sandeep Somavarapu 已提交
651
	test('update tasks configuration in a folder', () => {
652
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
653 654 655
		return testObject.updateValue('tasks', { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }, { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE_FOLDER)
			.then(() => assert.deepEqual(testObject.getValue('tasks', { resource: workspace.folders[0].uri }), { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }));
	});
S
Sandeep Somavarapu 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669

	test('update tasks configuration in a workspace is not supported', () => {
		const workspace = workspaceContextService.getWorkspace();
		return testObject.updateValue('tasks', { 'version': '1.0.0', tasks: [{ 'taskName': 'myTask' }] }, { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE, true)
			.then(() => assert.fail('Should not be supported'), (e) => assert.equal(e.code, ConfigurationEditingErrorCode.ERROR_INVALID_WORKSPACE_TARGET));
	});

	test('update launch configuration in a workspace is not supported', () => {
		const workspace = workspaceContextService.getWorkspace();
		return testObject.updateValue('launch', { 'version': '1.0.0', configurations: [{ 'name': 'myLaunch' }] }, { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE, true)
			.then(() => assert.fail('Should not be supported'), (e) => assert.equal(e.code, ConfigurationEditingErrorCode.ERROR_INVALID_WORKSPACE_TARGET));
	});

	test('task configurations are not read from workspace', () => {
S
Sandeep Somavarapu 已提交
670
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'tasks', value: { 'version': '1.0' } }, true)
S
Sandeep Somavarapu 已提交
671 672
			.then(() => testObject.reloadConfiguration())
			.then(() => {
S
Sandeep Somavarapu 已提交
673
				const actual = testObject.inspect('tasks.version');
S
Sandeep Somavarapu 已提交
674 675 676 677 678
				assert.equal(actual.workspace, void 0);
			});
	});

	test('launch configurations are not read from workspace', () => {
S
Sandeep Somavarapu 已提交
679
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'launch', value: { 'version': '1.0' } }, true)
S
Sandeep Somavarapu 已提交
680 681
			.then(() => testObject.reloadConfiguration())
			.then(() => {
S
Sandeep Somavarapu 已提交
682
				const actual = testObject.inspect('launch.version');
S
Sandeep Somavarapu 已提交
683 684 685
				assert.equal(actual.workspace, void 0);
			});
	});
S
Sandeep Somavarapu 已提交
686
});