Skip to content

Commit 8c3fbb8

Browse files
author
Dave Bartolomeo
committed
Fix test code
1 parent 4ddf1c2 commit 8c3fbb8

6 files changed

Lines changed: 108 additions & 100 deletions

File tree

extensions/ql-vscode/src/legacy-query-server/run-queries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { redactableError } from "../pure/errors";
2727
import { CoreQueryResults, CoreQueryTarget } from "../queryRunner";
2828
import { Position } from "../pure/messages-shared";
2929

30-
async function compileQuery(
30+
export async function compileQuery(
3131
qs: qsClient.QueryServerClient,
3232
program: messages.QlProgram,
3333
quickEvalPosition: Position | undefined,
@@ -185,7 +185,7 @@ export class QueryInProgress {
185185
readonly querySaveDir: string,
186186
readonly dbItemPath: string,
187187
databaseHasMetadataFile: boolean,
188-
readonly queryDbscheme: string, // the dbscheme file the query expects, based on library path resolution
188+
readonly queryDbscheme: string, // the dbscheme file the query expects, ba`sed on library path resolution
189189
readonly quickEvalPosition?: messages.Position,
190190
readonly metadata?: QueryMetadata,
191191
readonly templates?: Record<string, string>,

extensions/ql-vscode/src/local-queries.ts

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import {
2222
tryGetQueryMetadata,
2323
} from "./helpers";
2424
import { displayQuickQuery } from "./quick-query";
25-
import { CoreQueryResults, QueryRunner } from "./queryRunner";
25+
import {
26+
CoreCompletedQuery,
27+
CoreQueryResults,
28+
QueryRunner,
29+
} from "./queryRunner";
2630
import { QueryHistoryManager } from "./query-history/query-history-manager";
2731
import { DatabaseUI } from "./local-databases-ui";
2832
import { ResultsView } from "./interface";
@@ -406,64 +410,82 @@ export class LocalQueries extends DisposableObject {
406410
databaseItem: DatabaseItem | undefined,
407411
range?: Range,
408412
): Promise<void> {
409-
if (this.queryRunner !== undefined) {
410-
const selectedQuery = await determineSelectedQuery(
411-
queryUri,
412-
quickEval,
413-
range,
414-
);
413+
await this.compileAndRunQueryInternal(
414+
quickEval,
415+
queryUri,
416+
progress,
417+
token,
418+
databaseItem,
419+
range,
420+
);
421+
}
415422

416-
// If no databaseItem is specified, use the database currently selected in the Databases UI
417-
databaseItem =
418-
databaseItem ||
419-
(await this.databaseUI.getDatabaseItem(progress, token));
420-
if (databaseItem === undefined) {
421-
throw new Error("Can't run query without a selected database");
422-
}
423+
/** Used by tests */
424+
public async compileAndRunQueryInternal(
425+
quickEval: boolean,
426+
queryUri: Uri | undefined,
427+
progress: ProgressCallback,
428+
token: CancellationToken,
429+
databaseItem: DatabaseItem | undefined,
430+
range?: Range,
431+
): Promise<CoreCompletedQuery> {
432+
const selectedQuery = await determineSelectedQuery(
433+
queryUri,
434+
quickEval,
435+
range,
436+
);
423437

424-
const coreQueryRun = this.queryRunner.createQueryRun(
425-
databaseItem.databaseUri.fsPath,
426-
{
427-
queryPath: selectedQuery.queryPath,
428-
quickEvalPosition: selectedQuery.quickEvalPosition,
429-
},
430-
true,
431-
getOnDiskWorkspaceFolders(),
432-
this.queryStorageDir,
433-
undefined,
434-
undefined,
438+
// If no databaseItem is specified, use the database currently selected in the Databases UI
439+
databaseItem =
440+
databaseItem || (await this.databaseUI.getDatabaseItem(progress, token));
441+
if (databaseItem === undefined) {
442+
throw new Error("Can't run query without a selected database");
443+
}
444+
445+
const coreQueryRun = this.queryRunner.createQueryRun(
446+
databaseItem.databaseUri.fsPath,
447+
{
448+
queryPath: selectedQuery.queryPath,
449+
quickEvalPosition: selectedQuery.quickEvalPosition,
450+
},
451+
true,
452+
getOnDiskWorkspaceFolders(),
453+
this.queryStorageDir,
454+
undefined,
455+
undefined,
456+
);
457+
458+
// handle cancellation from the history view.
459+
const source = new CancellationTokenSource();
460+
try {
461+
token.onCancellationRequested(() => source.cancel());
462+
463+
const localQueryRun = await this.createLocalQueryRun(
464+
selectedQuery,
465+
databaseItem,
466+
coreQueryRun.outputDir,
467+
source,
435468
);
436469

437-
// handle cancellation from the history view.
438-
const source = new CancellationTokenSource();
439470
try {
440-
token.onCancellationRequested(() => source.cancel());
441-
442-
const localQueryRun = await this.createLocalQueryRun(
443-
selectedQuery,
444-
databaseItem,
445-
coreQueryRun.outputDir,
446-
source,
471+
const results = await coreQueryRun.evaluate(
472+
progress,
473+
source.token,
474+
localQueryRun.logger,
447475
);
448476

449-
try {
450-
const results = await coreQueryRun.evaluate(
451-
progress,
452-
source.token,
453-
localQueryRun.logger,
454-
);
477+
await localQueryRun.complete(results);
455478

456-
await localQueryRun.complete(results);
457-
} catch (e) {
458-
const err = asError(e);
459-
err.message = `Error running query: ${err.message}`;
460-
localQueryRun.queryInfo.failureReason = err.message;
461-
throw e;
462-
}
463-
} finally {
464-
await this.queryHistoryManager.refreshTreeView();
465-
source.dispose();
479+
return results;
480+
} catch (e) {
481+
const err = asError(e);
482+
err.message = `Error running query: ${err.message}`;
483+
localQueryRun.queryInfo.failureReason = err.message;
484+
throw e;
466485
}
486+
} finally {
487+
await this.queryHistoryManager.refreshTreeView();
488+
source.dispose();
467489
}
468490
}
469491

extensions/ql-vscode/test/vscode-tests/cli-integration/queries.test.ts

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import {
1919
import { importArchiveDatabase } from "../../../src/databaseFetcher";
2020
import { CliVersionConstraint, CodeQLCliServer } from "../../../src/cli";
2121
import { describeWithCodeQL } from "../cli";
22-
import { tmpDir } from "../../../src/helpers";
23-
import { createInitialQueryInfo } from "../../../src/run-queries-shared";
2422
import { QueryRunner } from "../../../src/queryRunner";
25-
import { CompletedQueryInfo } from "../../../src/query-results";
2623
import { SELECT_QUERY_NAME } from "../../../src/contextual/locationFinder";
2724
import { createMockCommandManager } from "../../__mocks__/commandsMock";
2825
import { LocalQueries } from "../../../src/local-queries";
26+
import { QueryResultType } from "../../../src/pure/new-messages";
2927

3028
jest.setTimeout(20_000);
3129

@@ -134,22 +132,21 @@ describeWithCodeQL()("Queries", () => {
134132
}
135133

136134
async function runQueryWithExtensions() {
137-
const result = new CompletedQueryInfo(
138-
await localQueries.compileAndRunQueryAgainstDatabase(
139-
dbItem,
140-
await mockInitialQueryInfo(queryUsingExtensionPath),
141-
join(tmpDir.name, "mock-storage-path"),
142-
progress,
143-
token,
144-
),
135+
const result = await localQueries.compileAndRunQueryInternal(
136+
false,
137+
Uri.file(queryUsingExtensionPath),
138+
progress,
139+
token,
140+
dbItem,
141+
undefined,
145142
);
146143

147144
// Check that query was successful
148-
expect(result.successful).toBe(true);
145+
expect(result.resultType).toBe(QueryResultType.SUCCESS);
149146

150147
// Load query results
151148
const chunk = await qs.cliServer.bqrsDecode(
152-
result.getResultsPath(SELECT_QUERY_NAME, true),
149+
result.outputDir.bqrsPath,
153150
SELECT_QUERY_NAME,
154151
{
155152
// there should only be one result
@@ -165,31 +162,33 @@ describeWithCodeQL()("Queries", () => {
165162

166163
it("should run a query", async () => {
167164
const queryPath = join(__dirname, "data", "simple-query.ql");
168-
const result = localQueries.compileAndRunQueryAgainstDatabase(
169-
dbItem,
170-
await mockInitialQueryInfo(queryPath),
171-
join(tmpDir.name, "mock-storage-path"),
165+
const result = await localQueries.compileAndRunQueryInternal(
166+
false,
167+
Uri.file(queryPath),
172168
progress,
173169
token,
170+
dbItem,
171+
undefined,
174172
);
175173

176174
// just check that the query was successful
177-
expect((await result).successful).toBe(true);
175+
expect(result.resultType).toBe(QueryResultType.SUCCESS);
178176
});
179177

180178
// Asserts a fix for bug https://github.com/github/vscode-codeql/issues/733
181179
it("should restart the database and run a query", async () => {
182180
await commands.executeCommand("codeQL.restartQueryServer");
183181
const queryPath = join(__dirname, "data", "simple-query.ql");
184-
const result = await localQueries.compileAndRunQueryAgainstDatabase(
185-
dbItem,
186-
await mockInitialQueryInfo(queryPath),
187-
join(tmpDir.name, "mock-storage-path"),
182+
const result = await localQueries.compileAndRunQueryInternal(
183+
false,
184+
Uri.file(queryPath),
188185
progress,
189186
token,
187+
dbItem,
188+
undefined,
190189
);
191190

192-
expect(result.successful).toBe(true);
191+
expect(result.resultType).toBe(QueryResultType.SUCCESS);
193192
});
194193

195194
it("should create a quick query", async () => {
@@ -239,15 +238,4 @@ describeWithCodeQL()("Queries", () => {
239238
// ignore
240239
}
241240
}
242-
243-
async function mockInitialQueryInfo(queryPath: string) {
244-
return await createInitialQueryInfo(
245-
Uri.file(queryPath),
246-
{
247-
name: dbItem.name,
248-
databaseUri: dbItem.databaseUri.toString(),
249-
},
250-
false,
251-
);
252-
}
253241
});

extensions/ql-vscode/test/vscode-tests/minimal-workspace/local-databases.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CancellationToken, ExtensionContext, Uri, workspace } from "vscode";
55

66
import {
77
DatabaseContents,
8+
DatabaseContentsWithDbScheme,
89
DatabaseEventKind,
910
DatabaseItemImpl,
1011
DatabaseManager,
@@ -687,7 +688,7 @@ describe("local databases", () => {
687688

688689
resolveDatabaseContentsSpy = jest
689690
.spyOn(DatabaseResolver, "resolveDatabaseContents")
690-
.mockResolvedValue({} as DatabaseContents);
691+
.mockResolvedValue({} as DatabaseContentsWithDbScheme);
691692

692693
addDatabaseSourceArchiveFolderSpy = jest.spyOn(
693694
databaseManager,

extensions/ql-vscode/test/vscode-tests/no-workspace/contextual/astBuilder.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readFileSync } from "fs-extra";
33
import AstBuilder from "../../../../src/contextual/astBuilder";
44
import { CodeQLCliServer } from "../../../../src/cli";
55
import { Uri } from "vscode";
6-
import { QueryWithResults } from "../../../../src/run-queries-shared";
6+
import { QueryOutputDir } from "../../../../src/run-queries-shared";
77
import { mockDatabaseItem, mockedObject } from "../../utils/mocking.helpers";
88

99
/**
@@ -137,13 +137,7 @@ describe("AstBuilder", () => {
137137

138138
function createAstBuilder() {
139139
return new AstBuilder(
140-
{
141-
query: {
142-
resultsPaths: {
143-
resultsPath: "/a/b/c",
144-
},
145-
},
146-
} as QueryWithResults,
140+
new QueryOutputDir("/a/b/c"),
147141
mockCli,
148142
mockDatabaseItem({
149143
resolveSourceFile: undefined,

extensions/ql-vscode/test/vscode-tests/no-workspace/run-queries.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { tmpDir } from "../../../src/helpers";
1313
import { QueryServerClient } from "../../../src/legacy-query-server/queryserver-client";
1414
import { CodeQLCliServer } from "../../../src/cli";
1515
import { SELECT_QUERY_NAME } from "../../../src/contextual/locationFinder";
16-
import { QueryInProgress } from "../../../src/legacy-query-server/run-queries";
16+
import {
17+
QueryInProgress,
18+
compileQuery as compileQueryLegacy,
19+
} from "../../../src/legacy-query-server/run-queries";
1720
import { LegacyQueryRunner } from "../../../src/legacy-query-server/legacyRunner";
1821
import { DatabaseItem } from "../../../src/local-databases";
1922
import { DeepPartial, mockedObject } from "../utils/mocking.helpers";
@@ -30,7 +33,6 @@ describe("run-queries", () => {
3033
const saveDir = "query-save-dir";
3134
const info = createMockQueryInfo(true, saveDir);
3235

33-
expect(info.compiledQueryPath).toBe(join(saveDir, "compiledQuery.qlo"));
3436
expect(info.queryEvalInfo.dilPath).toBe(join(saveDir, "results.dil"));
3537
expect(info.queryEvalInfo.resultsPaths.resultsPath).toBe(
3638
join(saveDir, "results.bqrs"),
@@ -185,14 +187,15 @@ describe("run-queries", () => {
185187
queryPath: "",
186188
};
187189

188-
const results = await info.compile(
190+
const results = await compileQueryLegacy(
189191
qs as any,
190192
mockQlProgram,
193+
undefined,
194+
info.queryEvalInfo,
191195
mockProgress as any,
192196
mockCancel as any,
193197
qs.logger,
194198
);
195-
196199
expect(results).toEqual([{ message: "err", severity: Severity.ERROR }]);
197200

198201
expect(qs.sendRequest).toHaveBeenCalledTimes(1);
@@ -214,7 +217,7 @@ describe("run-queries", () => {
214217
timeoutSecs: 5,
215218
},
216219
queryToCheck: mockQlProgram,
217-
resultPath: info.compiledQueryPath,
220+
resultPath: info.queryEvalInfo.compileQueryPath,
218221
target: { query: {} },
219222
},
220223
mockCancel,

0 commit comments

Comments
 (0)