forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnativePythonFinder.unit.test.ts
More file actions
99 lines (83 loc) · 3.64 KB
/
nativePythonFinder.unit.test.ts
File metadata and controls
99 lines (83 loc) · 3.64 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { assert } from 'chai';
import * as sinon from 'sinon';
import * as typemoq from 'typemoq';
import { WorkspaceConfiguration } from 'vscode';
import {
getNativePythonFinder,
isNativeEnvInfo,
NativeEnvInfo,
NativePythonFinder,
} from '../../client/pythonEnvironments/base/locators/common/nativePythonFinder';
import * as windowsApis from '../../client/common/vscodeApis/windowApis';
import { MockOutputChannel } from '../mockClasses';
import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis';
suite('Native Python Finder', () => {
let finder: NativePythonFinder;
let createLogOutputChannelStub: sinon.SinonStub;
let getConfigurationStub: sinon.SinonStub;
let configMock: typemoq.IMock<WorkspaceConfiguration>;
let getWorkspaceFolderPathsStub: sinon.SinonStub;
setup(() => {
createLogOutputChannelStub = sinon.stub(windowsApis, 'createLogOutputChannel');
createLogOutputChannelStub.returns(new MockOutputChannel('locator'));
getWorkspaceFolderPathsStub = sinon.stub(workspaceApis, 'getWorkspaceFolderPaths');
getWorkspaceFolderPathsStub.returns([]);
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
configMock = typemoq.Mock.ofType<WorkspaceConfiguration>();
configMock.setup((c) => c.get<string>('venvPath')).returns(() => undefined);
configMock.setup((c) => c.get<string[]>('venvFolders')).returns(() => []);
configMock.setup((c) => c.get<string>('condaPath')).returns(() => '');
configMock.setup((c) => c.get<string>('poetryPath')).returns(() => '');
getConfigurationStub.returns(configMock.object);
finder = getNativePythonFinder();
});
teardown(() => {
sinon.restore();
});
suiteTeardown(() => {
finder.dispose();
});
test('Refresh should return python environments', async () => {
const envs = [];
for await (const env of finder.refresh()) {
envs.push(env);
}
// typically all test envs should have at least one environment
assert.isNotEmpty(envs);
});
test('Resolve should return python environments with version', async () => {
// Check if finder connection is still open before starting
const finderImpl = finder as { isConnectionClosed?: boolean };
if (finderImpl.isConnectionClosed) {
// Skip if the subprocess connection has closed
return;
}
const envs = [];
for await (const env of finder.refresh()) {
envs.push(env);
}
// If connection closed during refresh, envs may be empty - skip gracefully
if (finderImpl.isConnectionClosed || envs.length === 0) {
return;
}
// pick an env with version
const env: NativeEnvInfo | undefined = envs
.filter((e) => isNativeEnvInfo(e))
.find((e) => e.version && e.version.length > 0 && (e.executable || (e as NativeEnvInfo).prefix));
if (env) {
env.version = undefined;
} else {
assert.fail('Expected at least one env with valid version');
}
const envPath = env.executable ?? env.prefix;
if (envPath) {
const resolved = await finder.resolve(envPath);
assert.isString(resolved.version, 'Version must be a string');
assert.isTrue((resolved?.version?.length ?? 0) > 0, 'Version must not be empty');
} else {
assert.fail('Expected either executable or prefix to be defined');
}
});
});