configurationService.test.ts 56.1 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 * as pfs from 'vs/base/node/pfs';
import * as uuid from '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
#47154  
Sandeep Somavarapu 已提交
24
import { 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
import { ConfigurationTarget, IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
27 28
import { workbenchInstantiationService, TestTextResourceConfigurationService, TestTextFileService, TestLifecycleService, TestEnvironmentService, TestStorageService } from 'vs/workbench/test/workbenchTestServices';
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
29
import { FileService } from 'vs/workbench/services/files/electron-browser/fileService';
S
Sandeep Somavarapu 已提交
30 31 32 33
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 已提交
34 35
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
import { JSONEditingService } from 'vs/workbench/services/configuration/node/jsonEditingService';
S
Sandeep Somavarapu 已提交
36
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
S
Sandeep Somavarapu 已提交
37
import { IWindowConfiguration } from 'vs/platform/windows/common/windows';
38 39 40 41 42 43 44 45 46 47

class SettingsTestEnvironmentService extends EnvironmentService {

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

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

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

S
Sandeep Somavarapu 已提交
54 55 56
function setUpFolder(folderName: string, parentDir: string): TPromise<string> {
	const folderDir = path.join(parentDir, folderName);
	const workspaceSettingsDir = path.join(folderDir, '.vscode');
57
	return pfs.mkdirp(workspaceSettingsDir, 493).then(() => folderDir);
S
Sandeep Somavarapu 已提交
58 59
}

60
function setUpWorkspace(folders: string[]): TPromise<{ parentDir: string, configPath: string }> {
S
Sandeep Somavarapu 已提交
61 62 63 64

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

65
	return pfs.mkdirp(parentDir, 493)
S
Sandeep Somavarapu 已提交
66 67 68 69 70 71
		.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)))
72
				.then(() => ({ parentDir, configPath }));
S
Sandeep Somavarapu 已提交
73
		});
74

S
Sandeep Somavarapu 已提交
75 76
}

77

78 79
suite('WorkspaceContextService - Folder', () => {

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

	setup(() => {
S
Sandeep Somavarapu 已提交
83
		return setUpFolderWorkspace(workspaceName)
84
			.then(({ parentDir, folderDir }) => {
85
				parentResource = parentDir;
S
Sandeep Somavarapu 已提交
86
				workspaceResource = folderDir;
87 88
				const globalSettingsFile = path.join(parentDir, 'settings.json');
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
S
Sandeep Somavarapu 已提交
89
				workspaceContextService = new WorkspaceService(environmentService);
90
				return (<WorkspaceService>workspaceContextService).initialize(URI.file(folderDir));
91 92 93
			});
	});

94
	teardown(() => {
95 96 97 98
		if (workspaceContextService) {
			(<WorkspaceService>workspaceContextService).dispose();
		}
		if (parentResource) {
99
			return pfs.del(parentResource, os.tmpdir());
100
		}
101
		return void 0;
102 103 104 105 106 107
	});

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

		assert.equal(actual.folders.length, 1);
S
Sandeep Somavarapu 已提交
108
		assert.equal(actual.folders[0].uri.fsPath, URI.file(workspaceResource).fsPath);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		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', () => {
127
		assert.ok(workspaceContextService.isCurrentWorkspace(URI.file(workspaceResource)));
128 129 130
	});

	test('isCurrentWorkspace() => false', () => {
131
		assert.ok(!workspaceContextService.isCurrentWorkspace(URI.file(workspaceResource + 'abc')));
132 133 134
	});
});

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

137
	let parentResource: string, testObject: WorkspaceService;
S
Sandeep Somavarapu 已提交
138 139 140 141 142 143 144 145

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

				parentResource = parentDir;

				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
S
Sandeep Somavarapu 已提交
146
				const workspaceService = new WorkspaceService(environmentService);
S
Sandeep Somavarapu 已提交
147 148 149 150 151 152 153 154

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

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

