Skip to content

Commit f3b52e4

Browse files
committed
test/integration: use MockCfg for debug setting test
resolveDebugConfiguration uses 'inspect' API to determine whether the debug setting is default or user-configured. So, we need to mock the inspect method too. Utilize MockCfg used in the config.test.ts and move it to the mock directory. Fixes #1566 Change-Id: I49f703c69e3f4b4455004a37bb84ee18080fc942 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/327769 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Suzy Mueller <[email protected]>
1 parent c3047d2 commit f3b52e4

File tree

3 files changed

+91
-76
lines changed

3 files changed

+91
-76
lines changed

test/integration/config.test.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import * as assert from 'assert';
1111
import { Configuration } from '../../src/config';
12-
import vscode = require('vscode');
12+
import { MockCfg } from '../mocks/MockCfg';
1313

1414
suite('GoConfiguration Tests', () => {
1515
function check(trusted: boolean, workspaceConfig: { [key: string]: any }, key: string, expected: any) {
@@ -68,48 +68,3 @@ suite('GoConfiguration Tests', () => {
6868
checkGopls(false, { env: { GOBIN: 'foo' } }, 'env', { GOBIN: 'foo' });
6969
});
7070
});
71-
72-
// tslint:disable: no-any
73-
class MockCfg implements vscode.WorkspaceConfiguration {
74-
private map: Map<string, any>;
75-
private wrapped: vscode.WorkspaceConfiguration;
76-
77-
constructor(workspaceSettings: { [key: string]: any } = {}) {
78-
// getter
79-
Object.defineProperties(this, Object.getOwnPropertyDescriptors(workspaceSettings));
80-
this.map = new Map<string, any>(Object.entries(workspaceSettings));
81-
this.wrapped = vscode.workspace.getConfiguration('go'); // intentionally using vscode API directly.
82-
}
83-
84-
// tslint:disable: no-any
85-
public get(section: string, defaultValue?: any): any {
86-
if (this.map.has(section)) {
87-
return this.map.get(section);
88-
}
89-
return this.wrapped.get(section, defaultValue);
90-
}
91-
92-
public has(section: string): boolean {
93-
if (this.map.has(section)) {
94-
return true;
95-
}
96-
return this.wrapped.has(section);
97-
}
98-
99-
public inspect<T>(section: string) {
100-
const i = this.wrapped.inspect<T>(section);
101-
if (this.map.has(section)) {
102-
i.workspaceValue = this.map.get(section);
103-
}
104-
return i;
105-
}
106-
107-
public update(
108-
section: string,
109-
value: any,
110-
configurationTarget?: boolean | vscode.ConfigurationTarget,
111-
overrideInLanguage?: boolean
112-
): Thenable<void> {
113-
throw new Error('Method not implemented.');
114-
}
115-
}

test/integration/goDebugConfiguration.test.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { updateGoVarsFromConfig } from '../../src/goInstallTools';
1313
import { rmdirRecursive } from '../../src/util';
1414
import goEnv = require('../../src/goEnv');
1515
import { isInPreviewMode } from '../../src/goLanguageServer';
16+
import { MockCfg } from '../mocks/MockCfg';
1617

1718
suite('Debug Environment Variable Merge Test', () => {
1819
const debugConfigProvider = new GoDebugConfigurationProvider();
@@ -211,23 +212,21 @@ suite('Debug Configuration Merge User Settings', () => {
211212
// When this expected behavior changes, this test can be updated.
212213

213214
// Run resolveDebugConfiguration with the default workspace settings.
214-
const goConfig = Object.create(getGoConfig(), {
215+
const goConfig = new MockCfg({
215216
delveConfig: {
216-
value: {
217-
dlvLoadConfig: {
218-
followPointers: false,
219-
maxVariableRecurse: 3,
220-
maxStringLen: 32,
221-
maxArrayValues: 32,
222-
maxStructFields: 5
223-
},
224-
apiVersion: 1,
225-
showGlobalVariables: true,
226-
debugAdapter: 'dlv-dap',
227-
substitutePath: [{ from: 'hello', to: 'goodbye' }]
228-
}
217+
dlvLoadConfig: {
218+
followPointers: false,
219+
maxVariableRecurse: 3,
220+
maxStringLen: 32,
221+
maxArrayValues: 32,
222+
maxStructFields: 5
223+
},
224+
apiVersion: 1,
225+
showGlobalVariables: true,
226+
debugAdapter: 'dlv-dap',
227+
substitutePath: [{ from: 'hello', to: 'goodbye' }]
229228
}
230-
}) as vscode.WorkspaceConfiguration;
229+
});
231230
sinon.stub(config, 'getGoConfig').returns(goConfig);
232231

233232
const cfg = {
@@ -259,23 +258,21 @@ suite('Debug Configuration Merge User Settings', () => {
259258
// When this expected behavior changes, this test can be updated.
260259

261260
// Run resolveDebugConfiguration with the default workspace settings.
262-
const goConfig = Object.create(getGoConfig(), {
261+
const goConfig = new MockCfg({
263262
delveConfig: {
264-
value: {
265-
dlvLoadConfig: {
266-
followPointers: false,
267-
maxVariableRecurse: 3,
268-
maxStringLen: 32,
269-
maxArrayValues: 32,
270-
maxStructFields: 5
271-
},
272-
apiVersion: 1,
273-
showGlobalVariables: true,
274-
debugAdapter: 'dlv-dap',
275-
substitutePath: [{ from: 'hello', to: 'goodbye' }]
276-
}
263+
dlvLoadConfig: {
264+
followPointers: false,
265+
maxVariableRecurse: 3,
266+
maxStringLen: 32,
267+
maxArrayValues: 32,
268+
maxStructFields: 5
269+
},
270+
apiVersion: 1,
271+
showGlobalVariables: true,
272+
debugAdapter: 'dlv-dap',
273+
substitutePath: [{ from: 'hello', to: 'goodbye' }]
277274
}
278-
}) as vscode.WorkspaceConfiguration;
275+
});
279276
sinon.stub(config, 'getGoConfig').returns(goConfig);
280277

281278
const cfg: vscode.DebugConfiguration = {

test/mocks/MockCfg.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
/*---------------------------------------------------------
4+
* Copyright 2021 The Go Authors. All rights reserved.
5+
* Licensed under the MIT License. See LICENSE in the project root for license information.
6+
*--------------------------------------------------------*/
7+
'use strict';
8+
import vscode = require('vscode');
9+
10+
// tslint:disable: no-any
11+
export class MockCfg implements vscode.WorkspaceConfiguration {
12+
private map: Map<string, any>;
13+
private wrapped: vscode.WorkspaceConfiguration;
14+
15+
constructor(workspaceSettings: { [key: string]: any } = {}) {
16+
// getter
17+
Object.defineProperties(this, Object.getOwnPropertyDescriptors(workspaceSettings));
18+
this.map = new Map<string, any>(Object.entries(workspaceSettings));
19+
this.wrapped = vscode.workspace.getConfiguration('go'); // intentionally using vscode API directly.
20+
}
21+
22+
// tslint:disable: no-any
23+
public get(section: string, defaultValue?: any): any {
24+
if (this.map.has(section)) {
25+
return this.map.get(section);
26+
}
27+
return this.wrapped.get(section, defaultValue);
28+
}
29+
30+
public has(section: string): boolean {
31+
if (this.map.has(section)) {
32+
return true;
33+
}
34+
return this.wrapped.has(section);
35+
}
36+
37+
public inspect<T>(section: string) {
38+
const i = this.wrapped.inspect<T>(section);
39+
const part = section.split('.');
40+
if (this.map.has(part[0])) {
41+
let v: any = this.map.get(part[0]);
42+
for (let i = 1; i < part.length; i++) {
43+
if (Object.prototype.hasOwnProperty.call(v, part[i])) {
44+
v = v[part[i]];
45+
} else {
46+
v = undefined;
47+
break;
48+
}
49+
}
50+
i.workspaceValue = v;
51+
}
52+
return i;
53+
}
54+
55+
public update(
56+
section: string,
57+
value: any,
58+
configurationTarget?: boolean | vscode.ConfigurationTarget,
59+
overrideInLanguage?: boolean
60+
): Thenable<void> {
61+
throw new Error('Method not implemented.');
62+
}
63+
}

0 commit comments

Comments
 (0)