Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/files/test/common/files.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { isEqual, isEqualOrParent } from '../../../../base/common/extpath.js';
8
import { isLinux, isMacintosh, isWindows } from '../../../../base/common/platform.js';
9
import { URI } from '../../../../base/common/uri.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite, toResource } from '../../../../base/test/common/utils.js';
11
import { FileChangesEvent, FileChangeType, IFileChange, isParent } from '../../common/files.js';
12
13
suite('Files', () => {
14
15
test('FileChangesEvent - basics', function () {
16
const changes = [
17
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED },
18
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },
19
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED },
20
{ resource: toResource.call(this, '/bar/deleted.txt'), type: FileChangeType.DELETED },
21
{ resource: toResource.call(this, '/bar/folder'), type: FileChangeType.DELETED },
22
{ resource: toResource.call(this, '/BAR/FOLDER'), type: FileChangeType.DELETED }
23
];
24
25
for (const ignorePathCasing of [false, true]) {
26
const event = new FileChangesEvent(changes, ignorePathCasing);
27
28
assert(!event.contains(toResource.call(this, '/foo'), FileChangeType.UPDATED));
29
assert(event.affects(toResource.call(this, '/foo'), FileChangeType.UPDATED));
30
assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED));
31
assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED));
32
assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED));
33
assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED));
34
assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED, FileChangeType.DELETED));
35
assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED, FileChangeType.DELETED));
36
assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED));
37
assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED));
38
assert(!event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED));
39
40
assert(event.contains(toResource.call(this, '/bar/folder'), FileChangeType.DELETED));
41
assert(event.contains(toResource.call(this, '/BAR/FOLDER'), FileChangeType.DELETED));
42
assert(event.affects(toResource.call(this, '/BAR'), FileChangeType.DELETED));
43
if (ignorePathCasing) {
44
assert(event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED));
45
assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED));
46
} else {
47
assert(!event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED));
48
assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED));
49
}
50
assert(event.contains(toResource.call(this, '/bar/folder/somefile'), FileChangeType.DELETED));
51
assert(event.contains(toResource.call(this, '/bar/folder/somefile/test.txt'), FileChangeType.DELETED));
52
assert(event.contains(toResource.call(this, '/BAR/FOLDER/somefile/test.txt'), FileChangeType.DELETED));
53
if (ignorePathCasing) {
54
assert(event.contains(toResource.call(this, '/BAR/folder/somefile/test.txt'), FileChangeType.DELETED));
55
} else {
56
assert(!event.contains(toResource.call(this, '/BAR/folder/somefile/test.txt'), FileChangeType.DELETED));
57
}
58
assert(!event.contains(toResource.call(this, '/bar/folder2/somefile'), FileChangeType.DELETED));
59
60
assert.strictEqual(1, event.rawAdded.length);
61
assert.strictEqual(2, event.rawUpdated.length);
62
assert.strictEqual(3, event.rawDeleted.length);
63
assert.strictEqual(true, event.gotAdded());
64
assert.strictEqual(true, event.gotUpdated());
65
assert.strictEqual(true, event.gotDeleted());
66
}
67
});
68
69
test('FileChangesEvent - supports multiple changes on file tree', function () {
70
for (const type of [FileChangeType.ADDED, FileChangeType.UPDATED, FileChangeType.DELETED]) {
71
const changes = [
72
{ resource: toResource.call(this, '/foo/bar/updated.txt'), type },
73
{ resource: toResource.call(this, '/foo/bar/otherupdated.txt'), type },
74
{ resource: toResource.call(this, '/foo/bar'), type },
75
{ resource: toResource.call(this, '/foo'), type },
76
{ resource: toResource.call(this, '/bar'), type },
77
{ resource: toResource.call(this, '/bar/foo'), type },
78
{ resource: toResource.call(this, '/bar/foo/updated.txt'), type },
79
{ resource: toResource.call(this, '/bar/foo/otherupdated.txt'), type }
80
];
81
82
for (const ignorePathCasing of [false, true]) {
83
const event = new FileChangesEvent(changes, ignorePathCasing);
84
85
for (const change of changes) {
86
assert(event.contains(change.resource, type));
87
assert(event.affects(change.resource, type));
88
}
89
90
assert(event.affects(toResource.call(this, '/foo'), type));
91
assert(event.affects(toResource.call(this, '/bar'), type));
92
assert(event.affects(toResource.call(this, '/'), type));
93
assert(!event.affects(toResource.call(this, '/foobar'), type));
94
95
assert(!event.contains(toResource.call(this, '/some/foo/bar'), type));
96
assert(!event.affects(toResource.call(this, '/some/foo/bar'), type));
97
assert(!event.contains(toResource.call(this, '/some/bar'), type));
98
assert(!event.affects(toResource.call(this, '/some/bar'), type));
99
100
switch (type) {
101
case FileChangeType.ADDED:
102
assert.strictEqual(8, event.rawAdded.length);
103
break;
104
case FileChangeType.DELETED:
105
assert.strictEqual(8, event.rawDeleted.length);
106
break;
107
}
108
}
109
}
110
});
111
112
test('FileChangesEvent - correlation', function () {
113
let changes: IFileChange[] = [
114
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED },
115
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },
116
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED },
117
];
118
119
let event: FileChangesEvent = new FileChangesEvent(changes, true);
120
assert.strictEqual(event.hasCorrelation(), false);
121
assert.strictEqual(event.correlates(100), false);
122
123
changes = [
124
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },
125
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED, cId: 100 },
126
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },
127
];
128
129
event = new FileChangesEvent(changes, true);
130
assert.strictEqual(event.hasCorrelation(), true);
131
assert.strictEqual(event.correlates(100), true);
132
assert.strictEqual(event.correlates(120), false);
133
134
changes = [
135
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },
136
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },
137
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },
138
];
139
140
event = new FileChangesEvent(changes, true);
141
assert.strictEqual(event.hasCorrelation(), false);
142
assert.strictEqual(event.correlates(100), false);
143
assert.strictEqual(event.correlates(120), false);
144
145
changes = [
146
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },
147
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED, cId: 120 },
148
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },
149
];
150
151
event = new FileChangesEvent(changes, true);
152
assert.strictEqual(event.hasCorrelation(), false);
153
assert.strictEqual(event.correlates(100), false);
154
assert.strictEqual(event.correlates(120), false);
155
});
156
157
function testIsEqual(testMethod: (pA: string, pB: string, ignoreCase: boolean) => boolean): void {
158
159
// corner cases
160
assert(testMethod('', '', true));
161
assert(!testMethod(null!, '', true));
162
assert(!testMethod(undefined!, '', true));
163
164
// basics (string)
165
assert(testMethod('/', '/', true));
166
assert(testMethod('/some', '/some', true));
167
assert(testMethod('/some/path', '/some/path', true));
168
169
assert(testMethod('c:\\', 'c:\\', true));
170
assert(testMethod('c:\\some', 'c:\\some', true));
171
assert(testMethod('c:\\some\\path', 'c:\\some\\path', true));
172
173
assert(testMethod('/someöäü/path', '/someöäü/path', true));
174
assert(testMethod('c:\\someöäü\\path', 'c:\\someöäü\\path', true));
175
176
assert(!testMethod('/some/path', '/some/other/path', true));
177
assert(!testMethod('c:\\some\\path', 'c:\\some\\other\\path', true));
178
assert(!testMethod('c:\\some\\path', 'd:\\some\\path', true));
179
180
assert(testMethod('/some/path', '/some/PATH', true));
181
assert(testMethod('/someöäü/path', '/someÖÄÜ/PATH', true));
182
assert(testMethod('c:\\some\\path', 'c:\\some\\PATH', true));
183
assert(testMethod('c:\\someöäü\\path', 'c:\\someÖÄÜ\\PATH', true));
184
assert(testMethod('c:\\some\\path', 'C:\\some\\PATH', true));
185
}
186
187
test('isEqual (ignoreCase)', function () {
188
testIsEqual(isEqual);
189
190
// basics (uris)
191
assert(isEqual(URI.file('/some/path').fsPath, URI.file('/some/path').fsPath, true));
192
assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\path').fsPath, true));
193
194
assert(isEqual(URI.file('/someöäü/path').fsPath, URI.file('/someöäü/path').fsPath, true));
195
assert(isEqual(URI.file('c:\\someöäü\\path').fsPath, URI.file('c:\\someöäü\\path').fsPath, true));
196
197
assert(!isEqual(URI.file('/some/path').fsPath, URI.file('/some/other/path').fsPath, true));
198
assert(!isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\other\\path').fsPath, true));
199
200
assert(isEqual(URI.file('/some/path').fsPath, URI.file('/some/PATH').fsPath, true));
201
assert(isEqual(URI.file('/someöäü/path').fsPath, URI.file('/someÖÄÜ/PATH').fsPath, true));
202
assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\PATH').fsPath, true));
203
assert(isEqual(URI.file('c:\\someöäü\\path').fsPath, URI.file('c:\\someÖÄÜ\\PATH').fsPath, true));
204
assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('C:\\some\\PATH').fsPath, true));
205
});
206
207
test('isParent (ignorecase)', function () {
208
if (isWindows) {
209
assert(isParent('c:\\some\\path', 'c:\\', true));
210
assert(isParent('c:\\some\\path', 'c:\\some', true));
211
assert(isParent('c:\\some\\path', 'c:\\some\\', true));
212
assert(isParent('c:\\someöäü\\path', 'c:\\someöäü', true));
213
assert(isParent('c:\\someöäü\\path', 'c:\\someöäü\\', true));
214
assert(isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar', true));
215
assert(isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\', true));
216
217
assert(isParent('c:\\some\\path', 'C:\\', true));
218
assert(isParent('c:\\some\\path', 'c:\\SOME', true));
219
assert(isParent('c:\\some\\path', 'c:\\SOME\\', true));
220
221
assert(!isParent('c:\\some\\path', 'd:\\', true));
222
assert(!isParent('c:\\some\\path', 'c:\\some\\path', true));
223
assert(!isParent('c:\\some\\path', 'd:\\some\\path', true));
224
assert(!isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\barr', true));
225
assert(!isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test', true));
226
}
227
228
if (isMacintosh || isLinux) {
229
assert(isParent('/some/path', '/', true));
230
assert(isParent('/some/path', '/some', true));
231
assert(isParent('/some/path', '/some/', true));
232
assert(isParent('/someöäü/path', '/someöäü', true));
233
assert(isParent('/someöäü/path', '/someöäü/', true));
234
assert(isParent('/foo/bar/test.ts', '/foo/bar', true));
235
assert(isParent('/foo/bar/test.ts', '/foo/bar/', true));
236
237
assert(isParent('/some/path', '/SOME', true));
238
assert(isParent('/some/path', '/SOME/', true));
239
assert(isParent('/someöäü/path', '/SOMEÖÄÜ', true));
240
assert(isParent('/someöäü/path', '/SOMEÖÄÜ/', true));
241
242
assert(!isParent('/some/path', '/some/path', true));
243
assert(!isParent('/foo/bar/test.ts', '/foo/barr', true));
244
assert(!isParent('/foo/bar/test.ts', '/foo/bar/test', true));
245
}
246
});
247
248
test('isEqualOrParent (ignorecase)', function () {
249
250
// same assertions apply as with isEqual()
251
testIsEqual(isEqualOrParent); //
252
253
if (isWindows) {
254
assert(isEqualOrParent('c:\\some\\path', 'c:\\', true));
255
assert(isEqualOrParent('c:\\some\\path', 'c:\\some', true));
256
assert(isEqualOrParent('c:\\some\\path', 'c:\\some\\', true));
257
assert(isEqualOrParent('c:\\someöäü\\path', 'c:\\someöäü', true));
258
assert(isEqualOrParent('c:\\someöäü\\path', 'c:\\someöäü\\', true));
259
assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar', true));
260
assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\', true));
261
assert(isEqualOrParent('c:\\some\\path', 'c:\\some\\path', true));
262
assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test.ts', true));
263
264
assert(isEqualOrParent('c:\\some\\path', 'C:\\', true));
265
assert(isEqualOrParent('c:\\some\\path', 'c:\\SOME', true));
266
assert(isEqualOrParent('c:\\some\\path', 'c:\\SOME\\', true));
267
268
assert(!isEqualOrParent('c:\\some\\path', 'd:\\', true));
269
assert(!isEqualOrParent('c:\\some\\path', 'd:\\some\\path', true));
270
assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\barr', true));
271
assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test', true));
272
assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test.', true));
273
assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\BAR\\test.', true));
274
}
275
276
if (isMacintosh || isLinux) {
277
assert(isEqualOrParent('/some/path', '/', true));
278
assert(isEqualOrParent('/some/path', '/some', true));
279
assert(isEqualOrParent('/some/path', '/some/', true));
280
assert(isEqualOrParent('/someöäü/path', '/someöäü', true));
281
assert(isEqualOrParent('/someöäü/path', '/someöäü/', true));
282
assert(isEqualOrParent('/foo/bar/test.ts', '/foo/bar', true));
283
assert(isEqualOrParent('/foo/bar/test.ts', '/foo/bar/', true));
284
assert(isEqualOrParent('/some/path', '/some/path', true));
285
286
assert(isEqualOrParent('/some/path', '/SOME', true));
287
assert(isEqualOrParent('/some/path', '/SOME/', true));
288
assert(isEqualOrParent('/someöäü/path', '/SOMEÖÄÜ', true));
289
assert(isEqualOrParent('/someöäü/path', '/SOMEÖÄÜ/', true));
290
291
assert(!isEqualOrParent('/foo/bar/test.ts', '/foo/barr', true));
292
assert(!isEqualOrParent('/foo/bar/test.ts', '/foo/bar/test', true));
293
assert(!isEqualOrParent('foo/bar/test.ts', 'foo/bar/test.', true));
294
assert(!isEqualOrParent('foo/bar/test.ts', 'foo/BAR/test.', true));
295
}
296
});
297
298
ensureNoDisposablesAreLeakedInTestSuite();
299
});
300
301