fileUserDataProvider.test.ts 19.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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';
import * as os from 'os';
import * as path from 'vs/base/common/path';
import * as uuid from 'vs/base/common/uuid';
import * as pfs from 'vs/base/node/pfs';
import { IFileService, FileChangeType, IFileChange, IFileSystemProviderWithFileReadWriteCapability, IStat, FileType, FileSystemProviderCapabilities } from 'vs/platform/files/common/files';
12
import { FileService } from 'vs/platform/files/common/fileService';
13 14 15 16 17 18
import { NullLogService } from 'vs/platform/log/common/log';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { FileUserDataProvider } from 'vs/workbench/services/userData/common/fileUserDataProvider';
import { joinPath, dirname } from 'vs/base/common/resources';
import { VSBuffer } from 'vs/base/common/buffer';
19
import { DiskFileSystemProvider } from 'vs/platform/files/electron-browser/diskFileSystemProvider';
20 21 22 23 24 25
import { BACKUPS } from 'vs/platform/environment/common/environment';
import { DisposableStore, IDisposable, Disposable } from 'vs/base/common/lifecycle';
import { BrowserWorkbenchEnvironmentService } from 'vs/workbench/services/environment/browser/environmentService';
import { Emitter, Event } from 'vs/base/common/event';
import { timeout } from 'vs/base/common/async';

26 27 28 29 30 31 32 33 34
class TestBrowserWorkbenchEnvironmentService extends BrowserWorkbenchEnvironmentService {

	testUserRoamingDataHome!: URI;