S
Sandeep Somavarapu 已提交
155 156
					const fileService = new FileService(<IWorkspaceContextService>workspaceService, TestEnvironmentService, new TestTextResourceConfigurationService(), workspaceService, new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
					instantiationService.stub(IFileService, fileService);
S
Sandeep Somavarapu 已提交
157 158
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
159
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
160
					workspaceService.acquireFileService(fileService);
S
Sandeep Somavarapu 已提交
161 162 163 164 165 166

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

167
	teardown(() => {
S
Sandeep Somavarapu 已提交
168 169 170 171
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
172
			return pfs.del(parentResource, os.tmpdir());
S
Sandeep Somavarapu 已提交
173
		}
174
		return void 0;
S
Sandeep Somavarapu 已提交
175 176 177 178 179 180 181 182 183 184 185 186
	});

	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);
187
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }])
S
Sandeep Somavarapu 已提交
188 189 190 191 192 193
			.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');
B
Benjamin Pasero 已提交
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
				assert.equal(path.basename(actual[2].uri.fsPath), 'd');
				assert.equal(path.basename(actual[3].uri.fsPath), 'c');
			});
	});

	test('add folders (at specific index)', () => {
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }], 0)
			.then(() => {
				const actual = testObject.getWorkspace().folders;

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

	test('add folders (at specific wrong index)', () => {
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }], 10)
			.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');
S
Sandeep Somavarapu 已提交
222 223 224 225 226
				assert.equal(path.basename(actual[2].uri.fsPath), 'd');
				assert.equal(path.basename(actual[3].uri.fsPath), 'c');
			});
	});

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	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 已提交
243 244 245 246
	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 已提交
247 248 249
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }];
		return testObject.addFolders(addedFolders)
			.then(() => {
S
Sandeep Somavarapu 已提交
250
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
S
Sandeep Somavarapu 已提交
251 252 253 254 255
				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 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	});

	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 已提交
270 271 272 273 274 275 276 277 278 279 280
		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()]);
			});
	});

S
Sandeep Somavarapu 已提交
281 282
	test('remove folders and add them back by writing into the file', done => {
		const folders = testObject.getWorkspace().folders;
283
		testObject.removeFolders([folders[0].uri])
S
Sandeep Somavarapu 已提交
284 285 286 287 288 289 290
			.then(() => {
				testObject.onDidChangeWorkspaceFolders(actual => {
					assert.deepEqual(actual.added.map(r => r.uri.toString()), [folders[0].uri.toString()]);
					done();
				});
				const workspace = { folders: [{ path: folders[0].uri.fsPath }, { path: folders[1].uri.fsPath }] };
				fs.writeFileSync(testObject.getWorkspace().configuration.fsPath, JSON.stringify(workspace, null, '\t'));
291
			}, done);
S
Sandeep Somavarapu 已提交
292 293
	});

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	test('update folders (remove last and add to end)', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }];
		const removedFolders = [testObject.getWorkspace().folders[1]].map(f => f.uri);
		return testObject.updateFolders(addedFolders, removedFolders)
			.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.map(r => r.uri.toString()), removedFolders.map(a => a.toString()));
				assert.deepEqual(actual.changed, []);
			});
	});

	test('update folders (rename first via add and remove)', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'a')), name: 'The Folder' }];
		const removedFolders = [testObject.getWorkspace().folders[0]].map(f => f.uri);
		return testObject.updateFolders(addedFolders, removedFolders, 0)
			.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(r => r.uri.toString()), removedFolders.map(a => a.toString()));
			});
	});

	test('update folders (remove first and add to end)', () => {
		const target = sinon.spy();
		testObject.onDidChangeWorkspaceFolders(target);
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }];
		const removedFolders = [testObject.getWorkspace().folders[0]].map(f => f.uri);
		const changedFolders = [testObject.getWorkspace().folders[1]].map(f => f.uri);
		return testObject.updateFolders(addedFolders, removedFolders)
			.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.map(r => r.uri.toString()), removedFolders.map(a => a.toString()));
				assert.deepEqual(actual.changed.map(r => r.uri.toString()), changedFolders.map(a => a.toString()));
			});
	});

S
Sandeep Somavarapu 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	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 已提交
371 372 373 374
	});

});

