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