|
3 | 3 |
|
4 | 4 | import * as sinon from 'sinon'; |
5 | 5 | import * as typeMoq from 'typemoq'; |
6 | | -import { GlobalEnvironmentVariableCollection, workspace } from 'vscode'; |
| 6 | +import { GlobalEnvironmentVariableCollection, Uri, workspace, WorkspaceFolder } from 'vscode'; |
| 7 | +import * as workspaceApis from '../../common/workspace.apis'; |
7 | 8 | import { EnvVarManager } from '../../features/execution/envVariableManager'; |
8 | 9 | import { TerminalEnvVarInjector } from '../../features/terminal/terminalEnvVarInjector'; |
9 | 10 |
|
@@ -55,8 +56,14 @@ suite('TerminalEnvVarInjector Basic Tests', () => { |
55 | 56 | ({ |
56 | 57 | dispose: () => {}, |
57 | 58 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
58 | | - } as any), |
| 59 | + }) as any, |
59 | 60 | ); |
| 61 | + // Mock workspace.onDidChangeConfiguration to return a proper disposable |
| 62 | + Object.defineProperty(workspace, 'onDidChangeConfiguration', { |
| 63 | + value: () => ({ dispose: () => {} }), |
| 64 | + configurable: true, |
| 65 | + writable: true, |
| 66 | + }); |
60 | 67 | }); |
61 | 68 |
|
62 | 69 | teardown(() => { |
@@ -102,3 +109,132 @@ suite('TerminalEnvVarInjector Basic Tests', () => { |
102 | 109 | sinon.assert.match(eventHandlerRegistered, true); |
103 | 110 | }); |
104 | 111 | }); |
| 112 | + |
| 113 | +/** |
| 114 | + * Tests for variable clearing: Ensure that when .env file variables are commented out or removed, |
| 115 | + * they are properly removed from the terminal environment. |
| 116 | + * |
| 117 | + * These tests verify the clear() behavior when useEnvFile setting is disabled. |
| 118 | + * Tests for file-existence scenarios are integration-level and not covered here. |
| 119 | + */ |
| 120 | +suite('TerminalEnvVarInjector - Variable Clearing', () => { |
| 121 | + let envVarCollection: typeMoq.IMock<GlobalEnvironmentVariableCollection>; |
| 122 | + let injector: TerminalEnvVarInjector; |
| 123 | + let mockScopedCollection: MockScopedCollection; |
| 124 | + let mockGetConfiguration: sinon.SinonStub; |
| 125 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 126 | + let workspaceFoldersStub: any; |
| 127 | + let mockWorkspaceFolder: WorkspaceFolder; |
| 128 | + let mockEnvVarManager: { |
| 129 | + onDidChangeEnvironmentVariables: sinon.SinonStub; |
| 130 | + getEnvironmentVariables: sinon.SinonStub; |
| 131 | + }; |
| 132 | + |
| 133 | + interface MockWorkspaceConfig { |
| 134 | + get: sinon.SinonStub; |
| 135 | + } |
| 136 | + |
| 137 | + setup(() => { |
| 138 | + envVarCollection = typeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>(); |
| 139 | + |
| 140 | + // Create mock EnvVarManager using sinon stubs |
| 141 | + mockEnvVarManager = { |
| 142 | + onDidChangeEnvironmentVariables: sinon.stub().returns({ dispose: () => {} }), |
| 143 | + getEnvironmentVariables: sinon.stub().resolves({}), |
| 144 | + }; |
| 145 | + |
| 146 | + // Create a mock workspace folder |
| 147 | + mockWorkspaceFolder = { |
| 148 | + uri: Uri.file('/test/workspace'), |
| 149 | + name: 'test-workspace', |
| 150 | + index: 0, |
| 151 | + }; |
| 152 | + |
| 153 | + // Mock workspace.workspaceFolders property |
| 154 | + workspaceFoldersStub = [mockWorkspaceFolder]; |
| 155 | + Object.defineProperty(workspace, 'workspaceFolders', { |
| 156 | + get: () => workspaceFoldersStub, |
| 157 | + configurable: true, |
| 158 | + }); |
| 159 | + |
| 160 | + // Setup scoped collection mock |
| 161 | + mockScopedCollection = { |
| 162 | + clear: sinon.stub(), |
| 163 | + replace: sinon.stub(), |
| 164 | + delete: sinon.stub(), |
| 165 | + }; |
| 166 | + |
| 167 | + // Setup environment variable collection to return scoped collection |
| 168 | + envVarCollection |
| 169 | + .setup((x) => x.getScoped(typeMoq.It.isAny())) |
| 170 | + .returns( |
| 171 | + () => mockScopedCollection as unknown as ReturnType<GlobalEnvironmentVariableCollection['getScoped']>, |
| 172 | + ); |
| 173 | + envVarCollection.setup((x) => x.clear()).returns(() => {}); |
| 174 | + |
| 175 | + // Mock getConfiguration |
| 176 | + mockGetConfiguration = sinon.stub(workspaceApis, 'getConfiguration'); |
| 177 | + |
| 178 | + // Mock workspace.onDidChangeConfiguration to return a proper disposable |
| 179 | + Object.defineProperty(workspace, 'onDidChangeConfiguration', { |
| 180 | + value: () => ({ dispose: () => {} }), |
| 181 | + configurable: true, |
| 182 | + writable: true, |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + teardown(() => { |
| 187 | + sinon.restore(); |
| 188 | + injector?.dispose(); |
| 189 | + }); |
| 190 | + |
| 191 | + test('should call clear() when useEnvFile setting is disabled', async () => { |
| 192 | + // Arrange - mock configuration with useEnvFile disabled |
| 193 | + const mockConfig: MockWorkspaceConfig = { |
| 194 | + get: sinon.stub(), |
| 195 | + }; |
| 196 | + mockConfig.get.withArgs('terminal.useEnvFile', false).returns(false); |
| 197 | + mockConfig.get.withArgs('envFile').returns(undefined); |
| 198 | + mockGetConfiguration.returns(mockConfig); |
| 199 | + |
| 200 | + // Mock getEnvironmentVariables |
| 201 | + mockEnvVarManager.getEnvironmentVariables.resolves({ TEST_VAR: 'value' }); |
| 202 | + |
| 203 | + // Act |
| 204 | + injector = new TerminalEnvVarInjector(envVarCollection.object, mockEnvVarManager as unknown as EnvVarManager); |
| 205 | + |
| 206 | + // Wait for async initialization |
| 207 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 208 | + |
| 209 | + // Assert - clear() should be called, but replace() should NOT be called |
| 210 | + sinon.assert.called(mockScopedCollection.clear); |
| 211 | + sinon.assert.notCalled(mockScopedCollection.replace); |
| 212 | + }); |
| 213 | + |
| 214 | + test('should not inject variables when useEnvFile is disabled even if env vars exist', async () => { |
| 215 | + // Arrange - mock configuration with useEnvFile disabled |
| 216 | + const mockConfig: MockWorkspaceConfig = { |
| 217 | + get: sinon.stub(), |
| 218 | + }; |
| 219 | + mockConfig.get.withArgs('terminal.useEnvFile', false).returns(false); |
| 220 | + mockConfig.get.withArgs('envFile').returns('.env'); |
| 221 | + mockGetConfiguration.returns(mockConfig); |
| 222 | + |
| 223 | + // Mock getEnvironmentVariables to return multiple variables |
| 224 | + mockEnvVarManager.getEnvironmentVariables.resolves({ |
| 225 | + API_KEY: 'secret123', |
| 226 | + DATABASE_URL: 'postgres://localhost/db', |
| 227 | + DEBUG: 'true', |
| 228 | + }); |
| 229 | + |
| 230 | + // Act |
| 231 | + injector = new TerminalEnvVarInjector(envVarCollection.object, mockEnvVarManager as unknown as EnvVarManager); |
| 232 | + |
| 233 | + // Wait for async initialization |
| 234 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 235 | + |
| 236 | + // Assert - clear() should be called to remove any previous variables, but replace() should NOT be called |
| 237 | + sinon.assert.called(mockScopedCollection.clear); |
| 238 | + sinon.assert.notCalled(mockScopedCollection.replace); |
| 239 | + }); |
| 240 | +}); |
0 commit comments