forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvVars.test.ts
More file actions
275 lines (241 loc) · 11.7 KB
/
envVars.test.ts
File metadata and controls
275 lines (241 loc) · 11.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as path from 'path';
import * as shortid from 'shortid';
import { ICurrentProcess, IPathUtils } from '../../client/common/types';
import { IEnvironmentVariablesService } from '../../client/common/variables/types';
import {
DebugEnvironmentVariablesHelper,
IDebugEnvironmentVariablesService,
} from '../../client/debugger/extension/configuration/resolvers/helper';
import { ConsoleType, LaunchRequestArguments } from '../../client/debugger/types';
import { isOs, OSType } from '../common';
import { closeActiveWindows, initialize, initializeTest, IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
import { UnitTestIocContainer } from '../testing/serviceRegistry';
import { normCase } from '../../client/common/platform/fs-paths';
use(chaiAsPromised);
suite('Resolving Environment Variables when Debugging', () => {
let ioc: UnitTestIocContainer;
let debugEnvParser: IDebugEnvironmentVariablesService;
let pathVariableName: string;
let mockProcess: ICurrentProcess;
suiteSetup(async function () {
if (!IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) {
return this.skip();
}
await initialize();
});
setup(async () => {
await initializeDI();
await initializeTest();
const envParser = ioc.serviceContainer.get<IEnvironmentVariablesService>(IEnvironmentVariablesService);
const pathUtils = ioc.serviceContainer.get<IPathUtils>(IPathUtils);
mockProcess = ioc.serviceContainer.get<ICurrentProcess>(ICurrentProcess);
debugEnvParser = new DebugEnvironmentVariablesHelper(envParser, mockProcess);
pathVariableName = pathUtils.getPathVariableName();
});
suiteTeardown(closeActiveWindows);
teardown(async () => {
await ioc.dispose();
await closeActiveWindows();
});
async function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerProcessTypes();
ioc.registerFileSystemTypes();
ioc.registerVariableTypes();
ioc.registerMockProcess();
}
async function testBasicProperties(console: ConsoleType, expectedNumberOfVariables: number) {
const args = ({
program: '',
pythonPath: '',
args: [],
envFile: '',
console,
} as any) as LaunchRequestArguments;
const envVars = await debugEnvParser.getEnvironmentVariables(args);
expect(envVars).not.be.undefined;
expect(Object.keys(envVars)).lengthOf(expectedNumberOfVariables, 'Incorrect number of variables');
expect(envVars).to.have.property('PYTHONUNBUFFERED', '1', 'Property not found');
expect(envVars).to.have.property('PYTHONIOENCODING', 'UTF-8', 'Property not found');
}
test('Confirm basic environment variables exist when launched in external terminal', () =>
testBasicProperties('externalTerminal', 2));
test('Confirm basic environment variables exist when launched in intergrated terminal', () =>
testBasicProperties('integratedTerminal', 2));
test('Confirm base environment variables are merged without overwriting when provided', async () => {
const env: Record<string, string> = { DO_NOT_OVERWRITE: '1' };
const args = ({
program: '',
pythonPath: '',
args: [],
envFile: '',
console,
env,
} as any) as LaunchRequestArguments;
const baseEnvVars = { CONDA_PREFIX: 'path/to/conda/env', DO_NOT_OVERWRITE: '0' };
const envVars = await debugEnvParser.getEnvironmentVariables(args, baseEnvVars);
expect(envVars).not.be.undefined;
expect(Object.keys(envVars)).lengthOf(4, 'Incorrect number of variables');
expect(envVars).to.have.property('PYTHONUNBUFFERED', '1', 'Property not found');
expect(envVars).to.have.property('PYTHONIOENCODING', 'UTF-8', 'Property not found');
expect(envVars).to.have.property('CONDA_PREFIX', 'path/to/conda/env', 'Property not found');
expect(envVars).to.have.property('DO_NOT_OVERWRITE', '1', 'Property not found');
});
test('Confirm basic environment variables exist when launched in debug console', async () => {
let expectedNumberOfVariables = Object.keys(mockProcess.env).length;
if (mockProcess.env['PYTHONUNBUFFERED'] === undefined) {
expectedNumberOfVariables += 1;
}
if (mockProcess.env['PYTHONIOENCODING'] === undefined) {
expectedNumberOfVariables += 1;
}
await testBasicProperties('internalConsole', expectedNumberOfVariables);
});
async function testJsonEnvVariables(console: ConsoleType, expectedNumberOfVariables: number) {
const prop1 = normCase(shortid.generate());
const prop2 = normCase(shortid.generate());
const prop3 = normCase(shortid.generate());
const env: Record<string, string> = {};
env[prop1] = prop1;
env[prop2] = prop2;
mockProcess.env[prop3] = prop3;
const args = ({
program: '',
pythonPath: '',
args: [],
envFile: '',
console,
env,
} as any) as LaunchRequestArguments;
const envVars = await debugEnvParser.getEnvironmentVariables(args);
expect(envVars).not.be.undefined;
expect(Object.keys(envVars)).lengthOf(expectedNumberOfVariables, 'Incorrect number of variables');
expect(envVars).to.have.property('PYTHONUNBUFFERED', '1', 'Property not found');
expect(envVars).to.have.property('PYTHONIOENCODING', 'UTF-8', 'Property not found');
expect(envVars).to.have.property(prop1, prop1, 'Property not found');
expect(envVars).to.have.property(prop2, prop2, 'Property not found');
if (console === 'internalConsole') {
expect(envVars).to.have.property(prop3, prop3, 'Property not found');
} else {
expect(envVars).not.to.have.property(prop3, prop3, 'Property not found');
}
}
test('Confirm json environment variables exist when launched in external terminal', () =>
testJsonEnvVariables('externalTerminal', 2 + 2));
test('Confirm json environment variables exist when launched in intergrated terminal', () =>
testJsonEnvVariables('integratedTerminal', 2 + 2));
test('Confirm json environment variables exist when launched in debug console', async () => {
// Add 3 for the 3 new json env variables
let expectedNumberOfVariables = Object.keys(mockProcess.env).length + 3;
if (mockProcess.env['PYTHONUNBUFFERED'] === undefined) {
expectedNumberOfVariables += 1;
}
if (mockProcess.env['PYTHONIOENCODING'] === undefined) {
expectedNumberOfVariables += 1;
}
await testJsonEnvVariables('internalConsole', expectedNumberOfVariables);
});
async function testAppendingOfPaths(
console: ConsoleType,
expectedNumberOfVariables: number,
removePythonPath: boolean,
) {
if (removePythonPath && mockProcess.env.PYTHONPATH !== undefined) {
delete mockProcess.env.PYTHONPATH;
}
const customPathToAppend = shortid.generate();
const customPythonPathToAppend = shortid.generate();
const prop1 = shortid.generate();
const prop2 = shortid.generate();
const prop3 = shortid.generate();
const env: Record<string, string> = {};
env[pathVariableName] = customPathToAppend;
env['PYTHONPATH'] = customPythonPathToAppend;
env[prop1] = prop1;
env[prop2] = prop2;
mockProcess.env[prop3] = prop3;
const args = ({
program: '',
pythonPath: '',
args: [],
envFile: '',
console,
env,
} as any) as LaunchRequestArguments;
const envVars = await debugEnvParser.getEnvironmentVariables(args);
expect(envVars).not.be.undefined;
expect(Object.keys(envVars)).lengthOf(expectedNumberOfVariables, 'Incorrect number of variables');
expect(envVars).to.have.property('PYTHONPATH');
expect(envVars).to.have.property(pathVariableName);
expect(envVars).to.have.property('PYTHONUNBUFFERED', '1', 'Property not found');
expect(envVars).to.have.property('PYTHONIOENCODING', 'UTF-8', 'Property not found');
expect(envVars).to.have.property(prop1, prop1, 'Property not found');
expect(envVars).to.have.property(prop2, prop2, 'Property not found');
if (console === 'internalConsole') {
expect(envVars).to.have.property(prop3, prop3, 'Property not found');
} else {
expect(envVars).not.to.have.property(prop3, prop3, 'Property not found');
}
// Confirm the paths have been appended correctly.
const expectedPath = `${customPathToAppend}${path.delimiter}${mockProcess.env[pathVariableName]}`;
expect(envVars).to.have.property(pathVariableName, expectedPath, 'PATH is not correct');
// Confirm the paths have been appended correctly.
let expectedPythonPath = customPythonPathToAppend;
if (typeof mockProcess.env.PYTHONPATH === 'string' && mockProcess.env.PYTHONPATH.length > 0) {
expectedPythonPath = customPythonPathToAppend + path.delimiter + mockProcess.env.PYTHONPATH;
}
expect(envVars).to.have.property('PYTHONPATH', expectedPythonPath, 'PYTHONPATH is not correct');
if (console === 'internalConsole') {
// All variables in current process must be in here
expect(Object.keys(envVars).length).greaterThan(
Object.keys(mockProcess.env).length,
'Variables is not a subset',
);
Object.keys(mockProcess.env).forEach((key) => {
if (key === pathVariableName || key === 'PYTHONPATH') {
return;
}
expect(mockProcess.env[key]).equal(
envVars[key],
`Value for the environment variable '${key}' is incorrect.`,
);
});
}
}
test('Confirm paths get appended correctly when using json variables and launched in external terminal', async function () {
// test is flakey on windows, path separator problems. GH issue #4758
if (isOs(OSType.Windows)) {
return this.skip();
}
await testAppendingOfPaths('externalTerminal', 6, false);
});
test('Confirm paths get appended correctly when using json variables and launched in integrated terminal', async function () {
// test is flakey on windows, path separator problems. GH issue #4758
if (isOs(OSType.Windows)) {
return this.skip();
}
await testAppendingOfPaths('integratedTerminal', 6, false);
});
test('Confirm paths get appended correctly when using json variables and launched in debug console', async function () {
// test is flakey on windows, path separator problems. GH issue #4758
if (isOs(OSType.Windows)) {
return this.skip();
}
// Add 3 for the 3 new json env variables
let expectedNumberOfVariables = Object.keys(mockProcess.env).length + 3;
if (mockProcess.env['PYTHONUNBUFFERED'] === undefined) {
expectedNumberOfVariables += 1;
}
if (mockProcess.env['PYTHONPATH'] === undefined) {
expectedNumberOfVariables += 1;
}
if (mockProcess.env['PYTHONIOENCODING'] === undefined) {
expectedNumberOfVariables += 1;
}
await testAppendingOfPaths('internalConsole', expectedNumberOfVariables, false);
});
});