S
Sandeep Somavarapu 已提交
375 376 377
suite('WorkspaceService - Initialization', () => {

	let parentResource: string, workspaceConfigPath: string, testObject: WorkspaceService, globalSettingsFile: string;
378
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
S
Sandeep Somavarapu 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

	suiteSetup(() => {
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
				'initialization.testSetting1': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
				},
				'initialization.testSetting2': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
				}
			}
		});
	});

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

				parentResource = parentDir;
				workspaceConfigPath = configPath;
				globalSettingsFile = path.join(parentDir, 'settings.json');

				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
				const workspaceService = new WorkspaceService(environmentService);
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);

				return workspaceService.initialize(<IWindowConfiguration>{}).then(() => {
S
Sandeep Somavarapu 已提交
415 416
					const fileService = new FileService(<IWorkspaceContextService>workspaceService, TestEnvironmentService, new TestTextResourceConfigurationService(), workspaceService, new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
					instantiationService.stub(IFileService, fileService);
S
Sandeep Somavarapu 已提交
417 418
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
419
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
420
					workspaceService.acquireFileService(fileService);
S
Sandeep Somavarapu 已提交
421 422 423 424 425
					testObject = workspaceService;
				});
			});
	});

426
	teardown(() => {
S
Sandeep Somavarapu 已提交
427 428 429 430
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
431
			return pfs.del(parentResource, os.tmpdir());
S
Sandeep Somavarapu 已提交
432
		}
433
		return void 0;
S
Sandeep Somavarapu 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447
	});

	test('initialize a folder workspace from an empty workspace with no configuration changes', () => {

		fs.writeFileSync(globalSettingsFile, '{ "initialization.testSetting1": "userValue" }');

		return testObject.reloadConfiguration()
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

448
				return testObject.initialize(URI.file(path.join(parentResource, '1')))
S
Sandeep Somavarapu 已提交
449 450 451 452 453
					.then(() => {
						assert.equal(testObject.getValue('initialization.testSetting1'), 'userValue');
						assert.equal(target.callCount, 3);
						assert.deepEqual(target.args[0], [WorkbenchState.FOLDER]);
						assert.deepEqual(target.args[1], [undefined]);
S
Sandeep Somavarapu 已提交
454
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).changed, []);
					});

			});

	});

	test('initialize a folder workspace from an empty workspace with configuration changes', () => {

		fs.writeFileSync(globalSettingsFile, '{ "initialization.testSetting1": "userValue" }');

		return testObject.reloadConfiguration()
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

				fs.writeFileSync(path.join(parentResource, '1', '.vscode', 'settings.json'), '{ "initialization.testSetting1": "workspaceValue" }');

477
				return testObject.initialize(URI.file(path.join(parentResource, '1')))
S
Sandeep Somavarapu 已提交
478 479 480 481 482 483
					.then(() => {
						assert.equal(testObject.getValue('initialization.testSetting1'), 'workspaceValue');
						assert.equal(target.callCount, 4);
						assert.deepEqual((<IConfigurationChangeEvent>target.args[0][0]).affectedKeys, ['initialization.testSetting1']);
						assert.deepEqual(target.args[1], [WorkbenchState.FOLDER]);
						assert.deepEqual(target.args[2], [undefined]);
S
Sandeep Somavarapu 已提交
484
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
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
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).changed, []);
					});

			});

	});

	test('initialize a multi root workspace from an empty workspace with no configuration changes', () => {

		fs.writeFileSync(globalSettingsFile, '{ "initialization.testSetting1": "userValue" }');

		return testObject.reloadConfiguration()
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

				return testObject.initialize({ id: workspaceConfigPath, configPath: workspaceConfigPath })
					.then(() => {
						assert.equal(target.callCount, 3);
						assert.deepEqual(target.args[0], [WorkbenchState.WORKSPACE]);
						assert.deepEqual(target.args[1], [undefined]);
S
Sandeep Somavarapu 已提交
510
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath, URI.file(path.join(parentResource, '2')).fsPath]);
S
Sandeep Somavarapu 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).changed, []);
					});

			});

	});

	test('initialize a multi root workspace from an empty workspace with configuration changes', () => {

		fs.writeFileSync(globalSettingsFile, '{ "initialization.testSetting1": "userValue" }');

		return testObject.reloadConfiguration()
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

				fs.writeFileSync(path.join(parentResource, '1', '.vscode', 'settings.json'), '{ "initialization.testSetting1": "workspaceValue1" }');
				fs.writeFileSync(path.join(parentResource, '2', '.vscode', 'settings.json'), '{ "initialization.testSetting2": "workspaceValue2" }');

				return testObject.initialize({ id: workspaceConfigPath, configPath: workspaceConfigPath })
					.then(() => {
						assert.equal(target.callCount, 4);
						assert.deepEqual((<IConfigurationChangeEvent>target.args[0][0]).affectedKeys, ['initialization.testSetting1', 'initialization.testSetting2']);
						assert.deepEqual(target.args[1], [WorkbenchState.WORKSPACE]);
						assert.deepEqual(target.args[2], [undefined]);
S
Sandeep Somavarapu 已提交
540
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath, URI.file(path.join(parentResource, '2')).fsPath]);
S
Sandeep Somavarapu 已提交
541 542 543 544 545 546 547 548 549 550
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).changed, []);
					});

			});

	});

	test('initialize a folder workspace from a folder workspace with no configuration changes', () => {

551
		return testObject.initialize(URI.file(path.join(parentResource, '1')))
S
Sandeep Somavarapu 已提交
552 553 554 555 556 557 558 559 560 561 562
			.then(() => {
				fs.writeFileSync(globalSettingsFile, '{ "initialization.testSetting1": "userValue" }');

				return testObject.reloadConfiguration()
					.then(() => {
						const target = sinon.spy();
						testObject.onDidChangeWorkbenchState(target);
						testObject.onDidChangeWorkspaceName(target);
						testObject.onDidChangeWorkspaceFolders(target);
						testObject.onDidChangeConfiguration(target);

563
						return testObject.initialize(URI.file(path.join(parentResource, '2')))
S
Sandeep Somavarapu 已提交
564 565 566
							.then(() => {
								assert.equal(testObject.getValue('initialization.testSetting1'), 'userValue');
								assert.equal(target.callCount, 1);
S
Sandeep Somavarapu 已提交
567 568
								assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[0][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '2')).fsPath]);
								assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[0][0]).removed.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
569 570 571 572 573 574 575 576 577 578
								assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[0][0]).changed, []);
							});

					});
			});

	});

	test('initialize a folder workspace from a folder workspace with configuration changes', () => {

579
		return testObject.initialize(URI.file(path.join(parentResource, '1')))
S
Sandeep Somavarapu 已提交
580 581 582 583 584 585 586 587 588
			.then(() => {

				const target = sinon.spy();
				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

				fs.writeFileSync(path.join(parentResource, '2', '.vscode', 'settings.json'), '{ "initialization.testSetting1": "workspaceValue2" }');
589
				return testObject.initialize(URI.file(path.join(parentResource, '2')))
S
Sandeep Somavarapu 已提交
590 591 592 593
					.then(() => {
						assert.equal(testObject.getValue('initialization.testSetting1'), 'workspaceValue2');
						assert.equal(target.callCount, 2);
						assert.deepEqual((<IConfigurationChangeEvent>target.args[0][0]).affectedKeys, ['initialization.testSetting1']);
S
Sandeep Somavarapu 已提交
594 595
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[1][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '2')).fsPath]);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[1][0]).removed.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
596 597 598 599 600 601 602 603
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[1][0]).changed, []);
					});
			});

	});

	test('initialize a multi folder workspace from a folder workspacce triggers change events in the right order', () => {
		const folderDir = path.join(parentResource, '1');
604
		return testObject.initialize(URI.file(folderDir))
S
Sandeep Somavarapu 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
			.then(() => {

				const target = sinon.spy();

				testObject.onDidChangeWorkbenchState(target);
				testObject.onDidChangeWorkspaceName(target);
				testObject.onDidChangeWorkspaceFolders(target);
				testObject.onDidChangeConfiguration(target);

				fs.writeFileSync(path.join(parentResource, '1', '.vscode', 'settings.json'), '{ "initialization.testSetting1": "workspaceValue2" }');
				return testObject.initialize({ id: workspaceConfigPath, configPath: workspaceConfigPath })
					.then(() => {
						assert.equal(target.callCount, 4);
						assert.deepEqual((<IConfigurationChangeEvent>target.args[0][0]).affectedKeys, ['initialization.testSetting1']);
						assert.deepEqual(target.args[1], [WorkbenchState.WORKSPACE]);
						assert.deepEqual(target.args[2], [undefined]);
S
Sandeep Somavarapu 已提交
621
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '2')).fsPath]);
S
Sandeep Somavarapu 已提交
622 623 624 625 626 627 628 629
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).changed, []);
					});
			});
	});

});

