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

import * as assert from 'assert';
D
Daniel Imms 已提交
7
import * as platform from 'vs/base/common/platform';
8 9
import * as os from 'os';
import * as fs from 'fs';
10
import * as path from 'vs/base/common/path';
11
import * as pfs from 'vs/base/node/pfs';
12
import { URI as Uri } from 'vs/base/common/uri';
13
import { BackupFileService, BackupFilesModel, hashPath } from 'vs/workbench/services/backup/node/backupFileService';
B
Benjamin Pasero 已提交
14
import { FileService } from 'vs/workbench/services/files/node/fileService';
15
import { TextModel, createTextBufferFactory } from 'vs/editor/common/model/textModel';
B
Benjamin Pasero 已提交
16
import { TestContextService, TestTextResourceConfigurationService, TestLifecycleService, TestEnvironmentService, TestStorageService, TestWindowService } from 'vs/workbench/test/workbenchTestServices';
17
import { getRandomTestPath } from 'vs/base/test/node/testUtils';
18
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
19
import { Workspace, toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
20
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
21 22
import { DefaultEndOfLine } from 'vs/editor/common/model';
import { snapshotToString } from 'vs/platform/files/common/files';
23
import { Schemas } from 'vs/base/common/network';
B
Benjamin Pasero 已提交
24
import { IWindowConfiguration } from 'vs/platform/windows/common/windows';
25

B
Benjamin Pasero 已提交
26
const parentDir = getRandomTestPath(os.tmpdir(), 'vsctests', 'backupfileservice');
D
Daniel Imms 已提交
27 28 29 30
const backupHome = path.join(parentDir, 'Backups');
const workspacesJsonPath = path.join(backupHome, 'workspaces.json');

const workspaceResource = Uri.file(platform.isWindows ? 'c:\\workspace' : '/workspace');
31
const workspaceBackupPath = path.join(backupHome, hashPath(workspaceResource));
32 33
const fooFile = Uri.file(platform.isWindows ? 'c:\\Foo' : '/Foo');
const barFile = Uri.file(platform.isWindows ? 'c:\\Bar' : '/Bar');
34
const untitledFile = Uri.from({ scheme: Schemas.untitled, path: 'Untitled-1' });
35 36 37
const fooBackupPath = path.join(workspaceBackupPath, 'file', hashPath(fooFile));
const barBackupPath = path.join(workspaceBackupPath, 'file', hashPath(barFile));
const untitledBackupPath = path.join(workspaceBackupPath, 'untitled', hashPath(untitledFile));
D
Daniel Imms 已提交
38

B
Benjamin Pasero 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class TestBackupWindowService extends TestWindowService {

	private config: IWindowConfiguration;

	constructor(workspaceBackupPath: string) {
		super();

		this.config = Object.create(null);
		this.config.backupPath = workspaceBackupPath;
	}

	getConfiguration(): IWindowConfiguration {
		return this.config;
	}
}

55 56
class TestBackupFileService extends BackupFileService {
	constructor(workspace: Uri, backupHome: string, workspacesJsonPath: string) {
I
isidor 已提交
57
		const fileService = new FileService(new TestContextService(new Workspace(workspace.fsPath, toWorkspaceFolders([{ path: workspace.fsPath }]))), TestEnvironmentService, new TestTextResourceConfigurationService(), new TestConfigurationService(), new TestLifecycleService(), new TestStorageService(), new TestNotificationService(), { disableWatcher: true });
B
Benjamin Pasero 已提交
58
		const windowService = new TestBackupWindowService(workspaceBackupPath);
59

B
Benjamin Pasero 已提交
60
		super(windowService, fileService);
61 62
	}

63 64
	public toBackupResource(resource: Uri): Uri {
		return super.toBackupResource(resource);
65 66 67
	}
}

D
Daniel Imms 已提交
68
suite('BackupFileService', () => {
B
Benjamin Pasero 已提交
69
	let service: TestBackupFileService;
70

71
	setup(() => {
D
Daniel Imms 已提交
72
		service = new TestBackupFileService(workspaceResource, backupHome, workspacesJsonPath);
73

74
		// Delete any existing backups completely and then re-create it.
75 76 77
		return pfs.del(backupHome, os.tmpdir()).then(() => {
			return pfs.mkdirp(backupHome).then(() => {
				return pfs.writeFile(workspacesJsonPath, '');
78 79
			});
		});
80 81
	});

82 83
	teardown(() => {
		return pfs.del(backupHome, os.tmpdir());
84 85
	});

D
Daniel Imms 已提交
86 87 88 89
	suite('getBackupResource', () => {
		test('should get the correct backup path for text files', () => {
			// Format should be: <backupHome>/<workspaceHash>/<scheme>/<filePathHash>
			const backupResource = fooFile;
90 91
			const workspaceHash = hashPath(workspaceResource);
			const filePathHash = hashPath(backupResource);
D
Daniel Imms 已提交
92
			const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'file', filePathHash)).fsPath;
93
			assert.equal(service.toBackupResource(backupResource).fsPath, expectedPath);
D
Daniel Imms 已提交
94
		});
95

D
Daniel Imms 已提交
96 97
		test('should get the correct backup path for untitled files', () => {
			// Format should be: <backupHome>/<workspaceHash>/<scheme>/<filePath>
98
			const backupResource = Uri.from({ scheme: Schemas.untitled, path: 'Untitled-1' });
99 100
			const workspaceHash = hashPath(workspaceResource);
			const filePathHash = hashPath(backupResource);
D
Daniel Imms 已提交
101
			const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'untitled', filePathHash)).fsPath;
102
			assert.equal(service.toBackupResource(backupResource).fsPath, expectedPath);
D
Daniel Imms 已提交
103
		});
104 105
	});

