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

S
Sandeep Somavarapu 已提交
6
import * as assert from 'assert';
S
Sandeep Somavarapu 已提交
7
import * as sinon from 'sinon';
S
Sandeep Somavarapu 已提交
8
import * as fs from 'fs';
9
import * as path from 'vs/base/common/path';
S
Sandeep Somavarapu 已提交
10
import * as os from 'os';
11
import { URI } from 'vs/base/common/uri';
12
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
13
import { ParsedArgs, IEnvironmentService } from 'vs/platform/environment/common/environment';
J
Johannes Rieken 已提交
14 15
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { parseArgs } from 'vs/platform/environment/node/argv';
16 17
import * as pfs from 'vs/base/node/pfs';
import * as uuid from 'vs/base/common/uuid';
S
Sandeep Somavarapu 已提交
18
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
19 20
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configurationService';
import { ISingleFolderWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces';
21
import { ConfigurationEditingErrorCode } from 'vs/workbench/services/configuration/common/configurationEditingService';
22
import { IFileService, FileChangesEvent, FileChangeType } from 'vs/platform/files/common/files';
S
Sandeep Somavarapu 已提交
23
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFoldersChangeEvent } from 'vs/platform/workspace/common/workspace';
S
Sandeep Somavarapu 已提交
24
import { ConfigurationTarget, IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
25
import { workbenchInstantiationService, TestTextResourceConfigurationService, TestTextFileService, TestEnvironmentService } from 'vs/workbench/test/workbenchTestServices';
26
import { LegacyFileService } from 'vs/workbench/services/files/node/fileService';
S
Sandeep Somavarapu 已提交
27 28 29 30
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 已提交
31
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
32
import { JSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditingService';
33
import { createHash } from 'crypto';
34
import { Emitter } from 'vs/base/common/event';
35
import { Schemas } from 'vs/base/common/network';
36
import { originalFSPath } from 'vs/base/common/resources';
37 38
import { isLinux } from 'vs/base/common/platform';
import { IWorkspaceIdentifier } from 'vs/workbench/services/configuration/node/configuration';
39 40 41 42
import { IWindowConfiguration } from 'vs/platform/windows/common/windows';
import { RemoteAgentService } from 'vs/workbench/services/remote/electron-browser/remoteAgentServiceImpl';
import { RemoteAuthorityResolverService } from 'vs/platform/remote/electron-browser/remoteAuthorityResolverService';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
43 44 45
import { FileService2 } from 'vs/workbench/services/files2/common/fileService2';
import { NullLogService } from 'vs/platform/log/common/log';
import { DiskFileSystemProvider } from 'vs/workbench/services/files2/node/diskFileSystemProvider';
S
Sandeep Somavarapu 已提交
46 47
import { HashService } from 'vs/workbench/services/hash/node/hashService';
import { ConfigurationCache } from 'vs/workbench/services/configuration/node/configurationCache';
48 49 50

class SettingsTestEnvironmentService extends EnvironmentService {

51
	constructor(args: ParsedArgs, _execPath: string, private customAppSettingsHome: string) {
52 53 54 55 56 57
		super(args, _execPath);
	}

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

S
Sandeep Somavarapu 已提交
58
function setUpFolderWorkspace(folderName: string): Promise<{ parentDir: string, folderDir: string }> {
59 60
	const id = uuid.generateUuid();
	const parentDir = path.join(os.tmpdir(), 'vsctests', id);
61
	return setUpFolder(folderName, parentDir).then(folderDir => ({ parentDir, folderDir }));
S
Sandeep Somavarapu 已提交
62
}
63

S
Sandeep Somavarapu 已提交
64
function setUpFolder(folderName: string, parentDir: string): Promise<string> {
S
Sandeep Somavarapu 已提交
65 66
	const folderDir = path.join(parentDir, folderName);
	const workspaceSettingsDir = path.join(folderDir, '.vscode');
67
	return Promise.resolve(pfs.mkdirp(workspaceSettingsDir, 493).then(() => folderDir));
S
Sandeep Somavarapu 已提交
68 69
}

B
Benjamin Pasero 已提交
70
function convertToWorkspacePayload(folder: URI): ISingleFolderWorkspaceInitializationPayload {
71 72 73 74 75 76
	return {
		id: createHash('md5').update(folder.fsPath).digest('hex'),
		folder
	} as ISingleFolderWorkspaceInitializationPayload;
}

77
function setUpWorkspace(folders: string[]): Promise<{ parentDir: string, configPath: URI }> {
S
Sandeep Somavarapu 已提交
78 79 80 81

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

82
	return Promise.resolve(pfs.mkdirp(parentDir, 493)
S
Sandeep Somavarapu 已提交
83 84 85 86 87
		.then(() => {
			const configPath = path.join(parentDir, 'vsctests.code-workspace');
			const workspace = { folders: folders.map(path => ({ path })) };
			fs.writeFileSync(configPath, JSON.stringify(workspace, null, '\t'));

S
Sandeep Somavarapu 已提交
88
			return Promise.all(folders.map(folder => setUpFolder(folder, parentDir)))
89
				.then(() => ({ parentDir, configPath: URI.file(configPath) }));
90
		}));
91

S
Sandeep Somavarapu 已提交
92 93
}

94

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

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

	setup(() => {
S
Sandeep Somavarapu 已提交
100
		return setUpFolderWorkspace(workspaceName)
101
			.then(({ parentDir, folderDir }) => {
102
				parentResource = parentDir;
S
Sandeep Somavarapu 已提交
103
				workspaceResource = folderDir;
104 105
				const globalSettingsFile = path.join(parentDir, 'settings.json');
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
S
Sandeep Somavarapu 已提交
106
				workspaceContextService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), new RemoteAgentService(<IWindowConfiguration>{}, environmentService, new RemoteAuthorityResolverService()));
107
				return (<WorkspaceService>workspaceContextService).initialize(convertToWorkspacePayload(URI.file(folderDir)));
108 109 110
			});
	});

111
	teardown(() => {
112 113 114 115
		if (workspaceContextService) {
			(<WorkspaceService>workspaceContextService).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
116
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
117
		}
R
Rob Lourens 已提交
118
		return undefined;
119 120 121 122 123 124
	});

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

		assert.equal(actual.folders.length, 1);
S
Sandeep Somavarapu 已提交
125
		assert.equal(actual.folders[0].uri.fsPath, URI.file(workspaceResource).fsPath);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
		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', () => {
144
		assert.ok(workspaceContextService.isCurrentWorkspace(URI.file(workspaceResource)));
145 146 147
	});

	test('isCurrentWorkspace() => false', () => {
148
		assert.ok(!workspaceContextService.isCurrentWorkspace(URI.file(workspaceResource + 'abc')));
149
	});
