forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminalEnvVarInjectorBasic.unit.test.ts
More file actions
240 lines (202 loc) · 8.84 KB
/
terminalEnvVarInjectorBasic.unit.test.ts
File metadata and controls
240 lines (202 loc) · 8.84 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as sinon from 'sinon';
import * as typeMoq from 'typemoq';
import { GlobalEnvironmentVariableCollection, Uri, workspace, WorkspaceFolder } from 'vscode';
import * as workspaceApis from '../../common/workspace.apis';
import { EnvVarManager } from '../../features/execution/envVariableManager';
import { TerminalEnvVarInjector } from '../../features/terminal/terminalEnvVarInjector';
interface MockScopedCollection {
clear: sinon.SinonStub;
replace: sinon.SinonStub;
delete: sinon.SinonStub;
}
suite('TerminalEnvVarInjector Basic Tests', () => {
let envVarCollection: typeMoq.IMock<GlobalEnvironmentVariableCollection>;
let envVarManager: typeMoq.IMock<EnvVarManager>;
let injector: TerminalEnvVarInjector;
let mockScopedCollection: MockScopedCollection;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let workspaceFoldersStub: any;
setup(() => {
envVarCollection = typeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>();
envVarManager = typeMoq.Mock.ofType<EnvVarManager>();
// Mock workspace.workspaceFolders property
workspaceFoldersStub = [];
Object.defineProperty(workspace, 'workspaceFolders', {
get: () => workspaceFoldersStub,
configurable: true,
});
// Setup scoped collection mock
mockScopedCollection = {
clear: sinon.stub(),
replace: sinon.stub(),
delete: sinon.stub(),
};
// Setup environment variable collection to return scoped collection
envVarCollection
.setup((x) => x.getScoped(typeMoq.It.isAny()))
.returns(
() => mockScopedCollection as unknown as ReturnType<GlobalEnvironmentVariableCollection['getScoped']>,
);
envVarCollection.setup((x) => x.clear()).returns(() => {});
// Setup minimal mocks for event subscriptions
envVarManager
.setup((m) => m.onDidChangeEnvironmentVariables)
.returns(
() =>
({
dispose: () => {},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any,
);
// Mock workspace.onDidChangeConfiguration to return a proper disposable
Object.defineProperty(workspace, 'onDidChangeConfiguration', {
value: () => ({ dispose: () => {} }),
configurable: true,
writable: true,
});
});
teardown(() => {
sinon.restore();
injector?.dispose();
});
test('should initialize without errors', () => {
// Arrange & Act
injector = new TerminalEnvVarInjector(envVarCollection.object, envVarManager.object);
// Assert - should not throw
sinon.assert.match(injector, sinon.match.object);
});
test('should dispose cleanly', () => {
// Arrange
injector = new TerminalEnvVarInjector(envVarCollection.object, envVarManager.object);
// Act
injector.dispose();
// Assert - should clear on dispose
envVarCollection.verify((c) => c.clear(), typeMoq.Times.atLeastOnce());
});
test('should register environment variable change event handler', () => {
// Arrange
let eventHandlerRegistered = false;
envVarManager.reset();
envVarManager
.setup((m) => m.onDidChangeEnvironmentVariables)
.returns((_handler) => {
eventHandlerRegistered = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { dispose: () => {} } as any;
});
// Act
injector = new TerminalEnvVarInjector(envVarCollection.object, envVarManager.object);
// Assert
sinon.assert.match(eventHandlerRegistered, true);
});
});
/**
* Tests for variable clearing: Ensure that when .env file variables are commented out or removed,
* they are properly removed from the terminal environment.
*
* These tests verify the clear() behavior when useEnvFile setting is disabled.
* Tests for file-existence scenarios are integration-level and not covered here.
*/
suite('TerminalEnvVarInjector - Variable Clearing', () => {
let envVarCollection: typeMoq.IMock<GlobalEnvironmentVariableCollection>;
let injector: TerminalEnvVarInjector;
let mockScopedCollection: MockScopedCollection;
let mockGetConfiguration: sinon.SinonStub;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let workspaceFoldersStub: any;
let mockWorkspaceFolder: WorkspaceFolder;
let mockEnvVarManager: {
onDidChangeEnvironmentVariables: sinon.SinonStub;
getEnvironmentVariables: sinon.SinonStub;
};
interface MockWorkspaceConfig {
get: sinon.SinonStub;
}
setup(() => {
envVarCollection = typeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>();
// Create mock EnvVarManager using sinon stubs
mockEnvVarManager = {
onDidChangeEnvironmentVariables: sinon.stub().returns({ dispose: () => {} }),
getEnvironmentVariables: sinon.stub().resolves({}),
};
// Create a mock workspace folder
mockWorkspaceFolder = {
uri: Uri.file('/test/workspace'),
name: 'test-workspace',
index: 0,
};
// Mock workspace.workspaceFolders property
workspaceFoldersStub = [mockWorkspaceFolder];
Object.defineProperty(workspace, 'workspaceFolders', {
get: () => workspaceFoldersStub,
configurable: true,
});
// Setup scoped collection mock
mockScopedCollection = {
clear: sinon.stub(),
replace: sinon.stub(),
delete: sinon.stub(),
};
// Setup environment variable collection to return scoped collection
envVarCollection
.setup((x) => x.getScoped(typeMoq.It.isAny()))
.returns(
() => mockScopedCollection as unknown as ReturnType<GlobalEnvironmentVariableCollection['getScoped']>,
);
envVarCollection.setup((x) => x.clear()).returns(() => {});
// Mock getConfiguration
mockGetConfiguration = sinon.stub(workspaceApis, 'getConfiguration');
// Mock workspace.onDidChangeConfiguration to return a proper disposable
Object.defineProperty(workspace, 'onDidChangeConfiguration', {
value: () => ({ dispose: () => {} }),
configurable: true,
writable: true,
});
});
teardown(() => {
sinon.restore();
injector?.dispose();
});
test('should call clear() when useEnvFile setting is disabled', async () => {
// Arrange - mock configuration with useEnvFile disabled
const mockConfig: MockWorkspaceConfig = {
get: sinon.stub(),
};
mockConfig.get.withArgs('terminal.useEnvFile', false).returns(false);
mockConfig.get.withArgs('envFile').returns(undefined);
mockGetConfiguration.returns(mockConfig);
// Mock getEnvironmentVariables
mockEnvVarManager.getEnvironmentVariables.resolves({ TEST_VAR: 'value' });
// Act
injector = new TerminalEnvVarInjector(envVarCollection.object, mockEnvVarManager as unknown as EnvVarManager);
// Wait for async initialization
await new Promise((resolve) => setTimeout(resolve, 150));
// Assert - clear() should be called, but replace() should NOT be called
sinon.assert.called(mockScopedCollection.clear);
sinon.assert.notCalled(mockScopedCollection.replace);
});
test('should not inject variables when useEnvFile is disabled even if env vars exist', async () => {
// Arrange - mock configuration with useEnvFile disabled
const mockConfig: MockWorkspaceConfig = {
get: sinon.stub(),
};
mockConfig.get.withArgs('terminal.useEnvFile', false).returns(false);
mockConfig.get.withArgs('envFile').returns('.env');
mockGetConfiguration.returns(mockConfig);
// Mock getEnvironmentVariables to return multiple variables
mockEnvVarManager.getEnvironmentVariables.resolves({
API_KEY: 'secret123',
DATABASE_URL: 'postgres://localhost/db',
DEBUG: 'true',
});
// Act
injector = new TerminalEnvVarInjector(envVarCollection.object, mockEnvVarManager as unknown as EnvVarManager);
// Wait for async initialization
await new Promise((resolve) => setTimeout(resolve, 150));
// Assert - clear() should be called to remove any previous variables, but replace() should NOT be called
sinon.assert.called(mockScopedCollection.clear);
sinon.assert.notCalled(mockScopedCollection.replace);
});
});