Skip to content

Commit 1a10fd3

Browse files
committed
Remove calls to fail
Instead of calling `fail`, we can just let the error be caught by Jest, which will automatically fail the tests. For other instances where we're calling `fail` in case an error was not thrown, we will instead use `.rejects.toThrow`.
1 parent 614785f commit 1a10fd3

13 files changed

Lines changed: 276 additions & 382 deletions

extensions/ql-vscode/src/vscode-tests/cli-integration/databases.test.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,27 @@ describe("Databases", () => {
2929
jest.spyOn(window, "showInformationMessage").mockResolvedValue(undefined);
3030

3131
beforeEach(async () => {
32-
try {
33-
inputBoxStub.mockReset().mockResolvedValue(undefined);
34-
progressCallback.mockReset();
32+
inputBoxStub.mockReset().mockResolvedValue(undefined);
33+
progressCallback.mockReset();
3534

36-
const extension = await extensions
37-
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
38-
"GitHub.vscode-codeql",
39-
)!
40-
.activate();
41-
if ("databaseManager" in extension) {
42-
databaseManager = extension.databaseManager;
43-
} else {
44-
throw new Error(
45-
"Extension not initialized. Make sure cli is downloaded and installed properly.",
46-
);
47-
}
48-
49-
await cleanDatabases(databaseManager);
50-
} catch (e) {
51-
fail(e as Error);
35+
const extension = await extensions
36+
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
37+
"GitHub.vscode-codeql",
38+
)!
39+
.activate();
40+
if ("databaseManager" in extension) {
41+
databaseManager = extension.databaseManager;
42+
} else {
43+
throw new Error(
44+
"Extension not initialized. Make sure cli is downloaded and installed properly.",
45+
);
5246
}
47+
48+
await cleanDatabases(databaseManager);
5349
});
5450

5551
afterEach(async () => {
56-
try {
57-
await cleanDatabases(databaseManager);
58-
} catch (e) {
59-
fail(e as Error);
60-
}
52+
await cleanDatabases(databaseManager);
6153
});
6254

6355
it("should add a database from a folder", async () => {

extensions/ql-vscode/src/vscode-tests/cli-integration/jest.setup.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,18 @@ beforeAll(async () => {
2626
if (!fs.existsSync(dbLoc)) {
2727
console.log(`Downloading test database to ${dbLoc}`);
2828

29-
try {
30-
await new Promise((resolve, reject) => {
31-
return fetch(DB_URL).then((response) => {
32-
const dest = fs.createWriteStream(dbLoc);
33-
response.body.pipe(dest);
29+
await new Promise((resolve, reject) => {
30+
return fetch(DB_URL).then((response) => {
31+
const dest = fs.createWriteStream(dbLoc);
32+
response.body.pipe(dest);
3433

35-
response.body.on("error", reject);
36-
dest.on("error", reject);
37-
dest.on("close", () => {
38-
resolve(dbLoc);
39-
});
34+
response.body.on("error", reject);
35+
dest.on("error", reject);
36+
dest.on("close", () => {
37+
resolve(dbLoc);
4038
});
4139
});
42-
} catch (e) {
43-
fail("Failed to download test database: " + e);
44-
}
40+
});
4541
}
4642

4743
// Create the temp directory to be used as extension local storage.
@@ -59,14 +55,14 @@ beforeAll(async () => {
5955
// check that the codeql folder is found in the workspace
6056
const folders = workspace.workspaceFolders;
6157
if (!folders) {
62-
fail(
63-
'\n\n\nNo workspace folders found.\nYou will need a local copy of the codeql repo.\nMake sure you specify the path to it in launch.json.\nIt should be something along the lines of "${workspaceRoot}/../codeql" depending on where you have your local copy of the codeql repo.\n\n\n',
58+
throw new Error(
59+
'No workspace folders found.\nYou will need a local copy of the codeql repo.\nMake sure you specify the path to it in launch.json.\nIt should be something along the lines of "${workspaceRoot}/../codeql" depending on where you have your local copy of the codeql repo.',
6460
);
6561
} else {
6662
const codeqlFolder = folders.find((folder) => folder.name === "codeql");
6763
if (!codeqlFolder) {
68-
fail(
69-
'\n\n\nNo workspace folders found.\nYou will need a local copy of the codeql repo.\nMake sure you specify the path to it in launch.json.\nIt should be something along the lines of "${workspaceRoot}/../codeql" depending on where you have your local copy of the codeql repo.\n\n\n',
64+
throw new Error(
65+
'No workspace folders found.\nYou will need a local copy of the codeql repo.\nMake sure you specify the path to it in launch.json.\nIt should be something along the lines of "${workspaceRoot}/../codeql" depending on where you have your local copy of the codeql repo.\n\n\n',
7066
);
7167
}
7268
}

extensions/ql-vscode/src/vscode-tests/cli-integration/legacy-query.test.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as cli from "../../cli";
99
import { CellValue } from "../../pure/bqrs-cli-types";
1010
import { extensions } from "vscode";
1111
import { CodeQLExtensionInterface } from "../../extension";
12-
import { fail } from "assert";
1312
import { describeWithCodeQL } from "../cli";
1413
import { QueryServerClient } from "../../legacy-query-server/queryserver-client";
1514
import { logger, ProgressReporter } from "../../logging";
@@ -112,43 +111,39 @@ describeWithCodeQL()("using the legacy query server", () => {
112111
let cliServer: cli.CodeQLCliServer;
113112

114113
beforeAll(async () => {
115-
try {
116-
const extension = await extensions
117-
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
118-
"GitHub.vscode-codeql",
119-
)!
120-
.activate();
121-
if ("cliServer" in extension) {
122-
cliServer = extension.cliServer;
123-
cliServer.quiet = true;
114+
const extension = await extensions
115+
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
116+
"GitHub.vscode-codeql",
117+
)!
118+
.activate();
119+
if ("cliServer" in extension) {
120+
cliServer = extension.cliServer;
121+
cliServer.quiet = true;
124122

125-
qs = new QueryServerClient(
126-
{
127-
codeQlPath:
128-
(await extension.distributionManager.getCodeQlPathWithoutVersionCheck()) ||
129-
"",
130-
debug: false,
131-
cacheSize: 0,
132-
numThreads: 1,
133-
saveCache: false,
134-
timeoutSecs: 0,
135-
},
136-
cliServer,
137-
{
138-
contextStoragePath: tmpDir.name,
139-
logger,
140-
},
141-
(task) =>
142-
task(nullProgressReporter, new CancellationTokenSource().token),
143-
);
144-
await qs.startQueryServer();
145-
} else {
146-
throw new Error(
147-
"Extension not initialized. Make sure cli is downloaded and installed properly.",
148-
);
149-
}
150-
} catch (e) {
151-
fail(e as Error);
123+
qs = new QueryServerClient(
124+
{
125+
codeQlPath:
126+
(await extension.distributionManager.getCodeQlPathWithoutVersionCheck()) ||
127+
"",
128+
debug: false,
129+
cacheSize: 0,
130+
numThreads: 1,
131+
saveCache: false,
132+
timeoutSecs: 0,
133+
},
134+
cliServer,
135+
{
136+
contextStoragePath: tmpDir.name,
137+
logger,
138+
},
139+
(task) =>
140+
task(nullProgressReporter, new CancellationTokenSource().token),
141+
);
142+
await qs.startQueryServer();
143+
} else {
144+
throw new Error(
145+
"Extension not initialized. Make sure cli is downloaded and installed properly.",
146+
);
152147
}
153148
});
154149

extensions/ql-vscode/src/vscode-tests/cli-integration/new-query.test.ts

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as cli from "../../cli";
77
import { CellValue } from "../../pure/bqrs-cli-types";
88
import { extensions, Uri } from "vscode";
99
import { CodeQLExtensionInterface } from "../../extension";
10-
import { fail } from "assert";
1110
import { describeWithCodeQL } from "../cli";
1211
import { QueryServerClient } from "../../query-server/queryserver-client";
1312
import { logger, ProgressReporter } from "../../logging";
@@ -113,67 +112,61 @@ describeWithCodeQL()("using the new query server", () => {
113112
let cliServer: cli.CodeQLCliServer;
114113
let db: string;
115114
beforeAll(async () => {
116-
try {
117-
const extension = await extensions
118-
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
119-
"GitHub.vscode-codeql",
120-
)!
121-
.activate();
122-
if ("cliServer" in extension && "databaseManager" in extension) {
123-
cliServer = extension.cliServer;
124-
125-
cliServer.quiet = true;
126-
if (
127-
!(await cliServer.cliConstraints.supportsNewQueryServerForTests())
128-
) {
129-
testContext.ctx.skip();
130-
}
131-
qs = new QueryServerClient(
132-
{
133-
codeQlPath:
134-
(await extension.distributionManager.getCodeQlPathWithoutVersionCheck()) ||
135-
"",
136-
debug: false,
137-
cacheSize: 0,
138-
numThreads: 1,
139-
saveCache: false,
140-
timeoutSecs: 0,
141-
},
142-
cliServer,
143-
{
144-
contextStoragePath: tmpDir.name,
145-
logger,
146-
},
147-
(task) =>
148-
task(nullProgressReporter, new CancellationTokenSource().token),
149-
);
150-
await qs.startQueryServer();
151-
152-
// Unlike the old query sevre the new one wants a database and the empty direcrtory is not valid.
153-
// Add a database, but make sure the database manager is empty first
154-
await cleanDatabases(extension.databaseManager);
155-
const uri = Uri.file(dbLoc);
156-
const maybeDbItem = await importArchiveDatabase(
157-
uri.toString(true),
158-
extension.databaseManager,
159-
storagePath,
160-
() => {
161-
/**ignore progress */
162-
},
163-
token,
164-
);
115+
const extension = await extensions
116+
.getExtension<CodeQLExtensionInterface | Record<string, never>>(
117+
"GitHub.vscode-codeql",
118+
)!
119+
.activate();
120+
if ("cliServer" in extension && "databaseManager" in extension) {
121+
cliServer = extension.cliServer;
122+
123+
cliServer.quiet = true;
124+
if (!(await cliServer.cliConstraints.supportsNewQueryServerForTests())) {
125+
testContext.ctx.skip();
126+
}
127+
qs = new QueryServerClient(
128+
{
129+
codeQlPath:
130+
(await extension.distributionManager.getCodeQlPathWithoutVersionCheck()) ||
131+
"",
132+
debug: false,
133+
cacheSize: 0,
134+
numThreads: 1,
135+
saveCache: false,
136+
timeoutSecs: 0,
137+
},
138+
cliServer,
139+
{
140+
contextStoragePath: tmpDir.name,
141+
logger,
142+
},
143+
(task) =>
144+
task(nullProgressReporter, new CancellationTokenSource().token),
145+
);
146+
await qs.startQueryServer();
147+
148+
// Unlike the old query sevre the new one wants a database and the empty direcrtory is not valid.
149+
// Add a database, but make sure the database manager is empty first
150+
await cleanDatabases(extension.databaseManager);
151+
const uri = Uri.file(dbLoc);
152+
const maybeDbItem = await importArchiveDatabase(
153+
uri.toString(true),
154+
extension.databaseManager,
155+
storagePath,
156+
() => {
157+
/**ignore progress */
158+
},
159+
token,
160+
);
165161

166-
if (!maybeDbItem) {
167-
throw new Error("Could not import database");
168-
}
169-
db = maybeDbItem.databaseUri.fsPath;
170-
} else {
171-
throw new Error(
172-
"Extension not initialized. Make sure cli is downloaded and installed properly.",
173-
);
162+
if (!maybeDbItem) {
163+
throw new Error("Could not import database");
174164
}
175-
} catch (e) {
176-
fail(e as Error);
165+
db = maybeDbItem.databaseUri.fsPath;
166+
} else {
167+
throw new Error(
168+
"Extension not initialized. Make sure cli is downloaded and installed properly.",
169+
);
177170
}
178171
});
179172

0 commit comments

Comments
 (0)