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.
This commit is contained in:
Koen Vlaswinkel
2023-02-13 11:37:11 +01:00
parent 3917b14a58
commit 14eb6b4f89
3 changed files with 87 additions and 65 deletions

View File

@@ -1 +1,12 @@
import "../jest.activated-extension.setup";
import {
beforeAllAction,
beforeEachAction,
} from "../jest.activated-extension.setup";
beforeAll(async () => {
await beforeAllAction();
});
beforeEach(async () => {
await beforeEachAction();
});

View File

@@ -1,6 +1,61 @@
import { workspace } from "vscode";
import "../jest.activated-extension.setup";
import {
beforeAllAction,
beforeEachAction,
} from "../jest.activated-extension.setup";
import * as tmp from "tmp";
import {
createWriteStream,
existsSync,
mkdirpSync,
realpathSync,
} from "fs-extra";
import { dirname } from "path";
import { DB_URL, dbLoc, setStoragePath, storagePath } from "../global.helper";
import fetch from "node-fetch";
// create an extension storage location
let removeStorage: tmp.DirResult["removeCallback"] | undefined;
beforeAll(async () => {
// ensure the test database is downloaded
mkdirpSync(dirname(dbLoc));
if (!existsSync(dbLoc)) {
console.log(`Downloading test database to ${dbLoc}`);
await new Promise((resolve, reject) => {
return fetch(DB_URL).then((response) => {
const dest = createWriteStream(dbLoc);
response.body.pipe(dest);
response.body.on("error", reject);
dest.on("error", reject);
dest.on("close", () => {
resolve(dbLoc);
});
});
});
}
// Create the temp directory to be used as extension local storage.
const dir = tmp.dirSync();
let storagePath = realpathSync(dir.name);
if (storagePath.substring(0, 2).match(/[A-Z]:/)) {
storagePath =
storagePath.substring(0, 1).toLocaleLowerCase() +
storagePath.substring(1);
}
setStoragePath(storagePath);
removeStorage = dir.removeCallback;
await beforeAllAction();
});
beforeEach(async () => {
await beforeEachAction();
});
beforeAll(() => {
// check that the codeql folder is found in the workspace
@@ -20,3 +75,15 @@ beforeAll(() => {
}
}
});
// ensure extension is cleaned up.
afterAll(async () => {
// ensure temp directory is cleaned up.
try {
removeStorage?.();
} catch (e) {
// we are exiting anyway so don't worry about it.
// most likely the directory this is a test on Windows and some files are locked.
console.warn(`Failed to remove storage directory '${storagePath}': ${e}`);
}
});

View File

@@ -1,82 +1,26 @@
import {
mkdirpSync,
existsSync,
createWriteStream,
realpathSync,
} from "fs-extra";
import { dirname } from "path";
import fetch from "node-fetch";
import { DB_URL, dbLoc, setStoragePath, storagePath } from "./global.helper";
import * as tmp from "tmp";
import { CUSTOM_CODEQL_PATH_SETTING } from "../../src/config";
import { ConfigurationTarget, env, extensions } from "vscode";
import { beforeEachAction } from "./test-config";
import { beforeEachAction as testConfigBeforeEachAction } from "./test-config";
// create an extension storage location
let removeStorage: tmp.DirResult["removeCallback"] | undefined;
beforeAll(async () => {
export async function beforeAllAction() {
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
await beforeEachAction();
await testConfigBeforeEachAction();
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
process.env.CLI_PATH,
ConfigurationTarget.Workspace,
);
// ensure the test database is downloaded
mkdirpSync(dirname(dbLoc));
if (!existsSync(dbLoc)) {
console.log(`Downloading test database to ${dbLoc}`);
await new Promise((resolve, reject) => {
return fetch(DB_URL).then((response) => {
const dest = createWriteStream(dbLoc);
response.body.pipe(dest);
response.body.on("error", reject);
dest.on("error", reject);
dest.on("close", () => {
resolve(dbLoc);
});
});
});
}
// Create the temp directory to be used as extension local storage.
const dir = tmp.dirSync();
let storagePath = realpathSync(dir.name);
if (storagePath.substring(0, 2).match(/[A-Z]:/)) {
storagePath =
storagePath.substring(0, 1).toLocaleLowerCase() +
storagePath.substring(1);
}
setStoragePath(storagePath);
removeStorage = dir.removeCallback;
// Activate the extension
await extensions.getExtension("GitHub.vscode-codeql")?.activate();
});
}
beforeEach(async () => {
export async function beforeEachAction() {
jest.spyOn(env, "openExternal").mockResolvedValue(false);
await beforeEachAction();
await testConfigBeforeEachAction();
await CUSTOM_CODEQL_PATH_SETTING.updateValue(
process.env.CLI_PATH,
ConfigurationTarget.Workspace,
);
});
// ensure extension is cleaned up.
afterAll(async () => {
// ensure temp directory is cleaned up.
try {
removeStorage?.();
} catch (e) {
// we are exiting anyway so don't worry about it.
// most likely the directory this is a test on Windows and some files are locked.
console.warn(`Failed to remove storage directory '${storagePath}': ${e}`);
}
});
}