Skip to content

Commit cfcd2b9

Browse files
committed
fix conda global
1 parent 1fe3322 commit cfcd2b9

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/managers/conda/condaEnvManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
311311
: undefined;
312312

313313
if (scope === undefined) {
314+
this.globalEnv = checkedEnv;
314315
await setCondaForGlobal(checkedEnv?.environmentPath?.fsPath);
315316
} else if (scope instanceof Uri) {
316317
const folder = this.api.getPythonProject(scope);
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import assert from 'assert';
3+
import * as sinon from 'sinon';
4+
import { Uri } from 'vscode';
5+
import { PythonEnvironment, PythonEnvironmentApi } from '../../../api';
6+
import { PythonEnvironmentImpl } from '../../../internal.api';
7+
import { CondaEnvManager } from '../../../managers/conda/condaEnvManager';
8+
import * as condaUtils from '../../../managers/conda/condaUtils';
9+
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
10+
11+
function makeEnv(name: string, envPath: string, version: string = '3.12.0'): PythonEnvironment {
12+
return new PythonEnvironmentImpl(
13+
{ id: `${name}-test`, managerId: 'ms-python.python:conda' },
14+
{
15+
name,
16+
displayName: `${name} (${version})`,
17+
displayPath: envPath,
18+
version,
19+
environmentPath: Uri.file(envPath),
20+
sysPrefix: envPath,
21+
execInfo: {
22+
run: { executable: 'python' },
23+
},
24+
},
25+
);
26+
}
27+
28+
function createManager(): CondaEnvManager {
29+
const manager = new CondaEnvManager(
30+
{} as NativePythonFinder,
31+
{} as PythonEnvironmentApi,
32+
{ info: sinon.stub(), error: sinon.stub(), warn: sinon.stub() } as any,
33+
);
34+
// Bypass initialization
35+
(manager as any)._initialized = { completed: true, promise: Promise.resolve() };
36+
(manager as any).collection = [];
37+
return manager;
38+
}
39+
40+
suite('CondaEnvManager.set - globalEnv update', () => {
41+
let setCondaForGlobalStub: sinon.SinonStub;
42+
let checkNoPythonStub: sinon.SinonStub;
43+
44+
setup(() => {
45+
setCondaForGlobalStub = sinon.stub(condaUtils, 'setCondaForGlobal').resolves();
46+
checkNoPythonStub = sinon.stub(condaUtils, 'checkForNoPythonCondaEnvironment');
47+
});
48+
49+
teardown(() => {
50+
sinon.restore();
51+
});
52+
53+
test('set(undefined, env) updates globalEnv in memory', async () => {
54+
const manager = createManager();
55+
const oldEnv = makeEnv('base', '/miniconda3', '3.11.0');
56+
const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
57+
(manager as any).globalEnv = oldEnv;
58+
59+
// checkForNoPythonCondaEnvironment returns the env as-is (has Python)
60+
checkNoPythonStub.resolves(newEnv);
61+
62+
await manager.set(undefined, newEnv);
63+
64+
// globalEnv should now be updated in memory
65+
const result = await manager.get(undefined);
66+
assert.strictEqual(result, newEnv, 'get(undefined) should return the newly set environment');
67+
assert.notStrictEqual(result, oldEnv, 'get(undefined) should NOT return the old environment');
68+
});
69+
70+
test('set(undefined, env) persists to disk', async () => {
71+
const manager = createManager();
72+
const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
73+
checkNoPythonStub.resolves(newEnv);
74+
75+
await manager.set(undefined, newEnv);
76+
77+
assert.ok(setCondaForGlobalStub.calledOnce, 'setCondaForGlobal should be called');
78+
assert.strictEqual(
79+
setCondaForGlobalStub.firstCall.args[0],
80+
newEnv.environmentPath.fsPath,
81+
'should persist the correct path',
82+
);
83+
});
84+
85+
test('set(undefined, undefined) clears globalEnv', async () => {
86+
const manager = createManager();
87+
const oldEnv = makeEnv('base', '/miniconda3', '3.11.0');
88+
(manager as any).globalEnv = oldEnv;
89+
90+
await manager.set(undefined, undefined);
91+
92+
const result = await manager.get(undefined);
93+
assert.strictEqual(result, undefined, 'get(undefined) should return undefined after clearing');
94+
});
95+
96+
test('set(undefined, noPythonEnv) where user declines install clears globalEnv', async () => {
97+
const manager = createManager();
98+
const oldEnv = makeEnv('base', '/miniconda3', '3.11.0');
99+
const noPythonEnv = makeEnv('nopy', '/miniconda3/envs/nopy', 'no-python');
100+
(manager as any).globalEnv = oldEnv;
101+
102+
// User declined to install Python
103+
checkNoPythonStub.resolves(undefined);
104+
105+
await manager.set(undefined, noPythonEnv);
106+
107+
const result = await manager.get(undefined);
108+
assert.strictEqual(result, undefined, 'globalEnv should be cleared when checkedEnv is undefined');
109+
});
110+
});

0 commit comments

Comments
 (0)