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

'use strict';

import * as assert from 'assert';
import * as platform from 'vs/base/common/platform';
10 11 12 13
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as pfs from 'vs/base/node/pfs';
14
import Uri from 'vs/base/common/uri';
15 16
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { parseArgs } from 'vs/platform/environment/node/argv';
D
Daniel Imms 已提交
17
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
18
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
19 20
import { HotExitConfiguration } from 'vs/platform/files/common/files';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
21
import { ConsoleLogMainService } from 'vs/platform/log/common/log';
B
Benjamin Pasero 已提交
22 23
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { createHash } from 'crypto';
B
Benjamin Pasero 已提交
24
import { getRandomTestPath } from 'vs/workbench/test/workbenchTestServices';
25
import { Schemas } from 'vs/base/common/network';
26

27
suite('BackupMainService', () => {
28 29 30 31 32

	function assertEqualUris(actual: Uri[], expected: Uri[]) {
		assert.deepEqual(actual.map(a => a.toString()), expected.map(a => a.toString()));
	}

B
Benjamin Pasero 已提交
33
	const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backupservice');
34 35
	const backupHome = path.join(parentDir, 'Backups');
	const backupWorkspacesPath = path.join(backupHome, 'workspaces.json');
36

37
	const environmentService = new EnvironmentService(parseArgs(process.argv), process.execPath);
38

39
	class TestBackupMainService extends BackupMainService {
40

41
		constructor(backupHome: string, backupWorkspacesPath: string, configService: TestConfigurationService) {
S
Sandeep Somavarapu 已提交
42
			super(environmentService, configService, new ConsoleLogMainService());
43

44 45
			this.backupHome = backupHome;
			this.workspacesJsonPath = backupWorkspacesPath;
46

47 48 49
			// Force a reload with the new paths
			this.loadSync();
		}
50

51 52 53
		public loadSync(): void {
			super.loadSync();
		}
54

55
		public toBackupPath(workspacePath: Uri): string {
56 57
			return path.join(this.backupHome, super.getFolderHash(workspacePath));
		}
58

59
		public getFolderHash(folderPath: Uri): string {
60 61 62 63 64 65 66 67 68 69 70
			return super.getFolderHash(folderPath);
		}
	}

	function toWorkspace(path: string): IWorkspaceIdentifier {
		return {
			id: createHash('md5').update(sanitizePath(path)).digest('hex'),
			configPath: path
		};
	}

71 72 73 74 75 76 77 78 79 80 81 82
	function ensureFolderExists(uri: Uri): void {
		if (!fs.existsSync(uri.fsPath)) {
			fs.mkdirSync(uri.fsPath);
		}
		const backupFolder = service.toBackupPath(uri);
		if (!fs.existsSync(backupFolder)) {
			fs.mkdirSync(backupFolder);
			fs.mkdirSync(path.join(backupFolder, Schemas.file));
			fs.writeFile(path.join(backupFolder, Schemas.file, 'foo.txt'), 'Hello');
		}
	}

83 84 85
	function sanitizePath(p: string): string {
		return platform.isLinux ? p : p.toLowerCase();
	}
86 87 88 89

	const fooFile = Uri.file(platform.isWindows ? 'C:\\foo' : '/foo');
	const barFile = Uri.file(platform.isWindows ? 'C:\\bar' : '/bar');

90 91
	const existingTestFolder = Uri.file(path.join(parentDir, 'folder1'));

92
	let service: TestBackupMainService;
93
	let configService: TestConfigurationService;
94

95
	setup(() => {
96 97
		configService = new TestConfigurationService();
		service = new TestBackupMainService(backupHome, backupWorkspacesPath, configService);
98 99

		// Delete any existing backups completely and then re-create it.
100 101
		return pfs.del(backupHome, os.tmpdir()).then(() => {
			return pfs.mkdirp(backupHome);
102 103 104
		});
	});

105 106
	teardown(() => {
		return pfs.del(backupHome, os.tmpdir());
107 108
	});

B
Benjamin Pasero 已提交
109 110
	test('service validates backup workspaces on startup and cleans up (folder workspaces)', function () {
		this.timeout(1000 * 10); // increase timeout for this test
111

112
		// 1) backup workspace path does not exist
113 114
		service.registerFolderBackupSync(fooFile);
		service.registerFolderBackupSync(barFile);
115
		service.loadSync();
116
		assertEqualUris(service.getFolderBackupPaths(), []);
117 118

		// 2) backup workspace path exists with empty contents within
119 120 121 122
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
		service.registerFolderBackupSync(fooFile);
		service.registerFolderBackupSync(barFile);
123
		service.loadSync();
124 125 126
		assertEqualUris(service.getFolderBackupPaths(), []);
		assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
		assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
127 128

		// 3) backup workspace path exists with empty folders within
129 130 131 132 133 134
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
		fs.mkdirSync(path.join(service.toBackupPath(fooFile), Schemas.file));
		fs.mkdirSync(path.join(service.toBackupPath(barFile), Schemas.untitled));
		service.registerFolderBackupSync(fooFile);
		service.registerFolderBackupSync(barFile);
135
		service.loadSync();
136 137 138
		assertEqualUris(service.getFolderBackupPaths(), []);
		assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
		assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
139

140 141
		// 4) backup workspace path points to a workspace that no longer exists
		// so it should convert the backup worspace to an empty workspace backup
142 143 144
		const fileBackups = path.join(service.toBackupPath(fooFile), Schemas.file);
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
145
		fs.mkdirSync(fileBackups);
146
		service.registerFolderBackupSync(fooFile);
147 148 149 150 151 152 153 154
		assert.equal(service.getFolderBackupPaths().length, 1);
		assert.equal(service.getEmptyWindowBackupPaths().length, 0);
		fs.writeFileSync(path.join(fileBackups, 'backup.txt'), '');
		service.loadSync();
		assert.equal(service.getFolderBackupPaths().length, 0);
		assert.equal(service.getEmptyWindowBackupPaths().length, 1);
	});

B
Benjamin Pasero 已提交
155 156
	test('service validates backup workspaces on startup and cleans up (root workspaces)', function () {
		this.timeout(1000 * 10); // increase timeout for this test
157 158

		// 1) backup workspace path does not exist
159 160
		service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath));
		service.registerWorkspaceBackupSync(toWorkspace(barFile.fsPath));
161
		service.loadSync();
162
		assert.deepEqual(service.getWorkspaceBackups(), []);
163 164

		// 2) backup workspace path exists with empty contents within
165 166
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
167 168
		service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath));
		service.registerWorkspaceBackupSync(toWorkspace(barFile.fsPath));
169
		service.loadSync();
170
		assert.deepEqual(service.getWorkspaceBackups(), []);
171 172
		assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
		assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
173 174

		// 3) backup workspace path exists with empty folders within
175 176 177 178
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
		fs.mkdirSync(path.join(service.toBackupPath(fooFile), Schemas.file));
		fs.mkdirSync(path.join(service.toBackupPath(barFile), Schemas.untitled));
179 180
		service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath));
		service.registerWorkspaceBackupSync(toWorkspace(barFile.fsPath));
181
		service.loadSync();
182
		assert.deepEqual(service.getWorkspaceBackups(), []);
183 184
		assert.ok(!fs.existsSync(service.toBackupPath(fooFile)));
		assert.ok(!fs.existsSync(service.toBackupPath(barFile)));
185 186 187

		// 4) backup workspace path points to a workspace that no longer exists
		// so it should convert the backup worspace to an empty workspace backup
188 189 190
		const fileBackups = path.join(service.toBackupPath(fooFile), Schemas.file);
		fs.mkdirSync(service.toBackupPath(fooFile));
		fs.mkdirSync(service.toBackupPath(barFile));
191
		fs.mkdirSync(fileBackups);
192 193
		service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath));
		assert.equal(service.getWorkspaceBackups().length, 1);
194
		assert.equal(service.getEmptyWindowBackupPaths().length, 0);
195 196
		fs.writeFileSync(path.join(fileBackups, 'backup.txt'), '');
		service.loadSync();
