|
| 1 | +import { ConfigurationTarget, workspace } from 'vscode'; |
| 2 | +import { CUSTOM_CODEQL_PATH_SETTING, REMOTE_CONTROLLER_REPO, REMOTE_REPO_LISTS, Setting } from '../config'; |
| 3 | + |
| 4 | +class TestSetting<T> { |
| 5 | + private settingState: { |
| 6 | + globalValue?: T; |
| 7 | + workspaceValue?: T, |
| 8 | + workspaceFolderValue?: T, |
| 9 | + } | undefined; |
| 10 | + |
| 11 | + constructor( |
| 12 | + public readonly setting: Setting, |
| 13 | + private initialTestValue: T | undefined = undefined |
| 14 | + ) { } |
| 15 | + |
| 16 | + public async get(): Promise<T | undefined> { |
| 17 | + return this.section.get(this.setting.name); |
| 18 | + } |
| 19 | + |
| 20 | + public async set(value: T | undefined, target: ConfigurationTarget = ConfigurationTarget.Global): Promise<void> { |
| 21 | + await this.section.update(this.setting.name, value, target); |
| 22 | + } |
| 23 | + |
| 24 | + public async setInitialTestValue(value: T | undefined) { |
| 25 | + this.initialTestValue = value; |
| 26 | + } |
| 27 | + |
| 28 | + public async initialSetup() { |
| 29 | + this.settingState = this.section.inspect(this.setting.name); |
| 30 | + |
| 31 | + // Unfortunately it's not well-documented how to check whether we can write to a workspace |
| 32 | + // configuration. This is the best I could come up with. It only fails for initial test values |
| 33 | + // which are not undefined. |
| 34 | + if (this.settingState?.workspaceValue !== undefined) { |
| 35 | + await this.set(this.initialTestValue, ConfigurationTarget.Workspace); |
| 36 | + } |
| 37 | + if (this.settingState?.workspaceFolderValue !== undefined) { |
| 38 | + await this.set(this.initialTestValue, ConfigurationTarget.WorkspaceFolder); |
| 39 | + } |
| 40 | + |
| 41 | + await this.setup(); |
| 42 | + } |
| 43 | + |
| 44 | + public async setup() { |
| 45 | + await this.set(this.initialTestValue, ConfigurationTarget.Global); |
| 46 | + } |
| 47 | + |
| 48 | + public async restoreToInitialValues() { |
| 49 | + const state = this.section.inspect(this.setting.name); |
| 50 | + |
| 51 | + // We need to check the state of the setting before we restore it. This is less important for the global |
| 52 | + // configuration target, but the workspace/workspace folder configuration might not even exist. If they |
| 53 | + // don't exist, VSCode will error when trying to write the new value (even if that value is undefined). |
| 54 | + if (state?.globalValue !== this.settingState?.globalValue) { |
| 55 | + await this.set(this.settingState?.globalValue, ConfigurationTarget.Global); |
| 56 | + } |
| 57 | + if (state?.workspaceValue !== this.settingState?.workspaceValue) { |
| 58 | + await this.set(this.settingState?.workspaceValue, ConfigurationTarget.Workspace); |
| 59 | + } |
| 60 | + if (state?.workspaceFolderValue !== this.settingState?.workspaceFolderValue) { |
| 61 | + await this.set(this.settingState?.workspaceFolderValue, ConfigurationTarget.WorkspaceFolder); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private get section() { |
| 66 | + if (this.setting.parent === undefined) { |
| 67 | + throw new Error('Cannot get the value of a root setting.'); |
| 68 | + } |
| 69 | + return workspace.getConfiguration(this.setting.parent.qualifiedName); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export const testConfig = { |
| 74 | + remoteControllerRepo: new TestSetting<string>(REMOTE_CONTROLLER_REPO), |
| 75 | + remoteRepoLists: new TestSetting<Record<string, string[]>>(REMOTE_REPO_LISTS), |
| 76 | + cliExecutablePath: new TestSetting<string>(CUSTOM_CODEQL_PATH_SETTING), |
| 77 | +}; |
| 78 | + |
| 79 | +export const testConfigHelper = async (mocha: Mocha) => { |
| 80 | + // Read in all current settings |
| 81 | + await Promise.all(Object.values(testConfig).map(setting => setting.initialSetup())); |
| 82 | + |
| 83 | + mocha.rootHooks({ |
| 84 | + async beforeEach() { |
| 85 | + // Reset the settings to their initial values before each test |
| 86 | + await Promise.all(Object.values(testConfig).map(setting => setting.setup())); |
| 87 | + }, |
| 88 | + async afterAll() { |
| 89 | + // Restore all settings to their default values after each test suite |
| 90 | + await Promise.all(Object.values(testConfig).map(setting => setting.restoreToInitialValues())); |
| 91 | + } |
| 92 | + }); |
| 93 | +}; |
0 commit comments