forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreterPathService.unit.test.ts
More file actions
646 lines (569 loc) · 34.3 KB
/
interpreterPathService.unit.test.ts
File metadata and controls
646 lines (569 loc) · 34.3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { assert, expect } from 'chai';
import * as sinon from 'sinon';
import * as TypeMoq from 'typemoq';
import {
ConfigurationChangeEvent,
ConfigurationTarget,
Event,
EventEmitter,
Uri,
WorkspaceConfiguration
} from 'vscode';
import { IWorkspaceService } from '../../client/common/application/types';
import {
defaultInterpreterPathSetting,
InterpreterPathService,
isGlobalSettingCopiedKey,
workspaceFolderKeysForWhichTheCopyIsDone_Key,
workspaceKeysForWhichTheCopyIsDone_Key
} from '../../client/common/interpreterPathService';
import { FileSystemPaths } from '../../client/common/platform/fs-paths';
import { InterpreterConfigurationScope, IPersistentState, IPersistentStateFactory } from '../../client/common/types';
import { createDeferred, sleep } from '../../client/common/utils/async';
suite('Interpreter Path Service', async () => {
let interpreterPathService: InterpreterPathService;
let persistentStateFactory: TypeMoq.IMock<IPersistentStateFactory>;
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
const resource = Uri.parse('a');
const resourceOutsideOfWorkspace = Uri.parse('b');
const interpreterPath = 'path/to/interpreter';
const fs = FileSystemPaths.withDefaults();
setup(() => {
const event = TypeMoq.Mock.ofType<Event<ConfigurationChangeEvent>>();
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
workspaceService
.setup((w) => w.getWorkspaceFolder(resource))
.returns(() => ({
uri: resource,
name: 'Workspacefolder',
index: 0
}));
workspaceService.setup((w) => w.getWorkspaceFolder(resourceOutsideOfWorkspace)).returns(() => undefined);
persistentStateFactory = TypeMoq.Mock.ofType<IPersistentStateFactory>();
workspaceService.setup((w) => w.onDidChangeConfiguration).returns(() => event.object);
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
});
teardown(() => {
sinon.restore();
});
test('Ensure execution of method copyOldInterpreterStorageValuesToNew() goes as expected', async () => {
const _copyWorkspaceFolderValueToNewStorage = sinon.stub(
InterpreterPathService.prototype,
'_copyWorkspaceFolderValueToNewStorage'
);
const _copyWorkspaceValueToNewStorage = sinon.stub(
InterpreterPathService.prototype,
'_copyWorkspaceValueToNewStorage'
);
const _moveGlobalSettingValueToNewStorage = sinon.stub(
InterpreterPathService.prototype,
'_moveGlobalSettingValueToNewStorage'
);
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup((w) => w.getConfiguration('python', resource)).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('pythonPath'))
.returns(
() =>
({
globalValue: 'globalPythonPath',
workspaceFolderValue: 'workspaceFolderPythonPath',
workspaceValue: 'workspacePythonPath'
// tslint:disable-next-line: no-any
} as any)
);
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService.copyOldInterpreterStorageValuesToNew(resource);
assert(_copyWorkspaceFolderValueToNewStorage.calledWith(resource, 'workspaceFolderPythonPath'));
assert(_copyWorkspaceValueToNewStorage.calledWith(resource, 'workspacePythonPath'));
assert(_moveGlobalSettingValueToNewStorage.calledWith('globalPythonPath'));
});
test('If the one-off transfer to new storage has not happened yet for the workspace folder, do it and record the transfer', async () => {
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceFolderKeysForWhichTheCopyIsDone_Key, []))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => ['...storedWorkspaceFolderKeys']);
persistentState
.setup((p) => p.updateValue([resource.fsPath, '...storedWorkspaceFolderKeys']))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._copyWorkspaceFolderValueToNewStorage(resource, 'workspaceFolderPythonPath');
assert(update.calledWith(resource, ConfigurationTarget.WorkspaceFolder, 'workspaceFolderPythonPath'));
persistentState.verifyAll();
});
test('If the one-off transfer to new storage has already happened for the workspace folder, do not update and simply return', async () => {
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceFolderKeysForWhichTheCopyIsDone_Key, []))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => [resource.fsPath, '...storedWorkspaceKeys']);
persistentState.setup((p) => p.updateValue(TypeMoq.It.isAny())).verifiable(TypeMoq.Times.never());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._copyWorkspaceFolderValueToNewStorage(resource, 'workspaceFolderPythonPath');
assert(update.notCalled);
persistentState.verifyAll();
});
test('If the one-off transfer to new storage has not happened yet for the workspace, do it and record the transfer', async () => {
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
const expectedWorkspaceKey = fs.normCase(workspaceFileUri.fsPath);
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
workspaceService.setup((w) => w.workspaceFile).returns(() => workspaceFileUri);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceKeysForWhichTheCopyIsDone_Key, []))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => ['...storedWorkspaceKeys']);
persistentState
.setup((p) => p.updateValue([expectedWorkspaceKey, '...storedWorkspaceKeys']))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._copyWorkspaceValueToNewStorage(resource, 'workspacePythonPath');
assert(update.calledWith(resource, ConfigurationTarget.Workspace, 'workspacePythonPath'));
persistentState.verifyAll();
});
test('If the one-off transfer to new storage has already happened for the workspace, do not update and simply return', async () => {
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
const expectedWorkspaceKey = fs.normCase(workspaceFileUri.fsPath);
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
workspaceService.setup((w) => w.workspaceFile).returns(() => workspaceFileUri);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceKeysForWhichTheCopyIsDone_Key, []))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => [expectedWorkspaceKey, '...storedWorkspaceKeys']);
persistentState.setup((p) => p.updateValue(TypeMoq.It.isAny())).verifiable(TypeMoq.Times.never());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._copyWorkspaceValueToNewStorage(resource, 'workspacePythonPath');
assert(update.notCalled);
persistentState.verifyAll();
});
test('Do not update workspace settings and if a folder is directly opened', async () => {
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceKeysForWhichTheCopyIsDone_Key, []))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).verifiable(TypeMoq.Times.never());
persistentState.setup((p) => p.updateValue(TypeMoq.It.isAny())).verifiable(TypeMoq.Times.never());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._copyWorkspaceValueToNewStorage(resource, 'workspacePythonPath');
assert(update.notCalled);
persistentState.verifyAll();
});
test('If the one-off transfer to new storage has not happened yet for the user setting, do it, record the transfer and remove the original user setting', async () => {
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<boolean>>();
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<boolean>(isGlobalSettingCopiedKey, false))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => false);
persistentState.setup((p) => p.updateValue(true)).verifiable(TypeMoq.Times.once());
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.update('pythonPath', undefined, ConfigurationTarget.Global))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._moveGlobalSettingValueToNewStorage('globalPythonPath');
assert(update.calledWith(undefined, ConfigurationTarget.Global, 'globalPythonPath'));
persistentState.verifyAll();
workspaceConfig.verifyAll();
});
test('If the one-off transfer to new storage has already happened for the user setting, do not update and simply return', async () => {
const update = sinon.stub(InterpreterPathService.prototype, 'update');
const persistentState = TypeMoq.Mock.ofType<IPersistentState<boolean>>();
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<boolean>(isGlobalSettingCopiedKey, false))
.returns(() => persistentState.object);
persistentState.setup((p) => p.value).returns(() => true);
persistentState.setup((p) => p.updateValue(TypeMoq.It.isAny())).verifiable(TypeMoq.Times.never());
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
await interpreterPathService._moveGlobalSettingValueToNewStorage('globalPythonPath');
assert(update.notCalled);
persistentState.verifyAll();
});
test('Global settings are not updated if stored value is same as new value', async () => {
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('defaultInterpreterPath'))
.returns(
() =>
({
globalValue: interpreterPath
// tslint:disable-next-line: no-any
} as any)
);
workspaceConfig
.setup((w) => w.update('defaultInterpreterPath', interpreterPath, true))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.never());
await interpreterPathService.update(resource, ConfigurationTarget.Global, interpreterPath);
workspaceConfig.verifyAll();
});
test('Global settings are correctly updated otherwise', async () => {
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('defaultInterpreterPath'))
.returns(
() =>
({
globalValue: 'storedValue'
// tslint:disable-next-line: no-any
} as any)
);
workspaceConfig
.setup((w) => w.update('defaultInterpreterPath', interpreterPath, true))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.Global, interpreterPath);
workspaceConfig.verifyAll();
});
test('Workspace settings are not updated if stored value is same as new value', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.once());
persistentState.setup((p) => p.value).returns(() => interpreterPath);
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.never());
await interpreterPathService.update(resource, ConfigurationTarget.Workspace, interpreterPath);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Workspace settings are correctly updated if a folder is directly opened', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.once());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.Workspace, interpreterPath);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Ensure the correct event is fired if Workspace settings are updated', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object);
persistentState.setup((p) => p.updateValue(interpreterPath)).returns(() => Promise.resolve());
const _didChangeInterpreterEmitter = TypeMoq.Mock.ofType<EventEmitter<InterpreterConfigurationScope>>();
interpreterPathService._didChangeInterpreterEmitter = _didChangeInterpreterEmitter.object;
_didChangeInterpreterEmitter
.setup((emitter) => emitter.fire({ uri: resource, configTarget: ConfigurationTarget.Workspace }))
.returns(() => undefined)
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.Workspace, interpreterPath);
_didChangeInterpreterEmitter.verifyAll();
});
test('Workspace settings are correctly updated in case of multiroot folders', async () => {
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
const expectedSettingKey = `WORKSPACE_INTERPRETER_PATH_${fs.normCase(workspaceFileUri.fsPath)}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.workspaceFile).returns(() => workspaceFileUri);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.once());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.Workspace, interpreterPath);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Workspace folder settings are correctly updated in case of multiroot folders', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.once());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.WorkspaceFolder, interpreterPath);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Ensure the correct event is fired if Workspace folder settings are updated', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.once());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());
const _didChangeInterpreterEmitter = TypeMoq.Mock.ofType<EventEmitter<InterpreterConfigurationScope>>();
interpreterPathService._didChangeInterpreterEmitter = _didChangeInterpreterEmitter.object;
_didChangeInterpreterEmitter
.setup((emitter) => emitter.fire({ uri: resource, configTarget: ConfigurationTarget.WorkspaceFolder }))
.returns(() => undefined)
.verifiable(TypeMoq.Times.once());
await interpreterPathService.update(resource, ConfigurationTarget.WorkspaceFolder, interpreterPath);
_didChangeInterpreterEmitter.verifyAll();
});
test('Updating workspace settings throws error if no workspace is opened', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.never());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.never());
const promise = interpreterPathService.update(
resourceOutsideOfWorkspace,
ConfigurationTarget.Workspace,
interpreterPath
);
await expect(promise).to.eventually.be.rejectedWith(Error);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Updating workspace folder settings throws error if no workspace is opened', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.never());
persistentState
.setup((p) => p.updateValue(interpreterPath))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.never());
const promise = interpreterPathService.update(
resourceOutsideOfWorkspace,
ConfigurationTarget.Workspace,
interpreterPath
);
await expect(promise).to.eventually.be.rejectedWith(Error);
persistentState.verifyAll();
persistentStateFactory.verifyAll();
});
test('Inspecting settings returns as expected if no workspace is opened', async () => {
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('defaultInterpreterPath'))
.returns(
() =>
({
globalValue: 'default/path/to/interpreter'
// tslint:disable-next-line: no-any
} as any)
);
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => persistentState.object)
.verifiable(TypeMoq.Times.never());
const settings = interpreterPathService.inspect(resourceOutsideOfWorkspace);
assert.deepEqual(settings, {
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: undefined,
workspaceValue: undefined
});
persistentStateFactory.verifyAll();
});
test('Inspecting settings returns as expected if a folder is directly opened', async () => {
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
// No workspace file is present if a folder is directly opened
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('defaultInterpreterPath'))
.returns(
() =>
({
globalValue: 'default/path/to/interpreter'
// tslint:disable-next-line: no-any
} as any)
);
const workspaceFolderPersistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => workspaceFolderPersistentState.object);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedSettingKey, undefined))
.returns(() => workspaceFolderPersistentState.object);
workspaceFolderPersistentState.setup((p) => p.value).returns(() => 'workspaceFolderValue');
const settings = interpreterPathService.inspect(resource);
assert.deepEqual(settings, {
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: 'workspaceFolderValue',
workspaceValue: 'workspaceFolderValue'
});
});
test('Inspecting settings returns as expected in case of multiroot folders', async () => {
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
const expectedWorkspaceSettingKey = `WORKSPACE_INTERPRETER_PATH_${fs.normCase(workspaceFileUri.fsPath)}`;
const expectedWorkspaceFolderSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
// A workspace file is present in case of multiroot workspace folders
workspaceService.setup((w) => w.workspaceFile).returns(() => workspaceFileUri);
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
workspaceService.setup((w) => w.getConfiguration('python')).returns(() => workspaceConfig.object);
workspaceConfig
.setup((w) => w.inspect<string>('defaultInterpreterPath'))
.returns(
() =>
({
globalValue: 'default/path/to/interpreter'
// tslint:disable-next-line: no-any
} as any)
);
const workspaceFolderPersistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
const workspacePersistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
persistentStateFactory
.setup((p) =>
p.createGlobalPersistentState<string | undefined>(expectedWorkspaceFolderSettingKey, undefined)
)
.returns(() => workspaceFolderPersistentState.object);
persistentStateFactory
.setup((p) => p.createGlobalPersistentState<string | undefined>(expectedWorkspaceSettingKey, undefined))
.returns(() => workspacePersistentState.object);
workspaceFolderPersistentState.setup((p) => p.value).returns(() => 'workspaceFolderValue');
workspacePersistentState.setup((p) => p.value).returns(() => 'workspaceValue');
const settings = interpreterPathService.inspect(resource);
assert.deepEqual(settings, {
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: 'workspaceFolderValue',
workspaceValue: 'workspaceValue'
});
});
test(`Getting setting value returns workspace folder value if it's defined`, async () => {
interpreterPathService.inspect = () => ({
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: 'workspaceFolderValue',
workspaceValue: 'workspaceValue'
});
const settingValue = interpreterPathService.get(resource);
expect(settingValue).to.equal('workspaceFolderValue');
});
test(`Getting setting value returns workspace value if workspace folder value is 'undefined'`, async () => {
interpreterPathService.inspect = () => ({
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: undefined,
workspaceValue: 'workspaceValue'
});
const settingValue = interpreterPathService.get(resource);
expect(settingValue).to.equal('workspaceValue');
});
test(`Getting setting value returns workspace value if workspace folder value is 'undefined'`, async () => {
interpreterPathService.inspect = () => ({
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: undefined,
workspaceValue: 'workspaceValue'
});
const settingValue = interpreterPathService.get(resource);
expect(settingValue).to.equal('workspaceValue');
});
test(`Getting setting value returns global value if workspace folder & workspace value are 'undefined'`, async () => {
interpreterPathService.inspect = () => ({
globalValue: 'default/path/to/interpreter',
workspaceFolderValue: undefined,
workspaceValue: undefined
});
const settingValue = interpreterPathService.get(resource);
expect(settingValue).to.equal('default/path/to/interpreter');
});
test(`Getting setting value returns 'python' if all workspace folder, workspace, and global value are 'undefined'`, async () => {
interpreterPathService.inspect = () => ({
globalValue: undefined,
workspaceFolderValue: undefined,
workspaceValue: undefined
});
const settingValue = interpreterPathService.get(resource);
expect(settingValue).to.equal('python');
});
test('If defaultInterpreterPathSetting is changed, an event is fired', async () => {
const _didChangeInterpreterEmitter = TypeMoq.Mock.ofType<EventEmitter<InterpreterConfigurationScope>>();
const event = TypeMoq.Mock.ofType<ConfigurationChangeEvent>();
event
.setup((e) => e.affectsConfiguration(`python.${defaultInterpreterPathSetting}`))
.returns(() => true)
.verifiable(TypeMoq.Times.once());
interpreterPathService._didChangeInterpreterEmitter = _didChangeInterpreterEmitter.object;
_didChangeInterpreterEmitter
.setup((emitter) => emitter.fire({ uri: undefined, configTarget: ConfigurationTarget.Global }))
.returns(() => undefined)
.verifiable(TypeMoq.Times.once());
await interpreterPathService.onDidChangeConfiguration(event.object);
_didChangeInterpreterEmitter.verifyAll();
event.verifyAll();
});
test('If some other setting changed, no event is fired', async () => {
const _didChangeInterpreterEmitter = TypeMoq.Mock.ofType<EventEmitter<InterpreterConfigurationScope>>();
const event = TypeMoq.Mock.ofType<ConfigurationChangeEvent>();
event
.setup((e) => e.affectsConfiguration(`python.${defaultInterpreterPathSetting}`))
.returns(() => false)
.verifiable(TypeMoq.Times.once());
interpreterPathService._didChangeInterpreterEmitter = _didChangeInterpreterEmitter.object;
_didChangeInterpreterEmitter
.setup((emitter) => emitter.fire(TypeMoq.It.isAny()))
.returns(() => undefined)
.verifiable(TypeMoq.Times.never());
await interpreterPathService.onDidChangeConfiguration(event.object);
_didChangeInterpreterEmitter.verifyAll();
event.verifyAll();
});
test('Ensure on interpreter change captures the fired event with the correct arguments', async () => {
const deferred = createDeferred<true>();
const interpreterConfigurationScope = { uri: undefined, configTarget: ConfigurationTarget.Global };
interpreterPathService.onDidChange((i) => {
expect(i).to.equal(interpreterConfigurationScope);
deferred.resolve(true);
});
interpreterPathService._didChangeInterpreterEmitter.fire(interpreterConfigurationScope);
const eventCaptured = await Promise.race([deferred.promise, sleep(1000).then(() => false)]);
expect(eventCaptured).to.equal(true, 'Event should be captured');
});
});