Skip to content

Commit 726feb1

Browse files
committed
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.
1 parent 6e6ea76 commit 726feb1

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

extensions/ql-vscode/src/config.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { DistributionManager } from './distribution';
44
import { logger } from './logging';
55
import { ONE_DAY_IN_MS } from './pure/time';
66

7+
export const ALL_SETTINGS: Setting[] = [];
8+
79
/** Helper class to look up a labelled (and possibly nested) setting. */
810
export class Setting {
911
name: string;
@@ -12,6 +14,7 @@ export class Setting {
1214
constructor(name: string, parent?: Setting) {
1315
this.name = name;
1416
this.parent = parent;
17+
ALL_SETTINGS.push(this);
1518
}
1619

1720
get qualifiedName(): string {
@@ -344,12 +347,16 @@ const REMOTE_QUERIES_SETTING = new Setting('variantAnalysis', ROOT_SETTING);
344347
* This setting should be a JSON object where each key is a user-specified name (string),
345348
* and the value is an array of GitHub repositories (of the form `<owner>/<repo>`).
346349
*/
347-
export const REMOTE_REPO_LISTS = new Setting('repositoryLists', REMOTE_QUERIES_SETTING);
350+
const REMOTE_REPO_LISTS = new Setting('repositoryLists', REMOTE_QUERIES_SETTING);
348351

349352
export function getRemoteRepositoryLists(): Record<string, string[]> | undefined {
350353
return REMOTE_REPO_LISTS.getValue<Record<string, string[]>>() || undefined;
351354
}
352355

356+
export async function setRemoteRepositoryLists(lists: Record<string, string[]> | undefined) {
357+
await REMOTE_REPO_LISTS.updateValue(lists, ConfigurationTarget.Global);
358+
}
359+
353360
/**
354361
* Path to a file that contains lists of GitHub repositories that you want to query remotely via
355362
* the "Run Variant Analysis" command.
@@ -371,7 +378,7 @@ export function getRemoteRepositoryListsPath(): string | undefined {
371378
*
372379
* This setting should be a GitHub repository of the form `<owner>/<repo>`.
373380
*/
374-
export const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETTING);
381+
const REMOTE_CONTROLLER_REPO = new Setting('controllerRepo', REMOTE_QUERIES_SETTING);
375382

376383
export function getRemoteControllerRepo(): string | undefined {
377384
return REMOTE_CONTROLLER_REPO.getValue<string>() || undefined;

extensions/ql-vscode/src/vscode-tests/cli-integration/global.helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { fail } from 'assert';
77
import { commands, extensions, workspace } from 'vscode';
88
import { CodeQLExtensionInterface } from '../../extension';
99
import { DatabaseManager } from '../../databases';
10-
import { testConfig } from '../test-config';
10+
import { getTestSetting } from '../test-config';
11+
import { CUSTOM_CODEQL_PATH_SETTING } from '../../config';
1112

1213
// This file contains helpers shared between actual tests.
1314

@@ -59,7 +60,7 @@ export default function(mocha: Mocha) {
5960
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
6061
(mocha.options as any).globalSetup.push(
6162
async () => {
62-
await testConfig.cliExecutablePath.setInitialTestValue(process.env.CLI_PATH);
63+
await getTestSetting(CUSTOM_CODEQL_PATH_SETTING)?.setInitialTestValue(process.env.CLI_PATH);
6364
}
6465
);
6566

extensions/ql-vscode/src/vscode-tests/cli-integration/remote-queries/run-remote-query.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { QlPack, runRemoteQuery } from '../../../remote-queries/run-remote-query
1010
import { Credentials } from '../../../authentication';
1111
import { CliVersionConstraint, CodeQLCliServer } from '../../../cli';
1212
import { CodeQLExtensionInterface } from '../../../extension';
13+
import { setRemoteControllerRepo, setRemoteRepositoryLists } from '../../../config';
1314
import * as config from '../../../config';
1415
import { UserCancellationException } from '../../../commandRunner';
1516
import * as ghApiClient from '../../../remote-queries/gh-api/gh-api-client';
@@ -23,7 +24,6 @@ import { createMockApiResponse } from '../../factories/remote-queries/gh-api/var
2324
import { createMockExtensionContext } from '../../no-workspace';
2425
import { VariantAnalysisManager } from '../../../remote-queries/variant-analysis-manager';
2526
import { OutputChannelLogger } from '../../../logging';
26-
import { testConfig } from '../../test-config';
2727

2828
describe('Remote queries', function() {
2929
const baseDir = path.join(__dirname, '../../../../src/vscode-tests/cli-integration');
@@ -83,8 +83,8 @@ describe('Remote queries', function() {
8383
getRepositoryFromNwoStub = sandbox.stub(ghApiClient, 'getRepositoryFromNwo').resolves(dummyRepository);
8484

8585
// always run in the vscode-codeql repo
86-
await testConfig.remoteControllerRepo.set('github/vscode-codeql');
87-
await testConfig.remoteRepoLists.set({ 'vscode-codeql': ['github/vscode-codeql'] });
86+
await setRemoteControllerRepo('github/vscode-codeql');
87+
await setRemoteRepositoryLists({ 'vscode-codeql': ['github/vscode-codeql'] });
8888

8989
liveResultsStub = sandbox.stub(config, 'isVariantAnalysisLiveResultsEnabled').returns(false);
9090
});

extensions/ql-vscode/src/vscode-tests/test-config.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ConfigurationTarget } from 'vscode';
2-
import { CUSTOM_CODEQL_PATH_SETTING, InspectionResult, REMOTE_CONTROLLER_REPO, REMOTE_REPO_LISTS, Setting } from '../config';
2+
import { ALL_SETTINGS, InspectionResult, Setting } from '../config';
33

44
class TestSetting<T> {
55
private initialSettingState: InspectionResult<T> | undefined;
@@ -59,24 +59,27 @@ class TestSetting<T> {
5959
}
6060
}
6161

62-
export const testConfig = {
63-
remoteControllerRepo: new TestSetting<string>(REMOTE_CONTROLLER_REPO),
64-
remoteRepoLists: new TestSetting<Record<string, string[]>>(REMOTE_REPO_LISTS),
65-
cliExecutablePath: new TestSetting<string>(CUSTOM_CODEQL_PATH_SETTING),
62+
// The test settings are all settings in ALL_SETTINGS which don't have any children
63+
const TEST_SETTINGS = ALL_SETTINGS
64+
.filter(setting => ALL_SETTINGS.filter(s => s.parent === setting).length === 0)
65+
.map(setting => new TestSetting(setting));
66+
67+
export const getTestSetting = (setting: Setting): TestSetting<unknown> | undefined => {
68+
return TEST_SETTINGS.find(testSetting => testSetting.setting === setting);
6669
};
6770

6871
export const testConfigHelper = async (mocha: Mocha) => {
6972
// Read in all current settings
70-
await Promise.all(Object.values(testConfig).map(setting => setting.initialSetup()));
73+
await Promise.all(TEST_SETTINGS.map(setting => setting.initialSetup()));
7174

7275
mocha.rootHooks({
7376
async beforeEach() {
7477
// Reset the settings to their initial values before each test
75-
await Promise.all(Object.values(testConfig).map(setting => setting.setup()));
78+
await Promise.all(TEST_SETTINGS.map(setting => setting.setup()));
7679
},
7780
async afterAll() {
7881
// Restore all settings to their default values after each test suite
79-
await Promise.all(Object.values(testConfig).map(setting => setting.restoreToInitialValues()));
82+
await Promise.all(TEST_SETTINGS.map(setting => setting.restoreToInitialValues()));
8083
}
8184
});
8285
};

0 commit comments

Comments
 (0)