197
		assert.equal(service.getWorkspaceBackups().length, 0);
198
		assert.equal(service.getEmptyWindowBackupPaths().length, 1);
199 200
	});

201
	test('service supports to migrate backup data from another location', () => {
202
		const backupPathToMigrate = service.toBackupPath(fooFile);
203 204
		fs.mkdirSync(backupPathToMigrate);
		fs.writeFileSync(path.join(backupPathToMigrate, 'backup.txt'), 'Some Data');
205
		service.registerFolderBackupSync(Uri.file(backupPathToMigrate));
206 207 208 209 210 211 212

		const workspaceBackupPath = service.registerWorkspaceBackupSync(toWorkspace(barFile.fsPath), backupPathToMigrate);

		assert.ok(fs.existsSync(workspaceBackupPath));
		assert.ok(fs.existsSync(path.join(workspaceBackupPath, 'backup.txt')));
		assert.ok(!fs.existsSync(backupPathToMigrate));

213 214 215 216
		const emptyBackups = service.getEmptyWindowBackupPaths();
		assert.equal(0, emptyBackups.length);
	});

217
	test('service backup migration makes sure to preserve existing backups', () => {
218
		const backupPathToMigrate = service.toBackupPath(fooFile);
219 220
		fs.mkdirSync(backupPathToMigrate);
		fs.writeFileSync(path.join(backupPathToMigrate, 'backup.txt'), 'Some Data');
221
		service.registerFolderBackupSync(Uri.file(backupPathToMigrate));
222

223
		const backupPathToPreserve = service.toBackupPath(barFile);
224 225
		fs.mkdirSync(backupPathToPreserve);
		fs.writeFileSync(path.join(backupPathToPreserve, 'backup.txt'), 'Some Data');
226
		service.registerFolderBackupSync(Uri.file(backupPathToPreserve));
227 228 229 230 231 232 233 234 235 236

		const workspaceBackupPath = service.registerWorkspaceBackupSync(toWorkspace(barFile.fsPath), backupPathToMigrate);

		assert.ok(fs.existsSync(workspaceBackupPath));
		assert.ok(fs.existsSync(path.join(workspaceBackupPath, 'backup.txt')));
		assert.ok(!fs.existsSync(backupPathToMigrate));

		const emptyBackups = service.getEmptyWindowBackupPaths();
		assert.equal(1, emptyBackups.length);
		assert.equal(1, fs.readdirSync(path.join(backupHome, emptyBackups[0])).length);
237 238
	});