630
suite('WorkspaceConfigurationService - Folder', () => {
631

S
Sandeep Somavarapu 已提交
632
	let workspaceName = `testWorkspace${uuid.generateUuid()}`, parentResource: string, workspaceDir: string, testObject: IWorkspaceConfigurationService, globalSettingsFile: string;
633
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
634

635
	suiteSetup(() => {
B
Benjamin Pasero 已提交
636 637 638 639
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
S
Sandeep Somavarapu 已提交
640 641 642 643 644
				'configurationService.folder.applicationSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.APPLICATION
				},
645
				'configurationService.folder.testSetting': {
B
Benjamin Pasero 已提交
646
					'type': 'string',
S
Sandeep Somavarapu 已提交
647 648
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
S
Sandeep Somavarapu 已提交
649
				}
B
Benjamin Pasero 已提交
650 651
			}
		});
652 653
	});

654 655 656
	setup(() => {
		return setUpFolderWorkspace(workspaceName)
			.then(({ parentDir, folderDir }) => {
657

658 659 660
				parentResource = parentDir;
				workspaceDir = folderDir;
				globalSettingsFile = path.join(parentDir, 'settings.json');
661

662 663
				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
S
Sandeep Somavarapu 已提交
664
				const workspaceService = new WorkspaceService(environmentService);
665 666 667
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);
668

669
				return workspaceService.initialize(URI.file(folderDir)).then(() => {
S
Sandeep Somavarapu 已提交
670 671
					const fileService = new FileService(<IWorkspaceContextService>workspaceService, TestEnvironmentService, new TestTextResourceConfigurationService(), workspaceService, new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
					instantiationService.stub(IFileService, fileService);
672 673
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
674
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
675
					workspaceService.acquireFileService(fileService);
676
					testObject = workspaceService;
677 678 679 680
				});
			});
	});

