forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvExtApi.unit.test.ts
More file actions
235 lines (200 loc) · 11.1 KB
/
Copy pathenvExtApi.unit.test.ts
File metadata and controls
235 lines (200 loc) · 11.1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Disposable, EventEmitter, Uri } from 'vscode';
import { FileChangeType } from '../../client/common/platform/fileSystemWatcher';
import * as apiInternal from '../../client/envExt/api.internal';
import { createEnvExtApi } from '../../client/envExt/envExtApi';
import {
DidChangeEnvironmentEventArgs,
DidChangeEnvironmentsEventArgs,
EnvironmentChangeKind,
PythonEnvironment,
PythonEnvironmentApi,
} from '../../client/envExt/types';
import { PythonEnvCollectionChangedEvent } from '../../client/pythonEnvironments/base/watcher';
function buildCondaEnvironment(
name: string,
version: string,
prefix: string,
executable = Uri.joinPath(Uri.file(prefix), 'bin', 'python').fsPath,
): PythonEnvironment {
return {
envId: { id: `${name}-${version}`, managerId: 'ms-python.python:conda' },
name,
displayName: `${name} (${version})`,
displayPath: prefix,
version,
environmentPath: Uri.file(prefix),
execInfo: { run: { executable } },
sysPrefix: prefix,
};
}
suite('Python Environments extension discovery adapter', () => {
let environmentChanges: EventEmitter<DidChangeEnvironmentsEventArgs>;
let activeEnvironmentChanges: EventEmitter<DidChangeEnvironmentEventArgs>;
let disposables: Disposable[];
let envExtApi: PythonEnvironmentApi;
setup(() => {
environmentChanges = new EventEmitter<DidChangeEnvironmentsEventArgs>();
activeEnvironmentChanges = new EventEmitter<DidChangeEnvironmentEventArgs>();
disposables = [];
envExtApi = ({
onDidChangeEnvironments: environmentChanges.event,
onDidChangeEnvironment: activeEnvironmentChanges.event,
refreshEnvironments: sinon.stub().resolves(),
resolveEnvironment: sinon.stub().resolves(undefined),
} as unknown) as PythonEnvironmentApi;
sinon.stub(apiInternal, 'getEnvExtApi').resolves(envExtApi);
});
teardown(() => {
disposables.forEach((disposable) => disposable.dispose());
environmentChanges.dispose();
activeEnvironmentChanges.dispose();
sinon.restore();
});
test('skips a no-Python Conda environment without dropping later valid environments', async () => {
const api = await createEnvExtApi(disposables);
const noPython = buildCondaEnvironment('empty', 'no-python', '/conda/envs/empty', '/conda/bin/conda');
const first = buildCondaEnvironment('first', '3.12.1', '/conda/envs/first');
const second = buildCondaEnvironment('second', '3.11.9', '/conda/envs/second');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
expect(() =>
environmentChanges.fire([
{ kind: EnvironmentChangeKind.add, environment: noPython },
{ kind: EnvironmentChangeKind.add, environment: first },
{ kind: EnvironmentChangeKind.add, environment: second },
]),
).not.to.throw();
expect(api.getEnvs().map((env) => env.executable.filename)).to.deep.equal([
first.execInfo.run.executable,
second.execInfo.run.executable,
]);
expect(events.map((event) => event.type)).to.deep.equal([FileChangeType.Created, FileChangeType.Created]);
});
test('isolates an unexpected invalid version from later environments in the batch', async () => {
const api = await createEnvExtApi(disposables);
const first = buildCondaEnvironment('first', '3.12.1', '/conda/envs/first');
const invalid = buildCondaEnvironment('invalid', 'not-a-version', '/conda/envs/invalid');
const second = buildCondaEnvironment('second', '3.11.9', '/conda/envs/second');
expect(() =>
environmentChanges.fire([
{ kind: EnvironmentChangeKind.add, environment: first },
{ kind: EnvironmentChangeKind.add, environment: invalid },
{ kind: EnvironmentChangeKind.add, environment: second },
]),
).not.to.throw();
expect(api.getEnvs().map((env) => env.executable.filename)).to.deep.equal([
first.execInfo.run.executable,
second.execInfo.run.executable,
]);
});
test('isolates a structurally malformed event from later environments in the batch', async () => {
const api = await createEnvExtApi(disposables);
const valid = buildCondaEnvironment('valid', '3.12.1', '/conda/envs/valid');
expect(() =>
environmentChanges.fire([
({
kind: EnvironmentChangeKind.remove,
environment: undefined,
} as unknown) as DidChangeEnvironmentsEventArgs[number],
{ kind: EnvironmentChangeKind.add, environment: valid },
]),
).not.to.throw();
expect(api.getEnvs().map((env) => env.executable.filename)).to.deep.equal([valid.execInfo.run.executable]);
});
test('does not publish an active-environment event for a no-Python environment', async () => {
const api = await createEnvExtApi(disposables);
const noPython = buildCondaEnvironment('empty', 'no-python', '/conda/envs/empty', '/conda/bin/conda');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
expect(() => activeEnvironmentChanges.fire({ uri: undefined, old: undefined, new: noPython })).not.to.throw();
expect(events).to.be.empty;
expect(api.getEnvs()).to.be.empty;
});
test('does not publish a partial active-environment change when one side is invalid', async () => {
const api = await createEnvExtApi(disposables);
const noPython = buildCondaEnvironment('empty', 'no-python', '/conda/envs/empty', '/conda/bin/conda');
const valid = buildCondaEnvironment('valid', '3.12.1', '/conda/envs/valid');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
activeEnvironmentChanges.fire({ uri: Uri.file('/workspace'), old: noPython, new: valid });
expect(events).to.be.empty;
});
test('preserves valid active-environment changes', async () => {
const api = await createEnvExtApi(disposables);
const oldEnvironment = buildCondaEnvironment('old', '3.11.9', '/conda/envs/old');
const newEnvironment = buildCondaEnvironment('new', '3.12.1', '/conda/envs/new');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
activeEnvironmentChanges.fire({
uri: Uri.file('/workspace'),
old: oldEnvironment,
new: newEnvironment,
});
expect(events).to.have.length(1);
expect(events[0].type).to.equal(FileChangeType.Changed);
expect(events[0].old?.executable.filename).to.equal(oldEnvironment.execInfo.run.executable);
expect(events[0].new?.executable.filename).to.equal(newEnvironment.execInfo.run.executable);
expect(events[0].searchLocation?.fsPath).to.equal(Uri.file('/workspace').fsPath);
});
test('preserves valid active-environment set and clear events', async () => {
const api = await createEnvExtApi(disposables);
const environment = buildCondaEnvironment('valid', '3.12.1', '/conda/envs/valid');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
activeEnvironmentChanges.fire({ uri: Uri.file('/workspace'), old: undefined, new: environment });
activeEnvironmentChanges.fire({ uri: Uri.file('/workspace'), old: environment, new: undefined });
expect(events).to.have.length(2);
expect(events[0].old).to.equal(undefined);
expect(events[0].new?.executable.filename).to.equal(environment.execInfo.run.executable);
expect(events[1].old?.executable.filename).to.equal(environment.execInfo.run.executable);
expect(events[1].new).to.equal(undefined);
});
test('removes Conda environments using their executable identity', async () => {
const api = await createEnvExtApi(disposables);
const environment = buildCondaEnvironment('first', '3.12.1', '/conda/envs/first');
const events: PythonEnvCollectionChangedEvent[] = [];
disposables.push(api.onChanged((event) => events.push(event)));
environmentChanges.fire([{ kind: EnvironmentChangeKind.add, environment }]);
environmentChanges.fire([{ kind: EnvironmentChangeKind.remove, environment }]);
expect(api.getEnvs()).to.be.empty;
expect(events.map((event) => event.type)).to.deep.equal([FileChangeType.Created, FileChangeType.Deleted]);
expect(events[1].old?.executable.filename).to.equal(environment.execInfo.run.executable);
});
test('restores all valid Conda environments after a refresh batch containing a no-Python item', async () => {
const api = await createEnvExtApi(disposables);
const oldFirst = buildCondaEnvironment('first', '3.12.0', '/conda/envs/first');
const oldSecond = buildCondaEnvironment('second', '3.11.8', '/conda/envs/second');
environmentChanges.fire([
{ kind: EnvironmentChangeKind.add, environment: oldFirst },
{ kind: EnvironmentChangeKind.add, environment: oldSecond },
]);
const noPython = buildCondaEnvironment('empty', 'no-python', '/conda/envs/empty', '/conda/bin/conda');
const newFirst = buildCondaEnvironment('first', '3.12.1', '/conda/envs/first');
const newSecond = buildCondaEnvironment('second', '3.11.9', '/conda/envs/second');
environmentChanges.fire([
{ kind: EnvironmentChangeKind.remove, environment: oldFirst },
{ kind: EnvironmentChangeKind.remove, environment: oldSecond },
{ kind: EnvironmentChangeKind.add, environment: noPython },
{ kind: EnvironmentChangeKind.add, environment: newFirst },
{ kind: EnvironmentChangeKind.add, environment: newSecond },
]);
expect(api.getEnvs().map((env) => env.version.sysVersion)).to.deep.equal(['3.12.1', '3.11.9']);
});
test('completes a requested refresh and retains valid environments after a no-Python item', async () => {
const noPython = buildCondaEnvironment('empty', 'no-python', '/conda/envs/empty', '/conda/bin/conda');
const valid = buildCondaEnvironment('valid', '3.12.1', '/conda/envs/valid');
(envExtApi.refreshEnvironments as sinon.SinonStub).callsFake(async () => {
environmentChanges.fire([
{ kind: EnvironmentChangeKind.add, environment: noPython },
{ kind: EnvironmentChangeKind.add, environment: valid },
]);
});
const api = await createEnvExtApi(disposables);
await api.triggerRefresh();
expect(api.getEnvs().map((env) => env.executable.filename)).to.deep.equal([valid.execInfo.run.executable]);
});
});