chockidarWatcherService.test.ts 12.5 KB
Newer Older
M
Martin Aeschlimann 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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';
8
import * as path from 'vs/base/common/path';
M
Martin Aeschlimann 已提交
9 10 11 12 13
import * as pfs from 'vs/base/node/pfs';
import { normalizeRoots, ChokidarWatcherService } from '../chokidarWatcherService';
import { IWatcherRequest } from '../watcher';
import * as platform from 'vs/base/common/platform';
import { Delayer } from 'vs/base/common/async';
14
import { IDiskFileChange } from 'vs/platform/files/node/watcher/watcher';
M
Martin Aeschlimann 已提交
15 16
import { FileChangeType } from 'vs/platform/files/common/files';

17
function newRequest(basePath: string, ignored: string[] = []): IWatcherRequest {
18
	return { path: basePath, excludes: ignored };
M
Martin Aeschlimann 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
}

function assertNormalizedRootPath(inputPaths: string[], expectedPaths: string[]) {
	const requests = inputPaths.map(path => newRequest(path));
	const actual = normalizeRoots(requests);
	assert.deepEqual(Object.keys(actual).sort(), expectedPaths);
}

function assertNormalizedRequests(inputRequests: IWatcherRequest[], expectedRequests: { [path: string]: IWatcherRequest[] }) {
	const actual = normalizeRoots(inputRequests);
	const actualPath = Object.keys(actual).sort();
	const expectedPaths = Object.keys(expectedRequests).sort();
	assert.deepEqual(actualPath, expectedPaths);
	for (let path of actualPath) {
33 34
		let a = expectedRequests[path].sort((r1, r2) => r1.path.localeCompare(r2.path));
		let e = expectedRequests[path].sort((r1, r2) => r1.path.localeCompare(r2.path));
M
Martin Aeschlimann 已提交
35 36 37 38
		assert.deepEqual(a, e);
	}
}

39
function sort(changes: IDiskFileChange[]) {
M
Martin Aeschlimann 已提交
40 41 42 43 44 45 46 47 48
	return changes.sort((c1, c2) => {
		return c1.path.localeCompare(c2.path);
	});
}

function wait(time: number) {
	return new Delayer<void>(time).trigger(() => { });
}

49
async function assertFileEvents(actuals: IDiskFileChange[], expected: IDiskFileChange[]) {
M
Martin Aeschlimann 已提交
50 51 52 53 54 55 56 57
	let repeats = 40;
	while ((actuals.length < expected.length) && repeats-- > 0) {
		await wait(50);
	}
	assert.deepEqual(sort(actuals), sort(expected));
	actuals.length = 0;
}

