-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathProjectManager.spec.ts
More file actions
1149 lines (996 loc) · 44.8 KB
/
ProjectManager.spec.ts
File metadata and controls
1149 lines (996 loc) · 44.8 KB
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
import { expect } from 'chai';
import { ProjectManager } from './ProjectManager';
import { tempDir, rootDir, expectZeroDiagnostics, expectDiagnostics, expectCompletionsIncludes, workspaceSettings } from '../testHelpers.spec';
import * as fsExtra from 'fs-extra';
import util, { standardizePath as s } from '../util';
import type { SinonStub } from 'sinon';
import { createSandbox } from 'sinon';
import { Project } from './Project';
import { WorkerThreadProject } from './worker/WorkerThreadProject';
import { getWakeWorkerThreadPromise } from './worker/WorkerThreadProject.spec';
import type { LspDiagnostic } from './LspProject';
import { DiagnosticMessages } from '../DiagnosticMessages';
import { FileChangeType } from 'vscode-languageserver-protocol';
import { PathFilterer } from './PathFilterer';
import { Deferred } from '../deferred';
import type { DocumentActionWithStatus } from './DocumentManager';
import * as net from 'net';
import type { Program } from '../Program';
import * as getPort from 'get-port';
const sinon = createSandbox();
describe('ProjectManager', () => {
let manager: ProjectManager;
let pathFilterer: PathFilterer;
beforeEach(() => {
pathFilterer = new PathFilterer();
manager = new ProjectManager({
pathFilterer: pathFilterer
});
fsExtra.emptyDirSync(tempDir);
sinon.restore();
diagnosticsListeners = [];
diagnosticsResponses = [];
manager.on('diagnostics', (event) => {
if (diagnosticsListeners.length > 0) {
diagnosticsListeners.shift()?.(event.diagnostics);
} else {
diagnosticsResponses.push(event.diagnostics);
}
});
});
afterEach(() => {
fsExtra.emptyDirSync(tempDir);
sinon.restore();
manager.dispose();
});
let diagnosticsListeners: Array<(diagnostics: LspDiagnostic[]) => void> = [];
let diagnosticsResponses: Array<LspDiagnostic[]> = [];
/**
* Get a promise that resolves when the next diagnostics event is emitted (or pop the earliest unhandled diagnostics list if some are already here)
*/
function onNextDiagnostics() {
if (diagnosticsResponses.length > 0) {
return Promise.resolve(diagnosticsResponses.shift());
} else {
return new Promise<LspDiagnostic[]>((resolve) => {
diagnosticsListeners.push(resolve);
});
}
}
async function setFile(srcPath: string, contents: string) {
//set the namespace first
await manager.handleFileChanges([{
srcPath: srcPath,
type: FileChangeType.Changed,
fileContents: contents,
allowStandaloneProject: false
}]);
}
describe('on', () => {
it('emits events', async () => {
const stub = sinon.stub();
const off = manager.on('diagnostics', stub);
await manager['emit']('diagnostics', { project: undefined, diagnostics: [] });
expect(stub.callCount).to.eql(1);
await manager['emit']('diagnostics', { project: undefined, diagnostics: [] });
expect(stub.callCount).to.eql(2);
off();
await manager['emit']('diagnostics', { project: undefined, diagnostics: [] });
expect(stub.callCount).to.eql(2);
});
});
describe('validation tracking', () => {
it('tracks validation state', async () => {
await manager.syncProjects([workspaceSettings]);
const project = manager.projects[0] as Project;
//force validation to take a while
sinon.stub(project['builder'].program, 'validate').callsFake(async () => {
await util.sleep(100);
});
expect(manager.busyStatusTracker.status).to.eql('idle');
//run several validations (which cancel the previous)
void project.validate();
await util.sleep(10);
void project.validate();
await util.sleep(10);
void project.validate();
await util.sleep(10);
//busy status should be active
expect(manager.busyStatusTracker.status).to.eql('busy');
});
});
describe('syncProjects', () => {
it('does not crash on zero projects', async () => {
await manager.syncProjects([]);
});
it('finds bsconfig in a folder', async () => {
fsExtra.outputFileSync(`${rootDir}/bsconfig.json`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`
]);
});
it('finds bsconfig at root and also in subfolder', async () => {
fsExtra.outputFileSync(`${rootDir}/bsconfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/subdir/bsconfig.json`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`,
s`${rootDir}/subdir/bsconfig.json`
]);
});
it('skips excluded bsconfig bsconfig in a folder', async () => {
fsExtra.outputFileSync(`${rootDir}/bsconfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/subdir/bsconfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
excludePatterns: ['**/subdir/**/*']
}]);
expect(
manager.projects.map(x => x.projectKey)
).to.eql([
s`${rootDir}/bsconfig.json`
]);
});
it('uses rootDir when manifest found but no brightscript file', async () => {
fsExtra.outputFileSync(`${rootDir}/subdir/manifest`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey)
).to.eql([
s`${rootDir}`
]);
});
it('returns root folder when automatic discovery is disabled', async () => {
fsExtra.outputFileSync(`${rootDir}/project1/bsconfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/project2/bsconfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
enableProjectDiscovery: false
}
}]);
expect(
manager.projects.map(x => x.projectKey)
).to.eql([
s`${rootDir}`
]);
});
it('gets diagnostics from plugins added in afterProgramValidate', async () => {
fsExtra.outputFileSync(`${rootDir}/plugin.js`, `
module.exports = function () {
return {
afterProgramValidate: function(program) {
var file = program.getFile('source/main.brs');
//add a diagnostic from a plugin
file.addDiagnostic({
message: 'Test diagnostic',
code: 'test-123',
severity: 1
});
}
}
}
`);
fsExtra.outputJsonSync(`${rootDir}/bsconfig.json`, {
plugins: [
'./plugin.js'
]
});
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, `
sub test()
print nameNotDefined
end sub
`);
fsExtra.outputFileSync(`${rootDir}/manifest`, '');
await manager.syncProjects([workspaceSettings]);
expectDiagnostics(await onNextDiagnostics(), [
DiagnosticMessages.cannotFindName('nameNotDefined').message,
'Test diagnostic'
]);
});
it('uses subdir when manifest and brightscript file found', async () => {
fsExtra.outputFileSync(`${rootDir}/subdir/manifest`, '');
fsExtra.outputFileSync(`${rootDir}/subdir/source/main.brs`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey)
).to.eql([
s`${rootDir}/subdir`
]);
});
it('removes stale projects', async () => {
fsExtra.outputFileSync(`${rootDir}/subdir1/bsconfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/subdir2/bsconfig.json`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/subdir1/bsconfig.json`,
s`${rootDir}/subdir2/bsconfig.json`
]);
fsExtra.removeSync(`${rootDir}/subdir1/bsconfig.json`);
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/subdir2/bsconfig.json`
]);
});
it('keeps existing projects on subsequent sync calls', async () => {
fsExtra.outputFileSync(`${rootDir}/subdir1/bsconfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/subdir2/bsconfig.json`, '');
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/subdir1/bsconfig.json`,
s`${rootDir}/subdir2/bsconfig.json`
]);
await manager.syncProjects([workspaceSettings]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/subdir1/bsconfig.json`,
s`${rootDir}/subdir2/bsconfig.json`
]);
});
it('uses nonstandard json naming when specified in projects array', async () => {
fsExtra.outputFileSync(`${rootDir}/project1/testBrighterScriptConfig.json`, '');
fsExtra.outputFileSync(`${rootDir}/project1/bsconfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
projects: [
{ path: s`${rootDir}/project1/testBrighterScriptConfig.json` }
]
}]);
//we should NOT have found the `project1/bsconfig.json` file because it's not in the projects array
expect(
manager.projects.map(x => x.projectKey)
).to.eql([
s`${rootDir}/project1/testBrighterScriptConfig.json`
]);
});
it('supports pointing to a folder AND a bsconfig.json in projects array', async () => {
fsExtra.outputFileSync(`${rootDir}/project1/testBrighterScriptConfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
projects: [
{ path: s`${rootDir}/project1/testBrighterScriptConfig.json` },
{ path: s`${rootDir}/project1` }
]
}]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/project1`,
s`${rootDir}/project1/testBrighterScriptConfig.json`
]);
});
it('supports project with AND without bsconfig.json in same location in projects array', async () => {
fsExtra.outputFileSync(`${rootDir}/project1/bsconfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
projects: [
{ path: s`${rootDir}/project1` },
{ path: s`${rootDir}/project1/bsconfig.json` }
]
}]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/project1`,
s`${rootDir}/project1/bsconfig.json`
]);
});
it('ignores empty projects array configuration', async () => {
fsExtra.outputFileSync(`${rootDir}/project1/bsconfig.json`, '');
await manager.syncProjects([{
...workspaceSettings,
projects: []
}]);
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/project1/bsconfig.json`
]);
});
});
describe('maxDepth configuration', () => {
function writeTestFiles(files: Record<string, string>) {
for (const [filePath, content] of Object.entries(files)) {
fsExtra.outputFileSync(`${rootDir}/${filePath}`, content);
}
}
it('respects maxDepth of 1 when discovering projects', async () => {
// Create bsconfig.json files at different depths
writeTestFiles({
'bsconfig.json': '',
'level1/bsconfig.json': '',
'level1/level2/bsconfig.json': '',
'level1/level2/level3/bsconfig.json': ''
});
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
projectDiscoveryMaxDepth: 1
}
}]);
// maxDepth: 1 should find files at depth 0 only
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`
]);
});
it('respects maxDepth of 5 when discovering projects', async () => {
// Create bsconfig.json files at different depths
writeTestFiles({
'bsconfig.json': '',
'level1/bsconfig.json': '',
'level1/level2/bsconfig.json': '',
'level1/level2/level3/bsconfig.json': '',
'level1/level2/level3/level4/bsconfig.json': '',
'level1/level2/level3/level4/level5/bsconfig.json': '',
'level1/level2/level3/level4/level5/level6/bsconfig.json': ''
});
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
projectDiscoveryMaxDepth: 5
}
}]);
// maxDepth: 5 should find files at depths 0, 1, 2, 3, 4
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`,
s`${rootDir}/level1/bsconfig.json`,
s`${rootDir}/level1/level2/bsconfig.json`,
s`${rootDir}/level1/level2/level3/bsconfig.json`,
s`${rootDir}/level1/level2/level3/level4/bsconfig.json`
]);
});
it('respects maxDepth of 20 when discovering projects', async () => {
// Create bsconfig.json files at different depths, skipping some levels in between
// and proving it stops at level 20 by creating files at level 20 and 21
// Note: depth 20 means the file is in the 20th directory level from root
writeTestFiles({
'bsconfig.json': '',
'level1/bsconfig.json': '',
'level1/level2/level3/level4/level5/bsconfig.json': '',
'level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/level15/level16/level17/level18/level19/bsconfig.json': '',
'level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/level15/level16/level17/level18/level19/level20/bsconfig.json': ''
});
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
projectDiscoveryMaxDepth: 20
}
}]);
// maxDepth: 20 should find file at level 19 (depth 20) but not at level 20 (depth 21)
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`,
s`${rootDir}/level1/bsconfig.json`,
s`${rootDir}/level1/level2/level3/level4/level5/bsconfig.json`,
s`${rootDir}/level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/level15/level16/level17/level18/level19/bsconfig.json`
]);
});
it('uses default maxDepth of 15 when no maxDepth is specified', async () => {
// Create bsconfig.json files at different depths, skipping some levels in between
// and proving it stops at level 15 by creating files at level 15 and 16
// Note: depth 15 means the file is in the 15th directory level from root
writeTestFiles({
'bsconfig.json': '',
'level1/bsconfig.json': '',
'level1/level2/level3/level4/level5/bsconfig.json': '',
'level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/bsconfig.json': '',
'level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/level15/bsconfig.json': ''
});
await manager.syncProjects([workspaceSettings]);
// Default maxDepth: 15 should find file at level 14 (depth 15) but not at level 15 (depth 16)
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}/bsconfig.json`,
s`${rootDir}/level1/bsconfig.json`,
s`${rootDir}/level1/level2/level3/level4/level5/bsconfig.json`,
s`${rootDir}/level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/level11/level12/level13/level14/bsconfig.json`
]);
});
it('respects maxDepth of 1 when discovering roku projects with manifest files', async () => {
// Create manifest files at different depths
writeTestFiles({
'manifest': '',
'source/main.brs': '',
'level1/manifest': '',
'level1/source/main.brs': '',
'level1/level2/manifest': '',
'level1/level2/source/main.brs': '',
'level1/level2/level3/manifest': '',
'level1/level2/level3/source/main.brs': ''
});
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
projectDiscoveryMaxDepth: 1
}
}]);
// maxDepth: 1 should find projects at depth 0 only
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}`
]);
});
it('respects maxDepth of 5 when discovering roku projects with manifest files', async () => {
// Create manifest files at different depths
writeTestFiles({
'manifest': '',
'source/main.brs': '',
'level1/manifest': '',
'level1/source/main.brs': '',
'level1/level2/manifest': '',
'level1/level2/source/main.brs': '',
'level1/level2/level3/manifest': '',
'level1/level2/level3/source/main.brs': '',
'level1/level2/level3/level4/manifest': '',
'level1/level2/level3/level4/source/main.brs': '',
'level1/level2/level3/level4/level5/manifest': '',
'level1/level2/level3/level4/level5/source/main.brs': '',
'level1/level2/level3/level4/level5/level6/manifest': '',
'level1/level2/level3/level4/level5/level6/source/main.brs': ''
});
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
projectDiscoveryMaxDepth: 5
}
}]);
// maxDepth: 5 should find projects at depths 0, 1, 2, 3, 4
expect(
manager.projects.map(x => x.projectKey).sort()
).to.eql([
s`${rootDir}`,
s`${rootDir}/level1`,
s`${rootDir}/level1/level2`,
s`${rootDir}/level1/level2/level3`,
s`${rootDir}/level1/level2/level3/level4`
]);
});
});
describe('getCompletions', () => {
it('works for quick file changes', async () => {
//set up the project
await manager.syncProjects([workspaceSettings]);
//add the namespace first
await setFile(s`${rootDir}/source/alpha.bs`, `
namespace alpha
enum Direction
up
end enum
end namespace
`);
//add the baseline file
await setFile(s`${rootDir}/source/main.bs`, `
sub test()
thing = alpha.Directio
end sub
`);
await manager.onIdle();
//now for the test. type a char, request completions, type a char, request completions (just like how vscode does it)
void setFile(s`${rootDir}/source/main.bs`, `
sub test()
thing = alpha.Direction
end sub
`);
// const completionsPromise1 = manager.getCompletions({
// srcPath: s`${rootDir}/source/main.bs`,
// position: util.createPosition(2, 43)
// });
//request completions
void setFile(s`${rootDir}/source/main.bs`, `
sub test()
thing = alpha.Direction.
end sub
`);
const completionsPromise2 = manager.getCompletions({
srcPath: s`${rootDir}/source/main.bs`,
position: util.createPosition(2, 44)
});
// //the first set of completions should only have the `alpha.Direction` enum
// expectCompletionsIncludes(await completionsPromise1, [{
// label: 'Direction'
// }]);
//the next set of completions should only have the alpha.Direction.up enum member
expectCompletionsIncludes(await completionsPromise2, [{
label: 'up'
}]);
});
});
describe('flushDocumentChanges', () => {
it('does not crash when getting undefined back from projects', async () => {
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/project1/bsconfig.json`, {
rootDir: rootDir
});
await manager.syncProjects([workspaceSettings]);
sinon.stub(manager.projects[0], 'applyFileChanges').returns(Promise.resolve([
//return an undefined item, which used to cause a specific crash
undefined
]));
await manager['flushDocumentChanges']({
actions: [{
srcPath: s`${rootDir}/source/main.brs`,
type: 'set',
fileContents: 'sub main():end sub',
allowStandaloneProject: true
}]
});
});
});
describe('handleFileChanges', () => {
it('only sends files to the project that match the include patterns for that project', async () => {
fsExtra.outputFileSync(`${rootDir}/source/lib1/a.brs`, ``);
fsExtra.outputFileSync(`${rootDir}/source/lib2/a.brs`, ``);
fsExtra.outputFileSync(`${rootDir}/source/lib1/b.brs`, ``);
fsExtra.outputFileSync(`${rootDir}/source/lib2/b.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/project1/bsconfig.json`, {
rootDir: rootDir,
files: [
'source/**/a.brs'
]
});
fsExtra.outputJsonSync(`${rootDir}/project2/bsconfig.json`, {
rootDir: rootDir,
files: [
'source/**/b.brs'
]
});
await manager.syncProjects([workspaceSettings]);
let deferred1 = new Deferred();
let deferred2 = new Deferred();
const project1 = manager.projects.find(x => x.bsconfigPath.includes('project1')) as Project;
const project2 = manager.projects.find(x => x.bsconfigPath.includes('project2')) as Project;
const project1Stub: SinonStub = sinon.stub(project1, 'applyFileChanges').callsFake(async (...args) => {
const result = await project1Stub.wrappedMethod.apply(project1, args);
deferred1.resolve();
return result;
});
const project2Stub: SinonStub = sinon.stub(project2, 'applyFileChanges').callsFake(async (...args) => {
const result = await project2Stub.wrappedMethod.apply(project1, args);
deferred2.resolve();
return result;
});
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/lib1/a.brs`, type: FileChangeType.Changed },
{ srcPath: `${rootDir}/source/lib2/a.brs`, type: FileChangeType.Changed },
{ srcPath: `${rootDir}/source/lib1/b.brs`, type: FileChangeType.Changed },
{ srcPath: `${rootDir}/source/lib2/b.brs`, type: FileChangeType.Changed }
]);
//wait for the functions to finish being called
await Promise.all([
deferred1.promise,
deferred2.promise
]);
//project1 should only receive a.brs files
expect(project1Stub.getCall(0).args[0].map(x => x.srcPath)).to.eql([
s`${rootDir}/source/lib1/a.brs`,
s`${rootDir}/source/lib2/a.brs`
]);
//project2 should only receive b.brs files
expect(project2Stub.getCall(0).args[0].map(x => x.srcPath)).to.eql([
s`${rootDir}/source/lib1/b.brs`,
s`${rootDir}/source/lib2/b.brs`
]);
});
it('excludes files based on global exclude patterns', async () => {
fsExtra.outputFileSync(`${rootDir}/source/file1.md`, ``);
fsExtra.outputFileSync(`${rootDir}/source/file2.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/bsconfig.json`, {
files: [
'source/**/*.brs'
]
});
await manager.syncProjects([workspaceSettings]);
const stub = sinon.stub(manager as any, 'handleFileChange').callThrough();
//register an exclusion filter
pathFilterer.registerExcludeList(rootDir, [
'**/*.md'
]);
//make sure the .md file is ignored
await manager.handleFileChanges([
{ srcPath: s`${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: s`${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);
await manager.onIdle();
expect(
stub.getCalls().map(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file2.brs`
]);
stub.reset();
//remove all filters, make sure the markdown file is included
pathFilterer.clear();
await manager.handleFileChanges([
{ srcPath: s`${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: s`${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);
await manager.onIdle();
expect(
stub.getCalls().flatMap(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file1.md`,
s`${rootDir}/source/file2.brs`
]);
});
it('keeps files from bsconfig.json even if the path matches an exclude list', async () => {
fsExtra.outputFileSync(`${rootDir}/source/file1.md`, ``);
fsExtra.outputFileSync(`${rootDir}/source/file2.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/bsconfig.json`, {
files: ['source/**/*']
});
await manager.syncProjects([workspaceSettings]);
const stub = sinon.stub(manager['projects'][0], 'applyFileChanges').callThrough();
//register an exclusion filter
pathFilterer.registerExcludeList(rootDir, [
'**/*.md'
]);
//make sure the .md file is included because of its project's files array
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: `${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);
await manager.onIdle();
expect(
stub.getCalls().flatMap(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file1.md`,
s`${rootDir}/source/file2.brs`
]);
});
it('does not create a standalone project for files that exist in a known project', async () => {
fsExtra.outputFileSync(s`${rootDir}/source/main.brs`, `sub main() : end sub`);
await manager.syncProjects([workspaceSettings]);
await onNextDiagnostics();
await manager.handleFileChanges([
{ srcPath: s`${rootDir}/source/main.brs`, type: FileChangeType.Changed, fileContents: `'test`, allowStandaloneProject: true }
]);
await onNextDiagnostics();
//there should NOT be a standalone project
expect(manager['standaloneProjects'].size).to.eql(0);
});
it('converts a missing file to a delete', async () => {
await manager.syncProjects([workspaceSettings]);
await onNextDiagnostics();
let applyFileChangesDeferred = new Deferred<DocumentActionWithStatus[]>();
const project1 = manager.projects[0] as Project;
const project1Stub = sinon.stub(project1, 'applyFileChanges').callsFake(async (...args) => {
const result = await project1Stub.wrappedMethod.apply(project1, args);
applyFileChangesDeferred.resolve(result);
return result;
});
//emit created and changed events for files that don't exist. These turn into delete events
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/missing1.brs`, type: FileChangeType.Created },
{ srcPath: `${rootDir}/source/missing2.brs`, type: FileChangeType.Changed }
]);
//wait for the next set of diagnostics to arrive (signifying the files have been applied)
const result = await applyFileChangesDeferred.promise;
//make sure the project has these files
expect(
result.map(x => {
return { type: x.type, srcPath: x.srcPath };
})
).to.eql([{
srcPath: s`${rootDir}/source/missing1.brs`,
type: 'set'
}, {
srcPath: s`${rootDir}/source/missing2.brs`,
type: 'set'
}, {
srcPath: s`${rootDir}/source/missing1.brs`,
type: 'delete'
}, {
srcPath: s`${rootDir}/source/missing2.brs`,
type: 'delete'
}]);
});
it('properly syncs changes', async () => {
fsExtra.outputFileSync(`${rootDir}/source/lib1.brs`, `sub test1():print "alpha":end sub`);
fsExtra.outputFileSync(`${rootDir}/source/lib2.brs`, `sub test2():print "beta":end sub`);
await manager.syncProjects([workspaceSettings]);
expectZeroDiagnostics(await onNextDiagnostics());
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/lib1.brs`, fileContents: `sub test1():print alpha:end sub`, type: FileChangeType.Changed },
{ srcPath: `${rootDir}/source/lib2.brs`, fileContents: `sub test2()::print beta:end sub`, type: FileChangeType.Changed }
]);
expectDiagnostics(await onNextDiagnostics(), [
DiagnosticMessages.cannotFindName('alpha').message,
DiagnosticMessages.cannotFindName('beta').message
]);
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/lib1.brs`, fileContents: `sub test1():print "alpha":end sub`, type: FileChangeType.Changed },
{ srcPath: `${rootDir}/source/lib2.brs`, fileContents: `sub test2()::print "beta":end sub`, type: FileChangeType.Changed }
]);
expectZeroDiagnostics(await onNextDiagnostics());
});
it('adds all new files in a folder', async () => {
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, `sub main():print "main":end sub`);
await manager.syncProjects([workspaceSettings]);
expectZeroDiagnostics(await onNextDiagnostics());
//add a few files to a folder, then register that folder as an "add"
fsExtra.outputFileSync(`${rootDir}/source/libs/alpha/beta.brs`, `sub beta(): print one: end sub`);
fsExtra.outputFileSync(`${rootDir}/source/libs/alpha/charlie/delta.brs`, `sub delta():print two:end sub`);
fsExtra.outputFileSync(`${rootDir}/source/libs/echo/foxtrot.brs`, `sub foxtrot():print three:end sub`);
await manager.handleFileChanges([
//register the entire folder as an "add"
{ srcPath: `${rootDir}/source/libs`, type: FileChangeType.Created }
]);
expectDiagnostics(await onNextDiagnostics(), [
DiagnosticMessages.cannotFindName('one').message,
DiagnosticMessages.cannotFindName('two').message,
DiagnosticMessages.cannotFindName('three').message
]);
});
it('removes all files in a folder', async () => {
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, `sub main():print "main":end sub`);
fsExtra.outputFileSync(`${rootDir}/source/libs/alpha/beta.brs`, `sub beta(): print one: end sub`);
fsExtra.outputFileSync(`${rootDir}/source/libs/alpha/charlie/delta.brs`, `sub delta():print two:end sub`);
fsExtra.outputFileSync(`${rootDir}/source/libs/echo/foxtrot.brs`, `sub foxtrot():print three:end sub`);
await manager.syncProjects([workspaceSettings]);
expectDiagnostics(await onNextDiagnostics(), [
DiagnosticMessages.cannotFindName('one').message,
DiagnosticMessages.cannotFindName('two').message,
DiagnosticMessages.cannotFindName('three').message
]);
await manager.handleFileChanges([
//register the entire folder as an "add"
{ srcPath: `${rootDir}/source/libs`, type: FileChangeType.Deleted }
]);
expectZeroDiagnostics(await onNextDiagnostics());
});
});
describe('threading', () => {
before(async function workerThreadWarmup() {
this.timeout(20_000);
await getWakeWorkerThreadPromise();
});
it('spawns a worker thread when threading is enabled', async () => {
await manager.syncProjects([{
...workspaceSettings,
languageServer: {
...workspaceSettings.languageServer,
enableThreading: true
}
}]);
expect(manager.projects[0]).instanceof(WorkerThreadProject);
});
});
describe('getProject', () => {
it('uses .projectKey if param is not a string', async () => {
await manager.syncProjects([workspaceSettings]);
expect(
manager['getProject']({
projectKey: rootDir
}).projectKey
).to.eql(rootDir);
});
});
describe('createAndActivateProject', () => {
it('skips creating project if we already have it', async () => {
await manager.syncProjects([workspaceSettings]);
await manager['createAndActivateProject']({
projectKey: rootDir,
projectDir: rootDir,
workspaceFolder: rootDir,
bsconfigPath: undefined
});
expect(manager.projects.map(x => x.projectKey)).to.eql([
s`${rootDir}`
]);
});
it('uses given projectNumber', async () => {
await manager['createAndActivateProject']({
projectKey: rootDir,
projectDir: rootDir,
workspaceFolder: rootDir,
bsconfigPath: undefined,
projectNumber: 3
});
expect(manager.projects[0].projectNumber).to.eql(3);
});
it('properly tracks a failed run', async () => {
//force a total crash
sinon.stub(Project.prototype, 'activate').returns(
Promise.reject(new Error('Critical failure'))
);
let error;
try {
await manager['createAndActivateProject']({
projectKey: rootDir,
projectDir: rootDir,
workspaceFolder: rootDir,
bsconfigPath: 'subdir1/brsconfig.json'
});
} catch (e) {
error = e;
}
expect(error).to.include({ message: 'Critical failure' });
});
});
describe('removeProject', () => {
it('handles undefined', async () => {
manager['removeProject'](undefined);
await manager.syncProjects([workspaceSettings]);
manager['removeProject'](undefined);
});
it('does not crash when removing project that is not there', () => {
manager['removeProject']({
projectPath: rootDir,
dispose: () => { }
} as any);
});
});
describe('getSemanticTokens', () => {
it('waits until the project is ready', () => {
});
});
describe('standalone projects', () => {
it('creates a standalone project for files not found in a project', async () => {
await manager.syncProjects([]);
await manager.handleFileChanges([{
srcPath: `${rootDir}/source/main.brs`,
type: FileChangeType.Created,
fileContents: `sub main():print "main":end sub`,
allowStandaloneProject: true
}]);
await onNextDiagnostics();
expect(
[...manager['standaloneProjects'].values()][0]?.srcPath
).to.eql(s`${rootDir}/source/main.brs`);
//it deletes the standalone project when the file is closed
await manager.handleFileClose({
srcPath: `${rootDir}/source/main.brs`
});
expect(manager['standaloneProjects'].size).to.eql(0);
});
});