S
Sandeep Somavarapu 已提交
150 151

	test('workspace is complete', () => workspaceContextService.getCompleteWorkspace());
152 153
});

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

S
Sandeep Somavarapu 已提交
156 157 158 159 160 161 162 163
	let parentResource: string, testObject: WorkspaceService, instantiationService: TestInstantiationService;

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

				parentResource = parentDir;

164
				instantiationService = <TestInstantiationService>workbenchInstantiationService();
S
Sandeep Somavarapu 已提交
165
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
166 167
				const remoteAgentService = instantiationService.createInstance(RemoteAgentService, {});
				instantiationService.stub(IRemoteAgentService, remoteAgentService);
S
Sandeep Somavarapu 已提交
168
				const workspaceService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), remoteAgentService);
S
Sandeep Somavarapu 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);

				return workspaceService.initialize(getWorkspaceIdentifier(configPath)).then(() => {
					workspaceService.acquireInstantiationService(instantiationService);
					testObject = workspaceService;
				});
			});
	});

	teardown(() => {
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
186
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
S
Sandeep Somavarapu 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		}
		return undefined;
	});

	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('getWorkbenchState()', () => {
		const actual = testObject.getWorkbenchState();

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


	test('workspace is complete', () => testObject.getCompleteWorkspace());

});

suite('WorkspaceContextService - Workspace Editing', () => {

212
	let parentResource: string, testObject: WorkspaceService, instantiationService: TestInstantiationService, fileChangeEvent: Emitter<FileChangesEvent> = new Emitter<FileChangesEvent>();
S
Sandeep Somavarapu 已提交
213 214 215 216 217 218 219

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

				parentResource = parentDir;

220
				instantiationService = <TestInstantiationService>workbenchInstantiationService();
S
Sandeep Somavarapu 已提交
221
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
222 223
				const remoteAgentService = instantiationService.createInstance(RemoteAgentService, {});
				instantiationService.stub(IRemoteAgentService, remoteAgentService);
S
Sandeep Somavarapu 已提交
224
				const workspaceService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), remoteAgentService);