681
	teardown(() => {
682 683 684 685
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
686
			return pfs.del(parentResource, os.tmpdir());
687
		}
688
		return void 0;
689 690
	});

691
	test('defaults', () => {
S
Sandeep Somavarapu 已提交
692
		assert.deepEqual(testObject.getValue('configurationService'), { 'folder': { 'applicationSetting': 'isSet', 'testSetting': 'isSet' } });
693 694
	});

695 696 697 698
	test('globals override defaults', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'userValue'));
699 700
	});

701 702 703 704
	test('globals', () => {
		fs.writeFileSync(globalSettingsFile, '{ "testworkbench.editor.tabs": true }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('testworkbench.editor.tabs'), true));
705
	});
B
Benjamin Pasero 已提交
706

707 708 709 710
	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 已提交
711 712
	});

713 714 715 716 717
	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 已提交
718 719
	});

S
Sandeep Somavarapu 已提交
720 721 722 723
	test('workspace settings override user settings after defaults are registered ', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.newSetting": "userValue" }');
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.newSetting": "workspaceValue" }');
		return testObject.reloadConfiguration()
S
Sandeep Somavarapu 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
			.then(() => {

				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
						'configurationService.folder.newSetting': {
							'type': 'string',
							'default': 'isSet'
						}
					}
				});

				assert.equal(testObject.getValue('configurationService.folder.newSetting'), 'workspaceValue');
			});
S
Sandeep Somavarapu 已提交
739 740
	});

S
Sandeep Somavarapu 已提交
741 742 743 744 745 746 747
	test('application settings are not read from workspace', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.applicationSetting": "userValue" }');
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.applicationSetting": "workspaceValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.folder.applicationSetting'), 'userValue'));
	});

