forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.unit.test.ts
More file actions
330 lines (272 loc) · 12.7 KB
/
utils.unit.test.ts
File metadata and controls
330 lines (272 loc) · 12.7 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import assert from 'node:assert';
import path from 'path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import * as platformUtils from '../../../common/utils/platformUtils';
import { PythonEnvironment } from '../../../api';
// Import functions under test
import {
isNumber,
shortVersion,
isGreater,
sortEnvironments,
getLatest,
mergePackages,
pathForGitBash,
compareVersions,
getShellActivationCommands,
} from '../../../managers/common/utils';
suite('Utils - Pure Utility Functions', () => {
suite('isNumber', () => {
test('returns true for valid numbers', () => {
assert.strictEqual(isNumber(42), true);
assert.strictEqual(isNumber(0), true);
assert.strictEqual(isNumber(-1), true);
assert.strictEqual(isNumber(3.14), true);
});
test('returns false for NaN', () => {
assert.strictEqual(isNumber(NaN), false);
});
test('returns false for non-number types', () => {
assert.strictEqual(isNumber('42'), false);
assert.strictEqual(isNumber(null), false);
assert.strictEqual(isNumber(undefined), false);
assert.strictEqual(isNumber({}), false);
assert.strictEqual(isNumber([]), false);
});
});
suite('shortVersion', () => {
test('returns shortened version with patch number', () => {
assert.strictEqual(shortVersion('3.11.5'), '3.11.5');
assert.strictEqual(shortVersion('2.7.18'), '2.7.18');
});
test('returns shortened version without patch number', () => {
assert.strictEqual(shortVersion('3.11'), '3.11.x');
assert.strictEqual(shortVersion('2.7'), '2.7.x');
});
test('returns original string when no version pattern matches', () => {
assert.strictEqual(shortVersion('invalid'), 'invalid');
assert.strictEqual(shortVersion(''), '');
});
test('extracts version from strings with prefixes and suffixes', () => {
// The regex matches version patterns even with prefixes/suffixes
assert.strictEqual(shortVersion('v3.11.5-rc1'), '3.11.5');
assert.strictEqual(shortVersion('python3.10'), '3.10.x');
});
});
suite('compareVersions', () => {
test('returns 1 when first version is greater', () => {
assert.strictEqual(compareVersions('3.11.0', '3.10.0'), 1);
assert.strictEqual(compareVersions('3.11.1', '3.11.0'), 1);
assert.strictEqual(compareVersions('3.11.0.1', '3.11.0'), 1);
});
test('returns -1 when first version is lesser', () => {
assert.strictEqual(compareVersions('3.10.0', '3.11.0'), -1);
assert.strictEqual(compareVersions('3.11.0', '3.11.1'), -1);
assert.strictEqual(compareVersions('3.11.0', '3.11.0.1'), -1);
});
test('returns 0 when versions are equal', () => {
assert.strictEqual(compareVersions('3.11.0', '3.11.0'), 0);
assert.strictEqual(compareVersions('2.7.18', '2.7.18'), 0);
});
test('handles different version part counts', () => {
assert.strictEqual(compareVersions('3.11', '3.11.0'), 0);
assert.strictEqual(compareVersions('3.11.0', '3.11'), 0);
});
});
suite('isGreater', () => {
test('returns true when first version is greater', () => {
assert.strictEqual(isGreater('3.11.0', '3.10.0'), true);
assert.strictEqual(isGreater('3.11.1', '3.11.0'), true);
assert.strictEqual(isGreater('3.11.0.1', '3.11.0'), true);
});
test('returns false when first version is lesser or equal', () => {
assert.strictEqual(isGreater('3.10.0', '3.11.0'), false);
assert.strictEqual(isGreater('3.11.0', '3.11.0'), false);
assert.strictEqual(isGreater('3.11.0', '3.11.1'), false);
});
test('handles undefined versions', () => {
assert.strictEqual(isGreater(undefined, undefined), false);
assert.strictEqual(isGreater(undefined, '3.11.0'), false);
assert.strictEqual(isGreater('3.11.0', undefined), true);
});
test('returns false when version parsing fails', () => {
// Invalid version strings that cause parseInt to fail
const result = isGreater('invalid', 'also-invalid');
assert.strictEqual(result, false);
});
});
suite('pathForGitBash', () => {
let isWindowsStub: sinon.SinonStub;
setup(() => {
isWindowsStub = sinon.stub(platformUtils, 'isWindows');
});
teardown(() => {
sinon.restore();
});
test('converts Windows path to Git Bash format on Windows', () => {
isWindowsStub.returns(true);
const windowsPath = 'C:\\Users\\test\\venv\\Scripts\\python.exe';
const result = pathForGitBash(windowsPath);
assert.strictEqual(result, '/C/Users/test/venv/Scripts/python.exe');
});
test('handles drive letter conversion on Windows', () => {
isWindowsStub.returns(true);
assert.strictEqual(pathForGitBash('D:\\path'), '/D/path');
assert.strictEqual(pathForGitBash('E:\\another\\path'), '/E/another/path');
});
test('returns path unchanged on non-Windows', () => {
isWindowsStub.returns(false);
const unixPath = '/home/user/venv/bin/python';
const result = pathForGitBash(unixPath);
assert.strictEqual(result, unixPath);
});
});
});
suite('Utils - Data Manipulation Functions', () => {
suite('mergePackages', () => {
test('merges common packages with installed packages', () => {
const common = [
{ name: 'numpy', displayName: 'NumPy' },
{ name: 'pandas', displayName: 'Pandas' },
];
const installed = ['numpy', 'scipy', 'matplotlib'];
const result = mergePackages(common, installed);
// Verify all packages are present
const names = result.map((p) => p.name);
assert.deepStrictEqual(names.sort(), ['matplotlib', 'numpy', 'pandas', 'scipy']);
});
test('uses display names from common packages and package names for others', () => {
const common = [{ name: 'numpy', displayName: 'NumPy' }];
const installed = ['numpy', 'scipy'];
const result = mergePackages(common, installed);
assert.strictEqual(result.find((p) => p.name === 'numpy')?.displayName, 'NumPy');
assert.strictEqual(result.find((p) => p.name === 'scipy')?.displayName, 'scipy');
});
test('returns sorted list alphabetically by name', () => {
const common = [{ name: 'zebra', displayName: 'Zebra' }];
const installed = ['alpha', 'beta', 'zebra'];
const result = mergePackages(common, installed);
assert.deepStrictEqual(
result.map((p) => p.name),
['alpha', 'beta', 'zebra'],
);
});
test('handles empty common packages', () => {
const result = mergePackages([], ['numpy', 'scipy']);
assert.strictEqual(result.length, 2);
assert.strictEqual(result[0].name, 'numpy');
});
test('handles empty installed packages', () => {
const common = [{ name: 'numpy', displayName: 'NumPy' }];
const result = mergePackages(common, []);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].displayName, 'NumPy');
});
});
suite('sortEnvironments', () => {
function createEnv(version: string, name: string, fsPath: string): PythonEnvironment {
return {
version,
name,
environmentPath: Uri.file(fsPath),
} as PythonEnvironment;
}
test('sorts by version in descending order', () => {
const envs = [
createEnv('3.10.0', 'env1', '/path1'),
createEnv('3.12.0', 'env2', '/path2'),
createEnv('3.11.0', 'env3', '/path3'),
];
const result = sortEnvironments(envs);
assert.strictEqual(result[0].version, '3.12.0');
assert.strictEqual(result[1].version, '3.11.0');
assert.strictEqual(result[2].version, '3.10.0');
});
test('sorts by name when versions are equal', () => {
const envs = [
createEnv('3.11.0', 'charlie', '/path1'),
createEnv('3.11.0', 'alpha', '/path2'),
createEnv('3.11.0', 'beta', '/path3'),
];
const result = sortEnvironments(envs);
assert.strictEqual(result[0].name, 'alpha');
assert.strictEqual(result[1].name, 'beta');
assert.strictEqual(result[2].name, 'charlie');
});
test('sorts by path when version and name are equal', () => {
const envs = [
createEnv('3.11.0', 'env', '/path/z'),
createEnv('3.11.0', 'env', '/path/a'),
createEnv('3.11.0', 'env', '/path/m'),
];
const result = sortEnvironments(envs);
assert.strictEqual(result[0].environmentPath.fsPath, path.normalize('/path/a'));
assert.strictEqual(result[1].environmentPath.fsPath, path.normalize('/path/m'));
assert.strictEqual(result[2].environmentPath.fsPath, path.normalize('/path/z'));
});
});
suite('getLatest', () => {
function createEnv(version: string): PythonEnvironment {
return {
version,
name: 'env',
environmentPath: Uri.file('/path'),
} as PythonEnvironment;
}
test('returns environment with highest version', () => {
const envs = [createEnv('3.10.0'), createEnv('3.12.0'), createEnv('3.11.0')];
const result = getLatest(envs);
assert.strictEqual(result?.version, '3.12.0');
});
test('returns first environment when all versions are equal', () => {
const envs = [
{ ...createEnv('3.11.0'), name: 'first' },
{ ...createEnv('3.11.0'), name: 'second' },
];
const result = getLatest(envs);
assert.strictEqual(result?.name, 'first');
});
test('returns undefined for empty collection', () => {
const result = getLatest([]);
assert.strictEqual(result, undefined);
});
test('returns single environment from single-item collection', () => {
const envs = [createEnv('3.11.0')];
const result = getLatest(envs);
assert.strictEqual(result?.version, '3.11.0');
});
});
});
suite('Utils - Async Functions with Dependencies', () => {
// Note: getShellActivationCommands uses fs.pathExists which cannot be easily stubbed in unit tests
// These tests verify the shell command structure without mocking file system
suite('getShellActivationCommands - structure tests', () => {
test('returns Maps with shell activation and deactivation commands', async () => {
const result = await getShellActivationCommands('/test/bin');
// Verify return structure
assert.ok(result.shellActivation instanceof Map);
assert.ok(result.shellDeactivation instanceof Map);
// Verify basic shells are configured
assert.ok(result.shellActivation.size > 0);
assert.ok(result.shellDeactivation.size > 0);
});
test('includes expected shell types', async () => {
const result = await getShellActivationCommands('/test/bin');
// Common shells should be present (exact list depends on file existence)
const hasBasicShells =
result.shellActivation.has('bash') ||
result.shellActivation.has('sh') ||
result.shellActivation.has('zsh');
assert.ok(hasBasicShells, 'Should configure at least one basic shell');
});
});
// Note: removeFirstDefaultEnvManagerSettingDetailed and notifyMissingManagerIfDefault
// use VS Code APIs (workspace, window, commands) directly and are better suited for
// extension tests (*.test.ts) that run in a full VS Code environment rather than unit tests.
// These functions involve:
// - workspace.getConfiguration() - requires VS Code workspace context
// - window.showErrorMessage() - requires VS Code UI
// - commands.executeCommand() - requires VS Code command registry
// For comprehensive testing of these functions, create extension tests in a separate file.
});