-
Notifications
You must be signed in to change notification settings - Fork 30.3k
/
Copy pathworkspace.test.ts
1286 lines (1017 loc) · 43.9 KB
/
workspace.test.ts
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import * as fs from 'fs';
import { basename, join, posix } from 'path';
import * as vscode from 'vscode';
import { TestFS } from '../memfs';
import { assertNoRpc, closeAllEditors, createRandomFile, delay, deleteFile, disposeAll, pathEquals, revertAllDirty, rndName, testFs, withLogDisabled } from '../utils';
suite('vscode API - workspace', () => {
teardown(async function () {
assertNoRpc();
await closeAllEditors();
});
test('MarkdownString', function () {
let md = new vscode.MarkdownString();
assert.strictEqual(md.value, '');
assert.strictEqual(md.isTrusted, undefined);
md = new vscode.MarkdownString('**bold**');
assert.strictEqual(md.value, '**bold**');
md.appendText('**bold?**');
assert.strictEqual(md.value, '**bold**\\*\\*bold?\\*\\*');
md.appendMarkdown('**bold**');
assert.strictEqual(md.value, '**bold**\\*\\*bold?\\*\\***bold**');
});
test('textDocuments', () => {
assert.ok(Array.isArray(vscode.workspace.textDocuments));
assert.throws(() => (<any>vscode.workspace).textDocuments = null);
});
test('rootPath', () => {
assert.ok(pathEquals(vscode.workspace.rootPath!, join(__dirname, '../../testWorkspace')));
assert.throws(() => (vscode.workspace as any).rootPath = 'farboo');
});
test('workspaceFile', () => {
assert.ok(!vscode.workspace.workspaceFile);
});
test('workspaceFolders', () => {
if (vscode.workspace.workspaceFolders) {
assert.strictEqual(vscode.workspace.workspaceFolders.length, 1);
assert.ok(pathEquals(vscode.workspace.workspaceFolders[0].uri.fsPath, join(__dirname, '../../testWorkspace')));
}
});
test('getWorkspaceFolder', () => {
const folder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(join(__dirname, '../../testWorkspace/far.js')));
assert.ok(!!folder);
if (folder) {
assert.ok(pathEquals(folder.uri.fsPath, join(__dirname, '../../testWorkspace')));
}
});
test('openTextDocument', async () => {
const uri = await createRandomFile();
// not yet there
const existing1 = vscode.workspace.textDocuments.find(doc => doc.uri.toString() === uri.toString());
assert.strictEqual(existing1, undefined);
// open and assert its there
const doc = await vscode.workspace.openTextDocument(uri);
assert.ok(doc);
assert.strictEqual(doc.uri.toString(), uri.toString());
const existing2 = vscode.workspace.textDocuments.find(doc => doc.uri.toString() === uri.toString());
assert.strictEqual(existing2 === doc, true);
});
test('openTextDocument, illegal path', () => {
return vscode.workspace.openTextDocument('funkydonky.txt').then(_doc => {
throw new Error('missing error');
}, _err => {
// good!
});
});
test('openTextDocument, untitled is dirty', async function () {
return vscode.workspace.openTextDocument(vscode.workspace.workspaceFolders![0].uri.with({ scheme: 'untitled', path: posix.join(vscode.workspace.workspaceFolders![0].uri.path, 'newfile.txt') })).then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
assert.ok(doc.isDirty);
});
});
test('openTextDocument, untitled with host', function () {
const uri = vscode.Uri.parse('untitled://localhost/c%24/Users/jrieken/code/samples/foobar.txt');
return vscode.workspace.openTextDocument(uri).then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
});
});
test('openTextDocument, untitled without path', function () {
return vscode.workspace.openTextDocument().then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
assert.ok(doc.isDirty);
});
});
test('openTextDocument, untitled without path but language ID', function () {
return vscode.workspace.openTextDocument({ language: 'xml' }).then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
assert.strictEqual(doc.languageId, 'xml');
assert.ok(doc.isDirty);
});
});
test('openTextDocument, untitled without path but language ID and content', function () {
return vscode.workspace.openTextDocument({ language: 'html', content: '<h1>Hello world!</h1>' }).then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
assert.strictEqual(doc.languageId, 'html');
assert.ok(doc.isDirty);
assert.strictEqual(doc.getText(), '<h1>Hello world!</h1>');
});
});
test('openTextDocument, untitled closes on save', function () {
const path = join(vscode.workspace.rootPath || '', './newfile.txt');
return vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + path)).then(doc => {
assert.strictEqual(doc.uri.scheme, 'untitled');
assert.ok(doc.isDirty);
const closedDocuments: vscode.TextDocument[] = [];
const d0 = vscode.workspace.onDidCloseTextDocument(e => closedDocuments.push(e));
return vscode.window.showTextDocument(doc).then(() => {
return doc.save().then((didSave: boolean) => {
assert.strictEqual(didSave, true, `FAILED to save${doc.uri.toString()}`);
const closed = closedDocuments.filter(close => close.uri.toString() === doc.uri.toString())[0];
assert.ok(closed);
assert.ok(closed === doc);
assert.ok(!doc.isDirty);
assert.ok(fs.existsSync(path));
d0.dispose();
fs.unlinkSync(join(vscode.workspace.rootPath || '', './newfile.txt'));
});
});
});
});
test('openTextDocument, uri scheme/auth/path', function () {
const registration = vscode.workspace.registerTextDocumentContentProvider('sc', {
provideTextDocumentContent() {
return 'SC';
}
});
return Promise.all([
vscode.workspace.openTextDocument(vscode.Uri.parse('sc://auth')).then(doc => {
assert.strictEqual(doc.uri.authority, 'auth');
assert.strictEqual(doc.uri.path, '');
}),
vscode.workspace.openTextDocument(vscode.Uri.parse('sc:///path')).then(doc => {
assert.strictEqual(doc.uri.authority, '');
assert.strictEqual(doc.uri.path, '/path');
}),
vscode.workspace.openTextDocument(vscode.Uri.parse('sc://auth/path')).then(doc => {
assert.strictEqual(doc.uri.authority, 'auth');
assert.strictEqual(doc.uri.path, '/path');
})
]).then(() => {
registration.dispose();
});
});
test('openTextDocument, actual casing first', async function () {
const fs = new TestFS('this-fs', false);
const reg = vscode.workspace.registerFileSystemProvider(fs.scheme, fs, { isCaseSensitive: fs.isCaseSensitive });
const uriOne = vscode.Uri.parse('this-fs:/one');
const uriTwo = vscode.Uri.parse('this-fs:/two');
const uriONE = vscode.Uri.parse('this-fs:/ONE'); // same resource, different uri
const uriTWO = vscode.Uri.parse('this-fs:/TWO');
fs.writeFile(uriOne, Buffer.from('one'), { create: true, overwrite: true });
fs.writeFile(uriTwo, Buffer.from('two'), { create: true, overwrite: true });
// lower case (actual case) comes first
const docOne = await vscode.workspace.openTextDocument(uriOne);
assert.strictEqual(docOne.uri.toString(), uriOne.toString());
const docONE = await vscode.workspace.openTextDocument(uriONE);
assert.strictEqual(docONE === docOne, true);
assert.strictEqual(docONE.uri.toString(), uriOne.toString());
assert.strictEqual(docONE.uri.toString() !== uriONE.toString(), true); // yep
// upper case (NOT the actual case) comes first
const docTWO = await vscode.workspace.openTextDocument(uriTWO);
assert.strictEqual(docTWO.uri.toString(), uriTWO.toString());
const docTwo = await vscode.workspace.openTextDocument(uriTwo);
assert.strictEqual(docTWO === docTwo, true);
assert.strictEqual(docTwo.uri.toString(), uriTWO.toString());
assert.strictEqual(docTwo.uri.toString() !== uriTwo.toString(), true); // yep
reg.dispose();
});
test('eol, read', () => {
const a = createRandomFile('foo\nbar\nbar').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
assert.strictEqual(doc.eol, vscode.EndOfLine.LF);
});
});
const b = createRandomFile('foo\nbar\nbar\r\nbaz').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
assert.strictEqual(doc.eol, vscode.EndOfLine.LF);
});
});
const c = createRandomFile('foo\r\nbar\r\nbar').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
assert.strictEqual(doc.eol, vscode.EndOfLine.CRLF);
});
});
return Promise.all([a, b, c]);
});
test('eol, change via editor', () => {
return createRandomFile('foo\nbar\nbar').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
assert.strictEqual(doc.eol, vscode.EndOfLine.LF);
return vscode.window.showTextDocument(doc).then(editor => {
return editor.edit(builder => builder.setEndOfLine(vscode.EndOfLine.CRLF));
}).then(value => {
assert.ok(value);
assert.ok(doc.isDirty);
assert.strictEqual(doc.eol, vscode.EndOfLine.CRLF);
});
});
});
});
test('eol, change via applyEdit', () => {
return createRandomFile('foo\nbar\nbar').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
assert.strictEqual(doc.eol, vscode.EndOfLine.LF);
const edit = new vscode.WorkspaceEdit();
edit.set(file, [vscode.TextEdit.setEndOfLine(vscode.EndOfLine.CRLF)]);
return vscode.workspace.applyEdit(edit).then(value => {
assert.ok(value);
assert.ok(doc.isDirty);
assert.strictEqual(doc.eol, vscode.EndOfLine.CRLF);
});
});
});
});
test('eol, change via onWillSave', async function () {
let called = false;
const sub = vscode.workspace.onWillSaveTextDocument(e => {
called = true;
e.waitUntil(Promise.resolve([vscode.TextEdit.setEndOfLine(vscode.EndOfLine.LF)]));
});
const file = await createRandomFile('foo\r\nbar\r\nbar');
const doc = await vscode.workspace.openTextDocument(file);
assert.strictEqual(doc.eol, vscode.EndOfLine.CRLF);
const edit = new vscode.WorkspaceEdit();
edit.set(file, [vscode.TextEdit.insert(new vscode.Position(0, 0), '-changes-')]);
const successEdit = await vscode.workspace.applyEdit(edit);
assert.ok(successEdit);
const successSave = await doc.save();
assert.ok(successSave);
assert.ok(called);
assert.ok(!doc.isDirty);
assert.strictEqual(doc.eol, vscode.EndOfLine.LF);
sub.dispose();
});
test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', async () => {
const file = await createRandomFile();
const disposables: vscode.Disposable[] = [];
await revertAllDirty(); // needed for a clean state for `onDidSaveTextDocument` (#102365)
const onDidOpenTextDocument = new Set<vscode.TextDocument>();
const onDidChangeTextDocument = new Set<vscode.TextDocument>();
const onDidSaveTextDocument = new Set<vscode.TextDocument>();
disposables.push(vscode.workspace.onDidOpenTextDocument(e => {
onDidOpenTextDocument.add(e);
}));
disposables.push(vscode.workspace.onDidChangeTextDocument(e => {
onDidChangeTextDocument.add(e.document);
}));
disposables.push(vscode.workspace.onDidSaveTextDocument(e => {
onDidSaveTextDocument.add(e);
}));
const doc = await vscode.workspace.openTextDocument(file);
const editor = await vscode.window.showTextDocument(doc);
await editor.edit((builder) => {
builder.insert(new vscode.Position(0, 0), 'Hello World');
});
await doc.save();
assert.ok(Array.from(onDidOpenTextDocument).find(e => e.uri.toString() === file.toString()), 'did Open: ' + file.toString());
assert.ok(Array.from(onDidChangeTextDocument).find(e => e.uri.toString() === file.toString()), 'did Change: ' + file.toString());
assert.ok(Array.from(onDidSaveTextDocument).find(e => e.uri.toString() === file.toString()), 'did Save: ' + file.toString());
disposeAll(disposables);
return deleteFile(file);
});
test('events: onDidSaveTextDocument fires even for non dirty file when saved', async () => {
const file = await createRandomFile();
const disposables: vscode.Disposable[] = [];
await revertAllDirty(); // needed for a clean state for `onDidSaveTextDocument` (#102365)
const onDidSaveTextDocument = new Set<vscode.TextDocument>();
disposables.push(vscode.workspace.onDidSaveTextDocument(e => {
onDidSaveTextDocument.add(e);
}));
const doc = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(doc);
await vscode.commands.executeCommand('workbench.action.files.save');
assert.ok(onDidSaveTextDocument);
assert.ok(Array.from(onDidSaveTextDocument).find(e => e.uri.toString() === file.toString()), 'did Save: ' + file.toString());
disposeAll(disposables);
return deleteFile(file);
});
test('openTextDocument, with selection', function () {
return createRandomFile('foo\nbar\nbar').then(file => {
return vscode.workspace.openTextDocument(file).then(doc => {
return vscode.window.showTextDocument(doc, { selection: new vscode.Range(new vscode.Position(1, 1), new vscode.Position(1, 2)) }).then(editor => {
assert.strictEqual(editor.selection.start.line, 1);
assert.strictEqual(editor.selection.start.character, 1);
assert.strictEqual(editor.selection.end.line, 1);
assert.strictEqual(editor.selection.end.character, 2);
});
});
});
});
test('registerTextDocumentContentProvider, simple', function () {
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(uri) {
return uri.toString();
}
});
const uri = vscode.Uri.parse('foo://testing/virtual.js');
return vscode.workspace.openTextDocument(uri).then(doc => {
assert.strictEqual(doc.getText(), uri.toString());
assert.strictEqual(doc.isDirty, false);
assert.strictEqual(doc.uri.toString(), uri.toString());
registration.dispose();
});
});
test('registerTextDocumentContentProvider, constrains', function () {
// built-in
assert.throws(function () {
vscode.workspace.registerTextDocumentContentProvider('untitled', { provideTextDocumentContent() { return null; } });
});
// built-in
assert.throws(function () {
vscode.workspace.registerTextDocumentContentProvider('file', { provideTextDocumentContent() { return null; } });
});
// missing scheme
return vscode.workspace.openTextDocument(vscode.Uri.parse('notThere://foo/far/boo/bar')).then(() => {
assert.ok(false, 'expected failure');
}, _err => {
// expected
});
});
test('registerTextDocumentContentProvider, multiple', function () {
// duplicate registration
const registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(uri) {
if (uri.authority === 'foo') {
return '1';
}
return undefined;
}
});
const registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(uri) {
if (uri.authority === 'bar') {
return '2';
}
return undefined;
}
});
return Promise.all([
vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => { assert.strictEqual(doc.getText(), '1'); }),
vscode.workspace.openTextDocument(vscode.Uri.parse('foo://bar/bla')).then(doc => { assert.strictEqual(doc.getText(), '2'); })
]).then(() => {
registration1.dispose();
registration2.dispose();
});
});
test('registerTextDocumentContentProvider, evil provider', function () {
// duplicate registration
const registration1 = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri) {
return '1';
}
});
const registration2 = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri): string {
throw new Error('fail');
}
});
return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://foo/bla')).then(doc => {
assert.strictEqual(doc.getText(), '1');
registration1.dispose();
registration2.dispose();
});
});
test('registerTextDocumentContentProvider, invalid text', function () {
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri) {
return <any>123;
}
});
return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://auth/path')).then(() => {
assert.ok(false, 'expected failure');
}, _err => {
// expected
registration.dispose();
});
});
test('registerTextDocumentContentProvider, show virtual document', function () {
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri) {
return 'I am virtual';
}
});
return vscode.workspace.openTextDocument(vscode.Uri.parse('foo://something/path')).then(doc => {
return vscode.window.showTextDocument(doc).then(editor => {
assert.ok(editor.document === doc);
assert.strictEqual(editor.document.getText(), 'I am virtual');
registration.dispose();
});
});
});
test('registerTextDocumentContentProvider, open/open document', function () {
let callCount = 0;
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri) {
callCount += 1;
return 'I am virtual';
}
});
const uri = vscode.Uri.parse('foo://testing/path');
return Promise.all([vscode.workspace.openTextDocument(uri), vscode.workspace.openTextDocument(uri)]).then(docs => {
const [first, second] = docs;
assert.ok(first === second);
assert.ok(vscode.workspace.textDocuments.some(doc => doc.uri.toString() === uri.toString()));
assert.strictEqual(callCount, 1);
registration.dispose();
});
});
test('registerTextDocumentContentProvider, empty doc', function () {
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
provideTextDocumentContent(_uri) {
return '';
}
});
const uri = vscode.Uri.parse('foo:doc/empty');
return vscode.workspace.openTextDocument(uri).then(doc => {
assert.strictEqual(doc.getText(), '');
assert.strictEqual(doc.uri.toString(), uri.toString());
registration.dispose();
});
});
test('registerTextDocumentContentProvider, change event', async function () {
let callCount = 0;
const emitter = new vscode.EventEmitter<vscode.Uri>();
const registration = vscode.workspace.registerTextDocumentContentProvider('foo', {
onDidChange: emitter.event,
provideTextDocumentContent(_uri) {
return 'call' + (callCount++);
}
});
const uri = vscode.Uri.parse('foo://testing/path3');
const doc = await vscode.workspace.openTextDocument(uri);
assert.strictEqual(callCount, 1);
assert.strictEqual(doc.getText(), 'call0');
return new Promise<void>(resolve => {
const subscription = vscode.workspace.onDidChangeTextDocument(event => {
assert.ok(event.document === doc);
assert.strictEqual(event.document.getText(), 'call1');
subscription.dispose();
registration.dispose();
resolve();
});
emitter.fire(doc.uri);
});
});
test('findFiles', () => {
return vscode.workspace.findFiles('**/image.png').then((res) => {
assert.strictEqual(res.length, 2);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
});
});
test('findFiles - null exclude', async () => {
await vscode.workspace.findFiles('**/file.txt').then((res) => {
// search.exclude folder is still searched, files.exclude folder is not
assert.strictEqual(res.length, 1);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
});
await vscode.workspace.findFiles('**/file.txt', null).then((res) => {
// search.exclude and files.exclude folders are both searched
assert.strictEqual(res.length, 2);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
});
});
test('findFiles - exclude', () => {
return vscode.workspace.findFiles('**/image.png').then((res) => {
assert.strictEqual(res.length, 2);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
});
});
test('findFiles, exclude', () => {
return vscode.workspace.findFiles('**/image.png', '**/sub/**').then((res) => {
assert.strictEqual(res.length, 1);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'image.png');
});
});
test('findFiles, cancellation', () => {
const source = new vscode.CancellationTokenSource();
const token = source.token; // just to get an instance first
source.cancel();
return vscode.workspace.findFiles('*.js', null, 100, token).then((res) => {
assert.deepStrictEqual(res, []);
});
});
test('`findFiles2`', () => {
return vscode.workspace.findFiles2('**/image.png').then((res) => {
assert.strictEqual(res.length, 2);
});
});
test('findFiles2 - null exclude', async () => {
await vscode.workspace.findFiles2('**/file.txt', { useDefaultExcludes: true, useDefaultSearchExcludes: false }).then((res) => {
// file.exclude folder is still searched, search.exclude folder is not
assert.strictEqual(res.length, 1);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
});
await vscode.workspace.findFiles2('**/file.txt', { useDefaultExcludes: false, useDefaultSearchExcludes: false }).then((res) => {
// search.exclude and files.exclude folders are both searched
assert.strictEqual(res.length, 2);
assert.strictEqual(basename(vscode.workspace.asRelativePath(res[0])), 'file.txt');
});
});
test('findFiles2, exclude', () => {
return vscode.workspace.findFiles2('**/image.png', { exclude: '**/sub/**' }).then((res) => {
res.forEach(r => console.log(r.toString()));
assert.strictEqual(res.length, 1);
});
});
test('findFiles2, cancellation', () => {
const source = new vscode.CancellationTokenSource();
const token = source.token; // just to get an instance first
source.cancel();
return vscode.workspace.findFiles2('*.js', {}, token).then((res) => {
assert.deepStrictEqual(res, []);
});
});
test('findTextInFiles', async () => {
const options: vscode.FindTextInFilesOptions = {
include: '*.ts',
previewOptions: {
matchLines: 1,
charsPerLine: 100
}
};
const results: vscode.TextSearchResult[] = [];
await vscode.workspace.findTextInFiles({ pattern: 'foo' }, options, result => {
results.push(result);
});
assert.strictEqual(results.length, 1);
const match = <vscode.TextSearchMatch>results[0];
assert(match.preview.text.indexOf('foo') >= 0);
assert.strictEqual(basename(vscode.workspace.asRelativePath(match.uri)), '10linefile.ts');
});
test('findTextInFiles, cancellation', async () => {
const results: vscode.TextSearchResult[] = [];
const cancellation = new vscode.CancellationTokenSource();
cancellation.cancel();
await vscode.workspace.findTextInFiles({ pattern: 'foo' }, result => {
results.push(result);
}, cancellation.token);
});
test('applyEdit', async () => {
const doc = await vscode.workspace.openTextDocument(vscode.Uri.parse('untitled:' + join(vscode.workspace.rootPath || '', './new2.txt')));
const edit = new vscode.WorkspaceEdit();
edit.insert(doc.uri, new vscode.Position(0, 0), new Array(1000).join('Hello World'));
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);
assert.strictEqual(doc.isDirty, true);
});
test('applyEdit should fail when editing deleted resource', withLogDisabled(async () => {
const resource = await createRandomFile();
const edit = new vscode.WorkspaceEdit();
edit.deleteFile(resource);
edit.insert(resource, new vscode.Position(0, 0), '');
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, false);
}));
test('applyEdit should fail when renaming deleted resource', withLogDisabled(async () => {
const resource = await createRandomFile();
const edit = new vscode.WorkspaceEdit();
edit.deleteFile(resource);
edit.renameFile(resource, resource);
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, false);
}));
test('applyEdit should fail when editing renamed from resource', withLogDisabled(async () => {
const resource = await createRandomFile();
const newResource = vscode.Uri.file(resource.fsPath + '.1');
const edit = new vscode.WorkspaceEdit();
edit.renameFile(resource, newResource);
edit.insert(resource, new vscode.Position(0, 0), '');
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, false);
}));
test('applyEdit "edit A -> rename A to B -> edit B"', async () => {
await testEditRenameEdit(oldUri => oldUri.with({ path: oldUri.path + 'NEW' }));
});
test('applyEdit "edit A -> rename A to B (different case)" -> edit B', async () => {
await testEditRenameEdit(oldUri => oldUri.with({ path: oldUri.path.toUpperCase() }));
});
test('applyEdit "edit A -> rename A to B (same case)" -> edit B', async () => {
await testEditRenameEdit(oldUri => oldUri);
});
async function testEditRenameEdit(newUriCreator: (oldUri: vscode.Uri) => vscode.Uri): Promise<void> {
const oldUri = await createRandomFile();
const newUri = newUriCreator(oldUri);
const edit = new vscode.WorkspaceEdit();
edit.insert(oldUri, new vscode.Position(0, 0), 'BEFORE');
edit.renameFile(oldUri, newUri);
edit.insert(newUri, new vscode.Position(0, 0), 'AFTER');
assert.ok(await vscode.workspace.applyEdit(edit));
const doc = await vscode.workspace.openTextDocument(newUri);
assert.strictEqual(doc.getText(), 'AFTERBEFORE');
assert.strictEqual(doc.isDirty, true);
}
function nameWithUnderscore(uri: vscode.Uri) {
return uri.with({ path: posix.join(posix.dirname(uri.path), `_${posix.basename(uri.path)}`) });
}
test('WorkspaceEdit: applying edits before and after rename duplicates resource #42633', withLogDisabled(async function () {
const docUri = await createRandomFile();
const newUri = nameWithUnderscore(docUri);
const we = new vscode.WorkspaceEdit();
we.insert(docUri, new vscode.Position(0, 0), 'Hello');
we.insert(docUri, new vscode.Position(0, 0), 'Foo');
we.renameFile(docUri, newUri);
we.insert(newUri, new vscode.Position(0, 0), 'Bar');
assert.ok(await vscode.workspace.applyEdit(we));
const doc = await vscode.workspace.openTextDocument(newUri);
assert.strictEqual(doc.getText(), 'BarHelloFoo');
}));
test('WorkspaceEdit: Problem recreating a renamed resource #42634', withLogDisabled(async function () {
const docUri = await createRandomFile();
const newUri = nameWithUnderscore(docUri);
const we = new vscode.WorkspaceEdit();
we.insert(docUri, new vscode.Position(0, 0), 'Hello');
we.insert(docUri, new vscode.Position(0, 0), 'Foo');
we.renameFile(docUri, newUri);
we.createFile(docUri);
we.insert(docUri, new vscode.Position(0, 0), 'Bar');
assert.ok(await vscode.workspace.applyEdit(we));
const newDoc = await vscode.workspace.openTextDocument(newUri);
assert.strictEqual(newDoc.getText(), 'HelloFoo');
const doc = await vscode.workspace.openTextDocument(docUri);
assert.strictEqual(doc.getText(), 'Bar');
}));
test('WorkspaceEdit api - after saving a deleted file, it still shows up as deleted. #42667', withLogDisabled(async function () {
const docUri = await createRandomFile();
const we = new vscode.WorkspaceEdit();
we.deleteFile(docUri);
we.insert(docUri, new vscode.Position(0, 0), 'InsertText');
assert.ok(!(await vscode.workspace.applyEdit(we)));
try {
await vscode.workspace.openTextDocument(docUri);
assert.ok(false);
} catch (e) {
assert.ok(true);
}
}));
test('WorkspaceEdit: edit and rename parent folder duplicates resource #42641', async function () {
const dir = vscode.Uri.parse(`${testFs.scheme}:/before-${rndName()}`);
await testFs.createDirectory(dir);
const docUri = await createRandomFile('', dir);
const docParent = docUri.with({ path: posix.dirname(docUri.path) });
const newParent = nameWithUnderscore(docParent);
const we = new vscode.WorkspaceEdit();
we.insert(docUri, new vscode.Position(0, 0), 'Hello');
we.renameFile(docParent, newParent);
assert.ok(await vscode.workspace.applyEdit(we));
try {
await vscode.workspace.openTextDocument(docUri);
assert.ok(false);
} catch (e) {
assert.ok(true);
}
const newUri = newParent.with({ path: posix.join(newParent.path, posix.basename(docUri.path)) });
const doc = await vscode.workspace.openTextDocument(newUri);
assert.ok(doc);
assert.strictEqual(doc.getText(), 'Hello');
});
test('WorkspaceEdit: rename resource followed by edit does not work #42638', withLogDisabled(async function () {
const docUri = await createRandomFile();
const newUri = nameWithUnderscore(docUri);
const we = new vscode.WorkspaceEdit();
we.renameFile(docUri, newUri);
we.insert(newUri, new vscode.Position(0, 0), 'Hello');
assert.ok(await vscode.workspace.applyEdit(we));
const doc = await vscode.workspace.openTextDocument(newUri);
assert.strictEqual(doc.getText(), 'Hello');
}));
test('WorkspaceEdit: create & override', withLogDisabled(async function () {
const docUri = await createRandomFile('before');
let we = new vscode.WorkspaceEdit();
we.createFile(docUri);
assert.ok(!await vscode.workspace.applyEdit(we));
assert.strictEqual((await vscode.workspace.openTextDocument(docUri)).getText(), 'before');
we = new vscode.WorkspaceEdit();
we.createFile(docUri, { overwrite: true });
assert.ok(await vscode.workspace.applyEdit(we));
assert.strictEqual((await vscode.workspace.openTextDocument(docUri)).getText(), '');
}));
test('WorkspaceEdit: create & ignoreIfExists', withLogDisabled(async function () {
const docUri = await createRandomFile('before');
let we = new vscode.WorkspaceEdit();
we.createFile(docUri, { ignoreIfExists: true });
assert.ok(await vscode.workspace.applyEdit(we));
assert.strictEqual((await vscode.workspace.openTextDocument(docUri)).getText(), 'before');
we = new vscode.WorkspaceEdit();
we.createFile(docUri, { overwrite: true, ignoreIfExists: true });
assert.ok(await vscode.workspace.applyEdit(we));
assert.strictEqual((await vscode.workspace.openTextDocument(docUri)).getText(), '');
}));
test('WorkspaceEdit: rename & ignoreIfExists', withLogDisabled(async function () {
const aUri = await createRandomFile('aaa');
const bUri = await createRandomFile('bbb');
let we = new vscode.WorkspaceEdit();
we.renameFile(aUri, bUri);
assert.ok(!await vscode.workspace.applyEdit(we));
we = new vscode.WorkspaceEdit();
we.renameFile(aUri, bUri, { ignoreIfExists: true });
assert.ok(await vscode.workspace.applyEdit(we));
we = new vscode.WorkspaceEdit();
we.renameFile(aUri, bUri, { overwrite: false, ignoreIfExists: true });
assert.ok(!await vscode.workspace.applyEdit(we));
we = new vscode.WorkspaceEdit();
we.renameFile(aUri, bUri, { overwrite: true, ignoreIfExists: true });
assert.ok(await vscode.workspace.applyEdit(we));
}));
test('WorkspaceEdit: delete & ignoreIfNotExists', withLogDisabled(async function () {
const docUri = await createRandomFile();
let we = new vscode.WorkspaceEdit();
we.deleteFile(docUri, { ignoreIfNotExists: false });
assert.ok(await vscode.workspace.applyEdit(we));
we = new vscode.WorkspaceEdit();
we.deleteFile(docUri, { ignoreIfNotExists: false });
assert.ok(!await vscode.workspace.applyEdit(we));
we = new vscode.WorkspaceEdit();
we.deleteFile(docUri, { ignoreIfNotExists: true });
assert.ok(await vscode.workspace.applyEdit(we));
}));
test('WorkspaceEdit: insert & rename multiple', async function () {
const [f1, f2, f3] = await Promise.all([createRandomFile(), createRandomFile(), createRandomFile()]);
const we = new vscode.WorkspaceEdit();
we.insert(f1, new vscode.Position(0, 0), 'f1');
we.insert(f2, new vscode.Position(0, 0), 'f2');
we.insert(f3, new vscode.Position(0, 0), 'f3');
const f1_ = nameWithUnderscore(f1);
we.renameFile(f1, f1_);
assert.ok(await vscode.workspace.applyEdit(we));
assert.strictEqual((await vscode.workspace.openTextDocument(f3)).getText(), 'f3');
assert.strictEqual((await vscode.workspace.openTextDocument(f2)).getText(), 'f2');
assert.strictEqual((await vscode.workspace.openTextDocument(f1_)).getText(), 'f1');
try {
await vscode.workspace.fs.stat(f1);
assert.ok(false);
} catch {
assert.ok(true);
}
});
test('workspace.applyEdit drops the TextEdit if there is a RenameFile later #77735 (with opened editor)', async function () {
await test77735(true);
});
test('workspace.applyEdit drops the TextEdit if there is a RenameFile later #77735 (without opened editor)', async function () {
await test77735(false);
});
async function test77735(withOpenedEditor: boolean): Promise<void> {
const docUriOriginal = await createRandomFile();
const docUriMoved = docUriOriginal.with({ path: `${docUriOriginal.path}.moved` });
await deleteFile(docUriMoved);
if (withOpenedEditor) {
const document = await vscode.workspace.openTextDocument(docUriOriginal);
await vscode.window.showTextDocument(document);
} else {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
}
for (let i = 0; i < 4; i++) {
const we = new vscode.WorkspaceEdit();
let oldUri: vscode.Uri;
let newUri: vscode.Uri;
let expected: string;
if (i % 2 === 0) {
oldUri = docUriOriginal;
newUri = docUriMoved;
we.insert(oldUri, new vscode.Position(0, 0), 'Hello');
expected = 'Hello';
} else {
oldUri = docUriMoved;
newUri = docUriOriginal;
we.delete(oldUri, new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 5)));
expected = '';
}
we.renameFile(oldUri, newUri);
assert.ok(await vscode.workspace.applyEdit(we));
const document = await vscode.workspace.openTextDocument(newUri);
assert.strictEqual(document.isDirty, true);
const result = await document.save();
assert.strictEqual(result, true, `save failed in iteration: ${i} (docUriOriginal: ${docUriOriginal.fsPath})`);
assert.strictEqual(document.isDirty, false, `document still dirty in iteration: ${i} (docUriOriginal: ${docUriOriginal.fsPath})`);
assert.strictEqual(document.getText(), expected);
await delay(10);
}
}
test('The api workspace.applyEdit failed for some case of mixing resourceChange and textEdit #80688, 1/2', async function () {
const file1 = await createRandomFile();
const file2 = await createRandomFile();
const we = new vscode.WorkspaceEdit();
we.insert(file1, new vscode.Position(0, 0), 'import1;');
const file2Name = basename(file2.fsPath);
const file2NewUri = vscode.Uri.joinPath(file2, `../new/${file2Name}`);
we.renameFile(file2, file2NewUri);
we.insert(file1, new vscode.Position(0, 0), 'import2;');
await vscode.workspace.applyEdit(we);
const document = await vscode.workspace.openTextDocument(file1);
// const expected = 'import1;import2;';
const expected2 = 'import2;import1;';
assert.strictEqual(document.getText(), expected2);
});
test('The api workspace.applyEdit failed for some case of mixing resourceChange and textEdit #80688, 2/2', async function () {