	get userRoamingDataHome(): URI {
		return this.testUserRoamingDataHome;
	}
}

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
suite('FileUserDataProvider', () => {

	let testObject: IFileService;
	let rootPath: string;
	let userDataPath: string;
	let backupsPath: string;
	let userDataResource: URI;
	const disposables = new DisposableStore();

	setup(async () => {
		const logService = new NullLogService();
		testObject = new FileService(logService);
		disposables.add(testObject);

		const diskFileSystemProvider = new DiskFileSystemProvider(logService);
		disposables.add(diskFileSystemProvider);
		disposables.add(testObject.registerProvider(Schemas.file, diskFileSystemProvider));

		rootPath = path.join(os.tmpdir(), 'vsctests', uuid.generateUuid());
		userDataPath = path.join(rootPath, 'user');
		backupsPath = path.join(rootPath, BACKUPS);
		userDataResource = URI.file(userDataPath).with({ scheme: Schemas.userData });
		await Promise.all([pfs.mkdirp(userDataPath), pfs.mkdirp(backupsPath)]);

59 60
		const environmentService = new TestBrowserWorkbenchEnvironmentService({ remoteAuthority: 'remote', workspaceId: 'workspaceId', logsPath: URI.file('logFile') });
		environmentService.testUserRoamingDataHome = userDataResource;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

		const userDataFileSystemProvider = new FileUserDataProvider(URI.file(userDataPath), URI.file(backupsPath), diskFileSystemProvider, environmentService);
		disposables.add(userDataFileSystemProvider);
		disposables.add(testObject.registerProvider(Schemas.userData, userDataFileSystemProvider));
	});

	teardown(async () => {
		disposables.clear();
		await pfs.rimraf(rootPath, pfs.RimRafMode.MOVE);
	});

	test('exists return false when file does not exist', async () => {
		const exists = await testObject.exists(joinPath(userDataResource, 'settings.json'));
		assert.equal(exists, false);
	});

	test('read file throws error if not exist', async () => {
		try {
			await testObject.readFile(joinPath(userDataResource, 'settings.json'));
			assert.fail('Should fail since file does not exist');
		} catch (e) { }
	});

	test('read existing file', async () => {
		await pfs.writeFile(path.join(userDataPath, 'settings.json'), '{}');
		const result = await testObject.readFile(joinPath(userDataResource, 'settings.json'));
		assert.equal(result.value, '{}');
	});

	test('create file', async () => {
		const resource = joinPath(userDataResource, 'settings.json');
		const actual1 = await testObject.createFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual2 = await pfs.readFile(path.join(userDataPath, 'settings.json'));
		assert.equal(actual2, '{}');
	});

	test('write file creates the file if not exist', async () => {
		const resource = joinPath(userDataResource, 'settings.json');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual2 = await pfs.readFile(path.join(userDataPath, 'settings.json'));
		assert.equal(actual2, '{}');
	});

	test('write to existing file', async () => {
		const resource = joinPath(userDataResource, 'settings.json');
		await pfs.writeFile(path.join(userDataPath, 'settings.json'), '{}');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{a:1}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual2 = await pfs.readFile(path.join(userDataPath, 'settings.json'));
		assert.equal(actual2, '{a:1}');
	});

	test('delete file', async () => {
		await pfs.writeFile(path.join(userDataPath, 'settings.json'), '');
		await testObject.del(joinPath(userDataResource, 'settings.json'));
		const result = await pfs.exists(path.join(userDataPath, 'settings.json'));
		assert.equal(false, result);
	});

	test('resolve file', async () => {
		await pfs.writeFile(path.join(userDataPath, 'settings.json'), '');
		const result = await testObject.resolve(joinPath(userDataResource, 'settings.json'));
		assert.ok(!result.isDirectory);
		assert.ok(result.children === undefined);
	});

	test('exists return false for folder that does not exist', async () => {
		const exists = await testObject.exists(joinPath(userDataResource, 'snippets'));
		assert.equal(exists, false);
	});

	test('exists return true for folder that exists', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		const exists = await testObject.exists(joinPath(userDataResource, 'snippets'));
		assert.equal(exists, true);
	});

	test('read file throws error for folder', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		try {
			await testObject.readFile(joinPath(userDataResource, 'snippets'));
			assert.fail('Should fail since read file is not supported for folders');
		} catch (e) { }
	});

	test('read file under folder', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		await pfs.writeFile(path.join(userDataPath, 'snippets', 'settings.json'), '{}');
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual = await testObject.readFile(resource);
		assert.equal(actual.resource.toString(), resource.toString());
		assert.equal(actual.value, '{}');
	});

	test('read file under sub folder', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets', 'java'));
		await pfs.writeFile(path.join(userDataPath, 'snippets', 'java', 'settings.json'), '{}');
		const resource = joinPath(userDataResource, 'snippets/java/settings.json');
		const actual = await testObject.readFile(resource);
		assert.equal(actual.resource.toString(), resource.toString());
		assert.equal(actual.value, '{}');
	});

	test('create file under folder that exists', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual1 = await testObject.createFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual2 = await pfs.readFile(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(actual2, '{}');
	});

	test('create file under folder that does not exist', async () => {
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual1 = await testObject.createFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual2 = await pfs.readFile(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(actual2, '{}');
	});

	test('write to not existing file under container that exists', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual = await pfs.readFile(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(actual, '{}');
	});

	test('write to not existing file under container that does not exists', async () => {
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual = await pfs.readFile(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(actual, '{}');
	});

	test('write to existing file under container', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		await pfs.writeFile(path.join(userDataPath, 'snippets', 'settings.json'), '{}');
		const resource = joinPath(userDataResource, 'snippets/settings.json');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{a:1}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual = await pfs.readFile(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(actual.toString(), '{a:1}');
	});

	test('write file under sub container', async () => {
		const resource = joinPath(userDataResource, 'snippets/java/settings.json');
		const actual1 = await testObject.writeFile(resource, VSBuffer.fromString('{}'));
		assert.equal(actual1.resource.toString(), resource.toString());
		const actual = await pfs.readFile(path.join(userDataPath, 'snippets', 'java', 'settings.json'));
		assert.equal(actual, '{}');
	});

	test('delete throws error for folder that does not exist', async () => {
		try {
			await testObject.del(joinPath(userDataResource, 'snippets'));
			assert.fail('Should fail the folder does not exist');
		} catch (e) { }
	});

	test('delete not existing file under container that exists', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
		try {
			await testObject.del(joinPath(userDataResource, 'snippets/settings.json'));
			assert.fail('Should fail since file does not exist');
		} catch (e) { }
	});

	test('delete not existing file under container that does not exists', async () => {
		try {
			await testObject.del(joinPath(userDataResource, 'snippets/settings.json'));
			assert.fail('Should fail since file does not exist');
		} catch (e) { }
	});

	test('delete existing file under folder', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
S
Sandeep Somavarapu 已提交
242
		await pfs.writeFile(path.join(userDataPath, 'snippets', 'settings.json'), '{}');
243 244 245 246 247 248 249
		await testObject.del(joinPath(userDataResource, 'snippets/settings.json'));
		const exists = await pfs.exists(path.join(userDataPath, 'snippets', 'settings.json'));
		assert.equal(exists, false);
	});

	test('resolve folder', async () => {
		await pfs.mkdirp(path.join(userDataPath, 'snippets'));
S
Sandeep Somavarapu 已提交
250
		await pfs.writeFile(path.join(userDataPath, 'snippets', 'settings.json'), '{}');
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
		const result = await testObject.resolve(joinPath(userDataResource, 'snippets'));
		assert.ok(result.isDirectory);
		assert.ok(result.children !== undefined);
		assert.equal(result.children!.length, 1);
		assert.equal(result.children![0].resource.toString(), joinPath(userDataResource, 'snippets/settings.json').toString());
	});

	test('read backup file', async () => {
		await pfs.writeFile(path.join(backupsPath, 'backup.json'), '{}');
		const result = await testObject.readFile(joinPath(userDataResource, `${BACKUPS}/backup.json`));
		assert.equal(result.value, '{}');
	});

	test('create backup file', async () => {
		await testObject.createFile(joinPath(userDataResource, `${BACKUPS}/backup.json`), VSBuffer.fromString('{}'));
		const result = await pfs.readFile(path.join(backupsPath, 'backup.json'));
		assert.equal(result, '{}');
	});

	test('write backup file', async () => {
		await pfs.writeFile(path.join(backupsPath, 'backup.json'), '{}');
		await testObject.writeFile(joinPath(userDataResource, `${BACKUPS}/backup.json`), VSBuffer.fromString('{a:1}'));
		const result = await pfs.readFile(path.join(backupsPath, 'backup.json'));
		assert.equal(result, '{a:1}');
	});

	test('resolve backups folder', async () => {
S
Sandeep Somavarapu 已提交
278
		await pfs.writeFile(path.join(backupsPath, 'backup.json'), '{}');
279 280 281 282 283 284 285 286 287 288
		const result = await testObject.resolve(joinPath(userDataResource, BACKUPS));
		assert.ok(result.isDirectory);
		assert.ok(result.children !== undefined);
		assert.equal(result.children!.length, 1);
		assert.equal(result.children![0].resource.toString(), joinPath(userDataResource, `${BACKUPS}/backup.json`).toString());
	});
});

