|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright 2021 The Go Authors. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import * as assert from 'assert'; |
| 9 | +import vscode = require('vscode'); |
| 10 | +import { Configuration } from '../../src/config'; |
| 11 | + |
| 12 | +suite('GoConfiguration Tests', () => { |
| 13 | + function check(trusted: boolean, workspaceConfig: { [key: string]: any }, key: string, expected: any) { |
| 14 | + const getConfigurationFn = (section: string) => new MockCfg(workspaceConfig); |
| 15 | + const cfg = (new Configuration(trusted, getConfigurationFn)).get(); |
| 16 | + |
| 17 | + const got0 = JSON.stringify(cfg.get(key)); |
| 18 | + const got1 = JSON.stringify(cfg[key]); |
| 19 | + const want = JSON.stringify(expected); |
| 20 | + assert.strictEqual(got0, want, `cfg.get(${key}) = ${got0}, want ${want}`); |
| 21 | + assert.strictEqual(got1, want, `cfg[${key}] = ${got1}, want ${want}`); |
| 22 | + |
| 23 | + } |
| 24 | + test('trusted workspace', () => { |
| 25 | + check(true, { goroot: 'goroot_val' }, 'goroot', 'goroot_val'); |
| 26 | + check(true, { gopath: 'gopath_val' }, 'gopath', 'gopath_val'); |
| 27 | + check(true, { toolsGopath: 'toolsGopath_val' }, 'toolsGopath', 'toolsGopath_val'); |
| 28 | + check(true, { alternateTools: { go: 'foo' } }, 'alternateTools', { go: 'foo' }); |
| 29 | + |
| 30 | + check(true, { buildFlags: ['-v'] }, 'buildFlags', ['-v']); |
| 31 | + check(true, { languageServerFlags: ['-rpc.trace'] }, 'languageServerFlags', ['-rpc.trace']); |
| 32 | + }); |
| 33 | + |
| 34 | + test('untrusted workspace', () => { |
| 35 | + check(false, { goroot: 'goroot_val' }, 'goroot', null); |
| 36 | + check(false, { gopath: 'gopath_val' }, 'gopath', null); |
| 37 | + check(false, { toolsGopath: 'toolsGopath_val' }, 'toolsGopath', null); |
| 38 | + check(false, { alternateTools: { go: 'foo' } }, 'alternateTools', {}); |
| 39 | + |
| 40 | + check(false, { buildFlags: ['-v'] }, 'buildFlags', ['-v']); |
| 41 | + check(false, { languageServerFlags: ['-rpc.trace'] }, 'languageServerFlags', ['-rpc.trace']); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +// tslint:disable: no-any |
| 46 | +class MockCfg implements vscode.WorkspaceConfiguration { |
| 47 | + private map: Map<string, any>; |
| 48 | + private wrapped: vscode.WorkspaceConfiguration; |
| 49 | + |
| 50 | + constructor(workspaceSettings: { [key: string]: any } = {}) { |
| 51 | + // getter |
| 52 | + Object.defineProperties(this, Object.getOwnPropertyDescriptors(workspaceSettings)); |
| 53 | + this.map = new Map<string, any>(Object.entries(workspaceSettings)); |
| 54 | + this.wrapped = vscode.workspace.getConfiguration('go'); |
| 55 | + } |
| 56 | + |
| 57 | + // tslint:disable: no-any |
| 58 | + public get(section: string, defaultValue?: any): any { |
| 59 | + if (this.map.has(section)) { |
| 60 | + return this.map.get(section); |
| 61 | + } |
| 62 | + return this.wrapped.get(section, defaultValue); |
| 63 | + } |
| 64 | + |
| 65 | + public has(section: string): boolean { |
| 66 | + if (this.map.has(section)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + return this.wrapped.has(section); |
| 70 | + } |
| 71 | + |
| 72 | + public inspect<T>(section: string) { |
| 73 | + const i = this.wrapped.inspect<T>(section); |
| 74 | + if (this.map.has(section)) { |
| 75 | + i.workspaceValue = this.map.get(section); |
| 76 | + } |
| 77 | + return i; |
| 78 | + } |
| 79 | + |
| 80 | + public update( |
| 81 | + section: string, value: any, |
| 82 | + configurationTarget?: boolean | vscode.ConfigurationTarget, |
| 83 | + overrideInLanguage?: boolean): Thenable<void> { |
| 84 | + throw new Error('Method not implemented.'); |
| 85 | + } |
| 86 | +} |
0 commit comments