S
Sandeep Somavarapu 已提交
748 749
	test('get application scope settings are not loaded after defaults are registered', () => {
		fs.writeFileSync(path.join(workspaceDir, '.vscode', 'settings.json'), '{ "configurationService.folder.anotherApplicationSetting": "workspaceValue" }');
S
Sandeep Somavarapu 已提交
750 751 752 753 754 755
		return testObject.reloadConfiguration()
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
S
Sandeep Somavarapu 已提交
756
						'configurationService.folder.anotherApplicationSetting': {
S
Sandeep Somavarapu 已提交
757 758
							'type': 'string',
							'default': 'isSet',
S
Sandeep Somavarapu 已提交
759
							scope: ConfigurationScope.APPLICATION
S
Sandeep Somavarapu 已提交
760 761 762
						}
					}
				});
S
Sandeep Somavarapu 已提交
763
				assert.deepEqual(testObject.keys().workspace, []);
S
Sandeep Somavarapu 已提交
764 765 766
			});
	});

767 768 769 770 771
	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 已提交
772
	});
B
Benjamin Pasero 已提交
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	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 已提交
790 791
			});
	});
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	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');
828 829
					});
			});
S
Sandeep Somavarapu 已提交
830 831
	});

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	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 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870
			});
	});

	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'));
	});

S
Sandeep Somavarapu 已提交
871 872 873 874 875
	test('update application setting into workspace configuration in a workspace is not supported', () => {
		return testObject.updateValue('configurationService.folder.applicationSetting', 'workspaceValue', {}, ConfigurationTarget.WORKSPACE, true)
			.then(() => assert.fail('Should not be supported'), (e) => assert.equal(e.code, ConfigurationEditingErrorCode.ERROR_INVALID_WORKSPACE_CONFIGURATION_APPLICATION));
	});

S
Sandeep Somavarapu 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	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));
	});

});

S
Sandeep Somavarapu 已提交
904
suite('WorkspaceConfigurationService-Multiroot', () => {
S
Sandeep Somavarapu 已提交
905

S
Sandeep Somavarapu 已提交
906
	let parentResource: string, workspaceContextService: IWorkspaceContextService, environmentService: IEnvironmentService, jsonEditingServce: IJSONEditingService, testObject: IWorkspaceConfigurationService;
907
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
S
Sandeep Somavarapu 已提交
908 909 910 911 912 913 914 915 916 917

	suiteSetup(() => {
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
				'configurationService.workspace.testSetting': {
					'type': 'string',
					'default': 'isSet'
				},
S
Sandeep Somavarapu 已提交
918 919 920 921 922
				'configurationService.workspace.applicationSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.APPLICATION
				},
S
Sandeep Somavarapu 已提交
923 924 925 926 927 928 929 930 931 932 933
				'configurationService.workspace.testResourceSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
				}
			}
		});
	});

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

				parentResource = parentDir;
937

S
Sandeep Somavarapu 已提交
938
				environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
S
Sandeep Somavarapu 已提交
939
				const workspaceService = new WorkspaceService(environmentService);
S
Sandeep Somavarapu 已提交
940 941 942 943 944

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

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

S
Sandeep Somavarapu 已提交
948 949
					const fileService = new FileService(<IWorkspaceContextService>workspaceService, TestEnvironmentService, new TestTextResourceConfigurationService(), workspaceService, new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
					instantiationService.stub(IFileService, fileService);
950 951
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
952
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
953
					workspaceService.acquireFileService(fileService);
954 955

					workspaceContextService = workspaceService;
S
Sandeep Somavarapu 已提交
956
					jsonEditingServce = instantiationService.createInstance(JSONEditingService);
957 958
					testObject = workspaceService;
				});
S
Sandeep Somavarapu 已提交
959 960 961
			});
	});

962
	teardown(() => {
S
Sandeep Somavarapu 已提交
963 964 965 966
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
967
			return pfs.del(parentResource, os.tmpdir());
S
Sandeep Somavarapu 已提交
968
		}
969
		return void 0;
S
Sandeep Somavarapu 已提交
970 971
	});

