-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathvenvUtils.test.ts
136 lines (115 loc) · 6.35 KB
/
venvUtils.test.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzExtFsExtra } from '@microsoft/vscode-azext-utils';
import * as assert from 'assert';
import * as path from 'path';
import { cpUtils, delay, ext, getGlobalSetting, getRandomHexString, pythonVenvSetting, updateGlobalSetting, venvUtils } from '../extension.bundle';
import { longRunningTestsEnabled, testFolderPath } from './global.test';
import { runWithSetting } from './runWithSetting';
suite('venvUtils', () => {
const command: string = 'do a thing';
const venvName: string = '.venv';
const testFolder: string = path.join(testFolderPath, 'venvUtils');
let oldVenvValue: string | undefined;
suiteSetup(async function (this: Mocha.Context): Promise<void> {
oldVenvValue = getGlobalSetting(pythonVenvSetting);
await updateGlobalSetting(pythonVenvSetting, venvName);
if (longRunningTestsEnabled) {
this.timeout(60 * 1000);
await AzExtFsExtra.ensureDir(testFolder);
const pyAlias: string = process.platform === 'win32' ? 'py' : 'python3';
await cpUtils.executeCommand(ext.outputChannel, testFolder, pyAlias, '-m', 'venv', venvName);
}
});
suiteTeardown(async () => {
await updateGlobalSetting(pythonVenvSetting, oldVenvValue);
});
test('venvExists true', async function (this: Mocha.Context): Promise<void> {
if (!longRunningTestsEnabled) {
this.skip();
}
assert.equal(await venvUtils.venvExists(venvName, testFolder), true);
});
test('venvExists false', async () => {
assert.equal(await venvUtils.venvExists('nonExistentPath', testFolder), false);
const fileName: string = 'notAVenvFile';
await AzExtFsExtra.ensureFile(path.join(testFolder, fileName));
assert.equal(await venvUtils.venvExists(fileName, testFolder), false);
const folderName: string = 'notAVenvFolder';
await AzExtFsExtra.ensureDir(path.join(testFolder, folderName));
assert.equal(await venvUtils.venvExists(folderName, testFolder), false);
});
test('runCommandInVenv', async function (this: Mocha.Context): Promise<void> {
if (!longRunningTestsEnabled) {
this.skip();
}
await venvUtils.runCommandInVenv('python --version', venvName, testFolder);
});
test('convertToVenvCommand Windows powershell', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'win32') {
this.skip();
}
await runWithWindowsTerminal('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', () => {
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '.venv\\Scripts\\activate ; do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'win32'), '.venv\\Scripts\\python -m do a thing');
});
});
test('convertToVenvCommand Windows pwsh', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'win32') {
this.skip();
}
await runWithWindowsTerminal('C:\\Program Files\\PowerShell\\7\\pwsh.exe', () => {
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '.venv\\Scripts\\activate ; do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'win32'), '.venv\\Scripts\\python -m do a thing');
});
});
test('convertToVenvCommand Windows cmd', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'win32') {
this.skip();
}
await runWithWindowsTerminal('C:\\Windows\\System32\\cmd.exe', () => {
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '.venv\\Scripts\\activate && do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'win32'), '.venv\\Scripts\\python -m do a thing');
});
});
test('convertToVenvCommand Windows git bash', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'win32') {
this.skip();
}
await runWithWindowsTerminal('C:\\Program Files\\Git\\bin\\bash.exe', () => {
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '. .venv/Scripts/activate && do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'win32'), '.venv/Scripts/python -m do a thing');
});
});
test('convertToVenvCommand Mac', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'darwin') {
this.skip();
}
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '. .venv/bin/activate && do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'darwin'), '.venv/bin/python -m do a thing');
});
test('convertToVenvCommand Linux', async function (this: Mocha.Context): Promise<void> {
if (process.platform !== 'linux') {
this.skip();
}
assert.equal(venvUtils.convertToVenvCommand(command, testFolder), '. .venv/bin/activate && do a thing');
assert.equal(venvUtils.convertToVenvPythonCommand(command, venvName, 'linux'), '.venv/bin/python -m do a thing');
});
});
async function runWithWindowsTerminal(terminalPath: string, callback: () => void): Promise<void> {
if (!(await AzExtFsExtra.pathExists(terminalPath))) {
throw new Error(`Terminal path cannot be set because it does not exist: ${terminalPath}`)
}
const profileName = getRandomHexString();
const terminalProfiles = { [profileName]: { path: terminalPath } };
await runWithSetting('terminal.integrated.profiles.windows', terminalProfiles, async () => {
await runWithSetting('terminal.integrated.defaultProfile.windows', profileName, async () => {
// https://github.com/microsoft/vscode/issues/121760
// "it takes up to 2 seconds after changing a profile to apply the changes by design"
await delay(5 * 1000);
callback();
});
});
}