-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathhelper.environment.unit.test.ts
More file actions
165 lines (136 loc) · 5.19 KB
/
helper.environment.unit.test.ts
File metadata and controls
165 lines (136 loc) · 5.19 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as envParser from '../../../../extension/common/variables/environment';
import { getDebugEnvironmentVariables } from '../../../../extension/debugger/configuration/resolvers/helper';
import { LaunchRequestArguments } from '../../../../extension/types';
suite('Debugging - Environment Variable Substitution', () => {
let parseFileStub: sinon.SinonStub;
setup(() => {
parseFileStub = sinon.stub(envParser, 'parseFile');
sinon.stub(envParser, 'mergeVariables');
sinon.stub(envParser, 'appendPath');
sinon.stub(envParser, 'appendPythonPath');
});
teardown(() => {
sinon.restore();
});
test('Environment variables from process.env should be available for substitution in .env file', async () => {
// Arrange
const args: LaunchRequestArguments = {
name: 'Test',
type: 'debugpy',
request: 'launch',
envFile: '/path/to/.env',
env: {
CUSTOM_VAR: 'custom_value',
},
};
// Set up process.env
const originalPath = process.env.PATH;
process.env.PATH = '/usr/bin:/usr/local/bin';
parseFileStub.resolves({
EXPANDED_PATH: '${PATH}:/my/custom/path',
});
// Act
await getDebugEnvironmentVariables(args);
// Assert
// Verify that parseFile was called with merged environment variables
expect(parseFileStub.calledOnce).to.be.true;
const [envFilePath, baseVars] = parseFileStub.firstCall.args;
expect(envFilePath).to.equal('/path/to/.env');
expect(baseVars).to.have.property('PATH', '/usr/bin:/usr/local/bin');
expect(baseVars).to.have.property('CUSTOM_VAR', 'custom_value');
// Restore process.env
if (originalPath !== undefined) {
process.env.PATH = originalPath;
}
});
test('Debug launch env vars should override process.env during substitution', async () => {
// Arrange
const args: LaunchRequestArguments = {
name: 'Test',
type: 'debugpy',
request: 'launch',
envFile: '/path/to/.env',
env: {
PATH: '/custom/path',
MY_VAR: 'my_value',
},
};
// Set up process.env
const originalPath = process.env.PATH;
process.env.PATH = '/usr/bin:/usr/local/bin';
process.env.MY_VAR = 'system_value';
parseFileStub.resolves({});
// Act
await getDebugEnvironmentVariables(args);
// Assert
expect(parseFileStub.calledOnce).to.be.true;
const [, baseVars] = parseFileStub.firstCall.args;
// Debug launch vars should override process.env
expect(baseVars).to.have.property('PATH', '/custom/path');
expect(baseVars).to.have.property('MY_VAR', 'my_value');
// Restore process.env
if (originalPath !== undefined) {
process.env.PATH = originalPath;
}
delete process.env.MY_VAR;
});
test('All process.env variables should be available for substitution', async () => {
// Arrange
const args: LaunchRequestArguments = {
name: 'Test',
type: 'debugpy',
request: 'launch',
envFile: '/path/to/.env',
env: {},
};
// Set up process.env
const originalEnv = { ...process.env };
process.env.TEST_VAR_1 = 'value1';
process.env.TEST_VAR_2 = 'value2';
process.env.PATH = '/test/path';
parseFileStub.resolves({});
// Act
await getDebugEnvironmentVariables(args);
// Assert
expect(parseFileStub.calledOnce).to.be.true;
const [, baseVars] = parseFileStub.firstCall.args;
expect(baseVars).to.have.property('TEST_VAR_1', 'value1');
expect(baseVars).to.have.property('TEST_VAR_2', 'value2');
expect(baseVars).to.have.property('PATH', '/test/path');
// Restore process.env
Object.keys(process.env).forEach((key) => {
if (!(key in originalEnv)) {
delete process.env[key];
}
});
Object.assign(process.env, originalEnv);
});
test('Empty env in launch config should still provide process.env for substitution', async () => {
// Arrange
const args: LaunchRequestArguments = {
name: 'Test',
type: 'debugpy',
request: 'launch',
envFile: '/path/to/.env',
// No env property
};
const originalPath = process.env.PATH;
process.env.PATH = '/usr/bin';
parseFileStub.resolves({});
// Act
await getDebugEnvironmentVariables(args);
// Assert
expect(parseFileStub.calledOnce).to.be.true;
const [, baseVars] = parseFileStub.firstCall.args;
expect(baseVars).to.have.property('PATH', '/usr/bin');
// Restore
if (originalPath !== undefined) {
process.env.PATH = originalPath;
}
});
});