backupMainService.test.ts 12.5 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';
D
Daniel Imms 已提交
10
import fs = require('fs');
11 12 13 14 15 16
import os = require('os');
import path = require('path');
import extfs = require('vs/base/node/extfs');
import pfs = require('vs/base/node/pfs');
import Uri from 'vs/base/common/uri';
import { TestEnvironmentService } from 'vs/test/utils/servicesTestUtils';
D
Daniel Imms 已提交
17
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
18
import { IBackupWorkspacesFormat } from 'vs/platform/backup/common/backup';
19

D
Daniel Imms 已提交
20
class TestBackupMainService extends BackupMainService {
21
	constructor(backupHome: string, backupWorkspacesPath: string) {
22
		super(TestEnvironmentService);
23 24

		this.backupHome = backupHome;
D
Daniel Imms 已提交
25
		this.workspacesJsonPath = backupWorkspacesPath;
26 27 28

		// Force a reload with the new paths
		this.loadSync();
29
	}
30

31 32
	public removeBackupPathSync(workspaceIdenfitier: string, isEmptyWorkspace: boolean): void {
		return super.removeBackupPathSync(workspaceIdenfitier, isEmptyWorkspace);
33 34 35 36 37 38
	}

	public loadSync(): void {
		super.loadSync();
	}

D
Daniel Imms 已提交
39 40 41 42
	public sanitizeFolderWorkspaces(backups: IBackupWorkspacesFormat): void {
		super.sanitizeFolderWorkspaces(backups);
	}

43
	public toBackupPath(workspacePath: string): string {
D
Daniel Imms 已提交
44
		return path.join(this.backupHome, super.getWorkspaceHash(workspacePath));
45
	}
46 47 48 49

	public getWorkspaceHash(workspacePath: string): string {
		return super.getWorkspaceHash(workspacePath);
	}
50 51
}