S
Sandeep Somavarapu 已提交
225 226 227 228 229

				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);

230
				return workspaceService.initialize(getWorkspaceIdentifier(configPath)).then(() => {
231 232
					const fileService = new FileService2(new NullLogService());
					fileService.registerProvider(Schemas.file, new DiskFileSystemProvider(new NullLogService()));
233
					fileService.setLegacyService(new LegacyFileService(
234 235 236
						fileService,
						workspaceService,
						TestEnvironmentService,
237 238
						new TestTextResourceConfigurationService()
					));
S
Sandeep Somavarapu 已提交
239
					instantiationService.stub(IFileService, fileService);
S
Sandeep Somavarapu 已提交
240 241
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
242
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
243
					workspaceService.acquireFileService(fileService);
S
Sandeep Somavarapu 已提交
244 245 246 247 248 249

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

250
	teardown(() => {
S
Sandeep Somavarapu 已提交
251 252 253 254
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
255
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
S
Sandeep Somavarapu 已提交
256
		}
R
Rob Lourens 已提交
257
		return undefined;
S
Sandeep Somavarapu 已提交
258 259 260 261
	});

	test('add folders', () => {
		const workspaceDir = path.dirname(testObject.getWorkspace().folders[0].uri.fsPath);
262
		return testObject.addFolders([{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }])
S
Sandeep Somavarapu 已提交
263 264 265 266 267 268
			.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 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
				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 已提交
297 298 299 300 301
				assert.equal(path.basename(actual[2].uri.fsPath), 'd');
				assert.equal(path.basename(actual[3].uri.fsPath), 'c');
			});
	});

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	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 已提交
318 319 320 321
	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 已提交
322 323 324
		const addedFolders = [{ uri: URI.file(path.join(workspaceDir, 'd')) }, { uri: URI.file(path.join(workspaceDir, 'c')) }];
		return testObject.addFolders(addedFolders)
			.then(() => {
S
Sandeep Somavarapu 已提交
325
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
S
Sandeep Somavarapu 已提交
326 327 328 329 330
				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 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344
	});

	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 已提交
345 346 347
		const removedFolder = testObject.getWorkspace().folders[0];
		return testObject.removeFolders([removedFolder.uri])
			.then(() => {
348
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
S
Sandeep Somavarapu 已提交
349 350 351 352 353 354 355
				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 已提交
356 357
	test('remove folders and add them back by writing into the file', done => {
		const folders = testObject.getWorkspace().folders;
358
		testObject.removeFolders([folders[0].uri])
S
Sandeep Somavarapu 已提交
359 360 361 362 363 364
			.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 }] };
M
Matt Bierner 已提交
365
				instantiationService.get(IFileService).updateContent(testObject.getWorkspace().configuration!, JSON.stringify(workspace, null, '\t'))
366 367 368
					.then(() => {
						fileChangeEvent.fire(new FileChangesEvent([
							{
M
Matt Bierner 已提交
369
								resource: testObject.getWorkspace().configuration!,
370 371 372 373
								type: FileChangeType.UPDATED
							}
						]));
					}, done);
374
			}, done);
S
Sandeep Somavarapu 已提交
375 376
	});

377 378 379 380 381 382 383 384
	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(() => {
385
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
				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(() => {
401
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
				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(() => {
418
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
419 420 421 422 423 424 425
				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 已提交
426 427 428 429
	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 }] };
430
		fs.writeFileSync(testObject.getWorkspace().configuration!.fsPath, JSON.stringify(workspace, null, '\t'));
S
Sandeep Somavarapu 已提交
431 432
		return testObject.reloadConfiguration()
			.then(() => {
433
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
S
Sandeep Somavarapu 已提交
434 435 436 437 438 439 440 441 442 443 444
				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 }] };
445
		fs.writeFileSync(testObject.getWorkspace().configuration!.fsPath, JSON.stringify(workspace, null, '\t'));
S
Sandeep Somavarapu 已提交
446 447
		return testObject.reloadConfiguration()
			.then(() => {
448
				assert.equal(target.callCount, 1, `Should be called only once but called ${target.callCount} times`);
S
Sandeep Somavarapu 已提交
449 450 451 452 453
				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 已提交
454 455 456 457
	});

});

