1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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
242
243
244
245
246
247
248
249
250
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
278
279
280
281
282
283
284
285
286
287
288
289
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
|
/*---------------------------------------------------------------------------------------------
* 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 { Emitter, Event } from 'vs/base/common/event';
import { isLinux, isWindows } from 'vs/base/common/platform';
import { isEqual } from 'vs/base/common/resources';
import { URI as uri } from 'vs/base/common/uri';
import { FileChangesEvent, FileChangeType, IFileChange } from 'vs/platform/files/common/files';
import { IDiskFileChange, coalesceEvents, toFileChanges, parseWatcherPatterns } from 'vs/platform/files/common/watcher';
class TestFileWatcher {
private readonly _onDidFilesChange: Emitter<{ raw: IFileChange[]; event: FileChangesEvent }>;
constructor() {
this._onDidFilesChange = new Emitter<{ raw: IFileChange[]; event: FileChangesEvent }>();
}
get onDidFilesChange(): Event<{ raw: IFileChange[]; event: FileChangesEvent }> {
return this._onDidFilesChange.event;
}
report(changes: IDiskFileChange[]): void {
this.onRawFileEvents(changes);
}
private onRawFileEvents(events: IDiskFileChange[]): void {
// Coalesce
let coalescedEvents = coalesceEvents(events);
// Emit through event emitter
if (coalescedEvents.length > 0) {
this._onDidFilesChange.fire({ raw: toFileChanges(coalescedEvents), event: this.toFileChangesEvent(coalescedEvents) });
}
}
private toFileChangesEvent(changes: IDiskFileChange[]): FileChangesEvent {
return new FileChangesEvent(toFileChanges(changes), !isLinux);
}
}
enum Path {
UNIX,
WINDOWS,
UNC
}
suite('Watcher', () => {
(isWindows ? test.skip : test)('parseWatcherPatterns - posix', () => {
const path = '/users/data/src';
let parsedPattern = parseWatcherPatterns(path, ['*.js'])[0];
assert.strictEqual(parsedPattern('/users/data/src/foo.js'), true);
assert.strictEqual(parsedPattern('/users/data/src/foo.ts'), false);
assert.strictEqual(parsedPattern('/users/data/src/bar/foo.js'), false);
parsedPattern = parseWatcherPatterns(path, ['/users/data/src/*.js'])[0];
assert.strictEqual(parsedPattern('/users/data/src/foo.js'), true);
assert.strictEqual(parsedPattern('/users/data/src/foo.ts'), false);
assert.strictEqual(parsedPattern('/users/data/src/bar/foo.js'), false);
parsedPattern = parseWatcherPatterns(path, ['/users/data/src/bar/*.js'])[0];
assert.strictEqual(parsedPattern('/users/data/src/foo.js'), false);
assert.strictEqual(parsedPattern('/users/data/src/foo.ts'), false);
assert.strictEqual(parsedPattern('/users/data/src/bar/foo.js'), true);
parsedPattern = parseWatcherPatterns(path, ['**/*.js'])[0];
assert.strictEqual(parsedPattern('/users/data/src/foo.js'), true);
assert.strictEqual(parsedPattern('/users/data/src/foo.ts'), false);
assert.strictEqual(parsedPattern('/users/data/src/bar/foo.js'), true);
});
(!isWindows ? test.skip : test)('parseWatcherPatterns - windows', () => {
const path = 'c:\\users\\data\\src';
let parsedPattern = parseWatcherPatterns(path, ['*.js'])[0];
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.js'), true);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.ts'), false);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\bar/foo.js'), false);
parsedPattern = parseWatcherPatterns(path, ['c:\\users\\data\\src\\*.js'])[0];
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.js'), true);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.ts'), false);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\bar\\foo.js'), false);
parsedPattern = parseWatcherPatterns(path, ['c:\\users\\data\\src\\bar/*.js'])[0];
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.js'), false);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.ts'), false);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\bar\\foo.js'), true);
parsedPattern = parseWatcherPatterns(path, ['**/*.js'])[0];
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.js'), true);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\foo.ts'), false);
assert.strictEqual(parsedPattern('c:\\users\\data\\src\\bar\\foo.js'), true);
});
});
suite('Watcher Events Normalizer', () => {
test('simple add/update/delete', done => {
const watch = new TestFileWatcher();
const added = uri.file('/users/data/src/added.txt');
const updated = uri.file('/users/data/src/updated.txt');
const deleted = uri.file('/users/data/src/deleted.txt');
const raw: IDiskFileChange[] = [
{ path: added.fsPath, type: FileChangeType.ADDED },
{ path: updated.fsPath, type: FileChangeType.UPDATED },
{ path: deleted.fsPath, type: FileChangeType.DELETED },
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 3);
assert.ok(event.contains(added, FileChangeType.ADDED));
assert.ok(event.contains(updated, FileChangeType.UPDATED));
assert.ok(event.contains(deleted, FileChangeType.DELETED));
done();
});
watch.report(raw);
});
(isWindows ? [Path.WINDOWS, Path.UNC] : [Path.UNIX]).forEach(path => {
test(`delete only reported for top level folder (${path})`, done => {
const watch = new TestFileWatcher();
const deletedFolderA = uri.file(path === Path.UNIX ? '/users/data/src/todelete1' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\todelete1' : '\\\\localhost\\users\\data\\src\\todelete1');
const deletedFolderB = uri.file(path === Path.UNIX ? '/users/data/src/todelete2' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\todelete2' : '\\\\localhost\\users\\data\\src\\todelete2');
const deletedFolderBF1 = uri.file(path === Path.UNIX ? '/users/data/src/todelete2/file.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\todelete2\\file.txt' : '\\\\localhost\\users\\data\\src\\todelete2\\file.txt');
const deletedFolderBF2 = uri.file(path === Path.UNIX ? '/users/data/src/todelete2/more/test.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\todelete2\\more\\test.txt' : '\\\\localhost\\users\\data\\src\\todelete2\\more\\test.txt');
const deletedFolderBF3 = uri.file(path === Path.UNIX ? '/users/data/src/todelete2/super/bar/foo.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\todelete2\\super\\bar\\foo.txt' : '\\\\localhost\\users\\data\\src\\todelete2\\super\\bar\\foo.txt');
const deletedFileA = uri.file(path === Path.UNIX ? '/users/data/src/deleteme.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\deleteme.txt' : '\\\\localhost\\users\\data\\src\\deleteme.txt');
const addedFile = uri.file(path === Path.UNIX ? '/users/data/src/added.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\added.txt' : '\\\\localhost\\users\\data\\src\\added.txt');
const updatedFile = uri.file(path === Path.UNIX ? '/users/data/src/updated.txt' : path === Path.WINDOWS ? 'C:\\users\\data\\src\\updated.txt' : '\\\\localhost\\users\\data\\src\\updated.txt');
const raw: IDiskFileChange[] = [
{ path: deletedFolderA.fsPath, type: FileChangeType.DELETED },
{ path: deletedFolderB.fsPath, type: FileChangeType.DELETED },
{ path: deletedFolderBF1.fsPath, type: FileChangeType.DELETED },
{ path: deletedFolderBF2.fsPath, type: FileChangeType.DELETED },
{ path: deletedFolderBF3.fsPath, type: FileChangeType.DELETED },
{ path: deletedFileA.fsPath, type: FileChangeType.DELETED },
{ path: addedFile.fsPath, type: FileChangeType.ADDED },
{ path: updatedFile.fsPath, type: FileChangeType.UPDATED }
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 5);
assert.ok(event.contains(deletedFolderA, FileChangeType.DELETED));
assert.ok(event.contains(deletedFolderB, FileChangeType.DELETED));
assert.ok(event.contains(deletedFileA, FileChangeType.DELETED));
assert.ok(event.contains(addedFile, FileChangeType.ADDED));
assert.ok(event.contains(updatedFile, FileChangeType.UPDATED));
done();
});
watch.report(raw);
});
});
test('event coalescer: ignore CREATE followed by DELETE', done => {
const watch = new TestFileWatcher();
const created = uri.file('/users/data/src/related');
const deleted = uri.file('/users/data/src/related');
const unrelated = uri.file('/users/data/src/unrelated');
const raw: IDiskFileChange[] = [
{ path: created.fsPath, type: FileChangeType.ADDED },
{ path: deleted.fsPath, type: FileChangeType.DELETED },
{ path: unrelated.fsPath, type: FileChangeType.UPDATED },
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 1);
assert.ok(event.contains(unrelated, FileChangeType.UPDATED));
done();
});
watch.report(raw);
});
test('event coalescer: flatten DELETE followed by CREATE into CHANGE', done => {
const watch = new TestFileWatcher();
const deleted = uri.file('/users/data/src/related');
const created = uri.file('/users/data/src/related');
const unrelated = uri.file('/users/data/src/unrelated');
const raw: IDiskFileChange[] = [
{ path: deleted.fsPath, type: FileChangeType.DELETED },
{ path: created.fsPath, type: FileChangeType.ADDED },
{ path: unrelated.fsPath, type: FileChangeType.UPDATED },
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 2);
assert.ok(event.contains(deleted, FileChangeType.UPDATED));
assert.ok(event.contains(unrelated, FileChangeType.UPDATED));
done();
});
watch.report(raw);
});
test('event coalescer: ignore UPDATE when CREATE received', done => {
const watch = new TestFileWatcher();
const created = uri.file('/users/data/src/related');
const updated = uri.file('/users/data/src/related');
const unrelated = uri.file('/users/data/src/unrelated');
const raw: IDiskFileChange[] = [
{ path: created.fsPath, type: FileChangeType.ADDED },
{ path: updated.fsPath, type: FileChangeType.UPDATED },
{ path: unrelated.fsPath, type: FileChangeType.UPDATED },
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 2);
assert.ok(event.contains(created, FileChangeType.ADDED));
assert.ok(!event.contains(created, FileChangeType.UPDATED));
assert.ok(event.contains(unrelated, FileChangeType.UPDATED));
done();
});
watch.report(raw);
});
test('event coalescer: apply DELETE', done => {
const watch = new TestFileWatcher();
const updated = uri.file('/users/data/src/related');
const updated2 = uri.file('/users/data/src/related');
const deleted = uri.file('/users/data/src/related');
const unrelated = uri.file('/users/data/src/unrelated');
const raw: IDiskFileChange[] = [
{ path: updated.fsPath, type: FileChangeType.UPDATED },
{ path: updated2.fsPath, type: FileChangeType.UPDATED },
{ path: unrelated.fsPath, type: FileChangeType.UPDATED },
{ path: updated.fsPath, type: FileChangeType.DELETED }
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 2);
assert.ok(event.contains(deleted, FileChangeType.DELETED));
assert.ok(!event.contains(updated, FileChangeType.UPDATED));
assert.ok(event.contains(unrelated, FileChangeType.UPDATED));
done();
});
watch.report(raw);
});
test('event coalescer: track case renames', done => {
const watch = new TestFileWatcher();
const oldPath = uri.file('/users/data/src/added');
const newPath = uri.file('/users/data/src/ADDED');
const raw: IDiskFileChange[] = [
{ path: newPath.fsPath, type: FileChangeType.ADDED },
{ path: oldPath.fsPath, type: FileChangeType.DELETED }
];
watch.onDidFilesChange(({ event, raw }) => {
assert.ok(event);
assert.strictEqual(raw.length, 2);
for (const r of raw) {
if (isEqual(r.resource, oldPath)) {
assert.strictEqual(r.type, FileChangeType.DELETED);
} else if (isEqual(r.resource, newPath)) {
assert.strictEqual(r.type, FileChangeType.ADDED);
} else {
assert.fail();
}
}
done();
});
watch.report(raw);
});
});
|