D
Daniel Imms 已提交
52
suite('BackupMainService', () => {
D
Daniel Imms 已提交
53
	const parentDir = path.join(os.tmpdir(), 'vsctests', 'service');
54 55 56 57 58 59
	const backupHome = path.join(parentDir, 'Backups');
	const backupWorkspacesPath = path.join(backupHome, 'workspaces.json');

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

60
	let service: TestBackupMainService;
61 62

	setup(done => {
D
Daniel Imms 已提交
63
		service = new TestBackupMainService(backupHome, backupWorkspacesPath);
64 65 66 67

		// Delete any existing backups completely and then re-create it.
		extfs.del(backupHome, os.tmpdir(), () => {
			pfs.mkdirp(backupHome).then(() => {
D
Daniel Imms 已提交
68
				done();
69 70 71 72 73 74 75 76
			});
		});
	});

	teardown(done => {
		extfs.del(backupHome, os.tmpdir(), done);
	});

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	test('service validates backup workspaces on startup and cleans up', done => {
		// 1) backup workspace path does not exist
		service.registerWindowForBackups(1, false, null, fooFile.fsPath);
		service.registerWindowForBackups(2, false, null, barFile.fsPath);
		service.loadSync();
		assert.deepEqual(service.workspaceBackupPaths, []);

		// 2) backup workspace path exists with empty contents within
		fs.mkdirSync(service.toBackupPath(fooFile.fsPath));
		fs.mkdirSync(service.toBackupPath(barFile.fsPath));
		service.registerWindowForBackups(1, false, null, fooFile.fsPath);
		service.registerWindowForBackups(2, false, null, barFile.fsPath);
		service.loadSync();
		assert.deepEqual(service.workspaceBackupPaths, []);
		assert.ok(!fs.exists(service.toBackupPath(fooFile.fsPath)));
		assert.ok(!fs.exists(service.toBackupPath(barFile.fsPath)));

		// 3) backup workspace path exists with empty folders within
		fs.mkdirSync(service.toBackupPath(fooFile.fsPath));
		fs.mkdirSync(service.toBackupPath(barFile.fsPath));
		fs.mkdirSync(path.join(service.toBackupPath(fooFile.fsPath), 'file'));
		fs.mkdirSync(path.join(service.toBackupPath(barFile.fsPath), 'untitled'));
		service.registerWindowForBackups(1, false, null, fooFile.fsPath);
		service.registerWindowForBackups(2, false, null, barFile.fsPath);
		service.loadSync();
		assert.deepEqual(service.workspaceBackupPaths, []);
		assert.ok(!fs.exists(service.toBackupPath(fooFile.fsPath)));
		assert.ok(!fs.exists(service.toBackupPath(barFile.fsPath)));

		done();
	});

109 110 111 112
	suite('loadSync', () => {
		test('workspaceBackupPaths should return [] when workspaces.json doesn\'t exist', () => {
			assert.deepEqual(service.workspaceBackupPaths, []);
		});
D
Daniel Imms 已提交
113

114 115 116 117 118 119 120 121 122 123 124
		test('workspaceBackupPaths should return [] when workspaces.json is not properly formed JSON', () => {
			fs.writeFileSync(backupWorkspacesPath, '');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{]');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, 'foo');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
		});
D
Daniel Imms 已提交
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		test('workspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is absent', () => {
			fs.writeFileSync(backupWorkspacesPath, '{}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
		});

		test('workspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array', () => {
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{}}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": ["bar"]}}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": []}}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": "bar"}}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":"foo"}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":1}');
			service.loadSync();
			assert.deepEqual(service.workspaceBackupPaths, []);
		});
D
Daniel Imms 已提交
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		test('emptyWorkspaceBackupPaths should return [] when workspaces.json doesn\'t exist', () => {
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
		});

		test('emptyWorkspaceBackupPaths should return [] when workspaces.json is not properly formed JSON', () => {
			fs.writeFileSync(backupWorkspacesPath, '');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{]');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, 'foo');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
		});

		test('emptyWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is absent', () => {
			fs.writeFileSync(backupWorkspacesPath, '{}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
		});

		test('emptyWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array', () => {
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{}}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": ["bar"]}}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": []}}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":{"foo": "bar"}}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":"foo"}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
			fs.writeFileSync(backupWorkspacesPath, '{"emptyWorkspaces":1}');
			service.loadSync();
			assert.deepEqual(service.emptyWorkspaceBackupPaths, []);
		});
D
Daniel Imms 已提交
195
	});
196

D
Daniel Imms 已提交
197
	suite('sanitizeFolderWorkspaces', () => {
198 199 200 201 202 203
		test('should merge same cased paths on Windows and Mac', () => {
			// Skip test on Linux
			if (platform.isLinux) {
				return;
			}

D
Daniel Imms 已提交
204 205 206 207 208 209
			const backups: IBackupWorkspacesFormat = {
				folderWorkspaces: platform.isWindows ? ['c:\\foo', 'C:\\FOO', 'c:\\FOO'] : ['/foo', '/FOO'],
				emptyWorkspaces: []
			};

			service.sanitizeFolderWorkspaces(backups);
210 211

			if (platform.isWindows) {
D
Daniel Imms 已提交
212 213 214
				assert.deepEqual(backups.folderWorkspaces, ['c:\\foo']);
			} else {
				assert.deepEqual(backups.folderWorkspaces, ['/foo']);
215 216
			}
		});
D
Daniel Imms 已提交
217 218
	});

219 220 221 222 223 224 225 226
	suite('registerWindowForBackups', () => {
		test('pushWorkspaceBackupPathsSync should persist paths to workspaces.json', () => {
			service.registerWindowForBackups(1, false, null, fooFile.fsPath);
			service.registerWindowForBackups(2, false, null, barFile.fsPath);
			assert.deepEqual(service.workspaceBackupPaths, [fooFile.fsPath, barFile.fsPath]);
			pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
				assert.deepEqual(json.folderWorkspaces, [fooFile.fsPath, barFile.fsPath]);
227 228 229 230
			});
		});
	});