S
Sandeep Somavarapu 已提交
458 459
suite('WorkspaceService - Initialization', () => {

460
	let parentResource: string, workspaceConfigPath: URI, testObject: WorkspaceService, globalSettingsFile: string;
461
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
S
Sandeep Somavarapu 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

	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);
492 493
				const remoteAgentService = instantiationService.createInstance(RemoteAgentService, {});
				instantiationService.stub(IRemoteAgentService, remoteAgentService);
S
Sandeep Somavarapu 已提交
494
				const workspaceService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), remoteAgentService);
S
Sandeep Somavarapu 已提交
495 496 497 498
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);

499
				return workspaceService.initialize({ id: '' }).then(() => {
500 501
					const fileService = new FileService2(new NullLogService());
					fileService.registerProvider(Schemas.file, new DiskFileSystemProvider(new NullLogService()));
502
					fileService.setLegacyService(new LegacyFileService(
503 504 505
						fileService,
						workspaceService,
						TestEnvironmentService,
506 507
						new TestTextResourceConfigurationService()
					));
S
Sandeep Somavarapu 已提交
508
					instantiationService.stub(IFileService, fileService);
S
Sandeep Somavarapu 已提交
509 510
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
511
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
512
					workspaceService.acquireFileService(fileService);
S
Sandeep Somavarapu 已提交
513 514 515 516 517
					testObject = workspaceService;
				});
			});
	});

518
	teardown(() => {
S
Sandeep Somavarapu 已提交
519 520 521 522
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
523
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
S
Sandeep Somavarapu 已提交
524
		}
R
Rob Lourens 已提交
525
		return undefined;
S
Sandeep Somavarapu 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539
	});

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

540
				return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '1'))))
S
Sandeep Somavarapu 已提交
541 542 543 544 545
					.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 已提交
546
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[2][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
						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" }');

569
				return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '1'))))
S
Sandeep Somavarapu 已提交
570 571 572 573 574 575
					.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 已提交
576
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '1')).fsPath]);
S
Sandeep Somavarapu 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
						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);

597
				return testObject.initialize(getWorkspaceIdentifier(workspaceConfigPath))
S
Sandeep Somavarapu 已提交
598 599 600 601
					.then(() => {
						assert.equal(target.callCount, 3);
						assert.deepEqual(target.args[0], [WorkbenchState.WORKSPACE]);
						assert.deepEqual(target.args[1], [undefined]);
S
Sandeep Somavarapu 已提交
602
						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 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
						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" }');

626
				return testObject.initialize(getWorkspaceIdentifier(workspaceConfigPath))
S
Sandeep Somavarapu 已提交
627 628 629 630 631
					.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 已提交
632
						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 已提交
633 634 635 636 637 638 639 640 641 642
						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', () => {

643
		return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '1'))))
S
Sandeep Somavarapu 已提交
644 645 646 647 648 649 650 651 652 653 654
			.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);

655
						return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '2'))))
S
Sandeep Somavarapu 已提交
656 657 658
							.then(() => {
								assert.equal(testObject.getValue('initialization.testSetting1'), 'userValue');
								assert.equal(target.callCount, 1);
S
Sandeep Somavarapu 已提交
659 660
								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 已提交
661 662 663 664 665 666 667 668 669 670
								assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[0][0]).changed, []);
							});

					});
			});

	});

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

671
		return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '1'))))
S
Sandeep Somavarapu 已提交
672 673 674 675 676 677 678 679 680
			.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" }');
681
				return testObject.initialize(convertToWorkspacePayload(URI.file(path.join(parentResource, '2'))))
S
Sandeep Somavarapu 已提交
682 683 684 685
					.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 已提交
686 687
						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 已提交
688 689 690 691 692 693 694 695
						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');
696
		return testObject.initialize(convertToWorkspacePayload(URI.file(folderDir)))
S
Sandeep Somavarapu 已提交
697 698 699 700 701 702 703 704 705 706
			.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" }');
