Files
vscode-codeql/extensions/ql-vscode/src/vscode-tests/cli-integration/global.helper.ts
Andrew Eisenberg 351db4efc8 Fix cli-integration tests
The main problem this commit fixes is with vscode 1.67.0, an error is
thrown when inside of integration tests and a dialog box is opened. We
were opening the telemetry dialog box. Now, an env variable is set
during cli-integration tests that prevents the dialog from being
opened.

There are also other cleanups and improvements with cli-integration
tests that assist with running locally:

- `vscode-test` dependency has been renamed to `@vscode/test-electron`,
  so use that instead and make the small API changes to support it.
- Commit the codeql-pack.lock.yml file so it isn't recreated on each
  test run.
- Ensure all databases are removed before _and after_ each test run
  that manipulates the set of installed databases
- Similarly, for quick query files, delete them before and after each
  test.
- Change some async `forEach` blocks to for loops in order to support
  sequential operations more easily.
2022-05-09 13:50:28 -07:00

99 lines
3.3 KiB
TypeScript

import * as path from 'path';
import * as tmp from 'tmp';
import * as fs from 'fs-extra';
import fetch from 'node-fetch';
import { fail } from 'assert';
import { commands, ConfigurationTarget, extensions, workspace } from 'vscode';
import { CodeQLExtensionInterface } from '../../extension';
import { DatabaseManager } from '../../databases';
// This file contains helpers shared between actual tests.
export const DB_URL = 'https://github.com/github/vscode-codeql/files/5586722/simple-db.zip';
process.addListener('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
fail(String(reason));
});
// We need to resolve the path, but the final three segments won't exist until later, so we only resolve the
// first portion of the path.
export const dbLoc = path.join(fs.realpathSync(path.join(__dirname, '../../../')), 'build/tests/db.zip');
export let storagePath: string;
export default function(mocha: Mocha) {
// create an extension storage location
let removeStorage: tmp.DirResult['removeCallback'] | undefined;
// ensure the test database is downloaded
(mocha.options as any).globalSetup.push(
async () => {
fs.mkdirpSync(path.dirname(dbLoc));
if (!fs.existsSync(dbLoc)) {
try {
await new Promise((resolve, reject) => {
return fetch(DB_URL).then(response => {
const dest = fs.createWriteStream(dbLoc);
response.body.pipe(dest);
response.body.on('error', reject);
dest.on('error', reject);
dest.on('close', () => {
resolve(dbLoc);
});
});
});
} catch (e) {
fail('Failed to download test database: ' + e);
}
}
}
);
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
(mocha.options as any).globalSetup.push(
async () => {
await workspace.getConfiguration().update('codeQL.cli.executablePath', process.env.CLI_PATH, ConfigurationTarget.Global);
}
);
// Create the temp directory to be used as extension local storage.
(mocha.options as any).globalSetup.push(
() => {
const dir = tmp.dirSync();
storagePath = fs.realpathSync(dir.name);
if (storagePath.substring(0, 2).match(/[A-Z]:/)) {
storagePath = storagePath.substring(0, 1).toLocaleLowerCase() + storagePath.substring(1);
}
removeStorage = dir.removeCallback;
}
);
// ensure etension is cleaned up.
(mocha.options as any).globalTeardown.push(
async () => {
const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate();
// This shuts down the extension and can only be run after all tests have completed.
// If this is not called, then the test process will hang.
if ('dispose' in extension) {
extension.dispose();
}
}
);
// ensure temp directory is cleaned up.
(mocha.options as any).globalTeardown.push(
() => {
removeStorage?.();
}
);
}
export async function cleanDatabases(databaseManager: DatabaseManager) {
for (const item of databaseManager.databaseItems) {
await commands.executeCommand('codeQLDatabases.removeDatabase', item);
}
}