106
	suite('loadBackupResource', () => {
107 108
		test('should return whether a backup resource exists', () => {
			return pfs.mkdirp(path.dirname(fooBackupPath)).then(() => {
D
Daniel Imms 已提交
109 110
				fs.writeFileSync(fooBackupPath, 'foo');
				service = new TestBackupFileService(workspaceResource, backupHome, workspacesJsonPath);
111
				return service.loadBackupResource(fooFile).then(resource => {
112
					assert.ok(resource);
113
					assert.equal(path.basename(resource!.fsPath), path.basename(fooBackupPath));
114 115 116 117 118 119
					return service.hasBackups().then(hasBackups => {
						assert.ok(hasBackups);
					});
				});
			});
		});
D
Daniel Imms 已提交
120
	});
121

D
Daniel Imms 已提交
122
	suite('backupResource', () => {
123 124
		test('text file', function () {
			return service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
125 126 127
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
				assert.equal(fs.existsSync(fooBackupPath), true);
				assert.equal(fs.readFileSync(fooBackupPath), `${fooFile.toString()}\ntest`);
128 129 130
			});
		});

131 132
		test('untitled file', function () {
			return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
133 134 135
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
				assert.equal(fs.existsSync(untitledBackupPath), true);
				assert.equal(fs.readFileSync(untitledBackupPath), `${untitledFile.toString()}\ntest`);
136 137
			});
		});
138

139
		test('text file (ITextSnapshot)', function () {
140 141
			const model = TextModel.createFromString('test');

142
			return service.backupResource(fooFile, model.createSnapshot()).then(() => {
143 144 145 146 147 148 149
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
				assert.equal(fs.existsSync(fooBackupPath), true);
				assert.equal(fs.readFileSync(fooBackupPath), `${fooFile.toString()}\ntest`);
				model.dispose();
			});
		});

150
		test('untitled file (ITextSnapshot)', function () {
151 152
			const model = TextModel.createFromString('test');

153
			return service.backupResource(untitledFile, model.createSnapshot()).then(() => {
154 155 156 157 158 159 160
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
				assert.equal(fs.existsSync(untitledBackupPath), true);
				assert.equal(fs.readFileSync(untitledBackupPath), `${untitledFile.toString()}\ntest`);
				model.dispose();
			});
		});

161
		test('text file (large file, ITextSnapshot)', function () {
B
Benjamin Pasero 已提交
162
			const largeString = (new Array(10 * 1024)).join('Large String\n');
163 164
			const model = TextModel.createFromString(largeString);

165
			return service.backupResource(fooFile, model.createSnapshot()).then(() => {
166 167 168 169 170 171 172
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
				assert.equal(fs.existsSync(fooBackupPath), true);
				assert.equal(fs.readFileSync(fooBackupPath), `${fooFile.toString()}\n${largeString}`);
				model.dispose();
			});
		});

173
		test('untitled file (large file, ITextSnapshot)', function () {
B
Benjamin Pasero 已提交
174
			const largeString = (new Array(10 * 1024)).join('Large String\n');
175 176
			const model = TextModel.createFromString(largeString);

177
			return service.backupResource(untitledFile, model.createSnapshot()).then(() => {
178 179 180 181 182 183
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
				assert.equal(fs.existsSync(untitledBackupPath), true);
				assert.equal(fs.readFileSync(untitledBackupPath), `${untitledFile.toString()}\n${largeString}`);
				model.dispose();
			});
		});
184 185
	});

D
Daniel Imms 已提交
186
	suite('discardResourceBackup', () => {
187 188
		test('text file', function () {
			return service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
189
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
190
				return service.discardResourceBackup(fooFile).then(() => {
191
					assert.equal(fs.existsSync(fooBackupPath), false);
D
Daniel Imms 已提交
192
					assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 0);
193 194 195 196
				});
			});
		});

197 198
		test('untitled file', function () {
			return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
199
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
200
				return service.discardResourceBackup(untitledFile).then(() => {
D
Daniel Imms 已提交
201 202 203
					assert.equal(fs.existsSync(untitledBackupPath), false);
					assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 0);
				});
204 205 206
			});
		});
	});
B
Benjamin Pasero 已提交
207

D
Daniel Imms 已提交
208
	suite('discardAllWorkspaceBackups', () => {
209 210
		test('text file', function () {
			return service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
211
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
212
				return service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
213
					assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 2);
214
					return service.discardAllWorkspaceBackups().then(() => {
D
Daniel Imms 已提交
215 216 217
						assert.equal(fs.existsSync(fooBackupPath), false);
						assert.equal(fs.existsSync(barBackupPath), false);
						assert.equal(fs.existsSync(path.join(workspaceBackupPath, 'file')), false);
D
Daniel Imms 已提交
218 219 220 221 222
					});
				});
			});
		});

223 224
		test('untitled file', function () {
			return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
D
Daniel Imms 已提交
225
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
226
				return service.discardAllWorkspaceBackups().then(() => {
D
Daniel Imms 已提交
227 228 229
					assert.equal(fs.existsSync(untitledBackupPath), false);
					assert.equal(fs.existsSync(path.join(workspaceBackupPath, 'untitled')), false);
				});
230 231
			});
		});
232

233 234 235
		test('should disable further backups', function () {
			return service.discardAllWorkspaceBackups().then(() => {
				return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
236 237 238 239
					assert.equal(fs.existsSync(workspaceBackupPath), false);
				});
			});
		});
240 241
	});

D
Daniel Imms 已提交
242
	suite('getWorkspaceFileBackups', () => {
243 244 245
		test('("file") - text file', () => {
			return service.backupResource(fooFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
				return service.getWorkspaceFileBackups().then(textFiles => {
D
Daniel Imms 已提交
246
					assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath]);
247 248
					return service.backupResource(barFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
						return service.getWorkspaceFileBackups().then(textFiles => {
D
Daniel Imms 已提交
249 250 251 252 253 254 255
							assert.deepEqual(textFiles.map(f => f.fsPath), [fooFile.fsPath, barFile.fsPath]);
						});
					});
				});
			});
		});

256 257 258
		test('("file") - untitled file', () => {
			return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
				return service.getWorkspaceFileBackups().then(textFiles => {
D
Daniel Imms 已提交
259 260 261 262 263
					assert.deepEqual(textFiles.map(f => f.fsPath), [untitledFile.fsPath]);
				});
			});
		});

