-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathquery-server-client.test.ts
More file actions
257 lines (226 loc) · 7.48 KB
/
query-server-client.test.ts
File metadata and controls
257 lines (226 loc) · 7.48 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
import { join, basename } from "path";
import { dirSync } from "tmp";
import { CancellationTokenSource } from "vscode-jsonrpc";
import type { RunQueryParams } from "../../../../src/query-server/messages";
import {
clearCache,
QueryResultType,
registerDatabases,
runQuery,
trimCache,
trimCacheWithMode,
} from "../../../../src/query-server/messages";
import type { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
import type { BqrsCellValue } from "../../../../src/common/bqrs-cli-types";
import { describeWithCodeQL } from "../../cli";
import { QueryServerClient } from "../../../../src/query-server/query-server-client";
import type { ProgressReporter } from "../../../../src/common/logging/vscode";
import { extLogger } from "../../../../src/common/logging/vscode";
import { ensureTestDatabase, getActivatedExtension } from "../../global.helper";
import { createMockApp } from "../../../__mocks__/appMock";
const baseDir = join(__dirname, "../../../../test/data");
const tmpDir = dirSync({
prefix: "query_test_",
keep: false,
unsafeCleanup: true,
});
const RESULTS_PATH = join(tmpDir.name, "results.bqrs");
const source = new CancellationTokenSource();
const token = source.token;
class Checkpoint<T> {
private res: () => void;
private rej: (e: Error) => void;
private promise: Promise<T>;
constructor() {
this.res = () => {
/**/
};
this.rej = () => {
/**/
};
this.promise = new Promise((res, rej) => {
this.res = res as () => Record<string, never>;
this.rej = rej;
});
}
async done(): Promise<T> {
return this.promise;
}
async resolve(): Promise<void> {
this.res();
}
async reject(e: Error): Promise<void> {
this.rej(e);
}
}
type ResultSets = {
[name: string]: BqrsCellValue[][];
};
type QueryTestCase = {
queryPath: string;
expectedResultSets: ResultSets;
};
// Test cases: queries to run and their expected results.
const queryTestCases: QueryTestCase[] = [
{
queryPath: join(baseDir, "query.ql"),
expectedResultSets: {
"#select": [[42, 3.14159, "hello world", true]],
},
},
{
queryPath: join(baseDir, "compute-default-strings.ql"),
expectedResultSets: {
"#select": [[{ label: "(no string representation)" }]],
},
},
{
queryPath: join(baseDir, "multiple-result-sets.ql"),
expectedResultSets: {
edges: [
[1, 2],
[2, 3],
],
"#select": [["s"]],
},
},
];
const nullProgressReporter: ProgressReporter = {
report: () => {
/** ignore */
},
};
describeWithCodeQL()("using the query server", () => {
let qs: QueryServerClient;
let cliServer: CodeQLCliServer;
let db: string;
beforeAll(async () => {
const app = createMockApp({});
const extension = await getActivatedExtension();
cliServer = extension.cliServer;
cliServer.quiet = true;
qs = new QueryServerClient(
app,
{
codeQlPath:
(await extension.distributionManager.getCodeQlPathWithoutVersionCheck()) ||
"",
debug: false,
cacheSize: 0,
numThreads: 1,
timeoutSecs: 0,
},
cliServer,
{
contextStoragePath: tmpDir.name,
logger: extLogger,
},
(task) => task(nullProgressReporter, new CancellationTokenSource().token),
);
await qs.startQueryServer();
// Unlike the old query sevre the new one wants a database and the empty direcrtory is not valid.
const dbItem = await ensureTestDatabase(
extension.databaseManager,
cliServer,
);
db = dbItem.databaseUri.fsPath;
});
for (const queryTestCase of queryTestCases) {
const queryName = basename(queryTestCase.queryPath);
const evaluationSucceeded = new Checkpoint<void>();
const parsedResults = new Checkpoint<void>();
it("should register the database", async () => {
await qs.sendRequest(registerDatabases, { databases: [db] });
});
it(`should be able to run query ${queryName}`, async () => {
try {
const params: RunQueryParams = {
db,
queryPath: queryTestCase.queryPath,
outputPath: RESULTS_PATH,
additionalPacks: [],
externalInputs: {},
singletonExternalInputs: {},
target: { query: {} },
};
const result = await qs.sendRequest(runQuery, params, token, () => {
/**/
});
expect(result.resultType).toBe(QueryResultType.SUCCESS);
await evaluationSucceeded.resolve();
} catch (e) {
await evaluationSucceeded.reject(e as Error);
}
});
const actualResultSets: ResultSets = {};
it(`should be able to parse results of query ${queryName}`, async () => {
await evaluationSucceeded.done();
const info = await cliServer.bqrsInfo(RESULTS_PATH);
for (const resultSet of info["result-sets"]) {
const decoded = await cliServer.bqrsDecode(
RESULTS_PATH,
resultSet.name,
);
actualResultSets[resultSet.name] = decoded.tuples;
}
await parsedResults.resolve();
});
it(`should have correct results for query ${queryName}`, async () => {
await parsedResults.done();
expect(actualResultSets).not.toEqual({});
expect(Object.keys(actualResultSets).sort()).toEqual(
Object.keys(queryTestCase.expectedResultSets).sort(),
);
for (const name in queryTestCase.expectedResultSets) {
expect(actualResultSets[name]).toEqual(
queryTestCase.expectedResultSets[name],
);
}
});
it("should invoke codeQL.trimOverlayBaseCache command when queryServerTrimCacheWithMode is enabled", async () => {
const features = (await cliServer.getFeatures()) as {
[feature: string]: boolean | undefined;
};
// Register the database first (if not already done)
await qs.sendRequest(registerDatabases, { databases: [db] });
try {
// Send the trimCacheWithMode request
const params = {
db,
mode: "overlay",
};
const result = await qs.sendRequest(
trimCacheWithMode,
params,
token,
() => {},
);
// The result should contain a deletionMessage string
expect(result).toHaveProperty("deletionMessage");
expect(typeof result.deletionMessage).toBe("string");
expect(features.queryServerTrimCacheWithMode).toBeTruthy();
} catch (e) {
expect(features.queryServerTrimCacheWithMode).toBeFalsy();
expect((e as Error).message).toContain(
"Unsupported request method: evaluation/trimCacheWithMode",
);
}
});
it("should invoke trimCache command and receive a deletionMessage", async () => {
// Register the database first (if not already done)
await qs.sendRequest(registerDatabases, { databases: [db] });
const params = { db };
const result = await qs.sendRequest(trimCache, params, token, () => {});
expect(result).toHaveProperty("deletionMessage");
expect(typeof result.deletionMessage).toBe("string");
});
it("should invoke clearCache command and receive a deletionMessage", async () => {
// Register the database first (if not already done)
await qs.sendRequest(registerDatabases, { databases: [db] });
const params = { db, dryRun: false };
const result = await qs.sendRequest(clearCache, params, token, () => {});
expect(result).toHaveProperty("deletionMessage");
expect(typeof result.deletionMessage).toBe("string");
});
}
});