backupMainService.test.ts 6.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';
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 33 34 35 36 37 38 39 40 41

	public removeWorkspaceBackupPathSync(workspace: Uri): void {
		return super.removeWorkspaceBackupPathSync(workspace);
	}

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

	public toBackupPath(workspacePath: string): string {
		return super.toBackupPath(workspacePath);
	}
42 43
}

D
Daniel Imms 已提交
44
suite('BackupMainService', () => {
D
Daniel Imms 已提交
45
	const parentDir = path.join(os.tmpdir(), 'vsctests', 'service');
46 47 48 49 50 51
	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');

52
	let service: TestBackupMainService;
53 54

	setup(done => {
D
Daniel Imms 已提交
55
		service = new TestBackupMainService(backupHome, backupWorkspacesPath);
56 57 58 59

		// Delete any existing backups completely and then re-create it.
		extfs.del(backupHome, os.tmpdir(), () => {
			pfs.mkdirp(backupHome).then(() => {
D
Daniel Imms 已提交
60
				done();
61 62 63 64 65 66 67 68
			});
		});
	});

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

69
	test('getWorkspaceBackupPaths should return [] when workspaces.json doesn\'t exist', () => {
D
Daniel Imms 已提交
70
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
71 72
	});

73
	test('getWorkspaceBackupPaths should return [] when workspaces.json is not properly formed JSON', () => {
D
Daniel Imms 已提交
74
		fs.writeFileSync(backupWorkspacesPath, '');
D
Daniel Imms 已提交
75
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
76
		fs.writeFileSync(backupWorkspacesPath, '{]');
D
Daniel Imms 已提交
77
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
78
		fs.writeFileSync(backupWorkspacesPath, 'foo');
D
Daniel Imms 已提交
79
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
80 81
	});

82
	test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is absent', () => {
D
Daniel Imms 已提交
83
		fs.writeFileSync(backupWorkspacesPath, '{}');
D
Daniel Imms 已提交
84
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
85 86
	});

87
	test('getWorkspaceBackupPaths should return [] when folderWorkspaces in workspaces.json is not a string array', () => {
D
Daniel Imms 已提交
88
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{}}');
D
Daniel Imms 已提交
89
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
90
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": ["bar"]}}');
D
Daniel Imms 已提交
91
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
92
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": []}}');
D
Daniel Imms 已提交
93
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
94
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":{"foo": "bar"}}');
D
Daniel Imms 已提交
95
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
96
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":"foo"}');
D
Daniel Imms 已提交
97
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
98
		fs.writeFileSync(backupWorkspacesPath, '{"folderWorkspaces":1}');
D
Daniel Imms 已提交
99
		assert.deepEqual(service.getWorkspaceBackupPaths(), []);
D
Daniel Imms 已提交
100 101
	});

102
	test('pushWorkspaceBackupPathsSync should persist paths to workspaces.json', () => {
D
Daniel Imms 已提交
103 104
		service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
		assert.deepEqual(service.getWorkspaceBackupPaths(), [fooFile.fsPath, barFile.fsPath]);
105 106
	});

107
	test('removeWorkspaceBackupPath should remove workspaces from workspaces.json', done => {
D
Daniel Imms 已提交
108 109
		service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
		service.removeWorkspaceBackupPathSync(fooFile);
110 111 112
		pfs.readFile(backupWorkspacesPath, 'utf-8').then(buffer => {
			const json = <IBackupWorkspacesFormat>JSON.parse(buffer);
			assert.deepEqual(json.folderWorkspaces, [barFile.fsPath]);
D
Daniel Imms 已提交
113
			service.removeWorkspaceBackupPathSync(barFile);
114 115 116 117
			pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
				const json2 = <IBackupWorkspacesFormat>JSON.parse(content);
				assert.deepEqual(json2.folderWorkspaces, []);
				done();
118 119 120 121 122 123
			});
		});
	});

	test('removeWorkspaceBackupPath should fail gracefully when removing a path that doesn\'t exist', done => {
		const workspacesJson: IBackupWorkspacesFormat = { folderWorkspaces: [fooFile.fsPath] };
124
		pfs.writeFile(backupWorkspacesPath, JSON.stringify(workspacesJson)).then(() => {
D
Daniel Imms 已提交
125
			service.removeWorkspaceBackupPathSync(barFile);
126 127 128 129 130 131 132
			pfs.readFile(backupWorkspacesPath, 'utf-8').then(content => {
				const json = <IBackupWorkspacesFormat>JSON.parse(content);
				assert.deepEqual(json.folderWorkspaces, [fooFile.fsPath]);
				done();
			});
		});
	});
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	test('service validates backup workspaces on startup and cleans up', done => {

		// 1) backup workspace path does not exist
		service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
		service.loadSync();
		assert.equal(service.getWorkspaceBackupPaths().length, 0);

		// 2) backup workspace path exists with empty contents within
		fs.mkdirSync(service.toBackupPath(fooFile.fsPath));
		fs.mkdirSync(service.toBackupPath(barFile.fsPath));
		service.pushWorkspaceBackupPathsSync([fooFile, barFile]);
		service.loadSync();
		assert.equal(service.getWorkspaceBackupPaths().length, 0);
		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.pushWorkspaceBackupPathsSync([fooFile, barFile]);
		service.loadSync();
		assert.equal(service.getWorkspaceBackupPaths().length, 0);
		assert.ok(!fs.exists(service.toBackupPath(fooFile.fsPath)));
		assert.ok(!fs.exists(service.toBackupPath(barFile.fsPath)));

		done();
162
	});
163
});