264 265 266
		test('("untitled") - untitled file', () => {
			return service.backupResource(untitledFile, createTextBufferFactory('test').create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
				return service.getWorkspaceFileBackups().then(textFiles => {
D
Daniel Imms 已提交
267 268
					assert.deepEqual(textFiles.map(f => f.fsPath), ['Untitled-1']);
				});
269 270 271 272
			});
		});
	});

273 274 275
	test('resolveBackupContent', () => {
		test('should restore the original contents (untitled file)', () => {
			const contents = 'test\nand more stuff';
276
			service.backupResource(untitledFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
277 278 279 280 281 282 283 284 285 286 287 288 289 290
				service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
					assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
				});
			});
		});

		test('should restore the original contents (text file)', () => {
			const contents = [
				'Lorem ipsum ',
				'dolor öäü sit amet ',
				'consectetur ',
				'adipiscing ßß elit',
			].join('');

291
			service.backupResource(fooFile, createTextBufferFactory(contents).create(DefaultEndOfLine.LF).createSnapshot(false)).then(() => {
292 293 294 295
				service.resolveBackupContent(service.toBackupResource(untitledFile)).then(factory => {
					assert.equal(contents, snapshotToString(factory.create(platform.isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(true)));
				});
			});
D
Daniel Imms 已提交
296
		});
D
Daniel Imms 已提交
297
	});
D
Daniel Imms 已提交
298
});
D
Daniel Imms 已提交
299

D
Daniel Imms 已提交
300 301
suite('BackupFilesModel', () => {
	test('simple', () => {
B
Benjamin Pasero 已提交
302
		const model = new BackupFilesModel();
B
Benjamin Pasero 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

		const resource1 = Uri.file('test.html');

		assert.equal(model.has(resource1), false);

		model.add(resource1);

		assert.equal(model.has(resource1), true);
		assert.equal(model.has(resource1, 0), true);
		assert.equal(model.has(resource1, 1), false);

		model.remove(resource1);

		assert.equal(model.has(resource1), false);

		model.add(resource1);

		assert.equal(model.has(resource1), true);
		assert.equal(model.has(resource1, 0), true);
		assert.equal(model.has(resource1, 1), false);

		model.clear();

		assert.equal(model.has(resource1), false);

		model.add(resource1, 1);

		assert.equal(model.has(resource1), true);
		assert.equal(model.has(resource1, 0), false);
		assert.equal(model.has(resource1, 1), true);

		const resource2 = Uri.file('test1.html');
		const resource3 = Uri.file('test2.html');
		const resource4 = Uri.file('test3.html');

		model.add(resource2);
		model.add(resource3);
		model.add(resource4);

		assert.equal(model.has(resource1), true);
		assert.equal(model.has(resource2), true);
		assert.equal(model.has(resource3), true);
		assert.equal(model.has(resource4), true);
	});

348 349
	test('resolve', () => {
		return pfs.mkdirp(path.dirname(fooBackupPath)).then(() => {
B
Benjamin Pasero 已提交
350 351
			fs.writeFileSync(fooBackupPath, 'foo');

B
Benjamin Pasero 已提交
352
			const model = new BackupFilesModel();
B
Benjamin Pasero 已提交
353

354
			return model.resolve(workspaceBackupPath).then(model => {
B
Benjamin Pasero 已提交
355 356 357 358
				assert.equal(model.has(Uri.file(fooBackupPath)), true);
			});
		});
	});
359

D
Daniel Imms 已提交
360
	test('get', () => {
361 362
		const model = new BackupFilesModel();

363
		assert.deepEqual(model.get(), []);
364

D
Daniel Imms 已提交
365 366 367
		const file1 = Uri.file('/root/file/foo.html');
		const file2 = Uri.file('/root/file/bar.html');
		const untitled = Uri.file('/root/untitled/bar.html');
368

D
Daniel Imms 已提交
369 370 371 372
		model.add(file1);
		model.add(file2);
		model.add(untitled);

373
		assert.deepEqual(model.get().map(f => f.fsPath), [file1.fsPath, file2.fsPath, untitled.fsPath]);
374
	});
375
});