|
| 1 | +import { deepStrictEqual, strictEqual } from 'assert'; |
| 2 | +import { ConfigurationTarget, workspace } from 'vscode'; |
| 3 | +import { WorkspaceConfig } from '../client/WorkspaceConfig.js'; |
| 4 | +import { WORKSPACE_FOLDER } from './test-helpers.js'; |
| 5 | + |
| 6 | +suite('WorkspaceConfig', () => { |
| 7 | + setup(async () => { |
| 8 | + const workspaceConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER); |
| 9 | + const globalConfig = workspace.getConfiguration('oxc'); |
| 10 | + const keys = ['lint.run', 'configPath', 'flags']; |
| 11 | + |
| 12 | + await Promise.all(keys.map(key => workspaceConfig.update(key, undefined, ConfigurationTarget.WorkspaceFolder))); |
| 13 | + // VSCode will not save different workspace configuration inside a `.code-workspace` file. |
| 14 | + // Do not fail, we will make sure the global config is empty too. |
| 15 | + await Promise.all(keys.map(key => globalConfig.update(key, undefined))); |
| 16 | + }); |
| 17 | + |
| 18 | + teardown(async () => { |
| 19 | + const workspaceConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER); |
| 20 | + const globalConfig = workspace.getConfiguration('oxc'); |
| 21 | + const keys = ['lint.run', 'configPath', 'flags']; |
| 22 | + |
| 23 | + await Promise.all(keys.map(key => workspaceConfig.update(key, undefined, ConfigurationTarget.WorkspaceFolder))); |
| 24 | + // VSCode will not save different workspace configuration inside a `.code-workspace` file. |
| 25 | + // Do not fail, we will make sure the global config is empty too. |
| 26 | + await Promise.all(keys.map(key => globalConfig.update(key, undefined))); |
| 27 | + }); |
| 28 | + |
| 29 | + test('default values on initialization', () => { |
| 30 | + const config = new WorkspaceConfig(WORKSPACE_FOLDER); |
| 31 | + strictEqual(config.runTrigger, 'onType'); |
| 32 | + strictEqual(config.configPath, null); |
| 33 | + deepStrictEqual(config.flags, {}); |
| 34 | + }); |
| 35 | + |
| 36 | + test('configPath defaults to null when using nested configs and configPath is empty', async () => { |
| 37 | + const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER); |
| 38 | + await wsConfig.update('configPath', '', ConfigurationTarget.WorkspaceFolder); |
| 39 | + await wsConfig.update('flags', {}, ConfigurationTarget.WorkspaceFolder); |
| 40 | + |
| 41 | + const config = new WorkspaceConfig(WORKSPACE_FOLDER); |
| 42 | + |
| 43 | + deepStrictEqual(config.flags, {}); |
| 44 | + strictEqual(config.configPath, null); |
| 45 | + }); |
| 46 | + |
| 47 | + test('configPath defaults to .oxlintrc.json when not using nested configs and configPath is empty', async () => { |
| 48 | + const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER); |
| 49 | + await wsConfig.update('configPath', undefined, ConfigurationTarget.WorkspaceFolder); |
| 50 | + await wsConfig.update('flags', { disable_nested_config: '' }, ConfigurationTarget.WorkspaceFolder); |
| 51 | + |
| 52 | + const config = new WorkspaceConfig(WORKSPACE_FOLDER); |
| 53 | + |
| 54 | + deepStrictEqual(config.flags, { disable_nested_config: '' }); |
| 55 | + strictEqual(config.configPath, '.oxlintrc.json'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('updating values updates the workspace configuration', async () => { |
| 59 | + const config = new WorkspaceConfig(WORKSPACE_FOLDER); |
| 60 | + |
| 61 | + await Promise.all([ |
| 62 | + config.updateRunTrigger('onSave'), |
| 63 | + config.updateConfigPath('./somewhere'), |
| 64 | + config.updateFlags({ test: 'value' }), |
| 65 | + ]); |
| 66 | + |
| 67 | + const wsConfig = workspace.getConfiguration('oxc', WORKSPACE_FOLDER); |
| 68 | + |
| 69 | + strictEqual(wsConfig.get('lint.run'), 'onSave'); |
| 70 | + strictEqual(wsConfig.get('configPath'), './somewhere'); |
| 71 | + deepStrictEqual(wsConfig.get('flags'), { test: 'value' }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments