-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathdebugger.test.ts
More file actions
227 lines (201 loc) · 8.45 KB
/
debugger.test.ts
File metadata and controls
227 lines (201 loc) · 8.45 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
import type { TextEditor } from "vscode";
import { Position, Selection, Uri, window, workspace } from "vscode";
import type { DatabaseManager } from "../../../../src/databases/local-databases";
import {
cleanDatabases,
ensureTestDatabase,
getActivatedExtension,
} from "../../global.helper";
import { describeWithCodeQL } from "../../cli";
import { withDebugController } from "./debug-controller";
import type { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
import { createVSCodeCommandManager } from "../../../../src/common/vscode/commands";
import type { AllCommands } from "../../../../src/common/commands";
import { getDataFolderFilePath } from "../utils";
import type { CoreCompletedQuery } from "../../../../src/query-server";
async function selectForQuickEval(
path: string,
line: number,
column: number,
endLine: number,
endColumn: number,
): Promise<TextEditor> {
const document = await workspace.openTextDocument(path);
const editor = await window.showTextDocument(document);
editor.selection = new Selection(line, column, endLine, endColumn);
return editor;
}
async function getResultCount(
completedQuery: CoreCompletedQuery,
cli: CodeQLCliServer,
): Promise<number> {
const results = Array.from(completedQuery.results.values());
expect(results.length).toBe(1);
const info = await cli.bqrsInfo(
completedQuery.outputDir.getBqrsPath(results[0].outputBaseName),
100,
);
const resultSet = info["result-sets"][0];
return resultSet.rows;
}
/**
* Integration tests for the query debugger
*/
describeWithCodeQL()("Debugger", () => {
let databaseManager: DatabaseManager;
let cli: CodeQLCliServer;
const appCommands = createVSCodeCommandManager<AllCommands>();
const simpleQueryPath = getDataFolderFilePath("debugger/simple-query.ql");
const quickEvalQueryPath = getDataFolderFilePath(
"debugger/QuickEvalQuery.ql",
);
const quickEvalBigIntQueryPath = getDataFolderFilePath(
"debugger/QuickEvalBigIntQuery.ql",
);
const quickEvalLibPath = getDataFolderFilePath("debugger/QuickEvalLib.qll");
beforeEach(async () => {
const extension = await getActivatedExtension();
databaseManager = extension.databaseManager;
cli = extension.cliServer;
cli.quiet = true;
await ensureTestDatabase(databaseManager, cli);
});
afterEach(async () => {
await cleanDatabases(databaseManager);
});
it("should debug a query and keep the session active", async () => {
await withDebugController(appCommands, async (controller) => {
await controller.debugQuery(Uri.file(simpleQueryPath));
await controller.expectLaunched();
await controller.expectSucceeded();
await controller.expectStopped();
});
});
it("should run a query and then stop debugging", async () => {
await withDebugController(appCommands, async (controller) => {
await controller.startDebugging(
{
query: simpleQueryPath,
},
true,
);
await controller.expectLaunched();
await controller.expectSucceeded();
await controller.expectExited();
await controller.expectTerminated();
await controller.expectSessionClosed();
});
});
it("should run a quick evaluation", async () => {
await withDebugController(appCommands, async (controller) => {
await selectForQuickEval(quickEvalQueryPath, 18, 5, 18, 22);
// Don't specify a query path, so we'll default to the active document ("QuickEvalQuery.ql")
await controller.startDebuggingSelection({});
await controller.expectLaunched();
const result = await controller.expectSucceeded();
expect(result.started.quickEvalContext).toBeDefined();
expect(result.started.quickEvalContext!.quickEvalText).toBe(
"InterestingNumber",
);
expect(result.results.queryTargets.length).toBe(1);
expect(result.results.queryTargets[0].quickEvalPosition).toBeDefined();
expect(await getResultCount(result.results, cli)).toBe(8);
await controller.expectStopped();
});
});
it("should run a quick evaluation on a library without any query context", async () => {
await withDebugController(appCommands, async (controller) => {
await selectForQuickEval(quickEvalLibPath, 4, 15, 4, 32);
// Don't specify a query path, so we'll default to the active document ("QuickEvalLib.qll")
await controller.startDebuggingSelection({});
await controller.expectLaunched();
const result = await controller.expectSucceeded();
expect(result.started.quickEvalContext).toBeDefined();
expect(result.started.quickEvalContext!.quickEvalText).toBe(
"InterestingNumber",
);
expect(result.results.queryTargets.length).toBe(1);
expect(result.results.queryTargets[0].quickEvalPosition).toBeDefined();
expect(await getResultCount(result.results, cli)).toBe(0);
await controller.expectStopped();
});
});
it("should run a quick evaluation on a library in the context of a specific query", async () => {
await withDebugController(appCommands, async (controller) => {
await selectForQuickEval(quickEvalLibPath, 4, 15, 4, 32);
await controller.startDebuggingSelection({
query: quickEvalQueryPath, // The query context. This query extends the abstract class.
});
await controller.expectLaunched();
const result = await controller.expectSucceeded();
expect(result.started.quickEvalContext).toBeDefined();
expect(result.started.quickEvalContext!.quickEvalText).toBe(
"InterestingNumber",
);
expect(result.results.queryTargets.length).toBe(1);
expect(result.results.queryTargets[0].quickEvalPosition).toBeDefined();
expect(await getResultCount(result.results, cli)).toBe(8);
await controller.expectStopped();
});
});
it("should run a quick evaluation with a bigint-valued result column", async () => {
await withDebugController(appCommands, async (controller) => {
await selectForQuickEval(quickEvalBigIntQueryPath, 4, 23, 4, 37);
// Don't specify a query path, so we'll default to the active document ("QuickEvalBigIntQuery.ql")
await controller.startDebuggingSelection({});
await controller.expectLaunched();
const result = await controller.expectSucceeded();
expect(result.started.quickEvalContext).toBeDefined();
expect(result.started.quickEvalContext!.quickEvalText).toBe(
"getBigIntValue",
);
expect(result.results.queryTargets.length).toBe(1);
expect(result.results.queryTargets[0].quickEvalPosition).toBeDefined();
expect(await getResultCount(result.results, cli)).toBe(8);
await controller.expectStopped();
});
});
it("should save dirty documents before launching a debug session", async () => {
await withDebugController(appCommands, async (controller) => {
const editor = await selectForQuickEval(quickEvalLibPath, 4, 15, 4, 32);
expect(editor.document.isDirty).toBe(false);
await controller.startDebuggingSelection({
query: quickEvalQueryPath, // The query context. This query extends the abstract class.
});
await controller.expectLaunched();
// Should have saved the dirty document.
expect(editor.document.isDirty).toBe(false);
await controller.expectSucceeded();
await controller.expectStopped();
await editor.edit((editBuilder) => {
editBuilder.insert(new Position(0, 0), "/* another comment */");
});
expect(editor.document.isDirty).toBe(true);
await controller.continueDebuggingSelection();
await controller.expectSucceeded();
await controller.expectStopped();
expect(editor.document.isDirty).toBe(false);
});
});
it("should pass additionalArgs through to query server", async () => {
await withDebugController(appCommands, async (controller) => {
await controller.startDebugging(
{
query: quickEvalQueryPath,
additionalRunQueryArgs: {
// Overrides the value passed to the query server
queryPath: simpleQueryPath,
},
},
true,
);
await controller.expectLaunched();
const result = await controller.expectSucceeded();
await controller.expectExited();
await controller.expectTerminated();
await controller.expectSessionClosed();
// Expect the number of results to be the same as if we had run the simple query, not the quick eval query.
expect(await getResultCount(result.results, cli)).toBe(2);
});
});
});