-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathnativePythonFinder.getAllExtraSearchPaths.unit.test.ts
More file actions
867 lines (734 loc) · 42.5 KB
/
nativePythonFinder.getAllExtraSearchPaths.unit.test.ts
File metadata and controls
867 lines (734 loc) · 42.5 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
import assert from 'node:assert';
import * as path from 'node:path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import * as logging from '../../../common/logging';
import * as pathUtils from '../../../common/utils/pathUtils';
import * as workspaceApis from '../../../common/workspace.apis';
// Import the function under test
import { getAllExtraSearchPaths, resetWorkspaceSearchPathsGlobalWarningFlag } from '../../../managers/common/nativePythonFinder';
interface MockWorkspaceConfig {
get: sinon.SinonStub;
inspect: sinon.SinonStub;
update: sinon.SinonStub;
}
suite('getAllExtraSearchPaths Integration Tests', () => {
let mockGetConfiguration: sinon.SinonStub;
let mockUntildify: sinon.SinonStub;
let mockTraceError: sinon.SinonStub;
let mockTraceWarn: sinon.SinonStub;
let mockGetWorkspaceFolders: sinon.SinonStub;
// Mock configuration objects
let pythonConfig: MockWorkspaceConfig;
let envConfig: MockWorkspaceConfig;
setup(() => {
resetWorkspaceSearchPathsGlobalWarningFlag();
// Mock VS Code workspace APIs
mockGetConfiguration = sinon.stub(workspaceApis, 'getConfiguration');
mockGetWorkspaceFolders = sinon.stub(workspaceApis, 'getWorkspaceFolders');
mockUntildify = sinon.stub(pathUtils, 'untildify');
// Also stub the namespace import version that might be used by untildifyArray
// Handle both Unix (~/) and Windows-style paths
sinon
.stub(pathUtils, 'untildifyArray')
.callsFake((paths: string[]) =>
paths.map((p) => (p.startsWith('~/') ? p.replace('~/', '/home/user/') : p)),
);
mockTraceError = sinon.stub(logging, 'traceError');
mockTraceWarn = sinon.stub(logging, 'traceWarn');
// Default workspace behavior - no folders
mockGetWorkspaceFolders.returns(undefined);
// Create mock configuration objects
pythonConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
envConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
// Default untildify behavior - expand tildes to test paths
mockUntildify.callsFake((path: string) => {
if (path.startsWith('~/')) {
return path.replace('~/', '/home/user/');
}
return path;
});
// Set up default returns for legacy settings (return undefined by default)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
// Set up default returns for new settings
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Default configuration behavior
mockGetConfiguration.callsFake((section: string, _scope?: unknown) => {
if (section === 'python') {
return pythonConfig;
}
if (section === 'python-envs') {
return envConfig;
}
throw new Error(`Unexpected configuration section: ${section}`);
});
});
teardown(() => {
sinon.restore();
resetWorkspaceSearchPathsGlobalWarningFlag();
});
suite('Legacy Path Consolidation Tests', () => {
test('No legacy settings exist - returns empty paths', async () => {
// Mock → No legacy settings, no new settings
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert
assert.deepStrictEqual(result, []);
});
test('Legacy and global paths are consolidated (Unix)', async () => {
// Mock → Legacy paths and globalSearchPaths both exist (Unix-style)
pythonConfig.get.withArgs('venvPath').returns('/home/user/.virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['/home/user/venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/home/user/.virtualenvs', '/home/user/venvs', '/additional/path'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should consolidate all paths (duplicates removed)
const expected = new Set(['/home/user/.virtualenvs', '/home/user/venvs', '/additional/path']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Legacy and global paths are consolidated (Windows)', async () => {
// Mock → Legacy paths and globalSearchPaths both exist (Windows-style)
pythonConfig.get.withArgs('venvPath').returns('C:\\Users\\dev\\.virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['D:\\shared\\venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\Users\\dev\\.virtualenvs', 'D:\\shared\\venvs', 'E:\\additional\\path'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should consolidate all paths (duplicates removed), normalized to forward slashes
const expected = new Set(['C:/Users/dev/.virtualenvs', 'D:/shared/venvs', 'E:/additional/path']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Legacy paths included alongside new settings (Unix)', async () => {
// Mock → Legacy paths exist, no globalSearchPaths (Unix-style)
pythonConfig.get.withArgs('venvPath').returns('/home/user/.virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['/home/user/venvs', '/home/user/conda']);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should include all legacy paths
const expected = new Set(['/home/user/.virtualenvs', '/home/user/venvs', '/home/user/conda']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Legacy paths included alongside new settings (Windows)', async () => {
// Mock → Legacy paths exist, no globalSearchPaths (Windows-style)
pythonConfig.get.withArgs('venvPath').returns('C:\\Users\\dev\\.virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['C:\\Users\\dev\\venvs', 'D:\\conda\\envs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should include all legacy paths, normalized to forward slashes
const expected = new Set(['C:/Users/dev/.virtualenvs', 'C:/Users/dev/venvs', 'D:/conda/envs']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Legacy and global paths combined with deduplication', async () => {
// Mock → Some overlap between legacy and global paths
pythonConfig.get.withArgs('venvPath').returns('/home/user/.virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['/home/user/venvs', '/home/user/conda']);
envConfig.inspect
.withArgs('globalSearchPaths')
.returns({ globalValue: ['/home/user/.virtualenvs', '/additional/path'] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should include all paths with duplicates removed
const expected = new Set([
'/home/user/.virtualenvs',
'/home/user/venvs',
'/home/user/conda',
'/additional/path',
]);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Legacy paths with untildify support', async () => {
// Mock → Legacy paths with tilde expansion
// Note: getPythonSettingAndUntildify only untildifies strings, not array items
// So we return the venvPath with tilde (will be untildified) and venvFolders pre-expanded
pythonConfig.get.withArgs('venvPath').returns('~/virtualenvs');
pythonConfig.get.withArgs('venvFolders').returns(['/home/user/conda/envs']); // Pre-expanded
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert
const expected = new Set(['/home/user/virtualenvs', '/home/user/conda/envs']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
});
suite('Configuration Source Tests', () => {
test('Global search paths with tilde expansion (Unix)', async () => {
// Mock → No legacy, global paths with tildes (Unix ~ expansion)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['~/virtualenvs', '~/conda/envs'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
mockUntildify.withArgs('~/virtualenvs').returns('/home/user/virtualenvs');
mockUntildify.withArgs('~/conda/envs').returns('/home/user/conda/envs');
// Run
const result = await getAllExtraSearchPaths();
// Assert
const expected = new Set(['/home/user/virtualenvs', '/home/user/conda/envs']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Global search paths with absolute paths (Windows)', async () => {
// Mock → No legacy, global paths with Windows absolute paths
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\Users\\dev\\virtualenvs', 'D:\\conda\\envs'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Paths normalized to forward slashes
const expected = new Set(['C:/Users/dev/virtualenvs', 'D:/conda/envs']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Workspace folder setting preferred over workspace setting (Unix)', async () => {
// Mock → Workspace settings at different levels (Unix-style)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceValue: ['/workspace-level-path'],
workspaceFolderValue: ['/folder-level-path'],
});
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - workspaceFolderValue takes priority, absolute path is kept as-is
const expected = new Set(['/folder-level-path']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Workspace folder setting preferred over workspace setting (Windows)', async () => {
// Mock → Workspace settings at different levels (Windows-style paths in config)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceValue: ['D:\\workspace-level'],
workspaceFolderValue: ['C:\\folder-level\\path'],
});
// Use Unix-style URIs for workspace folders (Uri.file behavior is OS-dependent)
const workspace1 = Uri.file('/projects/project1');
const workspace2 = Uri.file('/projects/project2');
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - workspaceFolderValue takes priority, normalized to forward slashes
const expected = new Set(['C:/folder-level/path']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Global workspace setting logs error and is ignored', async () => {
// Mock → Workspace setting incorrectly set at global level
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
globalValue: ['should-be-ignored'],
});
// Run
const result = await getAllExtraSearchPaths();
// Assert
assert.deepStrictEqual(result, []);
// Check that error was logged with key terms - don't be brittle about exact wording
assert(
mockTraceWarn.calledWith(sinon.match(/workspaceSearchPaths.*global.*level/i)),
'Should log warning about incorrect setting level',
);
});
test('Global workspace setting warning is only logged once across multiple calls', async () => {
// Mock → Workspace setting incorrectly set at global level
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
globalValue: ['should-be-ignored'],
});
// Run multiple times
await getAllExtraSearchPaths();
await getAllExtraSearchPaths();
await getAllExtraSearchPaths();
// Assert - warning should only be logged once, not three times
const matchingCalls = mockTraceWarn
.getCalls()
.filter((call: sinon.SinonSpyCall) =>
/workspaceSearchPaths.*global.*level/i.test(String(call.args[0])),
);
assert.strictEqual(
matchingCalls.length,
1,
`Expected exactly 1 warning about workspaceSearchPaths global level, got ${matchingCalls.length}`,
);
});
test('Configuration read errors return empty arrays', async () => {
// Mock → Configuration throws errors
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').throws(new Error('Config read error'));
envConfig.inspect.withArgs('workspaceSearchPaths').throws(new Error('Config read error'));
// Run
const result = await getAllExtraSearchPaths();
// Assert
assert.deepStrictEqual(result, []);
// Just verify that configuration errors were logged - don't be brittle about exact wording
assert(
mockTraceError.calledWith(sinon.match(/globalSearchPaths/i), sinon.match.instanceOf(Error)),
'Should log globalSearchPaths error',
);
assert(
mockTraceError.calledWith(sinon.match(/workspaceSearchPaths/i), sinon.match.instanceOf(Error)),
'Should log workspaceSearchPaths error',
);
});
});
suite('Path Resolution Tests', () => {
test('Absolute paths used as-is (Unix)', async () => {
// Mock → Mix of absolute paths (Unix-style)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/absolute/path1', '/absolute/path2'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['/absolute/workspace/path'],
});
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - For absolute paths, they should remain unchanged
const expected = new Set(['/absolute/path1', '/absolute/path2', '/absolute/workspace/path']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Absolute paths used as-is (Windows)', async () => {
// Mock → Mix of absolute paths (Windows-style paths in config)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\absolute\\path1', 'D:\\absolute\\path2'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['E:\\workspace\\envs'],
});
// Use Unix-style URIs for workspace folders (Uri.file behavior is OS-dependent)
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Windows paths normalized to forward slashes
const expected = new Set(['C:/absolute/path1', 'D:/absolute/path2', 'E:/workspace/envs']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Relative paths are resolved against workspace folders', async () => {
// Mock → Relative workspace paths with multiple workspace folders
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['venvs', '.venv'],
});
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Relative paths are resolved against each workspace folder
// path.resolve behavior varies by platform, so check the paths contain expected segments
assert.strictEqual(result.length, 4, 'Should have 4 paths (2 relative × 2 workspaces)');
assert.ok(result.some((p) => p.includes('project1') && p.endsWith('venvs')));
assert.ok(result.some((p) => p.includes('project2') && p.endsWith('venvs')));
assert.ok(result.some((p) => p.includes('project1') && p.endsWith('.venv')));
assert.ok(result.some((p) => p.includes('project2') && p.endsWith('.venv')));
});
test('Relative paths without workspace folders logs warning', async () => {
// Mock → Relative paths but no workspace folders
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['relative-path'],
});
mockGetWorkspaceFolders.returns(undefined); // No workspace folders
// Run
const result = await getAllExtraSearchPaths();
// Assert - Path is not added and warning is logged
assert.deepStrictEqual(result, []);
assert.ok(mockTraceWarn.called, 'Should warn about missing workspace folders');
});
test('Multi-root workspace - each folder reads its own workspaceSearchPaths', async () => {
// Mock → Two folders with different folder-level workspaceSearchPaths
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
// Create separate config objects for each folder
const envConfig1: MockWorkspaceConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
const envConfig2: MockWorkspaceConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
envConfig1.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig1.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['/envs/project1'],
});
envConfig2.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig2.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['/envs/project2'],
});
// Return folder-specific configs based on the scope URI passed to getConfiguration
mockGetConfiguration.callsFake((section: string, scope?: unknown) => {
if (section === 'python') {
return pythonConfig;
}
if (section === 'python-envs') {
if (scope && (scope as Uri).fsPath === workspace1.fsPath) {
return envConfig1;
}
if (scope && (scope as Uri).fsPath === workspace2.fsPath) {
return envConfig2;
}
return envConfig; // fallback for unscoped calls
}
throw new Error(`Unexpected configuration section: ${section}`);
});
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - each folder's workspaceSearchPaths is read independently
assert.ok(result.includes('/envs/project1'), 'Should include project1 env path');
assert.ok(result.includes('/envs/project2'), 'Should include project2 env path');
assert.strictEqual(result.length, 2, 'Should have exactly 2 paths (one per folder)');
});
test('Multi-root workspace - relative paths resolved against the correct folder', async () => {
// Mock → Two folders, each with a relative workspaceSearchPaths
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
const envConfig1: MockWorkspaceConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
const envConfig2: MockWorkspaceConfig = {
get: sinon.stub(),
inspect: sinon.stub(),
update: sinon.stub(),
};
envConfig1.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig1.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['envs'],
});
envConfig2.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig2.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['venvs'],
});
mockGetConfiguration.callsFake((section: string, scope?: unknown) => {
if (section === 'python') {
return pythonConfig;
}
if (section === 'python-envs') {
if (scope && (scope as Uri).fsPath === workspace1.fsPath) {
return envConfig1;
}
if (scope && (scope as Uri).fsPath === workspace2.fsPath) {
return envConfig2;
}
return envConfig;
}
throw new Error(`Unexpected configuration section: ${section}`);
});
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - relative paths resolved only against their own folder
// .replace(/\\/g, '/') mirrors the normalization getAllExtraSearchPaths() applies to all
// returned paths, so results always use forward slashes regardless of platform.
const expected1 = path.resolve(workspace1.fsPath, 'envs').replace(/\\/g, '/');
const expected2 = path.resolve(workspace2.fsPath, 'venvs').replace(/\\/g, '/');
const wrong1In2 = path.resolve(workspace2.fsPath, 'envs').replace(/\\/g, '/');
const wrong2In1 = path.resolve(workspace1.fsPath, 'venvs').replace(/\\/g, '/');
assert.strictEqual(result.length, 2, 'Should have exactly 2 paths (one per folder)');
assert.ok(result.includes(expected1), 'project1/envs should come from project1 config');
assert.ok(result.includes(expected2), 'project2/venvs should come from project2 config');
assert.ok(
!result.includes(wrong1In2),
'project1 relative path should not be resolved against project2',
);
assert.ok(
!result.includes(wrong2In1),
'project2 relative path should not be resolved against project1',
);
});
test('Empty and whitespace paths are skipped', async () => {
// Mock → Mix of valid and invalid paths
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/valid/path', '', ' ', '/another/valid/path'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['/workspace/valid', '', ' \t\n ', '/workspace/another'],
});
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Empty strings filtered out, valid paths kept
const expected = new Set(['/valid/path', '/another/valid/path', '/workspace/valid', '/workspace/another']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
});
suite('Integration Scenarios', () => {
test('Fresh install - no settings configured', async () => {
// Mock → Clean slate
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: [] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert
assert.deepStrictEqual(result, []);
});
test('Power user - complex mix of all source types (Unix)', async () => {
// Mock → Complex real-world scenario (Unix-style)
pythonConfig.get.withArgs('venvPath').returns('/legacy/venv/path');
pythonConfig.get.withArgs('venvFolders').returns(['/legacy/venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/legacy/venv/path', '/legacy/venvs', '/global/conda', '~/personal/envs'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['.venv', '/shared/team/envs'],
});
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
mockUntildify.withArgs('~/personal/envs').returns('/home/user/personal/envs');
// Run
const result = await getAllExtraSearchPaths();
// Assert - Relative paths are resolved against workspace folders, absolutes kept as-is
assert.ok(result.includes('/legacy/venv/path'));
assert.ok(result.includes('/legacy/venvs'));
assert.ok(result.includes('/global/conda'));
assert.ok(result.includes('/home/user/personal/envs'));
assert.ok(result.includes('/shared/team/envs'));
// .venv resolved against both workspace folders
assert.ok(result.some((p) => p.includes('project1') && p.endsWith('.venv')));
assert.ok(result.some((p) => p.includes('project2') && p.endsWith('.venv')));
});
test('Power user - complex mix of all source types (Windows)', async () => {
// Mock → Complex real-world scenario (Windows-style paths in config)
pythonConfig.get.withArgs('venvPath').returns('C:\\legacy\\venv\\path');
pythonConfig.get.withArgs('venvFolders').returns(['D:\\legacy\\venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\legacy\\venv\\path', 'D:\\legacy\\venvs', 'E:\\global\\conda'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['.venv', 'F:\\shared\\team\\envs'],
});
// Use Unix-style URIs for workspace folders (Uri.file behavior is OS-dependent)
const workspace1 = Uri.file('/workspace/project1');
const workspace2 = Uri.file('/workspace/project2');
mockGetWorkspaceFolders.returns([{ uri: workspace1 }, { uri: workspace2 }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - All paths normalized to forward slashes
assert.ok(result.includes('C:/legacy/venv/path'));
assert.ok(result.includes('D:/legacy/venvs'));
assert.ok(result.includes('E:/global/conda'));
assert.ok(result.includes('F:/shared/team/envs'));
// .venv resolved against both workspace folders
assert.ok(result.some((p) => p.includes('project1') && p.endsWith('.venv')));
assert.ok(result.some((p) => p.includes('project2') && p.endsWith('.venv')));
// Verify no backslashes remain
for (const p of result) {
assert.ok(!p.includes('\\'), `Path should not contain backslashes: ${p}`);
}
});
test('Overlapping paths are deduplicated (Unix)', async () => {
// Mock → Duplicate paths from different sources (Unix-style)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/shared/path', '/global/unique'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['/shared/path', '/workspace/unique'],
});
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Duplicates should be removed
const expected = new Set(['/shared/path', '/global/unique', '/workspace/unique']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('Overlapping paths are deduplicated (Windows)', async () => {
// Mock → Duplicate paths from different sources (Windows-style paths in config)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\shared\\path', 'D:\\global\\unique'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['C:\\shared\\path', 'E:\\workspace\\unique'],
});
// Use Unix-style URIs for workspace folders (Uri.file behavior is OS-dependent)
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Duplicates should be removed, normalized to forward slashes
const expected = new Set(['C:/shared/path', 'D:/global/unique', 'E:/workspace/unique']);
const actual = new Set(result);
assert.strictEqual(actual.size, expected.size, 'Should have correct number of unique paths');
assert.deepStrictEqual(actual, expected, 'Should contain exactly the expected paths');
});
test('All path types consolidated together', async () => {
// Mock → Multiple path types from different sources
pythonConfig.get.withArgs('venvPath').returns('/legacy/path');
pythonConfig.get.withArgs('venvFolders').returns(['/legacy/folder']);
envConfig.inspect.withArgs('globalSearchPaths').returns({ globalValue: ['/global/path'] });
envConfig.inspect.withArgs('workspaceSearchPaths').returns({
workspaceFolderValue: ['.venv'],
});
const workspace = Uri.file('/workspace');
mockGetWorkspaceFolders.returns([{ uri: workspace }]);
// Run
const result = await getAllExtraSearchPaths();
// Assert - Should consolidate all path types, relative resolved against workspace
assert.ok(result.includes('/legacy/path'));
assert.ok(result.includes('/legacy/folder'));
assert.ok(result.includes('/global/path'));
assert.ok(result.some((p) => p.includes('workspace') && p.endsWith('.venv')));
});
});
suite('Cross-Platform Path Normalization', () => {
test('Backslashes are converted to forward slashes for glob compatibility', async () => {
// Mock → Windows-style paths with backslashes
pythonConfig.get.withArgs('venvPath').returns('C:\\Users\\test\\envs');
pythonConfig.get.withArgs('venvFolders').returns(['D:\\shared\\venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\Python\\environments', 'E:\\projects\\**\\.venv'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - All backslashes should be converted to forward slashes
for (const p of result) {
assert.ok(!p.includes('\\'), `Path should not contain backslashes: ${p}`);
}
assert.ok(result.includes('C:/Users/test/envs'));
assert.ok(result.includes('D:/shared/venvs'));
assert.ok(result.includes('C:/Python/environments'));
assert.ok(result.includes('E:/projects/**/.venv'));
});
test('Glob patterns with backslashes are normalized', async () => {
// Mock → Glob pattern with Windows backslashes
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:\\workspace\\**\\venv', 'D:\\projects\\*\\.venv'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Glob patterns should use forward slashes
assert.ok(result.includes('C:/workspace/**/venv'));
assert.ok(result.includes('D:/projects/*/.venv'));
});
test('Linux/macOS paths with forward slashes are preserved', async () => {
// Mock → Unix-style paths (already using forward slashes)
pythonConfig.get.withArgs('venvPath').returns('/home/user/envs');
pythonConfig.get.withArgs('venvFolders').returns(['/opt/shared/venvs']);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['/usr/local/python/environments', '/home/user/projects/**/.venv'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - Forward slashes should be preserved as-is
assert.ok(result.includes('/home/user/envs'));
assert.ok(result.includes('/opt/shared/venvs'));
assert.ok(result.includes('/usr/local/python/environments'));
assert.ok(result.includes('/home/user/projects/**/.venv'));
// Verify no backslashes were introduced
for (const p of result) {
assert.ok(!p.includes('\\'), `Path should not contain backslashes: ${p}`);
}
});
test('Mixed path separators are normalized to forward slashes', async () => {
// Mock → Paths with mixed separators (edge case)
pythonConfig.get.withArgs('venvPath').returns(undefined);
pythonConfig.get.withArgs('venvFolders').returns(undefined);
envConfig.inspect.withArgs('globalSearchPaths').returns({
globalValue: ['C:/Users\\test/projects\\.venv', '/home/user\\mixed/path'],
});
envConfig.inspect.withArgs('workspaceSearchPaths').returns({});
// Run
const result = await getAllExtraSearchPaths();
// Assert - All backslashes normalized to forward slashes
assert.ok(result.includes('C:/Users/test/projects/.venv'));
assert.ok(result.includes('/home/user/mixed/path'));
});
});
});