B
Benjamin Pasero 已提交
58
suite('Chokidar normalizeRoots', () => {
M
Martin Aeschlimann 已提交
59 60 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
	test('should not impacts roots that don\'t overlap', () => {
		if (platform.isWindows) {
			assertNormalizedRootPath(['C:\\a'], ['C:\\a']);
			assertNormalizedRootPath(['C:\\a', 'C:\\b'], ['C:\\a', 'C:\\b']);
			assertNormalizedRootPath(['C:\\a', 'C:\\b', 'C:\\c\\d\\e'], ['C:\\a', 'C:\\b', 'C:\\c\\d\\e']);
		} else {
			assertNormalizedRootPath(['/a'], ['/a']);
			assertNormalizedRootPath(['/a', '/b'], ['/a', '/b']);
			assertNormalizedRootPath(['/a', '/b', '/c/d/e'], ['/a', '/b', '/c/d/e']);
		}
	});

	test('should remove sub-folders of other roots', () => {
		if (platform.isWindows) {
			assertNormalizedRootPath(['C:\\a', 'C:\\a\\b'], ['C:\\a']);
			assertNormalizedRootPath(['C:\\a', 'C:\\b', 'C:\\a\\b'], ['C:\\a', 'C:\\b']);
			assertNormalizedRootPath(['C:\\b\\a', 'C:\\a', 'C:\\b', 'C:\\a\\b'], ['C:\\a', 'C:\\b']);
			assertNormalizedRootPath(['C:\\a', 'C:\\a\\b', 'C:\\a\\c\\d'], ['C:\\a']);
		} else {
			assertNormalizedRootPath(['/a', '/a/b'], ['/a']);
			assertNormalizedRootPath(['/a', '/b', '/a/b'], ['/a', '/b']);
			assertNormalizedRootPath(['/b/a', '/a', '/b', '/a/b'], ['/a', '/b']);
			assertNormalizedRootPath(['/a', '/a/b', '/a/c/d'], ['/a']);
			assertNormalizedRootPath(['/a/c/d/e', '/a/b/d', '/a/c/d', '/a/c/e/f', '/a/b'], ['/a/b', '/a/c/d', '/a/c/e/f']);
		}
	});

	test('should remove duplicates', () => {
		if (platform.isWindows) {
			assertNormalizedRootPath(['C:\\a', 'C:\\a\\', 'C:\\a'], ['C:\\a']);
		} else {
			assertNormalizedRootPath(['/a', '/a/', '/a'], ['/a']);
			assertNormalizedRootPath(['/a', '/b', '/a/b'], ['/a', '/b']);
			assertNormalizedRootPath(['/b/a', '/a', '/b', '/a/b'], ['/a', '/b']);
			assertNormalizedRootPath(['/a', '/a/b', '/a/c/d'], ['/a']);
		}
	});

	test('nested requests', () => {
		let p1, p2, p3;
		if (platform.isWindows) {
			p1 = 'C:\\a';
			p2 = 'C:\\a\\b';
			p3 = 'C:\\a\\b\\c';
		} else {
			p1 = '/a';
			p2 = '/a/b';
			p3 = '/a/b/c';
		}
		const r1 = newRequest(p1, ['**/*.ts']);
		const r2 = newRequest(p2, ['**/*.js']);
		const r3 = newRequest(p3, ['**/*.ts']);
		assertNormalizedRequests([r1, r2], { [p1]: [r1, r2] });
		assertNormalizedRequests([r2, r1], { [p1]: [r1, r2] });
		assertNormalizedRequests([r1, r2, r3], { [p1]: [r1, r2, r3] });
		assertNormalizedRequests([r1, r3], { [p1]: [r1] });
		assertNormalizedRequests([r2, r3], { [p2]: [r2, r3] });
	});
});