239
	suite('loadSync', () => {
240
		test('getFolderBackupPaths() should return [] when workspaces.json doesn\'t exist', () => {
241
			assertEqualUris(service.getFolderBackupPaths(), []);
242 243 244 245 246
		});

		test('getFolderBackupPaths() should return [] when workspaces.json is not properly formed JSON', () => {
			fs.writeFileSync(backupWorkspacesPath, '');
			service.loadSync();
247
			assertEqualUris(service.getFolderBackupPaths(), []);
248 249
			fs.writeFileSync(backupWorkspacesPath, '{]');
			service.loadSync();
250
			assertEqualUris(service.getFolderBackupPaths(), []);
251 252
			fs.writeFileSync(backupWorkspacesPath, 'foo');
			service.loadSync();
253
			assertEqualUris(service.getFolderBackupPaths(), []);
254 255 256 257 258
		});

		test('getFolderBackupPaths() should return [] when folderWorkspaces in workspaces.json is absent', () => {
			fs.writeFileSync(backupWorkspacesPath, '{}');
			service.loadSync();
259
			assertEqualUris(service.getFolderBackupPaths(), []);
260 261 262 263 264
		});

		test('getFolderBackupPaths() should return [] when folderWorkspaces in workspaces.json is not a string array', () => {
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{}}');
			service.loadSync();
265
			assertEqualUris(service.getFolderBackupPaths(), []);
266 267
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": ["bar"]}}');
			service.loadSync();
268
			assertEqualUris(service.getFolderBackupPaths(), []);
269 270
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": []}}');
			service.loadSync();
271
			assertEqualUris(service.getFolderBackupPaths(), []);
272 273
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": "bar"}}');
			service.loadSync();
274
			assertEqualUris(service.getFolderBackupPaths(), []);
275 276
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":"foo"}');
			service.loadSync();
277
			assertEqualUris(service.getFolderBackupPaths(), []);
278 279
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":1}');
			service.loadSync();
280
			assertEqualUris(service.getFolderBackupPaths(), []);
281 282 283
		});

		test('getFolderBackupPaths() should return [] when files.hotExit = "onExitAndWindowClose"', () => {
284 285
			service.registerFolderBackupSync(Uri.file(fooFile.fsPath.toUpperCase()));
			assertEqualUris(service.getFolderBackupPaths(), [Uri.file(fooFile.fsPath.toUpperCase())]);
286 287
			configService.setUserConfiguration('files.hotExit', HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE);
			service.loadSync();
288
			assertEqualUris(service.getFolderBackupPaths(), []);
289 290
		});

291 292
		test('getWorkspaceBackups() should return [] when workspaces.json doesn\'t exist', () => {
			assert.deepEqual(service.getWorkspaceBackups(), []);
293
		});
D
Daniel Imms 已提交
294

295
		test('getWorkspaceBackups() should return [] when workspaces.json is not properly formed JSON', () => {
296 297
			fs.writeFileSync(backupWorkspacesPath, '');
			service.loadSync();
298
			assert.deepEqual(service.getWorkspaceBackups(), []);
299 300
			fs.writeFileSync(backupWorkspacesPath, '{]');
			service.loadSync();
301
			assert.deepEqual(service.getWorkspaceBackups(), []);
302 303
			fs.writeFileSync(backupWorkspacesPath, 'foo');
			service.loadSync();
304
			assert.deepEqual(service.getWorkspaceBackups(), []);
305
		});
D
Daniel Imms 已提交
306

307
		test('getWorkspaceBackups() should return [] when folderWorkspaces in workspaces.json is absent', () => {
308 309
			fs.writeFileSync(backupWorkspacesPath, '{}');
			service.loadSync();
310
			assert.deepEqual(service.getWorkspaceBackups(), []);
311 312
		});

313
		test('getWorkspaceBackups() should return [] when rootWorkspaces in workspaces.json is not a object array', () => {
314
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{}}');
315
			service.loadSync();
316
			assert.deepEqual(service.getWorkspaceBackups(), []);
317
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": ["bar"]}}');
318
			service.loadSync();
319
			assert.deepEqual(service.getWorkspaceBackups(), []);
320
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": []}}');
321
			service.loadSync();
322
			assert.deepEqual(service.getWorkspaceBackups(), []);
323
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":{"foo": "bar"}}');
324
			service.loadSync();
325
			assert.deepEqual(service.getWorkspaceBackups(), []);
326
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":"foo"}');
327
			service.loadSync();
328
			assert.deepEqual(service.getWorkspaceBackups(), []);
329
			fs.writeFileSync(backupWorkspacesPath, '{"rootWorkspaces":1}');
330
			service.loadSync();
331
			assert.deepEqual(service.getWorkspaceBackups(), []);
332
		});
D
Daniel Imms 已提交
333

334 335 336 337
		test('getWorkspaceBackups() should return [] when files.hotExit = "onExitAndWindowClose"', () => {
			service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath.toUpperCase()));
			assert.equal(service.getWorkspaceBackups().length, 1);
			assert.deepEqual(service.getWorkspaceBackups().map(r => r.configPath), [fooFile.fsPath.toUpperCase()]);
338 339
			configService.setUserConfiguration('files.hotExit', HotExitConfiguration.ON_EXIT_AND_WINDOW_CLOSE);
			service.loadSync();
340
			assert.deepEqual(service.getWorkspaceBackups(), []);
341 342
		});

343
		test('getEmptyWorkspaceBackupPaths() should return [] when workspaces.json doesn\'t exist', () => {
344
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
345 346
		});

347
		test('getEmptyWorkspaceBackupPaths() should return [] when workspaces.json is not properly formed JSON', () => {
348 349
			fs.writeFileSync(backupWorkspacesPath, '');
			service.loadSync();
350
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
351 352
			fs.writeFileSync(backupWorkspacesPath, '{]');
			service.loadSync();
353
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
354 355
			fs.writeFileSync(backupWorkspacesPath, 'foo');
			service.loadSync();
356
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
357 358
		});

359
		test('getEmptyWorkspaceBackupPaths() should return [] when folderWorkspaces in workspaces.json is absent', () => {
360 361
			fs.writeFileSync(backupWorkspacesPath, '{}');
			service.loadSync();
362
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
363 364
		});

C
Christof Marti 已提交
365
		test('getEmptyWorkspaceBackupPaths() should return [] when folderWorkspaces in workspaces.json is not a string array', function () {
C
Christof Marti 已提交
366
			this.timeout(5000);
367 368
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{}}');
			service.loadSync();
369
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
370 371
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": ["bar"]}}');
			service.loadSync();
372
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
373 374
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": []}}');
			service.loadSync();
375
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
376 377
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": "bar"}}');
			service.loadSync();
378
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
379 380
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":"foo"}');
			service.loadSync();
381
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
382 383
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":1}');
			service.loadSync();
384
			assert.deepEqual(service.getEmptyWindowBackupPaths(), []);
385
		});
D
Daniel Imms 已提交
386
	});
387

388
	suite('dedupeFolderWorkspaces', () => {
389 390 391
		test('should ignore duplicates (folder workspace)', () => {

			ensureFolderExists(existingTestFolder);
392

393
			const workspacesJson: IBackupWorkspacesFormat = {
394
				rootWorkspaces: [],
395
				folderURIWorkspaces: [existingTestFolder.toString(), existingTestFolder.toString()],
D
Daniel Imms 已提交
396 397
				emptyWorkspaces: []
			};
398 399 400 401 402 403 404 405
			return pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
				service.loadSync();
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
					const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
					assert.deepEqual(json.folderURIWorkspaces, [existingTestFolder.toString()]);
				});
			});
		});
D
Daniel Imms 已提交
406

407
		test('should ignore duplicates on Windows and Mac (folder workspace)', () => {
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422
			ensureFolderExists(existingTestFolder);

			const workspacesJson: IBackupWorkspacesFormat = {
				rootWorkspaces: [],
				folderURIWorkspaces: [existingTestFolder.toString(), existingTestFolder.toString().toLowerCase()],
				emptyWorkspaces: []
			};
			return pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
				service.loadSync();
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
					const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
					assert.deepEqual(json.folderURIWorkspaces, [existingTestFolder.toString()]);
				});
			});
423
		});
424 425 426 427

		test('should ignore duplicates on Windows and Mac (root workspace)', () => {
			// Skip test on Linux
			if (platform.isLinux) {
428
				return null;
429 430
			}

431
			const workspacesJson: IBackupWorkspacesFormat = {
432
				rootWorkspaces: platform.isWindows ? [toWorkspace('c:\\FOO'), toWorkspace('C:\\FOO'), toWorkspace('c:\\foo')] : [toWorkspace('/FOO'), toWorkspace('/foo')],
433
				folderURIWorkspaces: [],
434 435
				emptyWorkspaces: []
			};
436 437 438 439 440 441 442 443 444 445 446 447
			return pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
				service.loadSync();
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
					const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
					assert.equal(json.rootWorkspaces.length, 1);
					if (platform.isWindows) {
						assert.deepEqual(json.rootWorkspaces.map(r => r.configPath), ['c:\\FOO'], 'should return the first duplicated entry');
					} else {
						assert.deepEqual(json.rootWorkspaces.map(r => r.configPath), ['/FOO'], 'should return the first duplicated entry');
					}
				});
			});
448 449

		});
D
Daniel Imms 已提交
450 451
	});

