-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenvironment.test.ts
More file actions
566 lines (478 loc) · 22.1 KB
/
environment.test.ts
File metadata and controls
566 lines (478 loc) · 22.1 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* eslint-disable @typescript-eslint/no-explicit-any */
import { execFileSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
getPlatformInfo,
getCodeQLExePath,
getJavaScriptExtractorRoot,
setupJavaScriptExtractorEnv,
getAutobuildScriptPath,
configureLgtmIndexFilters,
applyPathsIgnoreToLgtmFilters,
setupAndValidateEnvironment,
} from '../../src/environment';
import * as pathsIgnore from '../../src/paths-ignore';
jest.mock('../../src/paths-ignore', () => ({
getPathsIgnorePatterns: jest.fn().mockReturnValue([]),
}));
// Mock modules
jest.mock('child_process');
jest.mock('os');
jest.mock('path');
jest.mock('../../src/filesystem', () => {
const originalModule = jest.requireActual('../../src/filesystem');
return {
...originalModule,
dirExists: jest.fn().mockImplementation(path => {
// Default to true unless specified in a test
if (path === '/invalid/source') return false;
return true;
}),
fileExists: jest.fn().mockImplementation(_path => {
// Path-specific mock logic can be added here if needed
return true; // Default to true for all paths
}),
};
});
jest.mock('fs', () => ({
existsSync: jest.fn().mockImplementation(path => {
// Return true for autobuild script paths in the tests
if (path === '/path/to/js/extractor/tools/autobuild.sh') return true;
if (path === '/dist/js/extractor/tools/autobuild.sh') return true;
if (path === '/path/to/codeql_unpacked/codeql') return true;
if (path === '/dist/codeql/codeql') return true;
return false;
}),
}));
describe('environment', () => {
// Save original environment
const originalEnv = { ...process.env };
// Define a logger for capturing console output
let consoleSpy: {
log: jest.SpyInstance;
warn: jest.SpyInstance;
error: jest.SpyInstance;
};
beforeEach(() => {
jest.resetModules(); // Reset modules to clear cache
jest.resetAllMocks(); // Reset all mocks
// Reset environment variables before each test
process.env = { ...originalEnv };
// Spy on console methods
consoleSpy = {
log: jest.spyOn(console, 'log').mockImplementation(() => {}),
warn: jest.spyOn(console, 'warn').mockImplementation(() => {}),
error: jest.spyOn(console, 'error').mockImplementation(() => {}),
};
// Default path mocks
(path.resolve as jest.Mock).mockImplementation((p: string) => p);
(path.join as jest.Mock).mockImplementation((...args: string[]) => args.join('/'));
// Reset the fs.existsSync mock to its default implementation in the mock declaration
(fs.existsSync as jest.Mock).mockImplementation((path: string) => {
// Return true for autobuild script paths in the tests
if (path === '/path/to/js/extractor/tools/autobuild.sh') return true;
if (path === '/dist/js/extractor/tools/autobuild.sh') return true;
if (path === '/path/to/codeql_unpacked/codeql') return true;
if (path === '/dist/codeql/codeql') return true;
return false;
});
});
afterEach(() => {
// Restore environment variables after each test
process.env = { ...originalEnv };
// Restore console spies
consoleSpy.log.mockRestore();
consoleSpy.warn.mockRestore();
consoleSpy.error.mockRestore();
});
describe('getPlatformInfo', () => {
it('should correctly identify Windows platform', () => {
// Mock OS platform and architecture
(os.platform as jest.Mock).mockReturnValue('win32');
(os.arch as jest.Mock).mockReturnValue('x64');
const platformInfo = getPlatformInfo();
expect(platformInfo.platform).toBe('win32');
expect(platformInfo.arch).toBe('x64');
expect(platformInfo.isWindows).toBe(true);
expect(platformInfo.exeExtension).toBe('.exe');
});
it('should correctly identify non-Windows platform', () => {
// Mock OS platform and architecture
(os.platform as jest.Mock).mockReturnValue('darwin');
(os.arch as jest.Mock).mockReturnValue('x64');
const platformInfo = getPlatformInfo();
expect(platformInfo.platform).toBe('darwin');
expect(platformInfo.arch).toBe('x64');
expect(platformInfo.isWindows).toBe(false);
expect(platformInfo.exeExtension).toBe('');
});
});
describe('getCodeQLExePath', () => {
it('should resolve codeql.exe path on Windows when CODEQL_DIST is set and valid', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('win32');
process.env.CODEQL_DIST = '/path/to/codeql';
// Mock existsSync to return true specifically for the codeql.exe path
(fs.existsSync as jest.Mock).mockImplementation(
(p: string) => p === '/path/to/codeql/codeql.exe',
);
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('/path/to/codeql/codeql.exe');
expect(fs.existsSync).toHaveBeenCalledWith('/path/to/codeql/codeql.exe');
});
it('should resolve codeql path on non-Windows when CODEQL_DIST is set and valid', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
process.env.CODEQL_DIST = '/path/to/codeql';
// Mock existsSync to return true specifically for the codeql path
(fs.existsSync as jest.Mock).mockImplementation(
(p: string) => p === '/path/to/codeql/codeql',
);
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('/path/to/codeql/codeql');
expect(fs.existsSync).toHaveBeenCalledWith('/path/to/codeql/codeql');
});
it('should fall back to PATH search if CODEQL_DIST is set but path is invalid, then succeed if PATH is valid', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
process.env.CODEQL_DIST = '/invalid/dist/path';
// Mock file existence
(fs.existsSync as jest.Mock).mockImplementation((p: string) => {
if (p === '/invalid/dist/path/codeql') return false;
if (p === '/resolved/via/version/codeql') return true;
return false;
});
// Mock execFileSync to return unpackedLocation
(execFileSync as jest.Mock).mockReturnValueOnce(
JSON.stringify({ unpackedLocation: '/resolved/via/version' }),
);
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('/resolved/via/version/codeql');
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.stringContaining(
"CODEQL_DIST is set to '/invalid/dist/path', but CodeQL executable was not found",
),
);
expect(execFileSync).toHaveBeenCalledWith(
'codeql',
['version', '--format=json'],
expect.any(Object),
);
});
it('should find codeql via PATH using `codeql version --format=json` if CODEQL_DIST is not set', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
delete process.env.CODEQL_DIST;
// Mock file existence
(fs.existsSync as jest.Mock).mockImplementation(
(p: string) => p === '/resolved/via/version/codeql',
);
// Mock execFileSync to return unpackedLocation
(execFileSync as jest.Mock).mockReturnValueOnce(
JSON.stringify({ unpackedLocation: '/resolved/via/version' }),
);
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('/resolved/via/version/codeql');
expect(execFileSync).toHaveBeenCalledWith(
'codeql',
['version', '--format=json'],
expect.any(Object),
);
expect(fs.existsSync).toHaveBeenCalledWith('/resolved/via/version/codeql');
});
it('should return empty string if CODEQL_DIST is not set and `codeql version --format=json` fails (ENOENT)', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
delete process.env.CODEQL_DIST;
// Mock execFileSync to throw ENOENT error
const error = new Error('Command not found');
(error as any).code = 'ENOENT';
(execFileSync as jest.Mock).mockImplementation(() => {
throw error;
});
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('');
expect(consoleSpy.log).toHaveBeenCalledWith(
expect.stringContaining("INFO: The command 'codeql' was not found in your system PATH."),
);
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.stringContaining('ERROR: Failed to determine CodeQL executable path'),
);
});
it('should return empty string if CODEQL_DIST is not set and `codeql version --format=json` output is invalid (missing unpackedLocation)', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
delete process.env.CODEQL_DIST;
// Mock execFileSync to return invalid JSON without unpackedLocation
(execFileSync as jest.Mock).mockReturnValueOnce(JSON.stringify({ version: '1.2.3' }));
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('');
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringContaining(
"WARN: Could not determine CodeQL executable path from 'codeql version --format=json' output.",
),
);
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.stringContaining('ERROR: Failed to determine CodeQL executable path'),
);
});
it('should return empty string if `codeql version --format=json` provides unpackedLocation but executable is not found there', () => {
// Setup platform
(os.platform as jest.Mock).mockReturnValue('darwin');
delete process.env.CODEQL_DIST;
// Mock execFileSync to return valid JSON with unpackedLocation
(execFileSync as jest.Mock).mockReturnValueOnce(
JSON.stringify({ unpackedLocation: '/some/path' }),
);
// Mock existsSync to return false for the executable path
(fs.existsSync as jest.Mock).mockReturnValue(false);
const codeqlPath = getCodeQLExePath();
expect(codeqlPath).toBe('');
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringContaining(
"WARN: 'codeql version --format=json' provided unpackedLocation '/some/path', but executable not found",
),
);
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.stringContaining('ERROR: Failed to determine CodeQL executable path'),
);
});
});
describe('getJavaScriptExtractorRoot', () => {
it('should return CODEQL_EXTRACTOR_JAVASCRIPT_ROOT when set', () => {
process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT = '/path/to/js/extractor';
const jsExtractorRoot = getJavaScriptExtractorRoot('/path/to/codeql');
expect(jsExtractorRoot).toBe('/path/to/js/extractor');
expect(execFileSync).not.toHaveBeenCalled();
});
it('should resolve JS extractor root if env var not set and codeqlExePath is valid', () => {
delete process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT;
// Mock execFileSync to return a path
(execFileSync as jest.Mock).mockReturnValueOnce('/resolved/js/extractor/path\n');
const jsExtractorRoot = getJavaScriptExtractorRoot('/valid/codeql/path');
expect(jsExtractorRoot).toBe('/resolved/js/extractor/path');
expect(execFileSync).toHaveBeenCalledWith(
'/valid/codeql/path',
['resolve', 'extractor', '--language=javascript'],
expect.any(Object),
);
});
it('should return empty string if codeqlExePath is empty', () => {
delete process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT;
const jsExtractorRoot = getJavaScriptExtractorRoot('');
expect(jsExtractorRoot).toBe('');
expect(execFileSync).not.toHaveBeenCalled();
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringMatching(
/^\[CDS-.+ \d+\] WARN: Cannot resolve JavaScript extractor root because the CodeQL executable path was not provided or found\.$/,
),
);
});
it('should return empty string and log error if JS extractor resolution fails', () => {
delete process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT;
// Mock execFileSync to throw an error
(execFileSync as jest.Mock).mockImplementation(() => {
throw new Error('Command failed');
});
const jsExtractorRoot = getJavaScriptExtractorRoot('/valid/codeql/path');
expect(jsExtractorRoot).toBe('');
expect(consoleSpy.error).toHaveBeenCalledWith(
expect.stringContaining(
"Error resolving JavaScript extractor root using '/valid/codeql/path': Error: Command failed",
),
);
});
});
describe('setupJavaScriptExtractorEnv', () => {
it('should correctly map CDS environment variables to JavaScript environment variables', () => {
// Set sample environment variables for testing
process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE = 'cds-wip-db';
process.env.CODEQL_EXTRACTOR_CDS_DIAGNOSTIC_DIR = 'cds-diagnostic-dir';
process.env.CODEQL_EXTRACTOR_CDS_LOG_DIR = 'cds-log-dir';
process.env.CODEQL_EXTRACTOR_CDS_SCRATCH_DIR = 'cds-scratch-dir';
process.env.CODEQL_EXTRACTOR_CDS_TRAP_DIR = 'cds-trap-dir';
process.env.CODEQL_EXTRACTOR_CDS_SOURCE_ARCHIVE_DIR = 'cds-source-archive-dir';
setupJavaScriptExtractorEnv();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_WIP_DATABASE).toBe('cds-wip-db');
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_DIAGNOSTIC_DIR).toBe('cds-diagnostic-dir');
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_LOG_DIR).toBe('cds-log-dir');
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR).toBe('cds-scratch-dir');
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_TRAP_DIR).toBe('cds-trap-dir');
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_SOURCE_ARCHIVE_DIR).toBe(
'cds-source-archive-dir',
);
});
it('should handle missing CDS environment variables gracefully', () => {
// Clear all CDS environment variables
delete process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE;
delete process.env.CODEQL_EXTRACTOR_CDS_DIAGNOSTIC_DIR;
delete process.env.CODEQL_EXTRACTOR_CDS_LOG_DIR;
delete process.env.CODEQL_EXTRACTOR_CDS_SCRATCH_DIR;
delete process.env.CODEQL_EXTRACTOR_CDS_TRAP_DIR;
delete process.env.CODEQL_EXTRACTOR_CDS_SOURCE_ARCHIVE_DIR;
setupJavaScriptExtractorEnv();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_WIP_DATABASE).toBeUndefined();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_DIAGNOSTIC_DIR).toBeUndefined();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_LOG_DIR).toBeUndefined();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_SCRATCH_DIR).toBeUndefined();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_TRAP_DIR).toBeUndefined();
expect(process.env.CODEQL_EXTRACTOR_JAVASCRIPT_SOURCE_ARCHIVE_DIR).toBeUndefined();
});
});
describe('getAutobuildScriptPath', () => {
it('should return correct autobuild script path for Windows', () => {
(os.platform as jest.Mock).mockReturnValue('win32');
const autobuildScriptPath = getAutobuildScriptPath('/path/to/js/extractor');
expect(autobuildScriptPath).toBe('/path/to/js/extractor/tools/autobuild.cmd');
});
it('should return correct autobuild script path for non-Windows', () => {
(os.platform as jest.Mock).mockReturnValue('darwin');
const autobuildScriptPath = getAutobuildScriptPath('/path/to/js/extractor');
expect(autobuildScriptPath).toBe('/path/to/js/extractor/tools/autobuild.sh');
});
it('should return empty string if jsExtractorRoot is empty', () => {
const autobuildScriptPath = getAutobuildScriptPath('');
expect(autobuildScriptPath).toBe('');
});
});
describe('configureLgtmIndexFilters', () => {
it('should set up index filters with standard patterns when no existing filters are set', () => {
delete process.env.LGTM_INDEX_FILTERS;
configureLgtmIndexFilters();
expect(process.env.LGTM_INDEX_FILTERS).toBeDefined();
expect(process.env.LGTM_INDEX_TYPESCRIPT).toBe('NONE');
expect(process.env.LGTM_INDEX_FILETYPES).toBe('.cds:JSON');
});
it('should preserve existing exclusion filters except for generic exclusions', () => {
process.env.LGTM_INDEX_FILTERS = [
'exclude:**/*.*', // generic pattern
'exclude:specific/path/**/*', // specific pattern
].join('\n');
configureLgtmIndexFilters();
expect(process.env.LGTM_INDEX_FILTERS).toContain('include:**/*.cds.json');
expect(process.env.LGTM_INDEX_FILTERS).toContain('exclude:specific/path/**/*');
expect(process.env.LGTM_INDEX_TYPESCRIPT).toBe('NONE');
expect(process.env.LGTM_INDEX_FILETYPES).toBe('.cds:JSON');
});
});
describe('applyPathsIgnoreToLgtmFilters', () => {
beforeEach(() => {
(pathsIgnore.getPathsIgnorePatterns as jest.Mock).mockReturnValue([]);
});
it('should append exclude lines to LGTM_INDEX_FILTERS when patterns exist', () => {
(pathsIgnore.getPathsIgnorePatterns as jest.Mock).mockReturnValue(['vendor', '**/*.test.js']);
process.env.LGTM_INDEX_FILTERS = 'include:**/*.cds.json';
applyPathsIgnoreToLgtmFilters('/source');
expect(process.env.LGTM_INDEX_FILTERS).toContain('exclude:vendor');
expect(process.env.LGTM_INDEX_FILTERS).toContain('exclude:**/*.test.js');
expect(process.env.LGTM_INDEX_FILTERS).toContain('include:**/*.cds.json');
});
it('should not modify LGTM_INDEX_FILTERS when no patterns exist', () => {
(pathsIgnore.getPathsIgnorePatterns as jest.Mock).mockReturnValue([]);
process.env.LGTM_INDEX_FILTERS = 'include:**/*.cds.json';
applyPathsIgnoreToLgtmFilters('/source');
expect(process.env.LGTM_INDEX_FILTERS).toBe('include:**/*.cds.json');
});
it('should handle missing LGTM_INDEX_FILTERS env var', () => {
(pathsIgnore.getPathsIgnorePatterns as jest.Mock).mockReturnValue(['vendor']);
delete process.env.LGTM_INDEX_FILTERS;
applyPathsIgnoreToLgtmFilters('/source');
expect(process.env.LGTM_INDEX_FILTERS).toContain('exclude:vendor');
});
});
describe('setupAndValidateEnvironment', () => {
let filesystem: any;
beforeEach(() => {
filesystem = jest.requireMock('../../src/filesystem');
filesystem.dirExists = jest.fn().mockReturnValue(true);
// Ensure the mock is properly reset for each test
(fs.existsSync as jest.Mock).mockReset();
});
it('should report error if CodeQL executable is not found', () => {
// Setup CodeQL detection to fail
delete process.env.CODEQL_DIST;
// Mock execFileSync to throw ENOENT for codeql
const error = new Error('Command not found');
(error as any).code = 'ENOENT';
(execFileSync as jest.Mock).mockImplementation((_command: string, args: string[]) => {
if (args?.includes('version')) {
throw error;
}
return '';
});
// No codeql executable exists
(fs.existsSync as jest.Mock).mockReturnValue(false);
const result = setupAndValidateEnvironment('/path/to/source');
expect(result.success).toBe(false);
expect(result.errorMessages).toContain(
'Failed to find CodeQL executable. Ensure CODEQL_DIST is set and valid, or CodeQL CLI is in PATH.',
);
expect(result.errorMessages).toContain(
'Cannot determine JavaScript extractor root because CodeQL executable was not found.',
);
});
it('should report error when source root does not exist', () => {
// We're relying on the global mock to return false for '/invalid/source'
const result = setupAndValidateEnvironment('/invalid/source');
expect(result.success).toBe(false);
expect(result.errorMessages).toContain(
"Project root directory '/invalid/source' does not exist.",
);
});
it('should report error if JS extractor root cannot be resolved even if CodeQL is found', () => {
// Mock OS platform
(os.platform as jest.Mock).mockReturnValue('linux');
// Setup CodeQL detection
delete process.env.CODEQL_DIST;
// Mock execFileSync for version and extractor resolution
(execFileSync as jest.Mock).mockImplementation((_command: string, args: string[]) => {
if (args?.includes('version')) {
return JSON.stringify({ unpackedLocation: '/path/to/codeql_unpacked' });
}
if (args?.includes('extractor')) {
throw new Error('Extractor resolution failed');
}
return '';
});
// Mock fs.existsSync
(fs.existsSync as jest.Mock).mockImplementation((p: string) => {
if (p === '/path/to/codeql_unpacked/codeql') return true;
return false;
});
const result = setupAndValidateEnvironment('/path/to/source');
expect(result.success).toBe(false);
expect(result.codeqlExePath).toBe('/path/to/codeql_unpacked/codeql'); // CodeQL was found
expect(result.jsExtractorRoot).toBe(''); // But JS extractor root was not
expect(result.errorMessages).toContain(
'Failed to determine JavaScript extractor root using the found CodeQL executable.',
);
});
// Test for the specific runtime error condition
it('should fail validation if CODEQL_DIST is not set and codeql is not in PATH, leading to empty codeqlExePath', () => {
// Setup CodeQL detection to fail
delete process.env.CODEQL_DIST;
// Mock execFileSync to throw ENOENT for codeql
const error = new Error('Command not found');
(error as any).code = 'ENOENT';
(execFileSync as jest.Mock).mockImplementation((_command: string, args: string[]) => {
if (args?.includes('version')) {
throw error;
}
return '';
});
// No codeql executable exists
(fs.existsSync as jest.Mock).mockReturnValue(false);
const result = setupAndValidateEnvironment('/path/to/source');
expect(result.success).toBe(false);
expect(result.codeqlExePath).toBe(''); // This is key for the runtime error
expect(result.errorMessages).toContain(
'Failed to find CodeQL executable. Ensure CODEQL_DIST is set and valid, or CodeQL CLI is in PATH.',
);
expect(result.errorMessages).toContain(
'Cannot determine JavaScript extractor root because CodeQL executable was not found.',
);
});
});
});