B
Benjamin Pasero 已提交
119
suite.skip('Chokidar watching', () => {
M
Martin Aeschlimann 已提交
120
	const tmpdir = os.tmpdir();
B
Benjamin Pasero 已提交
121
	const testDir = path.join(tmpdir, 'chokidartest-' + Date.now());
M
Martin Aeschlimann 已提交
122 123 124 125 126
	const aFolder = path.join(testDir, 'a');
	const bFolder = path.join(testDir, 'b');
	const b2Folder = path.join(bFolder, 'b2');

	const service = new ChokidarWatcherService();
127
	const result: IDiskFileChange[] = [];
128
	let error: string | null = null;
M
Martin Aeschlimann 已提交
129 130 131 132 133 134 135

	suiteSetup(async () => {
		await pfs.mkdirp(testDir);
		await pfs.mkdirp(aFolder);
		await pfs.mkdirp(bFolder);
		await pfs.mkdirp(b2Folder);

136 137 138 139
		const opts = { verboseLogging: false, pollingInterval: 200 };
		service.watch(opts)(e => {
			if (Array.isArray(e)) {
				result.push(...e);
140 141 142 143 144 145
			}
		});
		service.onLogMessage(msg => {
			if (msg.type === 'error') {
				console.log('set error', msg.message);
				error = msg.message;
M
Martin Aeschlimann 已提交
146
			}
147
		});
M
Martin Aeschlimann 已提交
148 149 150
	});

	suiteTeardown(async () => {
B
Benjamin Pasero 已提交
151
		await pfs.rimraf(testDir, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
		await service.stop();
	});

	setup(() => {
		result.length = 0;
		assert.equal(error, null);
	});

	teardown(() => {
		assert.equal(error, null);
	});

	test('simple file operations, single root, no ignore', async () => {
165
		let request: IWatcherRequest = { path: testDir, excludes: [] };
M
Martin Aeschlimann 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		service.setRoots([request]);
		await wait(300);

		assert.equal(service.wacherCount, 1);

		// create a file
		let testFilePath = path.join(testDir, 'file.txt');
		await pfs.writeFile(testFilePath, '');
		await assertFileEvents(result, [{ path: testFilePath, type: FileChangeType.ADDED }]);

		// modify a file
		await pfs.writeFile(testFilePath, 'Hello');
		await assertFileEvents(result, [{ path: testFilePath, type: FileChangeType.UPDATED }]);

		// create a folder
		let testFolderPath = path.join(testDir, 'newFolder');
		await pfs.mkdirp(testFolderPath);
		// copy a file
		let copiedFilePath = path.join(testFolderPath, 'file2.txt');
		await pfs.copy(testFilePath, copiedFilePath);
		await assertFileEvents(result, [{ path: copiedFilePath, type: FileChangeType.ADDED }, { path: testFolderPath, type: FileChangeType.ADDED }]);

		// delete a file
B
Benjamin Pasero 已提交
189
		await pfs.rimraf(copiedFilePath, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
190 191 192 193 194 195
		let renamedFilePath = path.join(testFolderPath, 'file3.txt');
		// move a file
		await pfs.rename(testFilePath, renamedFilePath);
		await assertFileEvents(result, [{ path: copiedFilePath, type: FileChangeType.DELETED }, { path: testFilePath, type: FileChangeType.DELETED }, { path: renamedFilePath, type: FileChangeType.ADDED }]);

		// delete a folder
B
Benjamin Pasero 已提交
196
		await pfs.rimraf(testFolderPath, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
197 198 199 200
		await assertFileEvents(result, [{ path: testFolderPath, type: FileChangeType.DELETED }, { path: renamedFilePath, type: FileChangeType.DELETED }]);
	});

	test('simple file operations, ignore', async () => {
201
		let request: IWatcherRequest = { path: testDir, excludes: ['**/b/**', '**/*.js', '.git/**'] };
M
Martin Aeschlimann 已提交
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
		service.setRoots([request]);
		await wait(300);

		assert.equal(service.wacherCount, 1);

		// create various ignored files
		let file1 = path.join(bFolder, 'file1.txt'); // hidden
		await pfs.writeFile(file1, 'Hello');
		let file2 = path.join(b2Folder, 'file2.txt'); // hidden
		await pfs.writeFile(file2, 'Hello');
		let folder1 = path.join(bFolder, 'folder1'); // hidden
		await pfs.mkdirp(folder1);
		let folder2 = path.join(aFolder, 'b'); // hidden
		await pfs.mkdirp(folder2);
		let folder3 = path.join(testDir, '.git'); // hidden
		await pfs.mkdirp(folder3);
		let folder4 = path.join(testDir, '.git1');
		await pfs.mkdirp(folder4);
		let folder5 = path.join(aFolder, '.git');
		await pfs.mkdirp(folder5);
		let file3 = path.join(aFolder, 'file3.js'); // hidden
		await pfs.writeFile(file3, 'var x;');
		let file4 = path.join(aFolder, 'file4.txt');
		await pfs.writeFile(file4, 'Hello');
		await assertFileEvents(result, [{ path: file4, type: FileChangeType.ADDED }, { path: folder4, type: FileChangeType.ADDED }, { path: folder5, type: FileChangeType.ADDED }]);

		// move some files
		let movedFile1 = path.join(folder2, 'file1.txt'); // from ignored to ignored
		await pfs.rename(file1, movedFile1);
		let movedFile2 = path.join(aFolder, 'file2.txt'); // from ignored to visible
		await pfs.rename(file2, movedFile2);
		let movedFile3 = path.join(aFolder, 'file3.txt'); // from ignored file ext to visible
		await pfs.rename(file3, movedFile3);
		await assertFileEvents(result, [{ path: movedFile2, type: FileChangeType.ADDED }, { path: movedFile3, type: FileChangeType.ADDED }]);

		// delete all files
B
Benjamin Pasero 已提交
238 239 240 241 242 243 244 245 246
		await pfs.rimraf(movedFile1); // hidden
		await pfs.rimraf(movedFile2, pfs.RimRafMode.MOVE);
		await pfs.rimraf(movedFile3, pfs.RimRafMode.MOVE);
		await pfs.rimraf(folder1); // hidden
		await pfs.rimraf(folder2); // hidden
		await pfs.rimraf(folder3); // hidden
		await pfs.rimraf(folder4, pfs.RimRafMode.MOVE);
		await pfs.rimraf(folder5, pfs.RimRafMode.MOVE);
		await pfs.rimraf(file4, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
247 248 249 250
		await assertFileEvents(result, [{ path: movedFile2, type: FileChangeType.DELETED }, { path: movedFile3, type: FileChangeType.DELETED }, { path: file4, type: FileChangeType.DELETED }, { path: folder4, type: FileChangeType.DELETED }, { path: folder5, type: FileChangeType.DELETED }]);
	});

	test('simple file operations, multiple roots', async () => {
251 252
		let request1: IWatcherRequest = { path: aFolder, excludes: ['**/*.js'] };
		let request2: IWatcherRequest = { path: b2Folder, excludes: ['**/*.ts'] };
M
Martin Aeschlimann 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		service.setRoots([request1, request2]);
		await wait(300);

		assert.equal(service.wacherCount, 2);

		// create some files
		let folderPath1 = path.join(aFolder, 'folder1');
		await pfs.mkdirp(folderPath1);
		let filePath1 = path.join(folderPath1, 'file1.json');
		await pfs.writeFile(filePath1, '');
		let filePath2 = path.join(folderPath1, 'file2.js'); // filtered
		await pfs.writeFile(filePath2, '');
		let folderPath2 = path.join(b2Folder, 'folder2');
		await pfs.mkdirp(folderPath2);
		let filePath3 = path.join(folderPath2, 'file3.ts'); // filtered
		await pfs.writeFile(filePath3, '');
		let filePath4 = path.join(testDir, 'file4.json'); // outside roots
		await pfs.writeFile(filePath4, '');

		await assertFileEvents(result, [{ path: folderPath1, type: FileChangeType.ADDED }, { path: filePath1, type: FileChangeType.ADDED }, { path: folderPath2, type: FileChangeType.ADDED }]);

		// change roots
275
		let request3: IWatcherRequest = { path: aFolder, excludes: ['**/*.json'] };
M
Martin Aeschlimann 已提交
276 277 278 279 280 281
		service.setRoots([request3]);
		await wait(300);

		assert.equal(service.wacherCount, 1);

		// delete all
B
Benjamin Pasero 已提交
282 283 284
		await pfs.rimraf(folderPath1, pfs.RimRafMode.MOVE);
		await pfs.rimraf(folderPath2, pfs.RimRafMode.MOVE);
		await pfs.rimraf(filePath4, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
285 286 287 288 289

		await assertFileEvents(result, [{ path: folderPath1, type: FileChangeType.DELETED }, { path: filePath2, type: FileChangeType.DELETED }]);
	});

	test('simple file operations, nested roots', async () => {
290 291
		let request1: IWatcherRequest = { path: testDir, excludes: ['**/b2/**'] };
		let request2: IWatcherRequest = { path: bFolder, excludes: ['**/b3/**'] };
M
Martin Aeschlimann 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
		service.setRoots([request1, request2]);
		await wait(300);

		assert.equal(service.wacherCount, 1);

		// create files
		let filePath1 = path.join(bFolder, 'file1.xml'); // visible by both
		await pfs.writeFile(filePath1, '');
		let filePath2 = path.join(b2Folder, 'file2.xml'); // filtered by root1, but visible by root2
		await pfs.writeFile(filePath2, '');
		let folderPath1 = path.join(b2Folder, 'b3'); // filtered
		await pfs.mkdirp(folderPath1);
		let filePath3 = path.join(folderPath1, 'file3.xml'); // filtered
		await pfs.writeFile(filePath3, '');

		await assertFileEvents(result, [{ path: filePath1, type: FileChangeType.ADDED }, { path: filePath2, type: FileChangeType.ADDED }]);

		let renamedFilePath2 = path.join(folderPath1, 'file2.xml');
		// move a file
		await pfs.rename(filePath2, renamedFilePath2);
		await assertFileEvents(result, [{ path: filePath2, type: FileChangeType.DELETED }]);

		// delete all
B
Benjamin Pasero 已提交
315 316
		await pfs.rimraf(folderPath1, pfs.RimRafMode.MOVE);
		await pfs.rimraf(filePath1, pfs.RimRafMode.MOVE);
M
Martin Aeschlimann 已提交
317 318 319 320 321 322

		await assertFileEvents(result, [{ path: filePath1, type: FileChangeType.DELETED }]);
	});

});