-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcondaEnvManager.setEvents.unit.test.ts
More file actions
132 lines (106 loc) · 5.49 KB
/
condaEnvManager.setEvents.unit.test.ts
File metadata and controls
132 lines (106 loc) · 5.49 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import assert from 'assert';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import { DidChangeEnvironmentEventArgs, PythonEnvironmentApi, PythonProject } from '../../../api';
import { normalizePath } from '../../../common/utils/pathUtils';
import { CondaEnvManager } from '../../../managers/conda/condaEnvManager';
import * as condaUtils from '../../../managers/conda/condaUtils';
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
import { makeMockCondaEnvironment as makeEnv } from '../../mocks/pythonEnvironment';
function createManager(apiOverrides?: Partial<PythonEnvironmentApi>): CondaEnvManager {
const api = {
getPythonProject: sinon.stub().returns(undefined),
...apiOverrides,
} as any as PythonEnvironmentApi;
const manager = new CondaEnvManager(
{} as NativePythonFinder,
api,
{ info: sinon.stub(), error: sinon.stub(), warn: sinon.stub() } as any,
);
(manager as any)._initialized = { completed: true, promise: Promise.resolve() };
(manager as any).collection = [];
return manager;
}
suite('CondaEnvManager.set - onDidChangeEnvironment event firing', () => {
let checkNoPythonStub: sinon.SinonStub;
setup(() => {
sinon.stub(condaUtils, 'setCondaForGlobal').resolves();
sinon.stub(condaUtils, 'setCondaForWorkspace').resolves();
checkNoPythonStub = sinon.stub(condaUtils, 'checkForNoPythonCondaEnvironment');
});
teardown(() => {
sinon.restore();
});
test('set(undefined, env) fires onDidChangeEnvironment for global scope', async () => {
const manager = createManager();
const oldEnv = makeEnv('base', '/miniconda3', '3.11.0');
const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
(manager as any).globalEnv = oldEnv;
checkNoPythonStub.resolves(newEnv);
const events: DidChangeEnvironmentEventArgs[] = [];
manager.onDidChangeEnvironment((e) => events.push(e));
await manager.set(undefined, newEnv);
assert.strictEqual(events.length, 1, 'should fire exactly one event');
assert.strictEqual(events[0].uri, undefined, 'uri should be undefined for global scope');
assert.strictEqual(events[0].old, oldEnv);
assert.strictEqual(events[0].new, newEnv);
});
test('set(undefined, env) does not fire event when env is unchanged', async () => {
const manager = createManager();
const env = makeEnv('base', '/miniconda3', '3.11.0');
(manager as any).globalEnv = env;
checkNoPythonStub.resolves(env);
const events: DidChangeEnvironmentEventArgs[] = [];
manager.onDidChangeEnvironment((e) => events.push(e));
await manager.set(undefined, env);
assert.strictEqual(events.length, 0, 'should not fire event when env is unchanged');
});
test('set(Uri, env) fires onDidChangeEnvironment for single Uri scope', async () => {
const projectUri = Uri.file('/workspace/project');
const project = { uri: projectUri, name: 'project' } as PythonProject;
const manager = createManager({
getPythonProject: sinon.stub().returns(project) as any,
});
const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
checkNoPythonStub.resolves(newEnv);
const events: DidChangeEnvironmentEventArgs[] = [];
manager.onDidChangeEnvironment((e) => events.push(e));
await manager.set(projectUri, newEnv);
assert.strictEqual(events.length, 1, 'should fire exactly one event');
assert.strictEqual(events[0].uri, projectUri);
assert.strictEqual(events[0].old, undefined);
assert.strictEqual(events[0].new, newEnv);
});
test('set(Uri, env) does not fire event when env is unchanged', async () => {
const projectUri = Uri.file('/workspace/project');
const project = { uri: projectUri, name: 'project' } as PythonProject;
const manager = createManager({
getPythonProject: sinon.stub().returns(project) as any,
});
const env = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
checkNoPythonStub.resolves(env);
// Pre-populate the map with the same env
(manager as any).fsPathToEnv.set(normalizePath(projectUri.fsPath), env);
const events: DidChangeEnvironmentEventArgs[] = [];
manager.onDidChangeEnvironment((e) => events.push(e));
await manager.set(projectUri, env);
assert.strictEqual(events.length, 0, 'should not fire event when env is unchanged');
});
test('set(Uri, undefined) fires event when clearing environment', async () => {
const projectUri = Uri.file('/workspace/project');
const project = { uri: projectUri, name: 'project' } as PythonProject;
const manager = createManager({
getPythonProject: sinon.stub().returns(project) as any,
});
const oldEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0');
// Pre-populate the map
(manager as any).fsPathToEnv.set(normalizePath(projectUri.fsPath), oldEnv);
const events: DidChangeEnvironmentEventArgs[] = [];
manager.onDidChangeEnvironment((e) => events.push(e));
await manager.set(projectUri, undefined);
assert.strictEqual(events.length, 1, 'should fire event when clearing');
assert.strictEqual(events[0].old, oldEnv);
assert.strictEqual(events[0].new, undefined);
});
});