452
	suite('registerWindowForBackups', () => {
453
		test('should persist paths to workspaces.json (folder workspace)', () => {
454 455 456
			service.registerFolderBackupSync(fooFile);
			service.registerFolderBackupSync(barFile);
			assertEqualUris(service.getFolderBackupPaths(), [fooFile, barFile]);
457
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
458
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
459
				assert.deepEqual(json.folderURIWorkspaces, [fooFile.toString(), barFile.toString()]);
460 461
			});
		});
D
Daniel Imms 已提交
462

463
		test('should persist paths to workspaces.json (root workspace)', () => {
464 465 466 467 468 469 470 471 472
			const ws1 = toWorkspace(fooFile.fsPath);
			service.registerWorkspaceBackupSync(ws1);
			const ws2 = toWorkspace(barFile.fsPath);
			service.registerWorkspaceBackupSync(ws2);

			assert.deepEqual(service.getWorkspaceBackups().map(b => b.configPath), [fooFile.fsPath, barFile.fsPath]);
			assert.equal(ws1.id, service.getWorkspaceBackups()[0].id);
			assert.equal(ws2.id, service.getWorkspaceBackups()[1].id);

473
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
474
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
475 476 477 478

				assert.deepEqual(json.rootWorkspaces.map(b => b.configPath), [fooFile.fsPath, barFile.fsPath]);
				assert.equal(ws1.id, json.rootWorkspaces[0].id);
				assert.equal(ws2.id, json.rootWorkspaces[1].id);
479 480 481
			});
		});

482
		test('should always store the workspace path in workspaces.json using the case given, regardless of whether the file system is case-sensitive (folder workspace)', () => {
483 484
			service.registerFolderBackupSync(Uri.file(fooFile.fsPath.toUpperCase()));
			assertEqualUris(service.getFolderBackupPaths(), [Uri.file(fooFile.fsPath.toUpperCase())]);
485
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
D
Daniel Imms 已提交
486
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
487
				assert.deepEqual(json.folderURIWorkspaces, [Uri.file(fooFile.fsPath.toUpperCase()).toString()]);
D
Daniel Imms 已提交
488 489
			});
		});
490

491
		test('should always store the workspace path in workspaces.json using the case given, regardless of whether the file system is case-sensitive (root workspace)', () => {
492 493
			service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath.toUpperCase()));
			assert.deepEqual(service.getWorkspaceBackups().map(b => b.configPath), [fooFile.fsPath.toUpperCase()]);
494
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
495
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
496
				assert.deepEqual(json.rootWorkspaces.map(b => b.configPath), [fooFile.fsPath.toUpperCase()]);
497 498
			});
		});
499 500
	});

501
	suite('removeBackupPathSync', () => {
502
		test('should remove folder workspaces from workspaces.json (folder workspace)', () => {
503 504 505
			service.registerFolderBackupSync(fooFile);
			service.registerFolderBackupSync(barFile);
			service.unregisterFolderBackupSync(fooFile);
506
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
507
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
508 509
				assert.deepEqual(json.folderURIWorkspaces, [barFile.toString()]);
				service.unregisterFolderBackupSync(barFile);
510
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
511
					const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
512
					assert.deepEqual(json2.folderURIWorkspaces, []);
513
				});
514 515
			});
		});
516

517
		test('should remove folder workspaces from workspaces.json (root workspace)', () => {
518 519 520 521
			const ws1 = toWorkspace(fooFile.fsPath);
			service.registerWorkspaceBackupSync(ws1);
			const ws2 = toWorkspace(barFile.fsPath);
			service.registerWorkspaceBackupSync(ws2);
522
			service.unregisterWorkspaceBackupSync(ws1);
523
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
524
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
525
				assert.deepEqual(json.rootWorkspaces.map(r => r.configPath), [barFile.fsPath]);
526
				service.unregisterWorkspaceBackupSync(ws2);
527
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
528 529 530 531 532 533
					const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
					assert.deepEqual(json2.rootWorkspaces, []);
				});
			});
		});

