forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonVersion.unit.test.ts
More file actions
79 lines (69 loc) · 3.59 KB
/
Copy pathpythonVersion.unit.test.ts
File metadata and controls
79 lines (69 loc) · 3.59 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { assert } from 'chai';
import { join as pathJoin } from 'path';
import { It as TypeMoqIt, Mock, MockBehavior } from 'typemoq';
import { getPythonVersion, parsePythonVersion } from '../../../client/pythonEnvironments/info/pythonVersion';
interface IDeps {
exec(cmd: string, args: string[]): Promise<{ stdout: string }>;
}
suite('parsePythonVersion()', () => {
test('Must return undefined if empty string', async () => {
assert.equal(parsePythonVersion(''), undefined);
});
test('Must convert version correctly', async () => {
const version = parsePythonVersion('3.7.1')!;
assert.equal(version.raw, '3.7.1');
assert.equal(version.major, 3);
assert.equal(version.minor, 7);
assert.equal(version.patch, 1);
assert.deepEqual(version.prerelease, []);
});
test('Must convert version correctly with pre-release', async () => {
const version = parsePythonVersion('3.7.1-alpha')!;
assert.equal(version.raw, '3.7.1-alpha');
assert.equal(version.major, 3);
assert.equal(version.minor, 7);
assert.equal(version.patch, 1);
assert.deepEqual(version.prerelease, ['alpha']);
});
test('Must remove invalid pre-release channels', async () => {
assert.deepEqual(parsePythonVersion('3.7.1-alpha')!.prerelease, ['alpha']);
assert.deepEqual(parsePythonVersion('3.7.1-beta')!.prerelease, ['beta']);
assert.deepEqual(parsePythonVersion('3.7.1-candidate')!.prerelease, ['candidate']);
assert.deepEqual(parsePythonVersion('3.7.1-final')!.prerelease, ['final']);
assert.deepEqual(parsePythonVersion('3.7.1-unknown')!.prerelease, []);
assert.deepEqual(parsePythonVersion('3.7.1-')!.prerelease, []);
assert.deepEqual(parsePythonVersion('3.7.1-prerelease')!.prerelease, []);
});
test('Must default versions partgs to 0 if they are not numeric', async () => {
assert.deepEqual(parsePythonVersion('3.B.1')!.raw, '3.0.1');
assert.deepEqual(parsePythonVersion('3.B.C')!.raw, '3.0.0');
assert.deepEqual(parsePythonVersion('A.B.C')!.raw, '0.0.0');
});
});
suite('getPythonVersion()', () => {
test('Must return the Python Version', async () => {
const pythonPath = pathJoin('a', 'b', 'python');
const expected = 'Output from the Procecss';
const mock = Mock.ofType<IDeps>(undefined, MockBehavior.Strict);
mock.setup((p) => p.exec(TypeMoqIt.isValue(pythonPath), TypeMoqIt.isValue(['--version'])))
// Fake the process stdout.
.returns(() => Promise.resolve({ stdout: expected }));
const exec = (c: string, a: string[]) => mock.object.exec(c, a);
const pyVersion = await getPythonVersion(pythonPath, 'DEFAULT_TEST_VALUE', exec);
assert.equal(pyVersion, expected, 'Incorrect version');
mock.verifyAll();
});
test('Must return the default value when Python path is invalid', async () => {
const pythonPath = pathJoin('a', 'b', 'python');
const mock = Mock.ofType<IDeps>(undefined, MockBehavior.Strict);
mock.setup((p) => p.exec(TypeMoqIt.isValue(pythonPath), TypeMoqIt.isValue(['--version'])))
// Fake the process stdout.
.returns(() => Promise.reject(new Error()));
const exec = (c: string, a: string[]) => mock.object.exec(c, a);
const pyVersion = await getPythonVersion(pythonPath, 'DEFAULT_TEST_VALUE', exec);
assert.equal(pyVersion, 'DEFAULT_TEST_VALUE', 'Incorrect version');
});
});