S
Sandeep Somavarapu 已提交
972 973 974 975 976 977 978
	test('application settings are not read from workspace', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.applicationSetting": "userValue" }');
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'settings', value: { 'configurationService.workspace.applicationSetting': 'workspaceValue' } }, true)
			.then(() => testObject.reloadConfiguration())
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.applicationSetting'), 'userValue'));
	});

S
Sandeep Somavarapu 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	test('workspace settings override user settings after defaults are registered ', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.newSetting": "userValue" }');
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'settings', value: { 'configurationService.workspace.newSetting': 'workspaceValue' } }, true)
			.then(() => testObject.reloadConfiguration())
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
						'configurationService.workspace.newSetting': {
							'type': 'string',
							'default': 'isSet'
						}
					}
				});
				assert.equal(testObject.getValue('configurationService.workspace.newSetting'), 'workspaceValue');
			});
	});

S
Sandeep Somavarapu 已提交
998 999 1000 1001 1002 1003 1004
	test('application settings are not read from workspace folder', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.applicationSetting": "userValue" }');
		fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.applicationSetting": "workspaceFolderValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.applicationSetting'), 'userValue'));
	});

S
Sandeep Somavarapu 已提交
1005 1006 1007
	test('application settings are not read from workspace folder after defaults are registered', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.testNewApplicationSetting": "userValue" }');
		fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.testNewApplicationSetting": "workspaceFolderValue" }');
S
Sandeep Somavarapu 已提交
1008 1009 1010 1011 1012 1013
		return testObject.reloadConfiguration()
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
S
Sandeep Somavarapu 已提交
1014
						'configurationService.workspace.testNewApplicationSetting': {
S
Sandeep Somavarapu 已提交
1015 1016
							'type': 'string',
							'default': 'isSet',
S
Sandeep Somavarapu 已提交
1017
							scope: ConfigurationScope.APPLICATION
S
Sandeep Somavarapu 已提交
1018 1019 1020
						}
					}
				});
S
Sandeep Somavarapu 已提交
1021
				assert.equal(testObject.getValue('configurationService.workspace.testNewApplicationSetting', { resource: workspaceContextService.getWorkspace().folders[0].uri }), 'userValue');
S
Sandeep Somavarapu 已提交
1022 1023 1024
			});
	});

S
Sandeep Somavarapu 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	test('resource setting in folder is read after it is registered later', () => {
		fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.testNewResourceSetting2": "workspaceFolderValue" }');
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'settings', value: { 'configurationService.workspace.testNewResourceSetting2': 'workspaceValue' } }, true)
			.then(() => testObject.reloadConfiguration())
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
						'configurationService.workspace.testNewResourceSetting2': {
							'type': 'string',
							'default': 'isSet',
							scope: ConfigurationScope.RESOURCE
						}
					}
				});
				assert.equal(testObject.getValue('configurationService.workspace.testNewResourceSetting2', { resource: workspaceContextService.getWorkspace().folders[0].uri }), 'workspaceFolderValue');
			});
	});

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	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.workspace.testResourceSetting');
		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(environmentService.appSettingsPath, '{ "configurationService.workspace.testResourceSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => {
				actual = testObject.inspect('configurationService.workspace.testResourceSetting');
				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');

				return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'settings', value: { 'configurationService.workspace.testResourceSetting': 'workspaceValue' } }, true)
					.then(() => testObject.reloadConfiguration())
					.then(() => {
						actual = testObject.inspect('configurationService.workspace.testResourceSetting');
						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');

						fs.writeFileSync(workspaceContextService.getWorkspace().folders[0].toResource('.vscode/settings.json').fsPath, '{ "configurationService.workspace.testResourceSetting": "workspaceFolderValue" }');

						return testObject.reloadConfiguration()
							.then(() => {
								actual = testObject.inspect('configurationService.workspace.testResourceSetting', { resource: workspaceContextService.getWorkspace().folders[0].uri });
								assert.equal(actual.default, 'isSet');
								assert.equal(actual.user, 'userValue');
								assert.equal(actual.workspace, 'workspaceValue');
								assert.equal(actual.workspaceFolder, 'workspaceFolderValue');
								assert.equal(actual.value, 'workspaceFolderValue');
							});
					});
			});
	});
