|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import assert from 'assert'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import { PythonEnvironmentApi } from '../../../api'; |
| 5 | +import { CondaEnvManager } from '../../../managers/conda/condaEnvManager'; |
| 6 | +import * as condaUtils from '../../../managers/conda/condaUtils'; |
| 7 | +import { NativePythonFinder } from '../../../managers/common/nativePythonFinder'; |
| 8 | +import { makeMockCondaEnvironment as makeEnv } from '../../mocks/pythonEnvironment'; |
| 9 | + |
| 10 | +function createManager(): CondaEnvManager { |
| 11 | + const manager = new CondaEnvManager( |
| 12 | + {} as NativePythonFinder, |
| 13 | + {} as PythonEnvironmentApi, |
| 14 | + { info: sinon.stub(), error: sinon.stub(), warn: sinon.stub() } as any, |
| 15 | + ); |
| 16 | + // Bypass initialization |
| 17 | + (manager as any)._initialized = { completed: true, promise: Promise.resolve() }; |
| 18 | + (manager as any).collection = []; |
| 19 | + return manager; |
| 20 | +} |
| 21 | + |
| 22 | +suite('CondaEnvManager.set - globalEnv update', () => { |
| 23 | + let setCondaForGlobalStub: sinon.SinonStub; |
| 24 | + let checkNoPythonStub: sinon.SinonStub; |
| 25 | + |
| 26 | + setup(() => { |
| 27 | + setCondaForGlobalStub = sinon.stub(condaUtils, 'setCondaForGlobal').resolves(); |
| 28 | + checkNoPythonStub = sinon.stub(condaUtils, 'checkForNoPythonCondaEnvironment'); |
| 29 | + }); |
| 30 | + |
| 31 | + teardown(() => { |
| 32 | + sinon.restore(); |
| 33 | + }); |
| 34 | + |
| 35 | + test('set(undefined, env) updates globalEnv in memory', async () => { |
| 36 | + const manager = createManager(); |
| 37 | + const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); |
| 38 | + const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0'); |
| 39 | + (manager as any).globalEnv = oldEnv; |
| 40 | + |
| 41 | + // checkForNoPythonCondaEnvironment returns the env as-is (has Python) |
| 42 | + checkNoPythonStub.resolves(newEnv); |
| 43 | + |
| 44 | + await manager.set(undefined, newEnv); |
| 45 | + |
| 46 | + // globalEnv should now be updated in memory |
| 47 | + const result = await manager.get(undefined); |
| 48 | + assert.strictEqual(result, newEnv, 'get(undefined) should return the newly set environment'); |
| 49 | + assert.notStrictEqual(result, oldEnv, 'get(undefined) should NOT return the old environment'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('set(undefined, env) persists to disk', async () => { |
| 53 | + const manager = createManager(); |
| 54 | + const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0'); |
| 55 | + checkNoPythonStub.resolves(newEnv); |
| 56 | + |
| 57 | + await manager.set(undefined, newEnv); |
| 58 | + |
| 59 | + assert.ok(setCondaForGlobalStub.calledOnce, 'setCondaForGlobal should be called'); |
| 60 | + assert.strictEqual( |
| 61 | + setCondaForGlobalStub.firstCall.args[0], |
| 62 | + newEnv.environmentPath.fsPath, |
| 63 | + 'should persist the correct path', |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + test('set(undefined, undefined) clears globalEnv', async () => { |
| 68 | + const manager = createManager(); |
| 69 | + const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); |
| 70 | + (manager as any).globalEnv = oldEnv; |
| 71 | + |
| 72 | + await manager.set(undefined, undefined); |
| 73 | + |
| 74 | + const result = await manager.get(undefined); |
| 75 | + assert.strictEqual(result, undefined, 'get(undefined) should return undefined after clearing'); |
| 76 | + }); |
| 77 | + |
| 78 | + test('set(undefined, noPythonEnv) where user declines install clears globalEnv', async () => { |
| 79 | + const manager = createManager(); |
| 80 | + const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); |
| 81 | + const noPythonEnv = makeEnv('nopy', '/miniconda3/envs/nopy', 'no-python'); |
| 82 | + (manager as any).globalEnv = oldEnv; |
| 83 | + |
| 84 | + // User declined to install Python |
| 85 | + checkNoPythonStub.resolves(undefined); |
| 86 | + |
| 87 | + await manager.set(undefined, noPythonEnv); |
| 88 | + |
| 89 | + const result = await manager.get(undefined); |
| 90 | + assert.strictEqual(result, undefined, 'globalEnv should be cleared when checkedEnv is undefined'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments