forked from microsoft/vscode-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonTrue.unit.test.ts
More file actions
592 lines (470 loc) · 27.5 KB
/
pythonTrue.unit.test.ts
File metadata and controls
592 lines (470 loc) · 27.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Uri, Disposable, Extension, extensions } from 'vscode';
import * as path from 'path';
import * as pythonApi from '../../../extension/common/python';
import * as utilities from '../../../extension/common/utilities';
import { buildPythonEnvironment, buildPythonEnvironmentWithActivatedRun } from './helpers';
// Platform-specific path constants using path.join so tests assert using native separators.
// Leading root '/' preserved; on Windows this yields a leading backslash (e.g. '\\usr\\bin').
const PYTHON_PATH = path.join('/', 'usr', 'bin', 'python3');
const PYTHON_PATH_39 = path.join('/', 'usr', 'bin', 'python3.9');
const PYTHON_PATH_WITH_SPACES = path.join('/', 'path with spaces', 'python3');
const QUOTED_PYTHON_PATH = `"${PYTHON_PATH_WITH_SPACES}"`;
const PYTHON_PATH_DIR = path.join('/', 'usr', 'bin');
const PYTHON_LIB_PYTHON3_DIR = path.join('/', 'usr', 'lib', 'python3');
const WORKSPACE_FILE = path.join('/', 'workspace', 'file.py');
const WORKSPACE_PYTHON_DIR = path.join('/', 'workspace', 'python');
const INVALID_PATH = path.join('/', 'invalid', 'path');
const MOCK_PATH = path.join('/', 'mock', 'path');
suite('Python API Tests- useEnvironmentsExtension:true', () => {
let getExtensionStub: sinon.SinonStub;
let mockPythonExtension: Extension<any>;
let mockEnvsExtension: Extension<any>;
let mockPythonEnvApi: any;
setup(() => {
// Stub extensions.getExtension
getExtensionStub = sinon.stub(extensions, 'getExtension');
// Mock useEnvExtension to return true for this test suite
sinon.stub(utilities, 'useEnvExtension').returns(true);
// Create mock Python extension
mockPythonExtension = {
id: 'ms-python.python',
extensionUri: Uri.file(MOCK_PATH),
extensionPath: MOCK_PATH,
isActive: true,
packageJSON: {},
exports: undefined,
activate: sinon.stub().resolves(),
extensionKind: 1,
} as any;
// Create mock Python Envs extension
mockEnvsExtension = {
id: 'ms-python.vscode-python-envs',
extensionUri: Uri.file(MOCK_PATH),
extensionPath: MOCK_PATH,
isActive: true,
packageJSON: {},
exports: undefined,
activate: sinon.stub().resolves(),
extensionKind: 1,
} as any;
// Create mock Python environment API - for new environments extension
mockPythonEnvApi = {
getEnvironment: sinon.stub(),
setEnvironment: sinon.stub(),
resolveEnvironment: sinon.stub(),
getEnvironmentVariables: sinon.stub(),
onDidChangeEnvironment: sinon.stub().returns({ dispose: sinon.stub() }),
onDidChangeEnvironments: sinon.stub().returns({ dispose: sinon.stub() }),
refreshEnvironments: sinon.stub().resolves(),
getEnvironments: sinon.stub(),
};
// Setup default behavior
getExtensionStub.withArgs('ms-python.python').returns(mockPythonExtension);
getExtensionStub.withArgs('ms-python.vscode-python-envs').returns(mockEnvsExtension);
});
teardown(() => {
sinon.restore();
});
suite('initializePython', () => {
test('Should initialize python and set up event listeners', async () => {
const disposables: Disposable[] = [];
(mockEnvsExtension as any).exports = mockPythonEnvApi;
const mockPythonEnv = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
mockPythonEnvApi.getEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.resolveEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.onDidChangeEnvironments.returns({
dispose: sinon.stub(),
});
await pythonApi.initializePython(disposables);
expect(disposables.length).to.be.greaterThan(0);
expect(mockPythonEnvApi.onDidChangeEnvironments.called).to.be.true;
});
test('Should handle errors gracefully when python extension is not available', async () => {
const disposables: Disposable[] = [];
// Return undefined extension to simulate extension not found
getExtensionStub.withArgs('ms-python.vscode-python-envs').returns(undefined);
await pythonApi.initializePython(disposables);
// Should not throw, just handle error internally
expect(disposables.length).to.equal(0);
});
test('Should fire onDidChangePythonInterpreter event after initialization', async () => {
const disposables: Disposable[] = [];
const mockEventHandler = sinon.stub();
(mockEnvsExtension as any).exports = mockPythonEnvApi;
const mockPythonEnv = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
mockPythonEnvApi.getEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.resolveEnvironment.resolves(mockPythonEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Event should be fired during initialization
sinon.assert.called(mockEventHandler);
});
});
suite('getSettingsPythonPath', () => {
test('Should return execution details from Python extension API', async () => {
const expectedPath = [PYTHON_PATH];
// OLD API: Using getEnvironment() + resolveEnvironment() instead of settings.getExecutionDetails
const mockPythonEnv = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.resolveEnvironment.resolves(mockPythonEnv);
const result = await pythonApi.getSettingsPythonPath();
expect(result).to.deep.equal(expectedPath);
});
test('Should return execution details for specific resource', async () => {
const resource = Uri.file(WORKSPACE_FILE);
const expectedPath = [PYTHON_PATH];
// OLD API: Using getEnvironment() + resolveEnvironment() instead of settings.getExecutionDetails
const mockPythonEnv = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.resolveEnvironment.resolves(mockPythonEnv);
const result = await pythonApi.getSettingsPythonPath(resource);
expect(result).to.deep.equal(expectedPath);
// OLD API: Using getEnvironment() instead of settings.getExecutionDetails
sinon.assert.calledWith(mockPythonEnvApi.getEnvironment, resource);
});
test('Should return undefined when execCommand is not available', async () => {
// OLD API: Using getEnvironment() instead of settings.getExecutionDetails
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.resolves(undefined);
const result = await pythonApi.getSettingsPythonPath();
expect(result).to.be.undefined;
});
test('Should use run.executable instead of activatedRun.executable when they differ', async () => {
// Simulates environment managers like pixi/conda that set activatedRun to a wrapper command
const actualPythonPath = PYTHON_PATH;
const wrapperCommand = path.join('/', 'usr', 'local', 'bin', 'pixi');
const mockPythonEnv = buildPythonEnvironmentWithActivatedRun(actualPythonPath, wrapperCommand, '3.9.0', [
'run',
'python',
]);
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.resolves(mockPythonEnv);
mockPythonEnvApi.resolveEnvironment.resolves(mockPythonEnv);
const result = await pythonApi.getSettingsPythonPath();
// Should return the actual Python binary path, not the wrapper command
expect(result).to.deep.equal([actualPythonPath]);
});
});
suite('getEnvironmentVariables', () => {
test('Should return environment variables from Python extension API', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const expectedVars = { PATH: PYTHON_PATH_DIR, PYTHONPATH: PYTHON_LIB_PYTHON3_DIR };
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironmentVariables.resolves(expectedVars);
const result = await pythonApi.getEnvironmentVariables();
expect(result).to.deep.equal(expectedVars);
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
sinon.assert.calledWith(mockPythonEnvApi.getEnvironmentVariables, sinon.match.any);
});
test('Should get environment variables for specific resource', async () => {
const resource = Uri.file(WORKSPACE_FILE);
// eslint-disable-next-line @typescript-eslint/naming-convention
const expectedVars = { PATH: PYTHON_PATH_DIR };
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironmentVariables.resolves(expectedVars);
const result = await pythonApi.getEnvironmentVariables(resource);
expect(result).to.deep.equal(expectedVars);
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
sinon.assert.calledWith(mockPythonEnvApi.getEnvironmentVariables, resource);
});
test('Should handle undefined resource and return workspace environment variables', async () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const expectedVars = { PATH: PYTHON_PATH_DIR, PYTHONPATH: WORKSPACE_PYTHON_DIR };
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironmentVariables.resolves(expectedVars);
const result = await pythonApi.getEnvironmentVariables(undefined);
expect(result).to.deep.equal(expectedVars);
// OLD API: Using getEnvironmentVariables() instead of environments.getEnvironmentVariables
sinon.assert.calledWith(mockPythonEnvApi.getEnvironmentVariables, undefined);
});
});
suite('resolveEnvironment', () => {
test('Should resolve environment from path string', async () => {
const envPath = PYTHON_PATH;
// Use buildPythonEnvironment for realistic mock
const expectedEnv = buildPythonEnvironment(envPath, '3.9.0');
// OLD API: Using resolveEnvironment() instead of environments.resolveEnvironment
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.resolveEnvironment.resolves(expectedEnv);
const result = await pythonApi.resolveEnvironment(envPath);
expect(result).to.deep.equal(expectedEnv);
// OLD API: Using resolveEnvironment() instead of environments.resolveEnvironment
// sinon.assert.calledWith(mockPythonEnvApi.resolveEnvironment, envPath);
});
test('Should resolve environment from Environment object', async () => {
// Use buildPythonEnvironment for realistic mock
const expectedEnv = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
// OLD API: Using resolveEnvironment() instead of environments.resolveEnvironment
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.resolveEnvironment.resolves(expectedEnv);
const result = await pythonApi.resolveEnvironment(expectedEnv.environmentPath.fsPath);
expect(result).to.deep.equal(expectedEnv);
});
test('Should return undefined for invalid environment', async () => {
const envPath = INVALID_PATH;
// OLD API: Using resolveEnvironment() instead of environments.resolveEnvironment
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.resolveEnvironment.resolves(undefined);
const result = await pythonApi.resolveEnvironment(envPath);
expect(result).to.be.undefined;
});
});
suite('getActiveEnvironmentPath', () => {
test('Should return active environment path', async () => {
// Match production shape: getEnvironment() returns a PythonEnvironment-like object
const envObj = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns(envObj);
const result = await pythonApi.getActiveEnvironmentPath();
expect((result as any).environmentPath.fsPath).to.equal(PYTHON_PATH);
expect((result as any).execInfo.run.executable).to.equal(PYTHON_PATH);
});
test('Should return active environment path for specific resource', async () => {
const resource = Uri.file(WORKSPACE_FILE);
const envObj = buildPythonEnvironment(PYTHON_PATH, '3.9.0');
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns(envObj);
const result = await pythonApi.getActiveEnvironmentPath(resource);
expect((result as any).environmentPath.fsPath).to.equal(PYTHON_PATH);
sinon.assert.calledWith(mockPythonEnvApi.getEnvironment, resource);
});
});
suite('getInterpreterDetails', () => {
test('Should return interpreter details without resource', async () => {
const pythonPath = PYTHON_PATH;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails();
// Use Uri.file().fsPath to get platform-normalized path for comparison
expect(result.path).to.deep.equal([Uri.file(pythonPath).fsPath]);
expect(result.resource).to.be.undefined;
});
test('Should return interpreter details with resource', async () => {
const resource = Uri.file(WORKSPACE_FILE);
const pythonPath = PYTHON_PATH;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails(resource);
// Use Uri.file().fsPath to get platform-normalized path for comparison
expect(result.path).to.deep.equal([Uri.file(pythonPath).fsPath]);
expect(result.resource).to.deep.equal(resource);
});
test('Should not quote path with spaces', async () => {
// this should be updated when we fix the quoting logic in getInterpreterDetails
const pythonPath = PYTHON_PATH_WITH_SPACES;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails();
expect(result.path).to.deep.equal([`${pythonPath}`]);
});
test('Should not double-quote already quoted path', async () => {
const quotedPython = Uri.file(QUOTED_PYTHON_PATH);
const quotedPythonPath = quotedPython.fsPath;
const mockEnv = buildPythonEnvironment(quotedPythonPath, '3.9.0');
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: quotedPython });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails();
expect(result.path).to.deep.equal([quotedPythonPath]);
});
test('Should return undefined path when environment is not resolved', async () => {
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(PYTHON_PATH) });
mockPythonEnvApi.resolveEnvironment.resolves(undefined);
const result = await pythonApi.getInterpreterDetails();
expect(result.path).to.be.undefined;
expect(result.resource).to.be.undefined;
});
test('Should return undefined path when executable uri is not available', async () => {
const mockEnv = {
id: 'test-env',
execInfo: {
run: { executable: undefined, args: [] },
},
} as any;
// OLD API: Using getEnvironment() and resolveEnvironment() instead of environments.*
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(PYTHON_PATH) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails();
expect(result.path).to.be.undefined;
});
test('Should use run.executable instead of activatedRun.executable when they differ', async () => {
// Simulates environment managers like pixi/conda that set activatedRun to a wrapper command
const actualPythonPath = PYTHON_PATH;
const wrapperCommand = path.join('/', 'usr', 'local', 'bin', 'pixi');
const mockEnv = buildPythonEnvironmentWithActivatedRun(actualPythonPath, wrapperCommand, '3.9.0', [
'run',
'python',
]);
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(actualPythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
const result = await pythonApi.getInterpreterDetails();
// Should return the actual Python binary path, not the wrapper command
expect(result.path).to.deep.equal([actualPythonPath]);
expect(result.resource).to.be.undefined;
});
});
suite('onDidChangePythonInterpreter event', () => {
test('Should fire event when active environment path changes', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
const pythonPath = PYTHON_PATH_39;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Set up mocks for getInterpreterDetails() call in event handler
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
// Should be called at least twice: once during init, once from the event
expect(mockEventHandler.callCount).to.be.greaterThan(1);
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.deep.equal([pythonPath]);
expect(lastCall.resource).to.be.undefined;
});
test('Should handle WorkspaceFolder resource in event', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
const pythonPath = PYTHON_PATH_39;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Set up mocks for getInterpreterDetails() call in event handler
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.deep.equal([pythonPath]);
expect(lastCall.resource).to.be.undefined;
});
test('Should handle null resource in event', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
const pythonPath = PYTHON_PATH_39;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Set up mocks for getInterpreterDetails() call in event handler
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.deep.equal([pythonPath]);
expect(lastCall.resource).to.be.undefined;
});
test('Should handle undefined resource in event', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
const pythonPath = PYTHON_PATH_39;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Set up mocks for getInterpreterDetails() call in event handler
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.deep.equal([pythonPath]);
expect(lastCall.resource).to.be.undefined;
});
test('Should handle event with missing id', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
const pythonPath = PYTHON_PATH_39;
const mockEnv = buildPythonEnvironment(pythonPath, '3.9.0');
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Set up mocks for getInterpreterDetails() call in event handler
mockPythonEnvApi.getEnvironment.returns({ environmentPath: Uri.file(pythonPath) });
mockPythonEnvApi.resolveEnvironment.resolves(mockEnv);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.deep.equal([pythonPath]);
expect(lastCall.resource).to.be.undefined;
});
test('Should handle event with null path', async () => {
const disposables: Disposable[] = [];
let eventCallback: any;
const mockEventHandler = sinon.stub();
// OLD API: Using onDidChangeEnvironments instead of onDidChangeActiveEnvironmentPath
(mockEnvsExtension as any).exports = mockPythonEnvApi;
mockPythonEnvApi.onDidChangeEnvironments = (callback: any) => {
eventCallback = callback;
return { dispose: sinon.stub() };
};
// Test case where getEnvironment returns no environment (null path case)
mockPythonEnvApi.getEnvironment.returns(undefined);
mockPythonEnvApi.resolveEnvironment.resolves(undefined);
pythonApi.onDidChangePythonInterpreter(mockEventHandler);
await pythonApi.initializePython(disposables);
// Trigger the environment change event
await eventCallback();
const lastCall = mockEventHandler.lastCall.args[0];
expect(lastCall.path).to.be.undefined;
expect(lastCall.resource).to.be.undefined;
});
});
});