707
				return testObject.initialize(getWorkspaceIdentifier(workspaceConfigPath))
S
Sandeep Somavarapu 已提交
708 709 710 711 712
					.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 已提交
713
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).added.map(folder => folder.uri.fsPath), [URI.file(path.join(parentResource, '2')).fsPath]);
S
Sandeep Somavarapu 已提交
714 715 716 717 718 719 720 721
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).removed, []);
						assert.deepEqual((<IWorkspaceFoldersChangeEvent>target.args[3][0]).changed, []);
					});
			});
	});

});

722
suite('WorkspaceConfigurationService - Folder', () => {
723

724
	let workspaceName = `testWorkspace${uuid.generateUuid()}`, parentResource: string, workspaceDir: string, testObject: IConfigurationService, globalSettingsFile: string;
725
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
726

727
	suiteSetup(() => {
B
Benjamin Pasero 已提交
728 729 730 731
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
S
Sandeep Somavarapu 已提交
732 733 734 735 736
				'configurationService.folder.applicationSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.APPLICATION
				},
737
				'configurationService.folder.testSetting': {
B
Benjamin Pasero 已提交
738
					'type': 'string',
S
Sandeep Somavarapu 已提交
739 740
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
S
Sandeep Somavarapu 已提交
741
				}
B
Benjamin Pasero 已提交
742 743
			}
		});
744 745
	});

746 747 748
	setup(() => {
		return setUpFolderWorkspace(workspaceName)
			.then(({ parentDir, folderDir }) => {
749

750 751 752
				parentResource = parentDir;
				workspaceDir = folderDir;
				globalSettingsFile = path.join(parentDir, 'settings.json');
753

754 755
				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
				const environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, globalSettingsFile);
756 757
				const remoteAgentService = instantiationService.createInstance(RemoteAgentService, {});
				instantiationService.stub(IRemoteAgentService, remoteAgentService);
S
Sandeep Somavarapu 已提交
758
				const workspaceService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), remoteAgentService);
759 760 761
				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);
762

763
				return workspaceService.initialize(convertToWorkspacePayload(URI.file(folderDir))).then(() => {
764 765
					const fileService = new FileService2(new NullLogService());
					fileService.registerProvider(Schemas.file, new DiskFileSystemProvider(new NullLogService()));
766
					fileService.setLegacyService(new LegacyFileService(
767 768 769
						fileService,
						workspaceService,
						TestEnvironmentService,
770 771
						new TestTextResourceConfigurationService()
					));
S
Sandeep Somavarapu 已提交
772
					instantiationService.stub(IFileService, fileService);
773 774
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
775
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
776
					workspaceService.acquireFileService(fileService);
777
					testObject = workspaceService;
778 779 780 781
				});
			});
	});

782
	teardown(() => {
783 784 785 786
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
787
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
788
		}
R
Rob Lourens 已提交
789
		return undefined;
790 791
	});

792
	test('defaults', () => {
S
Sandeep Somavarapu 已提交
793
		assert.deepEqual(testObject.getValue('configurationService'), { 'folder': { 'applicationSetting': 'isSet', 'testSetting': 'isSet' } });
794 795
	});

796 797 798 799
	test('globals override defaults', () => {
		fs.writeFileSync(globalSettingsFile, '{ "configurationService.folder.testSetting": "userValue" }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'userValue'));
800 801
	});

802 803 804 805
	test('globals', () => {
		fs.writeFileSync(globalSettingsFile, '{ "testworkbench.editor.tabs": true }');
		return testObject.reloadConfiguration()
			.then(() => assert.equal(testObject.getValue('testworkbench.editor.tabs'), true));
806
	});
B
Benjamin Pasero 已提交
807

808 809 810 811
	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 已提交
812 813
	});

814 815 816 817 818
	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 已提交
819 820
	});

S
Sandeep Somavarapu 已提交
821 822 823 824
	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 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
			.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 已提交
840 841
	});

S
Sandeep Somavarapu 已提交
842 843 844 845 846 847 848
	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 已提交
849 850
	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 已提交
851 852 853 854 855 856
		return testObject.reloadConfiguration()
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
S
Sandeep Somavarapu 已提交
857
						'configurationService.folder.anotherApplicationSetting': {
S
Sandeep Somavarapu 已提交
858 859
							'type': 'string',
							'default': 'isSet',
S
Sandeep Somavarapu 已提交
860
							scope: ConfigurationScope.APPLICATION
S
Sandeep Somavarapu 已提交
861 862 863
						}
					}
				});
S
Sandeep Somavarapu 已提交
864
				assert.deepEqual(testObject.keys().workspace, []);
S
Sandeep Somavarapu 已提交
865 866 867
			});
	});

868 869 870 871 872
	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 已提交
873
	});
B
Benjamin Pasero 已提交
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
	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 已提交
891 892
			});
	});
893

894 895
	test('inspect', () => {
		let actual = testObject.inspect('something.missing');
R
Rob Lourens 已提交
896 897 898 899 900
		assert.equal(actual.default, undefined);
		assert.equal(actual.user, undefined);
		assert.equal(actual.workspace, undefined);
		assert.equal(actual.workspaceFolder, undefined);
		assert.equal(actual.value, undefined);
901 902 903

		actual = testObject.inspect('configurationService.folder.testSetting');
		assert.equal(actual.default, 'isSet');
R
Rob Lourens 已提交
904 905 906
		assert.equal(actual.user, undefined);
		assert.equal(actual.workspace, undefined);
		assert.equal(actual.workspaceFolder, undefined);
907 908 909 910 911 912 913 914
		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');
R
Rob Lourens 已提交
915 916
				assert.equal(actual.workspace, undefined);
				assert.equal(actual.workspaceFolder, undefined);
917 918 919 920 921 922 923 924 925 926
				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');
R
Rob Lourens 已提交
927
						assert.equal(actual.workspaceFolder, undefined);
928
						assert.equal(actual.value, 'workspaceValue');
929 930
					});
			});
S
Sandeep Somavarapu 已提交
931 932
	});

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	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 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971
			});
	});

	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 已提交
972 973 974 975 976
	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 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	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));
	});

996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	test('update memory configuration', () => {
		return testObject.updateValue('configurationService.folder.testSetting', 'memoryValue', ConfigurationTarget.MEMORY)
			.then(() => assert.equal(testObject.getValue('configurationService.folder.testSetting'), 'memoryValue'));
	});

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

S
Sandeep Somavarapu 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016
	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 已提交
1017
suite('WorkspaceConfigurationService-Multiroot', () => {
S
Sandeep Somavarapu 已提交
1018

1019
	let parentResource: string, workspaceContextService: IWorkspaceContextService, environmentService: IEnvironmentService, jsonEditingServce: IJSONEditingService, testObject: IConfigurationService;
1020
	const configurationRegistry = Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration);
S
Sandeep Somavarapu 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

	suiteSetup(() => {
		configurationRegistry.registerConfiguration({
			'id': '_test',
			'type': 'object',
			'properties': {
				'configurationService.workspace.testSetting': {
					'type': 'string',
					'default': 'isSet'
				},
S
Sandeep Somavarapu 已提交
1031 1032 1033 1034 1035
				'configurationService.workspace.applicationSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.APPLICATION
				},
S
Sandeep Somavarapu 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
				'configurationService.workspace.testResourceSetting': {
					'type': 'string',
					'default': 'isSet',
					scope: ConfigurationScope.RESOURCE
				}
			}
		});
	});

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

				parentResource = parentDir;
1050

1051
				const instantiationService = <TestInstantiationService>workbenchInstantiationService();
S
Sandeep Somavarapu 已提交
1052
				environmentService = new SettingsTestEnvironmentService(parseArgs(process.argv), process.execPath, path.join(parentDir, 'settings.json'));
1053 1054
				const remoteAgentService = instantiationService.createInstance(RemoteAgentService, {});
				instantiationService.stub(IRemoteAgentService, remoteAgentService);
S
Sandeep Somavarapu 已提交
1055
				const workspaceService = new WorkspaceService({ userSettingsPath: environmentService.appSettingsPath, configurationCache: new ConfigurationCache(environmentService) }, new HashService(), remoteAgentService);
S
Sandeep Somavarapu 已提交
1056 1057 1058 1059

				instantiationService.stub(IWorkspaceContextService, workspaceService);
				instantiationService.stub(IConfigurationService, workspaceService);
				instantiationService.stub(IEnvironmentService, environmentService);
1060

