Skip to content

Commit 14eb6b4

Browse files
committed
Only download test DB for CLI integration tests
The test database is not required for activated extension tests, so we only need to download the test database for CLI integration tests.
1 parent 3917b14 commit 14eb6b4

File tree

3 files changed

+87
-65
lines changed

3 files changed

+87
-65
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
import "../jest.activated-extension.setup";
1+
import {
2+
beforeAllAction,
3+
beforeEachAction,
4+
} from "../jest.activated-extension.setup";
5+
6+
beforeAll(async () => {
7+
await beforeAllAction();
8+
});
9+
10+
beforeEach(async () => {
11+
await beforeEachAction();
12+
});

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
11
import { workspace } from "vscode";
22

3-
import "../jest.activated-extension.setup";
3+
import {
4+
beforeAllAction,
5+
beforeEachAction,
6+
} from "../jest.activated-extension.setup";
7+
import * as tmp from "tmp";
8+
import {
9+
createWriteStream,
10+
existsSync,
11+
mkdirpSync,
12+
realpathSync,
13+
} from "fs-extra";
14+
import { dirname } from "path";
15+
import { DB_URL, dbLoc, setStoragePath, storagePath } from "../global.helper";
16+
import fetch from "node-fetch";
17+
18+
// create an extension storage location
19+
let removeStorage: tmp.DirResult["removeCallback"] | undefined;
20+
21+
beforeAll(async () => {
22+
// ensure the test database is downloaded
23+
mkdirpSync(dirname(dbLoc));
24+
if (!existsSync(dbLoc)) {
25+
console.log(`Downloading test database to ${dbLoc}`);
26+
27+
await new Promise((resolve, reject) => {
28+
return fetch(DB_URL).then((response) => {
29+
const dest = createWriteStream(dbLoc);
30+
response.body.pipe(dest);
31+
32+
response.body.on("error", reject);
33+
dest.on("error", reject);
34+
dest.on("close", () => {
35+
resolve(dbLoc);
36+
});
37+
});
38+
});
39+
}
40+
41+
// Create the temp directory to be used as extension local storage.
42+
const dir = tmp.dirSync();
43+
let storagePath = realpathSync(dir.name);
44+
if (storagePath.substring(0, 2).match(/[A-Z]:/)) {
45+
storagePath =
46+
storagePath.substring(0, 1).toLocaleLowerCase() +
47+
storagePath.substring(1);
48+
}
49+
setStoragePath(storagePath);
50+
51+
removeStorage = dir.removeCallback;
52+
53+
await beforeAllAction();
54+
});
55+
56+
beforeEach(async () => {
57+
await beforeEachAction();
58+
});
459

560
beforeAll(() => {
661
// check that the codeql folder is found in the workspace
@@ -20,3 +75,15 @@ beforeAll(() => {
2075
}
2176
}
2277
});
78+
79+
// ensure extension is cleaned up.
80+
afterAll(async () => {
81+
// ensure temp directory is cleaned up.
82+
try {
83+
removeStorage?.();
84+
} catch (e) {
85+
// we are exiting anyway so don't worry about it.
86+
// most likely the directory this is a test on Windows and some files are locked.
87+
console.warn(`Failed to remove storage directory '${storagePath}': ${e}`);
88+
}
89+
});
Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,26 @@
1-
import {
2-
mkdirpSync,
3-
existsSync,
4-
createWriteStream,
5-
realpathSync,
6-
} from "fs-extra";
7-
import { dirname } from "path";
8-
import fetch from "node-fetch";
9-
import { DB_URL, dbLoc, setStoragePath, storagePath } from "./global.helper";
10-
import * as tmp from "tmp";
111
import { CUSTOM_CODEQL_PATH_SETTING } from "../../src/config";
122
import { ConfigurationTarget, env, extensions } from "vscode";
13-
import { beforeEachAction } from "./test-config";
3+
import { beforeEachAction as testConfigBeforeEachAction } from "./test-config";
144

15-
// create an extension storage location
16-
let removeStorage: tmp.DirResult["removeCallback"] | undefined;
17-
18-
beforeAll(async () => {
5+
export async function beforeAllAction() {
196
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
20-
await beforeEachAction();
7+
await testConfigBeforeEachAction();
218
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
229
process.env.CLI_PATH,
2310
ConfigurationTarget.Workspace,
2411
);
2512

26-
// ensure the test database is downloaded
27-
mkdirpSync(dirname(dbLoc));
28-
if (!existsSync(dbLoc)) {
29-
console.log(`Downloading test database to ${dbLoc}`);
30-
31-
await new Promise((resolve, reject) => {
32-
return fetch(DB_URL).then((response) => {
33-
const dest = createWriteStream(dbLoc);
34-
response.body.pipe(dest);
35-
36-
response.body.on("error", reject);
37-
dest.on("error", reject);
38-
dest.on("close", () => {
39-
resolve(dbLoc);
40-
});
41-
});
42-
});
43-
}
44-
45-
// Create the temp directory to be used as extension local storage.
46-
const dir = tmp.dirSync();
47-
let storagePath = realpathSync(dir.name);
48-
if (storagePath.substring(0, 2).match(/[A-Z]:/)) {
49-
storagePath =
50-
storagePath.substring(0, 1).toLocaleLowerCase() +
51-
storagePath.substring(1);
52-
}
53-
setStoragePath(storagePath);
54-
55-
removeStorage = dir.removeCallback;
56-
5713
// Activate the extension
5814
await extensions.getExtension("GitHub.vscode-codeql")?.activate();
59-
});
15+
}
6016

61-
beforeEach(async () => {
17+
export async function beforeEachAction() {
6218
jest.spyOn(env, "openExternal").mockResolvedValue(false);
6319

64-
await beforeEachAction();
20+
await testConfigBeforeEachAction();
6521

6622
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
6723
process.env.CLI_PATH,
6824
ConfigurationTarget.Workspace,
6925
);
70-
});
71-
72-
// ensure extension is cleaned up.
73-
afterAll(async () => {
74-
// ensure temp directory is cleaned up.
75-
try {
76-
removeStorage?.();
77-
} catch (e) {
78-
// we are exiting anyway so don't worry about it.
79-
// most likely the directory this is a test on Windows and some files are locked.
80-
console.warn(`Failed to remove storage directory '${storagePath}': ${e}`);
81-
}
82-
});
26+
}

0 commit comments

Comments
 (0)