backupFileService.test.ts 6.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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';
D
Daniel Imms 已提交
9
import * as platform from 'vs/base/common/platform';
10 11
import crypto = require('crypto');
import os = require('os');
12
import fs = require('fs');
13 14 15 16 17
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';
18 19
import { BackupFileService } from 'vs/workbench/services/backup/node/backupFileService';
import { FileService } from 'vs/workbench/services/files/node/fileService';
20

21
class TestBackupFileService extends BackupFileService {
D
Daniel Imms 已提交
22
	constructor(workspace: Uri, backupHome: string, workspacesJsonPath: string) {
23
		const fileService = new FileService(workspace.fsPath, { disableWatcher: true }, null);
24
		super(workspace, TestEnvironmentService, fileService);
25 26

		this.backupHome = backupHome;
D
Daniel Imms 已提交
27
		this.workspacesJsonPath = workspacesJsonPath;
28 29
	}
}
30

31
suite('BackupFileService', () => {
32 33
	const parentDir = path.join(os.tmpdir(), 'vsctests', 'service')
	const backupHome = path.join(parentDir, 'Backups');
D
Daniel Imms 已提交
34
	const workspacesJsonPath = path.join(backupHome, 'workspaces.json');
35

36
	const workspaceResource = Uri.file(platform.isWindows ? 'C:\\workspace' : '/workspace');
37
	const workspaceBackupPath = path.join(backupHome, crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex'));
D
Daniel Imms 已提交
38 39 40
	const fooFile = Uri.file(platform.isWindows ? 'C:\\foo' : '/foo');
	const barFile = Uri.file(platform.isWindows ? 'C:\\bar' : '/bar');
	const bazFile = Uri.file(platform.isWindows ? 'C:\\baz' : '/baz');
41
	const untitledFile = Uri.from({ scheme: 'untitled', path: 'Untitled-1' });
42 43
	const fooBackupPath = path.join(workspaceBackupPath, 'file', crypto.createHash('md5').update(fooFile.fsPath).digest('hex'));
	const barBackupPath = path.join(workspaceBackupPath, 'file', crypto.createHash('md5').update(barFile.fsPath).digest('hex'));
44
	const untitledBackupPath = path.join(workspaceBackupPath, 'untitled', untitledFile.fsPath);
D
Daniel Imms 已提交
45

46
	let service: BackupFileService;
47 48

	setup(done => {
D
Daniel Imms 已提交
49
		service = new TestBackupFileService(workspaceResource, backupHome, workspacesJsonPath);
50

51 52 53
		// Delete any existing backups completely and then re-create it.
		extfs.del(backupHome, os.tmpdir(), () => {
			pfs.mkdirp(backupHome).then(() => {
D
Daniel Imms 已提交
54
				pfs.writeFileAndFlush(workspacesJsonPath, '').then(() => {
55 56 57 58
					done();
				});
			});
		});
59 60 61
	});

	teardown(done => {
62
		extfs.del(backupHome, os.tmpdir(), done);
63 64 65 66
	});

	test('getBackupResource should get the correct backup path for text files', () => {
		// Format should be: <backupHome>/<workspaceHash>/<scheme>/<filePathHash>
67
		const backupResource = fooFile;
68 69
		const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
		const filePathHash = crypto.createHash('md5').update(backupResource.fsPath).digest('hex');
70
		const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'file', filePathHash)).fsPath;
71
		assert.equal(service.getBackupResource(backupResource).fsPath, expectedPath);
72 73 74
	});

	test('getBackupResource should get the correct backup path for untitled files', () => {
75 76
		// Format should be: <backupHome>/<workspaceHash>/<scheme>/<filePath>
		const backupResource = Uri.from({ scheme: 'untitled', path: 'Untitled-1' });
77
		const workspaceHash = crypto.createHash('md5').update(workspaceResource.fsPath).digest('hex');
78
		const expectedPath = Uri.file(path.join(backupHome, workspaceHash, 'untitled', backupResource.fsPath)).fsPath;
79
		assert.equal(service.getBackupResource(backupResource).fsPath, expectedPath);
80 81
	});

82
	test('doesTextFileHaveBackup should return whether a backup resource exists', done => {
83
		service.hasTextFileBackup(fooFile).then(exists => {
84
			assert.equal(exists, false);
85 86
			pfs.mkdirp(path.dirname(fooBackupPath)).then(() => {
				fs.writeFileSync(fooBackupPath, 'foo');
87
				service.hasTextFileBackup(fooFile).then(exists2 => {
88
					assert.equal(exists2, true);
89
					done();
90 91 92 93
				});
			});
		});
	});
94

95 96
	test('backupResource - text file', function (done: () => void) {
		service.backupResource(fooFile, 'test').then(() => {
97 98 99 100 101 102 103
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
			assert.equal(fs.existsSync(fooBackupPath), true);
			assert.equal(fs.readFileSync(fooBackupPath), 'test');
			done();
		});
	});

104 105
	test('backupResource - untitled file', function (done: () => void) {
		service.backupResource(untitledFile, 'test').then(() => {
106 107 108 109 110 111 112
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
			assert.equal(fs.existsSync(untitledBackupPath), true);
			assert.equal(fs.readFileSync(untitledBackupPath), 'test');
			done();
		});
	});

113 114
	test('discardResourceBackup - text file', function (done: () => void) {
		service.backupResource(fooFile, 'test').then(() => {
115
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
116
			service.discardResourceBackup(fooFile).then(() => {
117 118 119 120 121 122 123
				assert.equal(fs.existsSync(fooBackupPath), false);
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 0);
				done();
			});
		});
	});

124 125
	test('discardResourceBackup - untitled file', function (done: () => void) {
		service.backupResource(untitledFile, 'test').then(() => {
126
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
127
			service.discardResourceBackup(untitledFile).then(() => {
128 129 130 131 132 133 134
				assert.equal(fs.existsSync(untitledBackupPath), false);
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 0);
				done();
			});
		});
	});

135 136
	test('discardAllWorkspaceBackups - text file', function (done: () => void) {
		service.backupResource(fooFile, 'test').then(() => {
137
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 1);
138
			service.backupResource(barFile, 'test').then(() => {
139
				assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'file')).length, 2);
140
				service.discardAllWorkspaceBackups().then(() => {
141 142 143 144 145 146 147 148 149
					assert.equal(fs.existsSync(fooBackupPath), false);
					assert.equal(fs.existsSync(barBackupPath), false);
					assert.equal(fs.existsSync(path.join(workspaceBackupPath, 'file')), false);
					done();
				});
			});
		});
	});

150 151
	test('discardAllWorkspaceBackups - untitled file', function (done: () => void) {
		service.backupResource(untitledFile, 'test').then(() => {
152
			assert.equal(fs.readdirSync(path.join(workspaceBackupPath, 'untitled')).length, 1);
153
			service.discardAllWorkspaceBackups().then(() => {
154 155 156 157 158 159
				assert.equal(fs.existsSync(untitledBackupPath), false);
				assert.equal(fs.existsSync(path.join(workspaceBackupPath, 'untitled')), false);
				done();
			});
		});
	});
160
});