Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/test/browser/extHostWorkspace.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 { CancellationToken } from '../../../../base/common/cancellation.js';
8
import { basename } from '../../../../base/common/path.js';
9
import { URI, UriComponents } from '../../../../base/common/uri.js';
10
import { ExtensionIdentifier } from '../../../../platform/extensions/common/extensions.js';
11
import { ILogService, NullLogService } from '../../../../platform/log/common/log.js';
12
import { IWorkspaceFolderData } from '../../../../platform/workspace/common/workspace.js';
13
import { MainThreadWorkspace } from '../../browser/mainThreadWorkspace.js';
14
import { IMainContext, IWorkspaceData, MainContext, ITextSearchComplete } from '../../common/extHost.protocol.js';
15
import { RelativePattern } from '../../common/extHostTypes.js';
16
import { ExtHostWorkspace } from '../../common/extHostWorkspace.js';
17
import { mock } from '../../../../base/test/common/mock.js';
18
import { TestRPCProtocol } from '../common/testRPCProtocol.js';
19
import { ExtHostRpcService } from '../../common/extHostRpcService.js';
20
import { IExtHostInitDataService } from '../../common/extHostInitDataService.js';
21
import { IFileQueryBuilderOptions, ITextQueryBuilderOptions } from '../../../services/search/common/queryBuilder.js';
22
import { IPatternInfo } from '../../../services/search/common/search.js';
23
import { isLinux, isWindows } from '../../../../base/common/platform.js';
24
import { IExtHostFileSystemInfo } from '../../common/extHostFileSystemInfo.js';
25
import { FileSystemProviderCapabilities } from '../../../../platform/files/common/files.js';
26
import { nullExtensionDescription as extensionDescriptor } from '../../../services/extensions/common/extensions.js';
27
import { IURITransformerService } from '../../common/extHostUriTransformerService.js';
28
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
29
import { ExcludeSettingOptions } from '../../../services/search/common/searchExtTypes.js';
30
31
function createExtHostWorkspace(mainContext: IMainContext, data: IWorkspaceData, logService: ILogService): ExtHostWorkspace {
32
const result = new ExtHostWorkspace(
33
new ExtHostRpcService(mainContext),
34
new class extends mock<IExtHostInitDataService>() { override workspace = data; },
35
new class extends mock<IExtHostFileSystemInfo>() { override getCapabilities() { return isLinux ? FileSystemProviderCapabilities.PathCaseSensitive : undefined; } },
36
logService,
37
new class extends mock<IURITransformerService>() { }
38
);
39
result.$initializeWorkspace(data, true);
40
return result;
41
}
42
43
suite('ExtHostWorkspace', function () {
44
45
ensureNoDisposablesAreLeakedInTestSuite();
46
47
function assertAsRelativePath(workspace: ExtHostWorkspace, input: string, expected: string, includeWorkspace?: boolean) {
48
const actual = workspace.getRelativePath(input, includeWorkspace);
49
assert.strictEqual(actual, expected);
50
}
51
52
test('asRelativePath', () => {
53
54
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/Applications/NewsWoWBot'), 0)], name: 'Test' }, new NullLogService());
55
56
assertAsRelativePath(ws, '/Coding/Applications/NewsWoWBot/bernd/das/brot', 'bernd/das/brot');
57
assertAsRelativePath(ws, '/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart',
58
'/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart');
59
60
assertAsRelativePath(ws, '', '');
61
assertAsRelativePath(ws, '/foo/bar', '/foo/bar');
62
assertAsRelativePath(ws, 'in/out', 'in/out');
63
});
64
65
test('asRelativePath, same paths, #11402', function () {
66
const root = '/home/aeschli/workspaces/samples/docker';
67
const input = '/home/aeschli/workspaces/samples/docker';
68
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
69
70
assertAsRelativePath(ws, input, input);
71
72
const input2 = '/home/aeschli/workspaces/samples/docker/a.file';
73
assertAsRelativePath(ws, input2, 'a.file');
74
});
75
76
test('asRelativePath, no workspace', function () {
77
const ws = createExtHostWorkspace(new TestRPCProtocol(), null!, new NullLogService());
78
assertAsRelativePath(ws, '', '');
79
assertAsRelativePath(ws, '/foo/bar', '/foo/bar');
80
});
81
82
test('asRelativePath, multiple folders', function () {
83
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
84
assertAsRelativePath(ws, '/Coding/One/file.txt', 'One/file.txt');
85
assertAsRelativePath(ws, '/Coding/Two/files/out.txt', 'Two/files/out.txt');
86
assertAsRelativePath(ws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
87
});
88
89
test('slightly inconsistent behaviour of asRelativePath and getWorkspaceFolder, #31553', function () {
90
const mrws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
91
92
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'One/file.txt');
93
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'One/file.txt', true);
94
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'file.txt', false);
95
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'Two/files/out.txt');
96
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'Two/files/out.txt', true);
97
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'files/out.txt', false);
98
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
99
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', true);
100
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', false);
101
102
const srws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0)], name: 'Test' }, new NullLogService());
103
assertAsRelativePath(srws, '/Coding/One/file.txt', 'file.txt');
104
assertAsRelativePath(srws, '/Coding/One/file.txt', 'file.txt', false);
105
assertAsRelativePath(srws, '/Coding/One/file.txt', 'One/file.txt', true);
106
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
107
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', true);
108
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', false);
109
});
110
111
test('getPath, legacy', function () {
112
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
113
assert.strictEqual(ws.getPath(), undefined);
114
115
ws = createExtHostWorkspace(new TestRPCProtocol(), null!, new NullLogService());
116
assert.strictEqual(ws.getPath(), undefined);
117
118
ws = createExtHostWorkspace(new TestRPCProtocol(), undefined!, new NullLogService());
119
assert.strictEqual(ws.getPath(), undefined);
120
121
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.file('Folder'), 0), aWorkspaceFolderData(URI.file('Another/Folder'), 1)] }, new NullLogService());
122
assert.strictEqual(ws.getPath()!.replace(/\\/g, '/'), '/Folder');
123
124
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.file('/Folder'), 0)] }, new NullLogService());
125
assert.strictEqual(ws.getPath()!.replace(/\\/g, '/'), '/Folder');
126
});
127
128
test('WorkspaceFolder has name and index', function () {
129
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
130
131
const [one, two] = ws.getWorkspaceFolders()!;
132
133
assert.strictEqual(one.name, 'One');
134
assert.strictEqual(one.index, 0);
135
assert.strictEqual(two.name, 'Two');
136
assert.strictEqual(two.index, 1);
137
});
138
139
test('getContainingWorkspaceFolder', () => {
140
const ws = createExtHostWorkspace(new TestRPCProtocol(), {
141
id: 'foo',
142
name: 'Test',
143
folders: [
144
aWorkspaceFolderData(URI.file('/Coding/One'), 0),
145
aWorkspaceFolderData(URI.file('/Coding/Two'), 1),
146
aWorkspaceFolderData(URI.file('/Coding/Two/Nested'), 2)
147
]
148
}, new NullLogService());
149
150
let folder = ws.getWorkspaceFolder(URI.file('/foo/bar'));
151
assert.strictEqual(folder, undefined);
152
153
folder = ws.getWorkspaceFolder(URI.file('/Coding/One/file/path.txt'))!;
154
assert.strictEqual(folder.name, 'One');
155
156
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/file/path.txt'))!;
157
assert.strictEqual(folder.name, 'Two');
158
159
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nest'))!;
160
assert.strictEqual(folder.name, 'Two');
161
162
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/file'))!;
163
assert.strictEqual(folder.name, 'Nested');
164
165
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/f'))!;
166
assert.strictEqual(folder.name, 'Nested');
167
168
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested'), true)!;
169
assert.strictEqual(folder.name, 'Two');
170
171
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/'), true)!;
172
assert.strictEqual(folder.name, 'Two');
173
174
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested'))!;
175
assert.strictEqual(folder.name, 'Nested');
176
177
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/'))!;
178
assert.strictEqual(folder.name, 'Nested');
179
180
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two'), true)!;
181
assert.strictEqual(folder, undefined);
182
183
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two'), false)!;
184
assert.strictEqual(folder.name, 'Two');
185
});
186
187
test('Multiroot change event should have a delta, #29641', function (done) {
188
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
189
190
let finished = false;
191
const finish = (error?: any) => {
192
if (!finished) {
193
finished = true;
194
done(error);
195
}
196
};
197
198
let sub = ws.onDidChangeWorkspace(e => {
199
try {
200
assert.deepStrictEqual(e.added, []);
201
assert.deepStrictEqual(e.removed, []);
202
} catch (error) {
203
finish(error);
204
}
205
});
206
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [] });
207
sub.dispose();
208
209
sub = ws.onDidChangeWorkspace(e => {
210
try {
211
assert.deepStrictEqual(e.removed, []);
212
assert.strictEqual(e.added.length, 1);
213
assert.strictEqual(e.added[0].uri.toString(), 'foo:bar');
214
} catch (error) {
215
finish(error);
216
}
217
});
218
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] });
219
sub.dispose();
220
221
sub = ws.onDidChangeWorkspace(e => {
222
try {
223
assert.deepStrictEqual(e.removed, []);
224
assert.strictEqual(e.added.length, 1);
225
assert.strictEqual(e.added[0].uri.toString(), 'foo:bar2');
226
} catch (error) {
227
finish(error);
228
}
229
});
230
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar2'), 1)] });
231
sub.dispose();
232
233
sub = ws.onDidChangeWorkspace(e => {
234
try {
235
assert.strictEqual(e.removed.length, 2);
236
assert.strictEqual(e.removed[0].uri.toString(), 'foo:bar');
237
assert.strictEqual(e.removed[1].uri.toString(), 'foo:bar2');
238
239
assert.strictEqual(e.added.length, 1);
240
assert.strictEqual(e.added[0].uri.toString(), 'foo:bar3');
241
} catch (error) {
242
finish(error);
243
}
244
});
245
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0)] });
246
sub.dispose();
247
finish();
248
});
249
250
test('Multiroot change keeps existing workspaces live', function () {
251
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }, new NullLogService());
252
253
const firstFolder = ws.getWorkspaceFolders()![0];
254
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar2'), 0), aWorkspaceFolderData(URI.parse('foo:bar'), 1, 'renamed')] });
255
256
assert.strictEqual(ws.getWorkspaceFolders()![1], firstFolder);
257
assert.strictEqual(firstFolder.index, 1);
258
assert.strictEqual(firstFolder.name, 'renamed');
259
260
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar2'), 1), aWorkspaceFolderData(URI.parse('foo:bar'), 2)] });
261
assert.strictEqual(ws.getWorkspaceFolders()![2], firstFolder);
262
assert.strictEqual(firstFolder.index, 2);
263
264
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0)] });
265
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar'), 1)] });
266
267
assert.notStrictEqual(firstFolder, ws.workspace!.folders[0]);
268
});
269
270
test('updateWorkspaceFolders - invalid arguments', function () {
271
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
272
273
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, null!, null!));
274
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 0));
275
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 1));
276
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 1, 0));
277
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, -1, 0));
278
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, -1, -1));
279
280
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }, new NullLogService());
281
282
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 1, 1));
283
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2));
284
assert.strictEqual(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 1, asUpdateWorkspaceFolderData(URI.parse('foo:bar'))));
285
});
286
287
test('updateWorkspaceFolders - valid arguments', function (done) {
288
let finished = false;
289
const finish = (error?: any) => {
290
if (!finished) {
291
finished = true;
292
done(error);
293
}
294
};
295
296
const protocol: IMainContext = {
297
getProxy: () => { return undefined!; },
298
set: () => { return undefined!; },
299
dispose: () => { },
300
assertRegistered: () => { },
301
drain: () => { return undefined!; },
302
};
303
304
const ws = createExtHostWorkspace(protocol, { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
305
306
//
307
// Add one folder
308
//
309
310
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar'))));
311
assert.strictEqual(1, ws.workspace!.folders.length);
312
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
313
314
const firstAddedFolder = ws.getWorkspaceFolders()![0];
315
316
let gotEvent = false;
317
let sub = ws.onDidChangeWorkspace(e => {
318
try {
319
assert.deepStrictEqual(e.removed, []);
320
assert.strictEqual(e.added.length, 1);
321
assert.strictEqual(e.added[0].uri.toString(), 'foo:bar');
322
assert.strictEqual(e.added[0], firstAddedFolder); // verify object is still live
323
gotEvent = true;
324
} catch (error) {
325
finish(error);
326
}
327
});
328
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }); // simulate acknowledgement from main side
329
assert.strictEqual(gotEvent, true);
330
sub.dispose();
331
assert.strictEqual(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
332
333
//
334
// Add two more folders
335
//
336
337
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 1, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar1')), asUpdateWorkspaceFolderData(URI.parse('foo:bar2'))));
338
assert.strictEqual(3, ws.workspace!.folders.length);
339
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
340
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
341
assert.strictEqual(ws.workspace!.folders[2].uri.toString(), URI.parse('foo:bar2').toString());
342
343
const secondAddedFolder = ws.getWorkspaceFolders()![1];
344
const thirdAddedFolder = ws.getWorkspaceFolders()![2];
345
346
gotEvent = false;
347
sub = ws.onDidChangeWorkspace(e => {
348
try {
349
assert.deepStrictEqual(e.removed, []);
350
assert.strictEqual(e.added.length, 2);
351
assert.strictEqual(e.added[0].uri.toString(), 'foo:bar1');
352
assert.strictEqual(e.added[1].uri.toString(), 'foo:bar2');
353
assert.strictEqual(e.added[0], secondAddedFolder);
354
assert.strictEqual(e.added[1], thirdAddedFolder);
355
gotEvent = true;
356
} catch (error) {
357
finish(error);
358
}
359
});
360
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar1'), 1), aWorkspaceFolderData(URI.parse('foo:bar2'), 2)] }); // simulate acknowledgement from main side
361
assert.strictEqual(gotEvent, true);
362
sub.dispose();
363
assert.strictEqual(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
364
assert.strictEqual(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
365
assert.strictEqual(ws.getWorkspaceFolders()![2], thirdAddedFolder); // verify object is still live
366
367
//
368
// Remove one folder
369
//
370
371
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 2, 1));
372
assert.strictEqual(2, ws.workspace!.folders.length);
373
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
374
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
375
376
gotEvent = false;
377
sub = ws.onDidChangeWorkspace(e => {
378
try {
379
assert.deepStrictEqual(e.added, []);
380
assert.strictEqual(e.removed.length, 1);
381
assert.strictEqual(e.removed[0], thirdAddedFolder);
382
gotEvent = true;
383
} catch (error) {
384
finish(error);
385
}
386
});
387
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar1'), 1)] }); // simulate acknowledgement from main side
388
assert.strictEqual(gotEvent, true);
389
sub.dispose();
390
assert.strictEqual(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
391
assert.strictEqual(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
392
393
//
394
// Rename folder
395
//
396
397
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar'), 'renamed 1'), asUpdateWorkspaceFolderData(URI.parse('foo:bar1'), 'renamed 2')));
398
assert.strictEqual(2, ws.workspace!.folders.length);
399
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
400
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
401
assert.strictEqual(ws.workspace!.folders[0].name, 'renamed 1');
402
assert.strictEqual(ws.workspace!.folders[1].name, 'renamed 2');
403
assert.strictEqual(ws.getWorkspaceFolders()![0].name, 'renamed 1');
404
assert.strictEqual(ws.getWorkspaceFolders()![1].name, 'renamed 2');
405
406
gotEvent = false;
407
sub = ws.onDidChangeWorkspace(e => {
408
try {
409
assert.deepStrictEqual(e.added, []);
410
assert.strictEqual(e.removed.length, 0);
411
gotEvent = true;
412
} catch (error) {
413
finish(error);
414
}
415
});
416
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0, 'renamed 1'), aWorkspaceFolderData(URI.parse('foo:bar1'), 1, 'renamed 2')] }); // simulate acknowledgement from main side
417
assert.strictEqual(gotEvent, true);
418
sub.dispose();
419
assert.strictEqual(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
420
assert.strictEqual(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
421
assert.strictEqual(ws.workspace!.folders[0].name, 'renamed 1');
422
assert.strictEqual(ws.workspace!.folders[1].name, 'renamed 2');
423
assert.strictEqual(ws.getWorkspaceFolders()![0].name, 'renamed 1');
424
assert.strictEqual(ws.getWorkspaceFolders()![1].name, 'renamed 2');
425
426
//
427
// Add and remove folders
428
//
429
430
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar3')), asUpdateWorkspaceFolderData(URI.parse('foo:bar4'))));
431
assert.strictEqual(2, ws.workspace!.folders.length);
432
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar3').toString());
433
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar4').toString());
434
435
const fourthAddedFolder = ws.getWorkspaceFolders()![0];
436
const fifthAddedFolder = ws.getWorkspaceFolders()![1];
437
438
gotEvent = false;
439
sub = ws.onDidChangeWorkspace(e => {
440
try {
441
assert.strictEqual(e.added.length, 2);
442
assert.strictEqual(e.added[0], fourthAddedFolder);
443
assert.strictEqual(e.added[1], fifthAddedFolder);
444
assert.strictEqual(e.removed.length, 2);
445
assert.strictEqual(e.removed[0], firstAddedFolder);
446
assert.strictEqual(e.removed[1], secondAddedFolder);
447
gotEvent = true;
448
} catch (error) {
449
finish(error);
450
}
451
});
452
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar4'), 1)] }); // simulate acknowledgement from main side
453
assert.strictEqual(gotEvent, true);
454
sub.dispose();
455
assert.strictEqual(ws.getWorkspaceFolders()![0], fourthAddedFolder); // verify object is still live
456
assert.strictEqual(ws.getWorkspaceFolders()![1], fifthAddedFolder); // verify object is still live
457
458
//
459
// Swap folders
460
//
461
462
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar4')), asUpdateWorkspaceFolderData(URI.parse('foo:bar3'))));
463
assert.strictEqual(2, ws.workspace!.folders.length);
464
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar4').toString());
465
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar3').toString());
466
467
assert.strictEqual(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
468
assert.strictEqual(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
469
470
gotEvent = false;
471
sub = ws.onDidChangeWorkspace(e => {
472
try {
473
assert.strictEqual(e.added.length, 0);
474
assert.strictEqual(e.removed.length, 0);
475
gotEvent = true;
476
} catch (error) {
477
finish(error);
478
}
479
});
480
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar4'), 0), aWorkspaceFolderData(URI.parse('foo:bar3'), 1)] }); // simulate acknowledgement from main side
481
assert.strictEqual(gotEvent, true);
482
sub.dispose();
483
assert.strictEqual(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
484
assert.strictEqual(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
485
assert.strictEqual(fifthAddedFolder.index, 0);
486
assert.strictEqual(fourthAddedFolder.index, 1);
487
488
//
489
// Add one folder after the other without waiting for confirmation (not supported currently)
490
//
491
492
assert.strictEqual(true, ws.updateWorkspaceFolders(extensionDescriptor, 2, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar5'))));
493
494
assert.strictEqual(3, ws.workspace!.folders.length);
495
assert.strictEqual(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar4').toString());
496
assert.strictEqual(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar3').toString());
497
assert.strictEqual(ws.workspace!.folders[2].uri.toString(), URI.parse('foo:bar5').toString());
498
499
const sixthAddedFolder = ws.getWorkspaceFolders()![2];
500
501
gotEvent = false;
502
sub = ws.onDidChangeWorkspace(e => {
503
try {
504
assert.strictEqual(e.added.length, 1);
505
assert.strictEqual(e.added[0], sixthAddedFolder);
506
gotEvent = true;
507
} catch (error) {
508
finish(error);
509
}
510
});
511
ws.$acceptWorkspaceData({
512
id: 'foo', name: 'Test', folders: [
513
aWorkspaceFolderData(URI.parse('foo:bar4'), 0),
514
aWorkspaceFolderData(URI.parse('foo:bar3'), 1),
515
aWorkspaceFolderData(URI.parse('foo:bar5'), 2)
516
]
517
}); // simulate acknowledgement from main side
518
assert.strictEqual(gotEvent, true);
519
sub.dispose();
520
521
assert.strictEqual(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
522
assert.strictEqual(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
523
assert.strictEqual(ws.getWorkspaceFolders()![2], sixthAddedFolder); // verify object is still live
524
525
finish();
526
});
527
528
test('Multiroot change event is immutable', function (done) {
529
let finished = false;
530
const finish = (error?: any) => {
531
if (!finished) {
532
finished = true;
533
done(error);
534
}
535
};
536
537
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
538
const sub = ws.onDidChangeWorkspace(e => {
539
try {
540
assert.throws(() => {
541
(<any>e).added = [];
542
});
543
// assert.throws(() => {
544
// (<any>e.added)[0] = null;
545
// });
546
} catch (error) {
547
finish(error);
548
}
549
});
550
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [] });
551
sub.dispose();
552
finish();
553
});
554
555
test('`vscode.workspace.getWorkspaceFolder(file)` don\'t return workspace folder when file open from command line. #36221', function () {
556
if (isWindows) {
557
558
const ws = createExtHostWorkspace(new TestRPCProtocol(), {
559
id: 'foo', name: 'Test', folders: [
560
aWorkspaceFolderData(URI.file('c:/Users/marek/Desktop/vsc_test/'), 0)
561
]
562
}, new NullLogService());
563
564
assert.ok(ws.getWorkspaceFolder(URI.file('c:/Users/marek/Desktop/vsc_test/a.txt')));
565
assert.ok(ws.getWorkspaceFolder(URI.file('C:/Users/marek/Desktop/vsc_test/b.txt')));
566
}
567
});
568
569
function aWorkspaceFolderData(uri: URI, index: number, name: string = ''): IWorkspaceFolderData {
570
return {
571
uri,
572
index,
573
name: name || basename(uri.path)
574
};
575
}
576
577
function asUpdateWorkspaceFolderData(uri: URI, name?: string): { uri: URI; name?: string } {
578
return { uri, name };
579
}
580
581
suite('findFiles -', function () {
582
test('string include', () => {
583
const root = '/project/foo';
584
const rpcProtocol = new TestRPCProtocol();
585
586
let mainThreadCalled = false;
587
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
588
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
589
mainThreadCalled = true;
590
assert.strictEqual(options.includePattern, 'foo');
591
assert.strictEqual(_includeFolder, null);
592
assert.strictEqual(options.excludePattern, undefined);
593
assert.strictEqual(options.disregardExcludeSettings, false);
594
assert.strictEqual(options.maxResults, 10);
595
return Promise.resolve(null);
596
}
597
});
598
599
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
600
return ws.findFiles('foo', undefined, 10, new ExtensionIdentifier('test')).then(() => {
601
assert(mainThreadCalled, 'mainThreadCalled');
602
});
603
});
604
605
function testFindFilesInclude(pattern: RelativePattern) {
606
const root = '/project/foo';
607
const rpcProtocol = new TestRPCProtocol();
608
609
let mainThreadCalled = false;
610
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
611
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
612
mainThreadCalled = true;
613
assert.strictEqual(options.includePattern, 'glob/**');
614
assert.deepStrictEqual(_includeFolder ? URI.from(_includeFolder).toJSON() : null, URI.file('/other/folder').toJSON());
615
assert.strictEqual(options.excludePattern, undefined);
616
assert.strictEqual(options.disregardExcludeSettings, false);
617
return Promise.resolve(null);
618
}
619
});
620
621
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
622
return ws.findFiles(pattern, undefined, 10, new ExtensionIdentifier('test')).then(() => {
623
assert(mainThreadCalled, 'mainThreadCalled');
624
});
625
}
626
627
test('RelativePattern include (string)', () => {
628
return testFindFilesInclude(new RelativePattern('/other/folder', 'glob/**'));
629
});
630
631
test('RelativePattern include (URI)', () => {
632
return testFindFilesInclude(new RelativePattern(URI.file('/other/folder'), 'glob/**'));
633
});
634
635
test('no excludes', () => {
636
const root = '/project/foo';
637
const rpcProtocol = new TestRPCProtocol();
638
639
let mainThreadCalled = false;
640
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
641
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
642
mainThreadCalled = true;
643
assert.strictEqual(options.includePattern, 'glob/**');
644
assert.deepStrictEqual(URI.revive(_includeFolder!).toString(), URI.file('/other/folder').toString());
645
assert.strictEqual(options.excludePattern, undefined);
646
assert.strictEqual(options.disregardExcludeSettings, true);
647
return Promise.resolve(null);
648
}
649
});
650
651
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
652
return ws.findFiles(new RelativePattern('/other/folder', 'glob/**'), null, 10, new ExtensionIdentifier('test')).then(() => {
653
assert(mainThreadCalled, 'mainThreadCalled');
654
});
655
});
656
657
test('with cancelled token', () => {
658
const root = '/project/foo';
659
const rpcProtocol = new TestRPCProtocol();
660
661
let mainThreadCalled = false;
662
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
663
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
664
mainThreadCalled = true;
665
return Promise.resolve(null);
666
}
667
});
668
669
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
670
671
const token = CancellationToken.Cancelled;
672
return ws.findFiles(new RelativePattern('/other/folder', 'glob/**'), null, 10, new ExtensionIdentifier('test'), token).then(() => {
673
assert(!mainThreadCalled, '!mainThreadCalled');
674
});
675
});
676
677
test('RelativePattern exclude', () => {
678
const root = '/project/foo';
679
const rpcProtocol = new TestRPCProtocol();
680
681
let mainThreadCalled = false;
682
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
683
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
684
mainThreadCalled = true;
685
assert.strictEqual(options.disregardExcludeSettings, false);
686
assert.strictEqual(options.excludePattern?.length, 1);
687
assert.strictEqual(options.excludePattern[0].pattern, 'glob/**'); // Note that the base portion is ignored, see #52651
688
return Promise.resolve(null);
689
}
690
});
691
692
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
693
return ws.findFiles('', new RelativePattern(root, 'glob/**'), 10, new ExtensionIdentifier('test')).then(() => {
694
assert(mainThreadCalled, 'mainThreadCalled');
695
});
696
});
697
});
698
699
suite('findFiles2 -', function () {
700
test('string include', () => {
701
const root = '/project/foo';
702
const rpcProtocol = new TestRPCProtocol();
703
704
let mainThreadCalled = false;
705
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
706
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
707
mainThreadCalled = true;
708
assert.strictEqual(options.filePattern, 'foo');
709
assert.strictEqual(options.includePattern, undefined);
710
assert.strictEqual(_includeFolder, null);
711
assert.strictEqual(options.excludePattern, undefined);
712
assert.strictEqual(options.disregardExcludeSettings, false);
713
assert.strictEqual(options.maxResults, 10);
714
return Promise.resolve(null);
715
}
716
});
717
718
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
719
return ws.findFiles2(['foo'], { maxResults: 10, useExcludeSettings: ExcludeSettingOptions.FilesExclude }, new ExtensionIdentifier('test')).then(() => {
720
assert(mainThreadCalled, 'mainThreadCalled');
721
});
722
});
723
724
function testFindFiles2Include(pattern: RelativePattern[]) {
725
const root = '/project/foo';
726
const rpcProtocol = new TestRPCProtocol();
727
728
let mainThreadCalled = false;
729
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
730
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
731
mainThreadCalled = true;
732
assert.strictEqual(options.filePattern, 'glob/**');
733
assert.strictEqual(options.includePattern, undefined);
734
assert.deepStrictEqual(_includeFolder ? URI.from(_includeFolder).toJSON() : null, URI.file('/other/folder').toJSON());
735
assert.strictEqual(options.excludePattern, undefined);
736
assert.strictEqual(options.disregardExcludeSettings, false);
737
return Promise.resolve(null);
738
}
739
});
740
741
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
742
return ws.findFiles2(pattern, { maxResults: 10 }, new ExtensionIdentifier('test')).then(() => {
743
assert(mainThreadCalled, 'mainThreadCalled');
744
});
745
}
746
747
test('RelativePattern include (string)', () => {
748
return testFindFiles2Include([new RelativePattern('/other/folder', 'glob/**')]);
749
});
750
751
test('RelativePattern include (URI)', () => {
752
return testFindFiles2Include([new RelativePattern(URI.file('/other/folder'), 'glob/**')]);
753
});
754
755
test('no excludes', () => {
756
const root = '/project/foo';
757
const rpcProtocol = new TestRPCProtocol();
758
759
let mainThreadCalled = false;
760
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
761
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
762
mainThreadCalled = true;
763
assert.strictEqual(options.filePattern, 'glob/**');
764
assert.strictEqual(options.includePattern, undefined);
765
assert.deepStrictEqual(URI.revive(_includeFolder!).toString(), URI.file('/other/folder').toString());
766
assert.strictEqual(options.excludePattern, undefined);
767
assert.strictEqual(options.disregardExcludeSettings, false);
768
return Promise.resolve(null);
769
}
770
});
771
772
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
773
return ws.findFiles2([new RelativePattern('/other/folder', 'glob/**')], {}, new ExtensionIdentifier('test')).then(() => {
774
assert(mainThreadCalled, 'mainThreadCalled');
775
});
776
});
777
778
test('with cancelled token', () => {
779
const root = '/project/foo';
780
const rpcProtocol = new TestRPCProtocol();
781
782
let mainThreadCalled = false;
783
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
784
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
785
mainThreadCalled = true;
786
return Promise.resolve(null);
787
}
788
});
789
790
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
791
792
const token = CancellationToken.Cancelled;
793
return ws.findFiles2([new RelativePattern('/other/folder', 'glob/**')], {}, new ExtensionIdentifier('test'), token).then(() => {
794
assert(!mainThreadCalled, '!mainThreadCalled');
795
});
796
});
797
798
test('RelativePattern exclude', () => {
799
const root = '/project/foo';
800
const rpcProtocol = new TestRPCProtocol();
801
802
let mainThreadCalled = false;
803
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
804
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
805
mainThreadCalled = true;
806
assert.strictEqual(options.disregardExcludeSettings, false);
807
assert.strictEqual(options.excludePattern?.length, 1);
808
assert.strictEqual(options.excludePattern[0].pattern, 'glob/**'); // Note that the base portion is ignored, see #52651
809
return Promise.resolve(null);
810
}
811
});
812
813
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
814
return ws.findFiles2([''], { exclude: [new RelativePattern(root, 'glob/**')] }, new ExtensionIdentifier('test')).then(() => {
815
assert(mainThreadCalled, 'mainThreadCalled');
816
});
817
});
818
test('useIgnoreFiles', () => {
819
const root = '/project/foo';
820
const rpcProtocol = new TestRPCProtocol();
821
822
let mainThreadCalled = false;
823
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
824
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
825
mainThreadCalled = true;
826
assert.strictEqual(options.disregardExcludeSettings, false);
827
assert.strictEqual(options.disregardIgnoreFiles, false);
828
assert.strictEqual(options.disregardGlobalIgnoreFiles, false);
829
assert.strictEqual(options.disregardParentIgnoreFiles, false);
830
return Promise.resolve(null);
831
}
832
});
833
834
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
835
return ws.findFiles2([''], { useIgnoreFiles: { local: true, parent: true, global: true } }, new ExtensionIdentifier('test')).then(() => {
836
assert(mainThreadCalled, 'mainThreadCalled');
837
});
838
});
839
840
test('use symlinks', () => {
841
const root = '/project/foo';
842
const rpcProtocol = new TestRPCProtocol();
843
844
let mainThreadCalled = false;
845
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
846
override $startFileSearch(_includeFolder: UriComponents | null, options: IFileQueryBuilderOptions, token: CancellationToken): Promise<URI[] | null> {
847
mainThreadCalled = true;
848
assert.strictEqual(options.ignoreSymlinks, false);
849
return Promise.resolve(null);
850
}
851
});
852
853
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
854
return ws.findFiles2([''], { followSymlinks: true }, new ExtensionIdentifier('test')).then(() => {
855
assert(mainThreadCalled, 'mainThreadCalled');
856
});
857
});
858
859
// todo: add tests with multiple filePatterns and excludes
860
861
});
862
863
suite('findTextInFiles -', function () {
864
test('no include', async () => {
865
const root = '/project/foo';
866
const rpcProtocol = new TestRPCProtocol();
867
868
let mainThreadCalled = false;
869
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
870
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
871
mainThreadCalled = true;
872
assert.strictEqual(query.pattern, 'foo');
873
assert.strictEqual(folder, null);
874
assert.strictEqual(options.includePattern, undefined);
875
assert.strictEqual(options.excludePattern, undefined);
876
return null;
877
}
878
});
879
880
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
881
await ws.findTextInFiles({ pattern: 'foo' }, {}, () => { }, new ExtensionIdentifier('test'));
882
assert(mainThreadCalled, 'mainThreadCalled');
883
});
884
885
test('string include', async () => {
886
const root = '/project/foo';
887
const rpcProtocol = new TestRPCProtocol();
888
889
let mainThreadCalled = false;
890
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
891
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
892
mainThreadCalled = true;
893
assert.strictEqual(query.pattern, 'foo');
894
assert.strictEqual(folder, null);
895
assert.strictEqual(options.includePattern, '**/files');
896
assert.strictEqual(options.excludePattern, undefined);
897
return null;
898
}
899
});
900
901
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
902
await ws.findTextInFiles({ pattern: 'foo' }, { include: '**/files' }, () => { }, new ExtensionIdentifier('test'));
903
assert(mainThreadCalled, 'mainThreadCalled');
904
});
905
906
test('RelativePattern include', async () => {
907
const root = '/project/foo';
908
const rpcProtocol = new TestRPCProtocol();
909
910
let mainThreadCalled = false;
911
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
912
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
913
mainThreadCalled = true;
914
assert.strictEqual(query.pattern, 'foo');
915
assert.deepStrictEqual(URI.revive(folder!).toString(), URI.file('/other/folder').toString());
916
assert.strictEqual(options.includePattern, 'glob/**');
917
assert.strictEqual(options.excludePattern, undefined);
918
return null;
919
}
920
});
921
922
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
923
await ws.findTextInFiles({ pattern: 'foo' }, { include: new RelativePattern('/other/folder', 'glob/**') }, () => { }, new ExtensionIdentifier('test'));
924
assert(mainThreadCalled, 'mainThreadCalled');
925
});
926
927
test('with cancelled token', async () => {
928
const root = '/project/foo';
929
const rpcProtocol = new TestRPCProtocol();
930
931
let mainThreadCalled = false;
932
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
933
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
934
mainThreadCalled = true;
935
return null;
936
}
937
});
938
939
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
940
const token = CancellationToken.Cancelled;
941
await ws.findTextInFiles({ pattern: 'foo' }, {}, () => { }, new ExtensionIdentifier('test'), token);
942
assert(!mainThreadCalled, '!mainThreadCalled');
943
});
944
945
test('RelativePattern exclude', async () => {
946
const root = '/project/foo';
947
const rpcProtocol = new TestRPCProtocol();
948
949
let mainThreadCalled = false;
950
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
951
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
952
mainThreadCalled = true;
953
assert.strictEqual(query.pattern, 'foo');
954
assert.deepStrictEqual(folder, null);
955
assert.strictEqual(options.includePattern, undefined);
956
assert.strictEqual(options.excludePattern?.length, 1);
957
assert.strictEqual(options.excludePattern[0].pattern, 'glob/**'); // exclude folder is ignored...
958
return null;
959
}
960
});
961
962
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
963
await ws.findTextInFiles({ pattern: 'foo' }, { exclude: new RelativePattern('/other/folder', 'glob/**') }, () => { }, new ExtensionIdentifier('test'));
964
assert(mainThreadCalled, 'mainThreadCalled');
965
});
966
});
967
968
suite('findTextInFiles2 -', function () {
969
test('no include', async () => {
970
const root = '/project/foo';
971
const rpcProtocol = new TestRPCProtocol();
972
973
let mainThreadCalled = false;
974
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
975
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
976
mainThreadCalled = true;
977
assert.strictEqual(query.pattern, 'foo');
978
assert.strictEqual(folder, null);
979
assert.strictEqual(options.includePattern, undefined);
980
assert.strictEqual(options.excludePattern, undefined);
981
return null;
982
}
983
});
984
985
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
986
await (ws.findTextInFiles2({ pattern: 'foo' }, {}, new ExtensionIdentifier('test'))).complete;
987
assert(mainThreadCalled, 'mainThreadCalled');
988
});
989
990
test('string include', async () => {
991
const root = '/project/foo';
992
const rpcProtocol = new TestRPCProtocol();
993
994
let mainThreadCalled = false;
995
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
996
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
997
mainThreadCalled = true;
998
assert.strictEqual(query.pattern, 'foo');
999
assert.strictEqual(folder, null);
1000
assert.strictEqual(options.includePattern, '**/files');
1001
assert.strictEqual(options.excludePattern, undefined);
1002
return null;
1003
}
1004
});
1005
1006
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
1007
await (ws.findTextInFiles2({ pattern: 'foo' }, { include: ['**/files'] }, new ExtensionIdentifier('test'))).complete;
1008
assert(mainThreadCalled, 'mainThreadCalled');
1009
});
1010
1011
test('RelativePattern include', async () => {
1012
const root = '/project/foo';
1013
const rpcProtocol = new TestRPCProtocol();
1014
1015
let mainThreadCalled = false;
1016
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
1017
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
1018
mainThreadCalled = true;
1019
assert.strictEqual(query.pattern, 'foo');
1020
assert.deepStrictEqual(URI.revive(folder!).toString(), URI.file('/other/folder').toString());
1021
assert.strictEqual(options.includePattern, 'glob/**');
1022
assert.strictEqual(options.excludePattern, undefined);
1023
return null;
1024
}
1025
});
1026
1027
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
1028
await (ws.findTextInFiles2({ pattern: 'foo' }, { include: [new RelativePattern('/other/folder', 'glob/**')] }, new ExtensionIdentifier('test'))).complete;
1029
assert(mainThreadCalled, 'mainThreadCalled');
1030
});
1031
1032
test('with cancelled token', async () => {
1033
const root = '/project/foo';
1034
const rpcProtocol = new TestRPCProtocol();
1035
1036
let mainThreadCalled = false;
1037
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
1038
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
1039
mainThreadCalled = true;
1040
return null;
1041
}
1042
});
1043
1044
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
1045
const token = CancellationToken.Cancelled;
1046
await (ws.findTextInFiles2({ pattern: 'foo' }, undefined, new ExtensionIdentifier('test'), token)).complete;
1047
assert(!mainThreadCalled, '!mainThreadCalled');
1048
});
1049
1050
test('RelativePattern exclude', async () => {
1051
const root = '/project/foo';
1052
const rpcProtocol = new TestRPCProtocol();
1053
1054
let mainThreadCalled = false;
1055
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
1056
override async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
1057
mainThreadCalled = true;
1058
assert.strictEqual(query.pattern, 'foo');
1059
assert.deepStrictEqual(folder, null);
1060
assert.strictEqual(options.includePattern, undefined);
1061
assert.strictEqual(options.excludePattern?.length, 1);
1062
assert.strictEqual(options.excludePattern[0].pattern, 'glob/**'); // exclude folder is ignored...
1063
return null;
1064
}
1065
});
1066
1067
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
1068
await (ws.findTextInFiles2({ pattern: 'foo' }, { exclude: [new RelativePattern('/other/folder', 'glob/**')] }, new ExtensionIdentifier('test'))).complete;
1069
assert(mainThreadCalled, 'mainThreadCalled');
1070
});
1071
1072
// TODO: test multiple includes/excludess
1073
});
1074
});
1075
1076