534
		test('should remove empty workspaces from workspaces.json', () => {
B
Benjamin Pasero 已提交
535 536
			service.registerEmptyWindowBackupSync('foo');
			service.registerEmptyWindowBackupSync('bar');
537
			service.unregisterEmptyWindowBackupSync('foo');
538
			return pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
D
Daniel Imms 已提交
539 540
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
				assert.deepEqual(json.emptyWorkspaces, ['bar']);
541
				service.unregisterEmptyWindowBackupSync('bar');
542
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
D
Daniel Imms 已提交
543 544 545 546 547 548
					const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
					assert.deepEqual(json2.emptyWorkspaces, []);
				});
			});
		});

549
		test('should fail gracefully when removing a path that doesn\'t exist', () => {
550 551 552 553

			ensureFolderExists(existingTestFolder); // make sure backup folder exists, so the folder is not removed on loadSync

			const workspacesJson: IBackupWorkspacesFormat = { rootWorkspaces: [], folderURIWorkspaces: [existingTestFolder.toString()], emptyWorkspaces: [] };
554
			return pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
555 556 557
				service.loadSync();
				service.unregisterFolderBackupSync(barFile);
				service.unregisterEmptyWindowBackupSync('test');
558
				return pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
559
					const json = <IBackupWorkspacesFormat>JSON.parse(content);
560
					assert.deepEqual(json.folderURIWorkspaces, [existingTestFolder.toString()]);
561 562 563
				});
			});
		});
564
	});
D
Daniel Imms 已提交
565

566
	suite('getWorkspaceHash', () => {
D
Daniel Imms 已提交
567
		test('should perform an md5 hash on the path', () => {
568
			assert.equal(service.getFolderHash(Uri.file('/foo')), '1effb2475fcfba4f9e8b8a1dbc8f3caf');
D
Daniel Imms 已提交
569 570
		});

571 572 573 574 575
		test('should ignore case on Windows and Mac', () => {
			// Skip test on Linux
			if (platform.isLinux) {
				return;
			}
D
Daniel Imms 已提交
576

577
			if (platform.isMacintosh) {
578
				assert.equal(service.getFolderHash(Uri.file('/foo')), service.getFolderHash(Uri.file('/FOO')));
579
			}
D
Daniel Imms 已提交
580

581
			if (platform.isWindows) {
582
				assert.equal(service.getFolderHash(Uri.file('c:\\foo')), service.getFolderHash(Uri.file('C:\\FOO')));
583 584
			}
		});
D
Daniel Imms 已提交
585
	});
586

587
	suite('mixed path casing', () => {
588
		test('should handle case insensitive paths properly (registerWindowForBackupsSync) (folder workspace)', () => {
589 590
			service.registerFolderBackupSync(fooFile);
			service.registerFolderBackupSync(Uri.file(fooFile.fsPath.toUpperCase()));
591

592 593 594 595 596 597 598
			if (platform.isLinux) {
				assert.equal(service.getFolderBackupPaths().length, 2);
			} else {
				assert.equal(service.getFolderBackupPaths().length, 1);
			}
		});

599
		test('should handle case insensitive paths properly (registerWindowForBackupsSync) (root workspace)', () => {
600 601
			service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath));
			service.registerWorkspaceBackupSync(toWorkspace(fooFile.fsPath.toUpperCase()));
602

603
			if (platform.isLinux) {
604
				assert.equal(service.getWorkspaceBackups().length, 2);
605
			} else {
606
				assert.equal(service.getWorkspaceBackups().length, 1);
607 608 609
			}
		});

610
		test('should handle case insensitive paths properly (removeBackupPathSync) (folder workspace)', () => {
611 612

			// same case
613 614
			service.registerFolderBackupSync(fooFile);
			service.unregisterFolderBackupSync(fooFile);
615
			assert.equal(service.getFolderBackupPaths().length, 0);
616 617

			// mixed case
618 619
			service.registerFolderBackupSync(fooFile);
			service.unregisterFolderBackupSync(Uri.file(fooFile.fsPath.toUpperCase()));
620 621 622 623 624 625 626

			if (platform.isLinux) {
				assert.equal(service.getFolderBackupPaths().length, 1);
			} else {
				assert.equal(service.getFolderBackupPaths().length, 0);
			}
		});
627
	});
628
});