S
Sandeep Somavarapu 已提交
1094

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	test('get launch configuration', () => {
		const expectedLaunchConfiguration = {
			'version': '0.1.0',
			'configurations': [
				{
					'type': 'node',
					'request': 'launch',
					'name': 'Gulp Build',
					'program': '${workspaceFolder}/node_modules/gulp/bin/gulp.js',
					'stopOnEntry': true,
					'args': [
						'watch-extension:json-client'
					],
					'cwd': '${workspaceFolder}'
				}
			]
		};
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'launch', value: expectedLaunchConfiguration }, true)
			.then(() => testObject.reloadConfiguration())
			.then(() => {
				const actual = testObject.getValue('launch');
				assert.deepEqual(actual, expectedLaunchConfiguration);
			});
	});

	test('inspect launch configuration', () => {
		const expectedLaunchConfiguration = {
			'version': '0.1.0',
			'configurations': [
				{
					'type': 'node',
					'request': 'launch',
					'name': 'Gulp Build',
					'program': '${workspaceFolder}/node_modules/gulp/bin/gulp.js',
					'stopOnEntry': true,
					'args': [
						'watch-extension:json-client'
					],
					'cwd': '${workspaceFolder}'
				}
			]
		};
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'launch', value: expectedLaunchConfiguration }, true)
			.then(() => testObject.reloadConfiguration())
			.then(() => {
				const actual = testObject.inspect('launch').workspace;
				assert.deepEqual(actual, expectedLaunchConfiguration);
			});
	});

S
Sandeep Somavarapu 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	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));
	});

S
Sandeep Somavarapu 已提交
1169 1170 1171 1172 1173
	test('update application setting into workspace configuration in a workspace is not supported', () => {
		return testObject.updateValue('configurationService.workspace.applicationSetting', 'workspaceValue', {}, ConfigurationTarget.WORKSPACE, true)
			.then(() => assert.fail('Should not be supported'), (e) => assert.equal(e.code, ConfigurationEditingErrorCode.ERROR_INVALID_WORKSPACE_CONFIGURATION_APPLICATION));
	});

S
Sandeep Somavarapu 已提交
1174
	test('update workspace folder configuration', () => {
1175
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1176 1177 1178 1179 1180
		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', () => {
1181
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1182 1183 1184 1185 1186 1187
		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 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	test('update workspace folder configuration second time should trigger change event before promise is resolve', () => {
		const workspace = workspaceContextService.getWorkspace();
		return testObject.updateValue('configurationService.workspace.testResourceSetting', 'workspaceFolderValue', { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE_FOLDER)
			.then(() => {
				const target = sinon.spy();
				testObject.onDidChangeConfiguration(target);
				return testObject.updateValue('configurationService.workspace.testResourceSetting', 'workspaceFolderValue2', { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE_FOLDER)
					.then(() => assert.ok(target.called));
			});
	});

S
Sandeep Somavarapu 已提交
1199
	test('update tasks configuration in a folder', () => {
1200
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1201 1202 1203
		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 已提交
1204 1205 1206 1207 1208 1209 1210

	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));
	});

1211
	test('update launch configuration in a workspace', () => {
S
Sandeep Somavarapu 已提交
1212 1213
		const workspace = workspaceContextService.getWorkspace();
		return testObject.updateValue('launch', { 'version': '1.0.0', configurations: [{ 'name': 'myLaunch' }] }, { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE, true)
1214
			.then(() => assert.deepEqual(testObject.getValue('launch'), { 'version': '1.0.0', configurations: [{ 'name': 'myLaunch' }] }));
S
Sandeep Somavarapu 已提交
1215 1216 1217
	});

	test('task configurations are not read from workspace', () => {
S
Sandeep Somavarapu 已提交
1218
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration, { key: 'tasks', value: { 'version': '1.0' } }, true)
S
Sandeep Somavarapu 已提交
1219 1220
			.then(() => testObject.reloadConfiguration())
			.then(() => {
S
Sandeep Somavarapu 已提交
1221
				const actual = testObject.inspect('tasks.version');
S
Sandeep Somavarapu 已提交
1222 1223 1224
				assert.equal(actual.workspace, void 0);
			});
	});
S
Sandeep Somavarapu 已提交
1225
});