231
	suite('removeBackupPathSync', () => {
D
Daniel Imms 已提交
232
		test('should remove folder workspaces from workspaces.json', done => {
233 234
			service.registerWindowForBackups(1, false, null, fooFile.fsPath);
			service.registerWindowForBackups(2, false, null, barFile.fsPath);
235
			service.removeBackupPathSync(fooFile.fsPath, false);
236 237 238
			pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
				assert.deepEqual(json.folderWorkspaces, [barFile.fsPath]);
239
				service.removeBackupPathSync(barFile.fsPath, false);
240 241 242 243 244
				pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
					const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
					assert.deepEqual(json2.folderWorkspaces, []);
					done();
				});
245 246
			});
		});
247

D
Daniel Imms 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		test('should remove empty workspaces from workspaces.json', done => {
			service.registerWindowForBackups(1, true, 'foo');
			service.registerWindowForBackups(2, true, 'bar');
			service.removeBackupPathSync('foo', true);
			pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
				const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
				assert.deepEqual(json.emptyWorkspaces, ['bar']);
				service.removeBackupPathSync('bar', true);
				pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
					const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
					assert.deepEqual(json2.emptyWorkspaces, []);
					done();
				});
			});
		});

264
		test('should fail gracefully when removing a path that doesn\'t exist', done => {
265 266
			const workspacesJson: IBackupWorkspacesFormat = { folderWorkspaces: [fooFile.fsPath], emptyWorkspaces: [] };
			pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
267
				service.removeBackupPathSync(barFile.fsPath, false);
D
Daniel Imms 已提交
268
				service.removeBackupPathSync('test', true);
269 270 271 272 273 274 275
				pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
					const json = <IBackupWorkspacesFormat>JSON.parse(content);
					assert.deepEqual(json.folderWorkspaces, [fooFile.fsPath]);
					done();
				});
			});
		});
276
	});
D
Daniel Imms 已提交
277

278
	suite('getWorkspaceHash', () => {
D
Daniel Imms 已提交
279 280 281 282
		test('should perform an md5 hash on the path', () => {
			assert.equal(service.getWorkspaceHash('/foo'), '1effb2475fcfba4f9e8b8a1dbc8f3caf');
		});

283 284 285 286 287
		test('should ignore case on Windows and Mac', () => {
			// Skip test on Linux
			if (platform.isLinux) {
				return;
			}
D
Daniel Imms 已提交
288

289 290 291
			if (platform.isMacintosh) {
				assert.equal(service.getWorkspaceHash('/foo'), service.getWorkspaceHash('/FOO'));
			}
D
Daniel Imms 已提交
292

293 294 295 296
			if (platform.isWindows) {
				assert.equal(service.getWorkspaceHash('c:\\foo'), service.getWorkspaceHash('C:\\FOO'));
			}
		});
D
Daniel Imms 已提交
297
	});
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

	suite('getBackupPath', () => {
		test('should return the window\'s correct path', done => {
			service.registerWindowForBackups(1, false, null, fooFile.fsPath);
			service.registerWindowForBackups(2, true, 'test');
			service.getBackupPath(1).then(window1Path => {
				assert.equal(window1Path, service.toBackupPath(fooFile.fsPath));
				service.getBackupPath(2).then(window2Path => {
					assert.equal(window2Path, path.join(backupHome, 'test'));
					done();
				});
			});
		});

		test('should override stale window paths with new paths', done => {
			service.registerWindowForBackups(1, false, null, fooFile.fsPath);
			service.registerWindowForBackups(1, false, null, barFile.fsPath);
			service.getBackupPath(1).then(windowPath => {
				assert.equal(windowPath, service.toBackupPath(barFile.fsPath));
				done();
			});
		});

		test('should throw when the window is not registered', () => {
			assert.throws(() => service.getBackupPath(1));
		});
	});
325
});