forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-results.test.ts
More file actions
451 lines (385 loc) · 14.5 KB
/
query-results.test.ts
File metadata and controls
451 lines (385 loc) · 14.5 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
import { expect } from 'chai';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as sinon from 'sinon';
import { LocalQueryInfo, InitialQueryInfo, interpretResultsSarif } from '../../query-results';
import { QueryWithResults } from '../../run-queries-shared';
import { DatabaseInfo, SortDirection, SortedResultSetInfo } from '../../pure/interface-types';
import { CodeQLCliServer, SourceInfo } from '../../cli';
import { CancellationTokenSource, Uri } from 'vscode';
import { tmpDir } from '../../helpers';
import { slurpQueryHistory, splatQueryHistory } from '../../query-serialization';
import { formatLegacyMessage, QueryInProgress } from '../../legacy-query-server/run-queries';
import { EvaluationResult, QueryResultType } from '../../pure/legacy-messages';
describe('query-results', () => {
let disposeSpy: sinon.SinonSpy;
let sandbox: sinon.SinonSandbox;
let queryPath: string;
let cnt = 0;
beforeEach(() => {
sandbox = sinon.createSandbox();
disposeSpy = sandbox.spy();
queryPath = path.join(Uri.file(tmpDir.name).fsPath, `query-${cnt++}`);
});
afterEach(() => {
sandbox.restore();
});
describe('FullQueryInfo', () => {
it('should get the query name', () => {
const fqi = createMockFullQueryInfo();
// from the query path
expect(fqi.getQueryName()).to.eq('hucairz');
fqi.completeThisQuery(createMockQueryWithResults(queryPath));
// from the metadata
expect(fqi.getQueryName()).to.eq('vwx');
// from quick eval position
(fqi.initialInfo as any).quickEvalPosition = {
line: 1,
endLine: 2,
fileName: '/home/users/yz'
};
expect(fqi.getQueryName()).to.eq('Quick evaluation of yz:1-2');
(fqi.initialInfo as any).quickEvalPosition.endLine = 1;
expect(fqi.getQueryName()).to.eq('Quick evaluation of yz:1');
});
it('should get the query file name', () => {
const fqi = createMockFullQueryInfo();
// from the query path
expect(fqi.getQueryFileName()).to.eq('hucairz');
// from quick eval position
(fqi.initialInfo as any).quickEvalPosition = {
line: 1,
endLine: 2,
fileName: '/home/users/yz'
};
expect(fqi.getQueryFileName()).to.eq('yz:1-2');
(fqi.initialInfo as any).quickEvalPosition.endLine = 1;
expect(fqi.getQueryFileName()).to.eq('yz:1');
});
it('should get the getResultsPath', () => {
const query = createMockQueryWithResults(queryPath);
const fqi = createMockFullQueryInfo('a', query);
const completedQuery = fqi.completedQuery!;
const expectedResultsPath = path.join(queryPath, 'results.bqrs');
// from results path
expect(completedQuery.getResultsPath('zxa', false)).to.eq(expectedResultsPath);
completedQuery.sortedResultsInfo['zxa'] = {
resultsPath: 'bxa'
} as SortedResultSetInfo;
// still from results path
expect(completedQuery.getResultsPath('zxa', false)).to.eq(expectedResultsPath);
// from sortedResultsInfo
expect(completedQuery.getResultsPath('zxa')).to.eq('bxa');
});
it('should format the statusString', () => {
const evalResult: EvaluationResult = {
resultType: QueryResultType.OTHER_ERROR,
evaluationTime: 12340,
queryId: 3,
runId: 1,
};
evalResult.message = 'Tremendously';
expect(formatLegacyMessage(evalResult)).to.eq('failed: Tremendously');
evalResult.resultType = QueryResultType.OTHER_ERROR;
expect(formatLegacyMessage(evalResult)).to.eq('failed: Tremendously');
evalResult.resultType = QueryResultType.CANCELLATION;
evalResult.evaluationTime = 2345;
expect(formatLegacyMessage(evalResult)).to.eq('cancelled after 2 seconds');
evalResult.resultType = QueryResultType.OOM;
expect(formatLegacyMessage(evalResult)).to.eq('out of memory');
evalResult.resultType = QueryResultType.SUCCESS;
expect(formatLegacyMessage(evalResult)).to.eq('finished in 2 seconds');
evalResult.resultType = QueryResultType.TIMEOUT;
expect(formatLegacyMessage(evalResult)).to.eq('timed out after 2 seconds');
});
it('should updateSortState', async () => {
// setup
const fqi = createMockFullQueryInfo('a', createMockQueryWithResults(queryPath));
const completedQuery = fqi.completedQuery!;
const spy = sandbox.spy();
const mockServer = {
sortBqrs: spy
} as unknown as CodeQLCliServer;
const sortState = {
columnIndex: 1,
sortDirection: SortDirection.desc
};
// test
await completedQuery.updateSortState(mockServer, 'a-result-set-name', sortState);
// verify
const expectedResultsPath = path.join(queryPath, 'results.bqrs');
const expectedSortedResultsPath = path.join(queryPath, 'sortedResults-a-result-set-name.bqrs');
expect(spy).to.have.been.calledWith(
expectedResultsPath,
expectedSortedResultsPath,
'a-result-set-name',
[sortState.columnIndex],
[sortState.sortDirection],
);
expect(completedQuery.sortedResultsInfo['a-result-set-name']).to.deep.equal({
resultsPath: expectedSortedResultsPath,
sortState
});
// delete the sort state
await completedQuery.updateSortState(mockServer, 'a-result-set-name');
expect(Object.values(completedQuery.sortedResultsInfo).length).to.eq(0);
});
});
it('should interpretResultsSarif', async function() {
// up to 2 minutes per test
this.timeout(2 * 60 * 1000);
const spy = sandbox.mock();
spy.returns({ a: '1234' });
const mockServer = {
interpretBqrsSarif: spy
} as unknown as CodeQLCliServer;
const interpretedResultsPath = path.join(tmpDir.name, 'interpreted.json');
const resultsPath = '123';
const sourceInfo = {};
const metadata = {
kind: 'my-kind',
id: 'my-id' as string | undefined,
scored: undefined
};
const results1 = await interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
expect(results1).to.deep.eq({ a: '1234', t: 'SarifInterpretationData' });
expect(spy).to.have.been.calledWith(
metadata,
resultsPath, interpretedResultsPath, sourceInfo
);
// Try again, but with no id
spy.reset();
spy.returns({ a: '1234' });
delete metadata.id;
const results2 = await interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
expect(results2).to.deep.eq({ a: '1234', t: 'SarifInterpretationData' });
expect(spy).to.have.been.calledWith(
{ kind: 'my-kind', id: 'dummy-id', scored: undefined },
resultsPath, interpretedResultsPath, sourceInfo
);
// try a third time, but this time we get from a valid small SARIF file
spy.reset();
fs.writeFileSync(interpretedResultsPath, JSON.stringify({
runs: [{ results: [] }] // A run needs results to succeed.
}), 'utf8');
const results3 = await interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
// We do not re-interpret if we are reading from a SARIF file.
expect(spy).to.not.have.been.called;
expect(results3).to.have.property('t', 'SarifInterpretationData');
expect(results3).to.have.nested.property('runs[0].results');
// try a fourth time, but this time we use an invalid small SARIF file
spy.reset();
fs.writeFileSync(interpretedResultsPath, JSON.stringify({
a: '6' // Invalid: no runs or results
}), 'utf8');
await expect(
interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo)
).to.be.rejectedWith('Parsing output of interpretation failed: Invalid SARIF file: expecting at least one run with result.');
// We do not attempt to re-interpret if we are reading from a SARIF file.
expect(spy).to.not.have.been.called;
// Try a fifth time with a valid large SARIF file
spy.reset();
const validSarifStream = fs.createWriteStream(interpretedResultsPath, { flags: 'w' });
validSarifStream.write(JSON.stringify({
runs: [{ results: [] }] // A run needs results to succeed.
}), 'utf8');
for (let i = 0; i < 10000000; i++) {
validSarifStream.write(JSON.stringify({
a: '6'
}), 'utf8');
}
validSarifStream.end();
const results5 = await interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
// We do not re-interpret if we are reading from a SARIF file.
expect(spy).to.not.have.been.called;
expect(results5).to.have.property('t', 'SarifInterpretationData');
expect(results5).to.have.nested.property('runs[0].results');
// Explicitly delete the large SARIF file — overwriting causes odd errors.
fs.unlink(interpretedResultsPath, (err) => {
if (err) {
throw err;
}
});
// Try a sixth time with an invalid large SARIF file
spy.reset();
const invalidSarifStream = fs.createWriteStream(interpretedResultsPath, { flags: 'w' });
for (let i = 0; i < 10000000; i++) {
invalidSarifStream.write(JSON.stringify({
a: '6'
}), 'utf8');
}
await expect(
interpretResultsSarif(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo)
).to.be.rejectedWith('Parsing output of interpretation failed: Invalid SARIF file: expecting at least one run with result.');
// We do not attempt to re-interpret if we are reading from a SARIF file.
expect(spy).to.not.have.been.called;
});
describe('splat and slurp', () => {
let infoSuccessRaw: LocalQueryInfo;
let infoSuccessInterpreted: LocalQueryInfo;
let infoEarlyFailure: LocalQueryInfo;
let infoLateFailure: LocalQueryInfo;
let infoInprogress: LocalQueryInfo;
let allHistory: LocalQueryInfo[];
beforeEach(() => {
infoSuccessRaw = createMockFullQueryInfo('a', createMockQueryWithResults(`${queryPath}-a`, false, false, '/a/b/c/a', false));
infoSuccessInterpreted = createMockFullQueryInfo('b', createMockQueryWithResults(`${queryPath}-b`, true, true, '/a/b/c/b', false));
infoEarlyFailure = createMockFullQueryInfo('c', undefined, true);
infoLateFailure = createMockFullQueryInfo('d', createMockQueryWithResults(`${queryPath}-c`, false, false, '/a/b/c/d', false));
infoInprogress = createMockFullQueryInfo('e');
allHistory = [
infoSuccessRaw,
infoSuccessInterpreted,
infoEarlyFailure,
infoLateFailure,
infoInprogress
];
});
it('should splat and slurp query history', async () => {
// the expected results only contains the history with completed queries
const expectedHistory = [
infoSuccessRaw,
infoSuccessInterpreted,
infoLateFailure,
];
const allHistoryPath = path.join(tmpDir.name, 'workspace-query-history.json');
// splat and slurp
await splatQueryHistory(allHistory, allHistoryPath);
const allHistoryActual = await slurpQueryHistory(allHistoryPath);
// the dispose methods will be different. Ignore them.
allHistoryActual.forEach(info => {
if (info.t === 'local' && info.completedQuery) {
const completedQuery = info.completedQuery;
(completedQuery as any).dispose = undefined;
// these fields should be missing on the slurped value
// but they are undefined on the original value
if (!('logFileLocation' in completedQuery)) {
(completedQuery as any).logFileLocation = undefined;
}
const query = completedQuery.query;
if (!('quickEvalPosition' in query)) {
(query as any).quickEvalPosition = undefined;
}
}
});
expectedHistory.forEach(info => {
if (info.completedQuery) {
(info.completedQuery as any).dispose = undefined;
}
});
// make the diffs somewhat sane by comparing each element directly
for (let i = 0; i < allHistoryActual.length; i++) {
expect(allHistoryActual[i]).to.deep.eq(expectedHistory[i]);
}
expect(allHistoryActual.length).to.deep.eq(expectedHistory.length);
});
it('should handle an invalid query history version', async () => {
const badPath = path.join(tmpDir.name, 'bad-query-history.json');
fs.writeFileSync(badPath, JSON.stringify({
version: 2,
queries: allHistory
}), 'utf8');
const allHistoryActual = await slurpQueryHistory(badPath);
// version number is invalid. Should return an empty array.
expect(allHistoryActual).to.deep.eq([]);
});
});
function createMockQueryWithResults(
queryPath: string,
didRunSuccessfully = true,
hasInterpretedResults = true,
dbPath = '/a/b/c',
includeSpies = true
): QueryWithResults {
// pretend that the results path exists
const resultsPath = path.join(queryPath, 'results.bqrs');
fs.mkdirpSync(queryPath);
fs.writeFileSync(resultsPath, '', 'utf8');
const query = new QueryInProgress(
queryPath,
Uri.file(dbPath).fsPath,
true,
'queryDbscheme',
undefined,
{
name: 'vwx'
},
);
const result: QueryWithResults = {
query: query.queryEvalInfo,
sucessful: didRunSuccessfully,
message: 'foo',
dispose: disposeSpy,
};
if (includeSpies) {
(query as any).hasInterpretedResults = () => Promise.resolve(hasInterpretedResults);
}
return result;
}
function createMockFullQueryInfo(dbName = 'a', queryWitbResults?: QueryWithResults, isFail = false): LocalQueryInfo {
const fqi = new LocalQueryInfo(
{
databaseInfo: {
name: dbName,
databaseUri: Uri.parse(`/a/b/c/${dbName}`).fsPath
} as unknown as DatabaseInfo,
start: new Date(),
queryPath: 'path/to/hucairz',
queryText: 'some query',
isQuickQuery: false,
isQuickEval: false,
id: `some-id-${dbName}`,
} as InitialQueryInfo,
{
dispose: () => { /**/ },
} as CancellationTokenSource
);
if (queryWitbResults) {
fqi.completeThisQuery(queryWitbResults);
}
if (isFail) {
fqi.failureReason = 'failure reason';
}
return fqi;
}
});