class TestFileSystemProvider implements IFileSystemProviderWithFileReadWriteCapability {

M
Matt Bierner 已提交
289
	constructor(readonly onDidChangeFile: Event<readonly IFileChange[]>) { }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

	readonly capabilities: FileSystemProviderCapabilities = FileSystemProviderCapabilities.FileReadWrite;

	readonly onDidChangeCapabilities: Event<void> = Event.None;

	watch(): IDisposable { return Disposable.None; }

	stat(): Promise<IStat> { throw new Error('Not Supported'); }

	mkdir(resource: URI): Promise<void> { throw new Error('Not Supported'); }

	rename(): Promise<void> { throw new Error('Not Supported'); }

	readFile(resource: URI): Promise<Uint8Array> { throw new Error('Not Supported'); }

	readdir(resource: URI): Promise<[string, FileType][]> { throw new Error('Not Supported'); }

	writeFile(): Promise<void> { throw new Error('Not Supported'); }

	delete(): Promise<void> { throw new Error('Not Supported'); }

}

suite('FileUserDataProvider - Watching', () => {

	let testObject: IFileService;
	let localBackupsResource: URI;
	let localUserDataResource: URI;
	let userDataResource: URI;
	const disposables = new DisposableStore();

M
Matt Bierner 已提交
321
	const fileEventEmitter: Emitter<readonly IFileChange[]> = new Emitter<readonly IFileChange[]>();
322 323 324 325 326 327 328 329 330 331 332
	disposables.add(fileEventEmitter);

	setup(() => {

		const rootPath = path.join(os.tmpdir(), 'vsctests', uuid.generateUuid());
		const userDataPath = path.join(rootPath, 'user');
		const backupsPath = path.join(rootPath, BACKUPS);
		localBackupsResource = URI.file(backupsPath);
		localUserDataResource = URI.file(userDataPath);
		userDataResource = localUserDataResource.with({ scheme: Schemas.userData });

333 334
		const environmentService = new TestBrowserWorkbenchEnvironmentService({ remoteAuthority: 'remote', workspaceId: 'workspaceId', logsPath: URI.file('logFile') });
		environmentService.testUserRoamingDataHome = userDataResource;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

		const userDataFileSystemProvider = new FileUserDataProvider(localUserDataResource, localBackupsResource, new TestFileSystemProvider(fileEventEmitter.event), environmentService);
		disposables.add(userDataFileSystemProvider);

		testObject = new FileService(new NullLogService());
		disposables.add(testObject);
		disposables.add(testObject.registerProvider(Schemas.userData, userDataFileSystemProvider));
	});

	teardown(() => {
		disposables.clear();
	});

	test('file added change event', done => {
		const expected = joinPath(userDataResource, 'settings.json');
		const target = joinPath(localUserDataResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.ADDED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.ADDED
		}]);
	});

	test('file updated change event', done => {
		const expected = joinPath(userDataResource, 'settings.json');
		const target = joinPath(localUserDataResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.UPDATED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.UPDATED
		}]);
	});

	test('file deleted change event', done => {
		const expected = joinPath(userDataResource, 'settings.json');
		const target = joinPath(localUserDataResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.DELETED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.DELETED
		}]);
	});

	test('file under folder created change event', done => {
		const expected = joinPath(userDataResource, 'snippets', 'settings.json');
		const target = joinPath(localUserDataResource, 'snippets', 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.ADDED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.ADDED
		}]);
	});

	test('file under folder updated change event', done => {
		const expected = joinPath(userDataResource, 'snippets', 'settings.json');
		const target = joinPath(localUserDataResource, 'snippets', 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.UPDATED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.UPDATED
		}]);
	});

	test('file under folder deleted change event', done => {
		const expected = joinPath(userDataResource, 'snippets', 'settings.json');
		const target = joinPath(localUserDataResource, 'snippets', 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.DELETED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.DELETED
		}]);
	});

	test('event is not triggered if file is not under user data', async () => {
		const target = joinPath(dirname(localUserDataResource), 'settings.json');
		let triggered = false;
		testObject.onFileChanges(() => triggered = true);
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.DELETED
		}]);
		await timeout(0);
		if (triggered) {
			assert.fail('event should not be triggered');
		}
	});

	test('backup file created change event', done => {
		const expected = joinPath(userDataResource, BACKUPS, 'settings.json');
		const target = joinPath(localBackupsResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.ADDED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.ADDED
		}]);
	});

	test('backup file update change event', done => {
		const expected = joinPath(userDataResource, BACKUPS, 'settings.json');
		const target = joinPath(localBackupsResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.UPDATED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.UPDATED
		}]);
	});

	test('backup file delete change event', done => {
		const expected = joinPath(userDataResource, BACKUPS, 'settings.json');
		const target = joinPath(localBackupsResource, 'settings.json');
		testObject.onFileChanges(e => {
			if (e.contains(expected, FileChangeType.DELETED)) {
				done();
			}
		});
		fileEventEmitter.fire([{
			resource: target,
			type: FileChangeType.DELETED
		}]);
	});
487
});