1061
				return workspaceService.initialize(getWorkspaceIdentifier(configPath)).then(() => {
1062 1063
					const fileService = new FileService2(new NullLogService());
					fileService.registerProvider(Schemas.file, new DiskFileSystemProvider(new NullLogService()));
1064
					fileService.setLegacyService(new LegacyFileService(
1065 1066 1067
						fileService,
						workspaceService,
						TestEnvironmentService,
1068 1069
						new TestTextResourceConfigurationService()
					));
S
Sandeep Somavarapu 已提交
1070
					instantiationService.stub(IFileService, fileService);
1071 1072
					instantiationService.stub(ITextFileService, instantiationService.createInstance(TestTextFileService));
					instantiationService.stub(ITextModelService, <ITextModelService>instantiationService.createInstance(TextModelResolverService));
S
#47154  
Sandeep Somavarapu 已提交
1073
					workspaceService.acquireInstantiationService(instantiationService);
S
Sandeep Somavarapu 已提交
1074
					workspaceService.acquireFileService(fileService);
1075 1076

					workspaceContextService = workspaceService;
S
Sandeep Somavarapu 已提交
1077
					jsonEditingServce = instantiationService.createInstance(JSONEditingService);
1078 1079
					testObject = workspaceService;
				});
S
Sandeep Somavarapu 已提交
1080 1081 1082
			});
	});

1083
	teardown(() => {
S
Sandeep Somavarapu 已提交
1084 1085 1086 1087
		if (testObject) {
			(<WorkspaceService>testObject).dispose();
		}
		if (parentResource) {
B
Benjamin Pasero 已提交
1088
			return pfs.rimraf(parentResource, pfs.RimRafMode.MOVE);
S
Sandeep Somavarapu 已提交
1089
		}
R
Rob Lourens 已提交
1090
		return undefined;
S
Sandeep Somavarapu 已提交
1091 1092
	});

S
Sandeep Somavarapu 已提交
1093 1094
	test('application settings are not read from workspace', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.applicationSetting": "userValue" }');
1095
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.applicationSetting': 'workspaceValue' } }, true)
S
Sandeep Somavarapu 已提交
1096 1097 1098 1099
			.then(() => testObject.reloadConfiguration())
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.applicationSetting'), 'userValue'));
	});

S
Sandeep Somavarapu 已提交
1100 1101
	test('workspace settings override user settings after defaults are registered ', () => {
		fs.writeFileSync(environmentService.appSettingsPath, '{ "configurationService.workspace.newSetting": "userValue" }');
1102
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.newSetting': 'workspaceValue' } }, true)
S
Sandeep Somavarapu 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
			.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 已提交
1119 1120 1121 1122 1123 1124 1125
	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 已提交
1126 1127 1128
	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 已提交
1129 1130 1131 1132 1133 1134
		return testObject.reloadConfiguration()
			.then(() => {
				configurationRegistry.registerConfiguration({
					'id': '_test',
					'type': 'object',
					'properties': {
S
Sandeep Somavarapu 已提交
1135
						'configurationService.workspace.testNewApplicationSetting': {
S
Sandeep Somavarapu 已提交
1136 1137
							'type': 'string',
							'default': 'isSet',
S
Sandeep Somavarapu 已提交
1138
							scope: ConfigurationScope.APPLICATION
S
Sandeep Somavarapu 已提交
1139 1140 1141
						}
					}
				});
S
Sandeep Somavarapu 已提交
1142
				assert.equal(testObject.getValue('configurationService.workspace.testNewApplicationSetting', { resource: workspaceContextService.getWorkspace().folders[0].uri }), 'userValue');
S
Sandeep Somavarapu 已提交
1143 1144 1145
			});
	});

S
Sandeep Somavarapu 已提交
1146 1147
	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" }');
1148
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.testNewResourceSetting2': 'workspaceValue' } }, true)
S
Sandeep Somavarapu 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
			.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');
			});
	});

