-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvironment-builder.test.ts
More file actions
395 lines (340 loc) · 14.7 KB
/
environment-builder.test.ts
File metadata and controls
395 lines (340 loc) · 14.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { delimiter } from 'path';
import { EnvironmentBuilder } from '../../src/bridge/environment-builder';
import type { DatabaseCopierFactory } from '../../src/bridge/environment-builder';
function createMockContext() {
return {
globalStorageUri: { fsPath: '/mock/global-storage/codeql-mcp' },
storageUri: { fsPath: '/mock/workspace-storage/codeql-mcp' },
} as any;
}
function createMockCliResolver() {
return {
resolve: vi.fn().mockResolvedValue('/usr/local/bin/codeql'),
invalidateCache: vi.fn(),
dispose: vi.fn(),
push: vi.fn(),
} as any;
}
function createMockStoragePaths() {
return {
getCodeqlGlobalStoragePath: vi.fn().mockReturnValue('/mock/global-storage/GitHub.vscode-codeql'),
getDatabaseStoragePath: vi.fn().mockReturnValue('/mock/global-storage/GitHub.vscode-codeql'),
getManagedDatabaseStoragePath: vi.fn().mockReturnValue('/mock/global-storage/codeql-mcp/databases'),
getWorkspaceDatabaseStoragePath: vi.fn().mockReturnValue('/mock/workspace-storage/ws-123/GitHub.vscode-codeql'),
getAllDatabaseStoragePaths: vi.fn().mockReturnValue([
'/mock/global-storage/GitHub.vscode-codeql',
'/mock/workspace-storage/ws-123/GitHub.vscode-codeql',
]),
getQueryStoragePath: vi.fn().mockReturnValue('/mock/global-storage/GitHub.vscode-codeql/queries'),
getVariantAnalysisStoragePath: vi.fn().mockReturnValue('/mock/global-storage/GitHub.vscode-codeql/variant-analyses'),
getGlobalStorageRoot: vi.fn().mockReturnValue('/mock/global-storage'),
dispose: vi.fn(),
push: vi.fn(),
} as any;
}
function createMockLogger() {
return {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
show: vi.fn(),
dispose: vi.fn(),
} as any;
}
function createMockCopierFactory(): { factory: DatabaseCopierFactory; syncAll: ReturnType<typeof vi.fn> } {
const syncAll = vi.fn().mockResolvedValue([]);
const factory: DatabaseCopierFactory = () => ({ syncAll } as any);
return { factory, syncAll };
}
describe('EnvironmentBuilder', () => {
let builder: EnvironmentBuilder;
let cliResolver: any;
let mockCopier: ReturnType<typeof createMockCopierFactory>;
beforeEach(() => {
vi.resetAllMocks();
cliResolver = createMockCliResolver();
mockCopier = createMockCopierFactory();
builder = new EnvironmentBuilder(
createMockContext(),
cliResolver,
createMockStoragePaths(),
createMockLogger(),
mockCopier.factory,
);
});
it('should be instantiable', () => {
expect(builder).toBeDefined();
});
it('should build environment with CODEQL_PATH', async () => {
const env = await builder.build();
expect(env.CODEQL_PATH).toBe('/usr/local/bin/codeql');
});
it('should build environment with TRANSPORT_MODE=stdio', async () => {
const env = await builder.build();
expect(env.TRANSPORT_MODE).toBe('stdio');
});
it('should include CODEQL_MCP_TMP_DIR under global storage when no workspace', async () => {
const env = await builder.build();
expect(env.CODEQL_MCP_TMP_DIR).toBe('/mock/global-storage/codeql-mcp/tmp');
});
it('should set CODEQL_MCP_TMP_DIR to workspace scratch dir when workspace folders exist', async () => {
const vscode = await import('vscode');
const origFolders = vscode.workspace.workspaceFolders;
(vscode.workspace.workspaceFolders as any) = [
{ uri: { fsPath: '/mock/workspace' }, name: 'ws', index: 0 },
];
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_MCP_TMP_DIR).toBe('/mock/workspace/.codeql/ql-mcp');
expect(env.CODEQL_MCP_SCRATCH_DIR).toBe('/mock/workspace/.codeql/ql-mcp');
(vscode.workspace.workspaceFolders as any) = origFolders;
});
it('should set CODEQL_MCP_WORKSPACE_FOLDERS with all workspace folder paths', async () => {
const vscode = await import('vscode');
const { delimiter } = await import('path');
const origFolders = vscode.workspace.workspaceFolders;
(vscode.workspace.workspaceFolders as any) = [
{ uri: { fsPath: '/mock/ws-a' }, name: 'a', index: 0 },
{ uri: { fsPath: '/mock/ws-b' }, name: 'b', index: 1 },
];
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_MCP_WORKSPACE_FOLDERS).toBe(['/mock/ws-a', '/mock/ws-b'].join(delimiter));
(vscode.workspace.workspaceFolders as any) = origFolders;
});
it('should include CODEQL_ADDITIONAL_PACKS with database storage path', async () => {
const env = await builder.build();
expect(env.CODEQL_ADDITIONAL_PACKS).toBeDefined();
expect(env.CODEQL_ADDITIONAL_PACKS).toContain('GitHub.vscode-codeql');
});
it('should include CODEQL_DATABASES_BASE_DIRS pointing to managed copy directory by default', async () => {
const env = await builder.build();
// With copyDatabases enabled (default), CODEQL_DATABASES_BASE_DIRS
// should point to the managed directory, not the source directories.
expect(env.CODEQL_DATABASES_BASE_DIRS).toBe('/mock/global-storage/codeql-mcp/databases');
expect(mockCopier.syncAll).toHaveBeenCalledWith([
'/mock/global-storage/GitHub.vscode-codeql',
'/mock/workspace-storage/ws-123/GitHub.vscode-codeql',
]);
});
it('should include CODEQL_QUERY_RUN_RESULTS_DIRS from storage paths', async () => {
const env = await builder.build();
expect(env.CODEQL_QUERY_RUN_RESULTS_DIRS).toBe('/mock/global-storage/GitHub.vscode-codeql/queries');
});
it('should include CODEQL_MRVA_RUN_RESULTS_DIRS from storage paths', async () => {
const env = await builder.build();
expect(env.CODEQL_MRVA_RUN_RESULTS_DIRS).toBe('/mock/global-storage/GitHub.vscode-codeql/variant-analyses');
});
it('should omit CODEQL_PATH when CLI is not found', async () => {
cliResolver.resolve.mockResolvedValue(undefined);
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_PATH).toBeUndefined();
});
it('should include additional env from user settings', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'additionalEnv') return { CUSTOM_VAR: 'custom_value' };
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate(); // Clear cache so it re-reads config
const env = await builder.build();
expect(env.CUSTOM_VAR).toBe('custom_value');
// Restore
vscode.workspace.getConfiguration = originalGetConfig;
});
it('should cache environment and honor invalidation', async () => {
const _env1 = await builder.build();
const _env2 = await builder.build();
// Should use cached result (resolve only called once)
expect(cliResolver.resolve).toHaveBeenCalledTimes(1);
builder.invalidate();
await builder.build();
expect(cliResolver.resolve).toHaveBeenCalledTimes(2);
});
it('should append user-configured dirs to CODEQL_DATABASES_BASE_DIRS alongside managed dir', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'additionalDatabaseDirs') return ['/custom/databases'];
if (_key === 'additionalQueryRunResultsDirs') return [];
if (_key === 'additionalMrvaRunResultsDirs') return [];
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_DATABASES_BASE_DIRS).toContain('/custom/databases');
expect(env.CODEQL_DATABASES_BASE_DIRS).toContain('/mock/global-storage/codeql-mcp/databases');
vscode.workspace.getConfiguration = originalGetConfig;
});
it('should use source paths directly when copyDatabases is disabled', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'copyDatabases') return false;
if (_key === 'additionalDatabaseDirs') return [];
if (_key === 'additionalQueryRunResultsDirs') return [];
if (_key === 'additionalMrvaRunResultsDirs') return [];
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_DATABASES_BASE_DIRS).toBe(
['/mock/global-storage/GitHub.vscode-codeql', '/mock/workspace-storage/ws-123/GitHub.vscode-codeql'].join(delimiter),
);
vscode.workspace.getConfiguration = originalGetConfig;
});
it('should fall back to source dirs when syncAll throws', async () => {
mockCopier.syncAll.mockRejectedValue(new Error('Failed to create managed database directory'));
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_DATABASES_BASE_DIRS).toBe(
['/mock/global-storage/GitHub.vscode-codeql', '/mock/workspace-storage/ws-123/GitHub.vscode-codeql'].join(delimiter),
);
});
it('should append user-configured dirs to CODEQL_QUERY_RUN_RESULTS_DIRS', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'additionalQueryRunResultsDirs') return ['/custom/query-results'];
if (_key === 'additionalDatabaseDirs') return [];
if (_key === 'additionalMrvaRunResultsDirs') return [];
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate();
const env = await builder.build();
expect(env.CODEQL_QUERY_RUN_RESULTS_DIRS).toContain('/custom/query-results');
expect(env.CODEQL_QUERY_RUN_RESULTS_DIRS).toContain('/mock/global-storage/GitHub.vscode-codeql/queries');
vscode.workspace.getConfiguration = originalGetConfig;
});
it('should be disposable', () => {
expect(() => builder.dispose()).not.toThrow();
});
it('should set ENABLE_ANNOTATION_TOOLS=true by default', async () => {
const env = await builder.build();
expect(env.ENABLE_ANNOTATION_TOOLS).toBe('true');
});
it('should not overwrite MONITORING_STORAGE_LOCATION if already set in parent env', async () => {
const vscode = await import('vscode');
const origFolders = vscode.workspace.workspaceFolders;
const origMonLoc = process.env.MONITORING_STORAGE_LOCATION;
try {
(vscode.workspace.workspaceFolders as any) = [
{ uri: { fsPath: '/mock/workspace' }, name: 'ws', index: 0 },
];
// Simulate parent process env with MONITORING_STORAGE_LOCATION already set
process.env.MONITORING_STORAGE_LOCATION = '/custom/storage/path';
builder.invalidate();
const env = await builder.build();
// process.env value should be preserved
expect(env.MONITORING_STORAGE_LOCATION).toBe('/custom/storage/path');
} finally {
(vscode.workspace.workspaceFolders as any) = origFolders;
if (origMonLoc === undefined) {
delete process.env.MONITORING_STORAGE_LOCATION;
} else {
process.env.MONITORING_STORAGE_LOCATION = origMonLoc;
}
}
});
it('should set ENABLE_ANNOTATION_TOOLS=false when setting is disabled', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
try {
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'enableAnnotationTools') return false;
if (_key === 'additionalDatabaseDirs') return [];
if (_key === 'additionalQueryRunResultsDirs') return [];
if (_key === 'additionalMrvaRunResultsDirs') return [];
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate();
const env = await builder.build();
expect(env.ENABLE_ANNOTATION_TOOLS).toBe('false');
} finally {
vscode.workspace.getConfiguration = originalGetConfig;
}
});
it('should set MONITORING_STORAGE_LOCATION to scratch dir when annotations enabled with workspace', async () => {
const vscode = await import('vscode');
const origFolders = vscode.workspace.workspaceFolders;
try {
(vscode.workspace.workspaceFolders as any) = [
{ uri: { fsPath: '/mock/workspace' }, name: 'ws', index: 0 },
];
builder.invalidate();
const env = await builder.build();
expect(env.MONITORING_STORAGE_LOCATION).toBe('/mock/workspace/.codeql/ql-mcp');
} finally {
(vscode.workspace.workspaceFolders as any) = origFolders;
}
});
it('should allow additionalEnv to override ENABLE_ANNOTATION_TOOLS', async () => {
const vscode = await import('vscode');
const originalGetConfig = vscode.workspace.getConfiguration;
try {
vscode.workspace.getConfiguration = () => ({
get: (_key: string, defaultVal?: any) => {
if (_key === 'additionalEnv') return { ENABLE_ANNOTATION_TOOLS: 'false' };
if (_key === 'additionalDatabaseDirs') return [];
if (_key === 'additionalQueryRunResultsDirs') return [];
if (_key === 'additionalMrvaRunResultsDirs') return [];
return defaultVal;
},
has: () => false,
inspect: () => undefined as any,
update: () => Promise.resolve(),
}) as any;
builder.invalidate();
const env = await builder.build();
// additionalEnv comes after the default, so it should override
expect(env.ENABLE_ANNOTATION_TOOLS).toBe('false');
} finally {
vscode.workspace.getConfiguration = originalGetConfig;
}
});
it('should preserve ENABLE_ANNOTATION_TOOLS from parent process environment', async () => {
const origValue = process.env.ENABLE_ANNOTATION_TOOLS;
try {
process.env.ENABLE_ANNOTATION_TOOLS = 'false';
builder.invalidate();
const env = await builder.build();
// Inherited process.env value should be preserved
expect(env.ENABLE_ANNOTATION_TOOLS).toBe('false');
} finally {
if (origValue === undefined) {
delete process.env.ENABLE_ANNOTATION_TOOLS;
} else {
process.env.ENABLE_ANNOTATION_TOOLS = origValue;
}
}
});
});