Register all settings as test setting automatically

This will register all settings for which a `Setting` instance is
created as settings which will be reset. This should make it less
error-prone to change settings in tests.
This commit is contained in:
Koen Vlaswinkel
2022-10-12 16:30:35 +02:00
parent 6e6ea76c97
commit 726feb19e1
4 changed files with 26 additions and 15 deletions

View File

@@ -4,6 +4,8 @@ import { DistributionManager } from './distribution';
import { logger } from './logging';
import { ONE_DAY_IN_MS } from './pure/time';
export const ALL_SETTINGS: Setting[] = [];
/** Helper class to look up a labelled (and possibly nested) setting. */
export class Setting {
name: string;
@@ -12,6 +14,7 @@ export class Setting {
constructor(name: string, parent?: Setting) {
this.name = name;
this.parent = parent;
ALL_SETTINGS.push(this);
}
get qualifiedName(): string {
@@ -344,12 +347,16 @@ const REMOTE_QUERIES_SETTING = new Setting('variantAnalysis', ROOT_SETTING);
* This setting should be a JSON object where each key is a user-specified name (string),
* and the value is an array of GitHub repositories (of the form `<owner>/<repo>`).
*/
export const REMOTE_REPO_LISTS = new Setting('repositoryLists', REMOTE_QUERIES_SETTING);
const REMOTE_REPO_LISTS = new Setting('repositoryLists', REMOTE_QUERIES_SETTING);
export function getRemoteRepositoryLists(): Record<string, string[]> | undefined {
return REMOTE_REPO_LISTS.getValue<Record<string, string[]>>() || undefined;
}
export async function setRemoteRepositoryLists(lists: Record<string, string[]> | undefined) {
await REMOTE_REPO_LISTS.updateValue(lists, ConfigurationTarget.Global);
}
/**
* Path to a file that contains lists of GitHub repositories that you want to query remotely via
* the "Run Variant Analysis" command.
@@ -371,7 +378,7 @@ export function getRemoteRepositoryListsPath(): string | undefined {
*
* This setting should be a GitHub repository of the form `<owner>/<repo>`.
*/
export const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETTING);
const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETTING);
export function getRemoteControllerRepo(): string | undefined {
return REMOTE_CONTROLLER_REPO.getValue<string>() || undefined;

View File

@@ -7,7 +7,8 @@ import { fail } from 'assert';
import { commands, extensions, workspace } from 'vscode';
import { CodeQLExtensionInterface } from '../../extension';
import { DatabaseManager } from '../../databases';
import { testConfig } from '../test-config';
import { getTestSetting } from '../test-config';
import { CUSTOM_CODEQL_PATH_SETTING } from '../../config';
// This file contains helpers shared between actual tests.
@@ -59,7 +60,7 @@ export default function(mocha: Mocha) {
// 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 testConfig.cliExecutablePath.setInitialTestValue(process.env.CLI_PATH);
await getTestSetting(CUSTOM_CODEQL_PATH_SETTING)?.setInitialTestValue(process.env.CLI_PATH);
}
);

View File

@@ -10,6 +10,7 @@ import { QlPack, runRemoteQuery } from '../../../remote-queries/run-remote-query
import { Credentials } from '../../../authentication';
import { CliVersionConstraint, CodeQLCliServer } from '../../../cli';
import { CodeQLExtensionInterface } from '../../../extension';
import { setRemoteControllerRepo, setRemoteRepositoryLists } from '../../../config';
import * as config from '../../../config';
import { UserCancellationException } from '../../../commandRunner';
import * as ghApiClient from '../../../remote-queries/gh-api/gh-api-client';
@@ -23,7 +24,6 @@ import { createMockApiResponse } from '../../factories/remote-queries/gh-api/var
import { createMockExtensionContext } from '../../no-workspace';
import { VariantAnalysisManager } from '../../../remote-queries/variant-analysis-manager';
import { OutputChannelLogger } from '../../../logging';
import { testConfig } from '../../test-config';
describe('Remote queries', function() {
const baseDir = path.join(__dirname, '../../../../src/vscode-tests/cli-integration');
@@ -83,8 +83,8 @@ describe('Remote queries', function() {
getRepositoryFromNwoStub = sandbox.stub(ghApiClient, 'getRepositoryFromNwo').resolves(dummyRepository);
// always run in the vscode-codeql repo
await testConfig.remoteControllerRepo.set('github/vscode-codeql');
await testConfig.remoteRepoLists.set({ 'vscode-codeql': ['github/vscode-codeql'] });
await setRemoteControllerRepo('github/vscode-codeql');
await setRemoteRepositoryLists({ 'vscode-codeql': ['github/vscode-codeql'] });
liveResultsStub = sandbox.stub(config, 'isVariantAnalysisLiveResultsEnabled').returns(false);
});

View File

@@ -1,5 +1,5 @@
import { ConfigurationTarget } from 'vscode';
import { CUSTOM_CODEQL_PATH_SETTING, InspectionResult, REMOTE_CONTROLLER_REPO, REMOTE_REPO_LISTS, Setting } from '../config';
import { ALL_SETTINGS, InspectionResult, Setting } from '../config';
class TestSetting<T> {
private initialSettingState: InspectionResult<T> | undefined;
@@ -59,24 +59,27 @@ class TestSetting<T> {
}
}
export const testConfig = {
remoteControllerRepo: new TestSetting<string>(REMOTE_CONTROLLER_REPO),
remoteRepoLists: new TestSetting<Record<string, string[]>>(REMOTE_REPO_LISTS),
cliExecutablePath: new TestSetting<string>(CUSTOM_CODEQL_PATH_SETTING),
// The test settings are all settings in ALL_SETTINGS which don't have any children
const TEST_SETTINGS = ALL_SETTINGS
.filter(setting => ALL_SETTINGS.filter(s => s.parent === setting).length === 0)
.map(setting => new TestSetting(setting));
export const getTestSetting = (setting: Setting): TestSetting<unknown> | undefined => {
return TEST_SETTINGS.find(testSetting => testSetting.setting === setting);
};
export const testConfigHelper = async (mocha: Mocha) => {
// Read in all current settings
await Promise.all(Object.values(testConfig).map(setting => setting.initialSetup()));
await Promise.all(TEST_SETTINGS.map(setting => setting.initialSetup()));
mocha.rootHooks({
async beforeEach() {
// Reset the settings to their initial values before each test
await Promise.all(Object.values(testConfig).map(setting => setting.setup()));
await Promise.all(TEST_SETTINGS.map(setting => setting.setup()));
},
async afterAll() {
// Restore all settings to their default values after each test suite
await Promise.all(Object.values(testConfig).map(setting => setting.restoreToInitialValues()));
await Promise.all(TEST_SETTINGS.map(setting => setting.restoreToInitialValues()));
}
});
};