1166 1167
	test('inspect', () => {
		let actual = testObject.inspect('something.missing');
R
Rob Lourens 已提交
1168 1169 1170 1171 1172
		assert.equal(actual.default, undefined);
		assert.equal(actual.user, undefined);
		assert.equal(actual.workspace, undefined);
		assert.equal(actual.workspaceFolder, undefined);
		assert.equal(actual.value, undefined);
1173 1174 1175

		actual = testObject.inspect('configurationService.workspace.testResourceSetting');
		assert.equal(actual.default, 'isSet');
R
Rob Lourens 已提交
1176 1177 1178
		assert.equal(actual.user, undefined);
		assert.equal(actual.workspace, undefined);
		assert.equal(actual.workspaceFolder, undefined);
1179 1180 1181 1182 1183 1184 1185 1186
		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');
R
Rob Lourens 已提交
1187 1188
				assert.equal(actual.workspace, undefined);
				assert.equal(actual.workspaceFolder, undefined);
1189 1190
				assert.equal(actual.value, 'userValue');

1191
				return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'settings', value: { 'configurationService.workspace.testResourceSetting': 'workspaceValue' } }, true)
1192 1193 1194 1195 1196 1197
					.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');
R
Rob Lourens 已提交
1198
						assert.equal(actual.workspaceFolder, undefined);
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
						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 已提交
1215

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
	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}'
				}
			]
		};
1233
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'launch', value: expectedLaunchConfiguration }, true)
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
			.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}'
				}
			]
		};
1258
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'launch', value: expectedLaunchConfiguration }, true)
1259 1260 1261 1262 1263 1264 1265
			.then(() => testObject.reloadConfiguration())
			.then(() => {
				const actual = testObject.inspect('launch').workspace;
				assert.deepEqual(actual, expectedLaunchConfiguration);
			});
	});

S
Sandeep Somavarapu 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	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 已提交
1290 1291 1292 1293 1294
	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 已提交
1295
	test('update workspace folder configuration', () => {
1296
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1297 1298 1299 1300 1301
		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', () => {
1302
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1303 1304 1305 1306 1307 1308
		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 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	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));
			});
	});

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	test('update memory configuration', () => {
		return testObject.updateValue('configurationService.workspace.testSetting', 'memoryValue', ConfigurationTarget.MEMORY)
			.then(() => assert.equal(testObject.getValue('configurationService.workspace.testSetting'), 'memoryValue'));
	});

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

S
Sandeep Somavarapu 已提交
1332
	test('update tasks configuration in a folder', () => {
1333
		const workspace = workspaceContextService.getWorkspace();
S
Sandeep Somavarapu 已提交
1334 1335 1336
		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 已提交
1337 1338 1339 1340 1341 1342 1343

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

1344
	test('update launch configuration in a workspace', () => {
S
Sandeep Somavarapu 已提交
1345 1346
		const workspace = workspaceContextService.getWorkspace();
		return testObject.updateValue('launch', { 'version': '1.0.0', configurations: [{ 'name': 'myLaunch' }] }, { resource: workspace.folders[0].uri }, ConfigurationTarget.WORKSPACE, true)
S
Sandeep Somavarapu 已提交
1347
			.then(() => assert.deepEqual(testObject.getValue('launch'), { 'version': '1.0.0', configurations: [{ 'name': 'myLaunch' }] }));
S
Sandeep Somavarapu 已提交
1348 1349 1350
	});

	test('task configurations are not read from workspace', () => {
1351
		return jsonEditingServce.write(workspaceContextService.getWorkspace().configuration!, { key: 'tasks', value: { 'version': '1.0' } }, true)
S
Sandeep Somavarapu 已提交
1352 1353
			.then(() => testObject.reloadConfiguration())
			.then(() => {
S
Sandeep Somavarapu 已提交
1354
				const actual = testObject.inspect('tasks.version');
R
Rob Lourens 已提交
1355
				assert.equal(actual.workspace, undefined);
S
Sandeep Somavarapu 已提交
1356 1357
			});
	});
S
Sandeep Somavarapu 已提交
1358
});
1359 1360

function getWorkspaceId(configPath: URI): string {
1361
	let workspaceConfigPath = configPath.scheme === Schemas.file ? originalFSPath(configPath) : configPath.toString();
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	if (!isLinux) {
		workspaceConfigPath = workspaceConfigPath.toLowerCase(); // sanitize for platform file system
	}

	return createHash('md5').update(workspaceConfigPath).digest('hex');
}

export function getWorkspaceIdentifier(configPath: URI): IWorkspaceIdentifier {
	return {
		configPath,
		id: getWorkspaceId(configPath)
	};
}