forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigSettings.pythonPath.unit.test.ts
More file actions
226 lines (208 loc) · 10.8 KB
/
configSettings.pythonPath.unit.test.ts
File metadata and controls
226 lines (208 loc) · 10.8 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as path from 'path';
import * as sinon from 'sinon';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import * as typemoq from 'typemoq';
import { Uri, WorkspaceConfiguration } from 'vscode';
import { IWorkspaceService } from '../../../client/common/application/types';
import { PythonSettings } from '../../../client/common/configSettings';
import { IExperimentService, IInterpreterPathService } from '../../../client/common/types';
import { noop } from '../../../client/common/utils/misc';
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';
import * as EnvFileTelemetry from '../../../client/telemetry/envFileTelemetry';
import { MockAutoSelectionService } from '../../mocks/autoSelector';
import { untildify } from '../../../client/common/helpers';
import { MockExtensions } from '../../mocks/extensions';
suite('Python Settings - pythonPath', () => {
class CustomPythonSettings extends PythonSettings {
public update(settings: WorkspaceConfiguration) {
return super.update(settings);
}
// eslint-disable-next-line class-methods-use-this
protected getPythonExecutable(pythonPath: string) {
return pythonPath;
}
// eslint-disable-next-line class-methods-use-this
public initialize() {
noop();
}
}
let configSettings: CustomPythonSettings;
let workspaceService: typemoq.IMock<IWorkspaceService>;
let experimentsManager: typemoq.IMock<IExperimentService>;
let interpreterPathService: typemoq.IMock<IInterpreterPathService>;
let pythonSettings: typemoq.IMock<WorkspaceConfiguration>;
setup(() => {
pythonSettings = typemoq.Mock.ofType<WorkspaceConfiguration>();
sinon.stub(EnvFileTelemetry, 'sendSettingTelemetry').returns();
interpreterPathService = typemoq.Mock.ofType<IInterpreterPathService>();
experimentsManager = typemoq.Mock.ofType<IExperimentService>();
workspaceService = typemoq.Mock.ofType<IWorkspaceService>();
pythonSettings.setup((p) => p.get(typemoq.It.isValue('defaultInterpreterPath'))).returns(() => 'python');
pythonSettings.setup((p) => p.get('logging')).returns(() => ({ level: 'error' }));
});
teardown(() => {
if (configSettings) {
configSettings.dispose();
}
sinon.restore();
});
test('Python Path from settings is used', () => {
const pythonPath = 'This is the python Path';
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => pythonPath);
configSettings = new CustomPythonSettings(
undefined,
new MockAutoSelectionService(),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(pythonPath);
});
test("Python Path from settings is used and relative path starting with '~' will be resolved from home directory", () => {
const pythonPath = `~${path.sep}This is the python Path`;
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => pythonPath);
configSettings = new CustomPythonSettings(
undefined,
new MockAutoSelectionService(),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(untildify(pythonPath));
});
test("Python Path from settings is used and relative path starting with '.' will be resolved from workspace folder", () => {
const pythonPath = `.${path.sep}This is the python Path`;
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => pythonPath);
const workspaceFolderUri = Uri.file(__dirname);
configSettings = new CustomPythonSettings(
workspaceFolderUri,
new MockAutoSelectionService(),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(path.resolve(workspaceFolderUri.fsPath, pythonPath));
});
test('Python Path from settings is used and ${workspacecFolder} value will be resolved from workspace folder', () => {
const workspaceFolderToken = '${workspaceFolder}';
const pythonPath = `${workspaceFolderToken}${path.sep}This is the python Path`;
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => pythonPath);
const workspaceFolderUri = Uri.file(__dirname);
configSettings = new CustomPythonSettings(
workspaceFolderUri,
new MockAutoSelectionService(),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(path.join(workspaceFolderUri.fsPath, 'This is the python Path'));
});
test("If we don't have a custom python path and no auto selected interpreters, then use default", () => {
const workspaceFolderUri = Uri.file(__dirname);
const selectionService = mock(MockAutoSelectionService);
const pythonPath = 'python';
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => pythonPath);
configSettings = new CustomPythonSettings(
workspaceFolderUri,
instance(selectionService),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal('python');
});
test("If a workspace is opened and if we don't have a custom python path but we do have an auto selected interpreter, then use it", () => {
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
const interpreter = { path: pythonPath } as PythonEnvironment;
const workspaceFolderUri = Uri.file(__dirname);
const selectionService = mock(MockAutoSelectionService);
when(selectionService.getAutoSelectedInterpreter(workspaceFolderUri)).thenReturn(interpreter);
when(selectionService.setWorkspaceInterpreter(workspaceFolderUri, anything())).thenResolve();
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => 'python');
configSettings = new CustomPythonSettings(
workspaceFolderUri,
instance(selectionService),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(pythonPath);
verify(selectionService.setWorkspaceInterpreter(workspaceFolderUri, interpreter)).once(); // Verify we set the autoselected interpreter
});
test("If no workspace is opened and we don't have a custom python path but we do have an auto selected interpreter, then use it", () => {
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
const interpreter = { path: pythonPath } as PythonEnvironment;
const workspaceFolderUri = Uri.file(__dirname);
const selectionService = mock(MockAutoSelectionService);
when(selectionService.getAutoSelectedInterpreter(workspaceFolderUri)).thenReturn(interpreter);
when(selectionService.setWorkspaceInterpreter(workspaceFolderUri, anything())).thenResolve();
interpreterPathService.setup((p) => p.get(typemoq.It.isAny())).returns(() => 'python');
configSettings = new CustomPythonSettings(
workspaceFolderUri,
instance(selectionService),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(pythonPath);
});
test("If we don't have a custom default python path and we do have an auto selected interpreter, then use it", () => {
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
const interpreter = { path: pythonPath } as PythonEnvironment;
const workspaceFolderUri = Uri.file(__dirname);
const selectionService = mock(MockAutoSelectionService);
when(selectionService.getAutoSelectedInterpreter(workspaceFolderUri)).thenReturn(interpreter);
configSettings = new CustomPythonSettings(
workspaceFolderUri,
instance(selectionService),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
interpreterPathService.setup((i) => i.get(typemoq.It.isAny())).returns(() => 'custom');
pythonSettings.setup((p) => p.get(typemoq.It.isValue('defaultInterpreterPath'))).returns(() => 'python');
configSettings.update(pythonSettings.object);
expect(configSettings.defaultInterpreterPath).to.be.equal(pythonPath);
});
test("If we don't have a custom python path, get the autoselected interpreter and use it if it's safe", () => {
const resource = Uri.parse('a');
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
const interpreter = { path: pythonPath } as PythonEnvironment;
const selectionService = mock(MockAutoSelectionService);
when(selectionService.getAutoSelectedInterpreter(resource)).thenReturn(interpreter);
when(selectionService.setWorkspaceInterpreter(resource, anything())).thenResolve();
configSettings = new CustomPythonSettings(
resource,
instance(selectionService),
workspaceService.object,
interpreterPathService.object,
undefined,
new MockExtensions(),
);
interpreterPathService.setup((i) => i.get(resource)).returns(() => 'python');
configSettings.update(pythonSettings.object);
expect(configSettings.pythonPath).to.be.equal(pythonPath);
experimentsManager.verifyAll();
interpreterPathService.verifyAll();
pythonSettings.verifyAll();
});
});