-
Notifications
You must be signed in to change notification settings - Fork 792
/
Copy pathgoDebugConfiguration.test.ts
661 lines (589 loc) · 18.2 KB
/
goDebugConfiguration.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-prototype-builtins */
import assert = require('assert');
import fs = require('fs');
import os = require('os');
import path = require('path');
import sinon = require('sinon');
import vscode = require('vscode');
import { getGoConfig } from '../../src/config';
import { GoDebugConfigurationProvider } from '../../src/goDebugConfiguration';
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
import { rmdirRecursive } from '../../src/util';
import goEnv = require('../../src/goEnv');
import { isInPreviewMode } from '../../src/goLanguageServer';
import { MockCfg } from '../mocks/MockCfg';
suite('Debug Environment Variable Merge Test', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
suiteSetup(async () => {
await updateGoVarsFromConfig();
// Set up the test fixtures.
const fixtureSourcePath = path.join(__dirname, '..', '..', '..', 'test', 'testdata');
const filePath = path.join(fixtureSourcePath, 'baseTest', 'test.go');
await vscode.workspace.openTextDocument(vscode.Uri.file(filePath));
});
suiteTeardown(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
let sandbox: sinon.SinonSandbox;
let tmpDir = '';
const toolExecutionEnv: NodeJS.Dict<string> = {};
setup(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'godebugconfig_test'));
sandbox = sinon.createSandbox();
});
teardown(() => {
sandbox.restore();
rmdirRecursive(tmpDir);
});
interface Input {
debugAdapter?: 'dlv-dap' | 'legacy';
env?: { [key: string]: any };
envFile?: string | string[];
toolsEnv?: { [key: string]: any };
}
function runTest(input: Input, expected: { [key: string]: any }) {
sandbox.stub(goEnv, 'toolExecutionEnvironment').returns(input.toolsEnv || {});
const config = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(undefined, {
type: 'go',
name: 'Launch',
request: 'launch',
env: input.env,
envFile: input.envFile,
debugAdapter: input.debugAdapter
});
const actual = config.env;
assert.deepStrictEqual(actual, expected);
}
test('works with empty launchArgs', () => {
runTest({}, {});
});
test('toolsEnvVars is propagated (legacy)', () => {
const debugAdapter = 'legacy';
const toolsEnv = {
GOPATH: '/gopath',
GOOS: 'valueFromToolsEnv'
};
runTest(
{
debugAdapter,
toolsEnv
},
{
GOPATH: '/gopath',
GOOS: 'valueFromToolsEnv'
}
);
});
test('toolsEnvVars is not propagated', () => {
const toolsEnv = {
GOPATH: '/gopath',
GOOS: 'valueFromToolsEnv'
};
runTest(
{
toolsEnv
},
{}
);
});
test('preserves settings from launchArgs.env', () => {
const env = { GOPATH: 'valueFromEnv', GOOS: 'valueFromEnv2' };
runTest(
{ env },
{
GOPATH: 'valueFromEnv',
GOOS: 'valueFromEnv2'
}
);
});
test('preserves settings from launchArgs.envFile', () => {
const envFile = path.join(tmpDir, 'env');
fs.writeFileSync(envFile, 'GOPATH=valueFromEnvFile');
runTest({ envFile }, { GOPATH: 'valueFromEnvFile' });
});
test('launchArgs.env overwrites launchArgs.envFile', () => {
const env = { SOMEVAR1: 'valueFromEnv' };
const envFile = path.join(tmpDir, 'env');
fs.writeFileSync(envFile, ['SOMEVAR1=valueFromEnvFile1', 'SOMEVAR2=valueFromEnvFile2'].join('\n'));
runTest(
{ env, envFile },
{
SOMEVAR1: 'valueFromEnv',
SOMEVAR2: 'valueFromEnvFile2'
}
);
});
test('launchArgs.env overwrites toolsEnvVar (legacy)', () => {
const toolsEnv = {
GOPATH: '/gopath',
SOMEVAR1: 'valueFromToolsEnvVar1',
SOMEVAR2: 'valueFromToolsEnvVar2'
};
const debugAdapter = 'legacy';
const env = { SOMEVAR1: 'valueFromEnv' };
runTest(
{ debugAdapter, env, toolsEnv },
{
GOPATH: '/gopath',
SOMEVAR1: 'valueFromEnv',
SOMEVAR2: 'valueFromToolsEnvVar2'
}
);
});
test('launchArgs.env is respected, toolsEnvVar is ignored (dlv-dap)', () => {
const toolsEnv = {
GOPATH: '/gopath',
SOMEVAR1: 'valueFromToolsEnvVar1',
SOMEVAR2: 'valueFromToolsEnvVar2'
};
const env = { SOMEVAR1: 'valueFromEnv' };
runTest(
{ env, toolsEnv },
{
SOMEVAR1: 'valueFromEnv'
}
);
});
test('launchArgs.envFile overwrites toolsEnvVar (legacy)', () => {
const toolsEnv = {
GOPATH: '/gopath',
SOMEVAR1: 'valueFromToolsEnvVar1',
SOMEVAR2: 'valueFromToolsEnvVar2'
};
const envFile = path.join(tmpDir, 'env');
fs.writeFileSync(envFile, ['SOMEVAR2=valueFromEnvFile2'].join('\n'));
const debugAdapter = 'legacy';
runTest(
{ debugAdapter, toolsEnv, envFile },
{
GOPATH: '/gopath',
SOMEVAR1: 'valueFromToolsEnvVar1',
SOMEVAR2: 'valueFromEnvFile2'
}
);
});
test('launchArgs.envFile is repected, and toolsEnvVar is ignored (dlv-dap)', () => {
const toolsEnv = {
GOPATH: '/gopath',
SOMEVAR1: 'valueFromToolsEnvVar1',
SOMEVAR2: 'valueFromToolsEnvVar2'
};
const envFile = path.join(tmpDir, 'env');
fs.writeFileSync(envFile, ['SOMEVAR2=valueFromEnvFile2'].join('\n'));
const debugAdapter = 'dlv-dap';
runTest(
{ debugAdapter, toolsEnv, envFile },
{
SOMEVAR2: 'valueFromEnvFile2'
}
);
});
});
suite('Debug Configuration Merge User Settings', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
const config = require('../../src/config');
teardown(() => sinon.restore());
suite("merge 'go' config from settings.json", () => {
test('go flags config does not affect debug config', async () => {
// This tests that the testFlags and GOOS and GOARCH set
// in settings.json do not affect the resolved debug configuration.
// When this expected behavior changes, this test can be updated.
// Run resolveDebugConfiguration with the default workspace settings.
const cfg1 = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}'
};
const emptyResult = await debugConfigProvider.resolveDebugConfiguration(undefined, cfg1);
const goConfig = Object.create(getGoConfig(), {
testFlags: { value: '-tags=myTagTest' },
buildFlags: { value: '-tags=myTagBuild' },
goroot: { value: '/path/to/goroot' },
gopath: { value: '/path/to/gopath' }
}) as vscode.WorkspaceConfiguration;
// Adjust the workspace config.
sinon.stub(config, 'getGoConfig').returns(goConfig);
const cfg2 = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}'
};
const filledResult = await debugConfigProvider.resolveDebugConfiguration(undefined, cfg2);
assert.strictEqual(filledResult.name, emptyResult.name);
assert.strictEqual(filledResult.type, emptyResult.type);
assert.strictEqual(filledResult.mode, emptyResult.mode);
assert.strictEqual(filledResult.request, emptyResult.request);
assert.strictEqual(filledResult.program, emptyResult.program);
assert.strictEqual(filledResult.dlvToolPath, emptyResult.dlvToolPath);
assert.strictEqual(filledResult.apiVersion, emptyResult.apiVersion);
assert.strictEqual(filledResult.showGlobalVariables, emptyResult.showGlobalVariables);
assert.strictEqual(filledResult.debugAdapter, emptyResult.debugAdapter);
assert.strictEqual(filledResult.substitutePath, emptyResult.substitutePath);
});
test('delve config in settings.json is added to debug config', async () => {
// This tests that the testFlags and GOOS and GOARCH set
// in settings.json do not affect the resolved debug configuration.
// When this expected behavior changes, this test can be updated.
// Run resolveDebugConfiguration with the default workspace settings.
const goConfig = new MockCfg({
delveConfig: {
dlvLoadConfig: {
followPointers: false,
maxVariableRecurse: 3,
maxStringLen: 32,
maxArrayValues: 32,
maxStructFields: 5
},
apiVersion: 1,
showGlobalVariables: true,
debugAdapter: 'dlv-dap',
substitutePath: [{ from: 'hello', to: 'goodbye' }]
}
});
sinon.stub(config, 'getGoConfig').returns(goConfig);
const cfg = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}'
};
const result = await debugConfigProvider.resolveDebugConfiguration(undefined, cfg);
assert.strictEqual(result.apiVersion, 1);
assert.strictEqual(result.showGlobalVariables, true);
assert.strictEqual(result.debugAdapter, 'dlv-dap');
assert.strictEqual(result.substitutePath.length, 1);
assert.strictEqual(result.substitutePath[0].from, 'hello');
assert.strictEqual(result.substitutePath[0].to, 'goodbye');
const dlvLoadConfig = result.dlvLoadConfig;
assert.strictEqual(dlvLoadConfig.followPointers, false);
assert.strictEqual(dlvLoadConfig.maxVariableRecurse, 3);
assert.strictEqual(dlvLoadConfig.maxStringLen, 32);
assert.strictEqual(dlvLoadConfig.maxArrayValues, 32);
assert.strictEqual(dlvLoadConfig.maxStructFields, 5);
});
test('delve config in settings.json is overriden by launch.json', async () => {
// This tests that the testFlags and GOOS and GOARCH set
// in settings.json do not affect the resolved debug configuration.
// When this expected behavior changes, this test can be updated.
// Run resolveDebugConfiguration with the default workspace settings.
const goConfig = new MockCfg({
delveConfig: {
dlvLoadConfig: {
followPointers: false,
maxVariableRecurse: 3,
maxStringLen: 32,
maxArrayValues: 32,
maxStructFields: 5
},
apiVersion: 1,
showGlobalVariables: true,
debugAdapter: 'dlv-dap',
substitutePath: [{ from: 'hello', to: 'goodbye' }]
}
});
sinon.stub(config, 'getGoConfig').returns(goConfig);
const cfg: vscode.DebugConfiguration = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}',
apiVersion: 2,
showGlobalVariables: false,
dlvLoadConfig: {
followPointers: true,
maxVariableRecurse: 6,
maxStringLen: 128,
maxArrayValues: 128,
maxStructFields: -1
},
debugAdapter: 'legacy',
substitutePath: []
};
const result = await debugConfigProvider.resolveDebugConfiguration(undefined, cfg);
assert.strictEqual(result.apiVersion, 2);
assert.strictEqual(result.showGlobalVariables, false);
assert.strictEqual(result.debugAdapter, 'legacy');
assert.strictEqual(result.substitutePath.length, 0);
const dlvLoadConfig = result.dlvLoadConfig;
assert.strictEqual(dlvLoadConfig.followPointers, true);
assert.strictEqual(dlvLoadConfig.maxVariableRecurse, 6);
assert.strictEqual(dlvLoadConfig.maxStringLen, 128);
assert.strictEqual(dlvLoadConfig.maxArrayValues, 128);
assert.strictEqual(dlvLoadConfig.maxStructFields, -1);
});
});
});
suite('Debug Configuration Modify User Config', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
suite('remove gcflags', () => {
test('remove user set -gcflags in buildFlags', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}',
env: {},
buildFlags: '-race -gcflags=-l -mod=mod'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.strictEqual(config.buildFlags, '-race -mod=mod');
});
test('remove user set -gcflags in GOFLAGS', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}',
env: { GOFLAGS: '-race -gcflags=-l -mod=mod' }
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.strictEqual(config.env.GOFLAGS, '-race -mod=mod');
});
});
});
suite('Debug Configuration Resolve Paths', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
test('resolve ~ in cwd', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}',
cwd: '~/main.go'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.notStrictEqual(config.cwd, '~/main.go');
});
test('do not resolve workspaceFolder or fileDirname', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '${fileDirname}',
cwd: '${workspaceFolder}'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.strictEqual(config.cwd, '${workspaceFolder}');
assert.strictEqual(config.program, '${fileDirname}');
});
});
suite('Debug Configuration Converts Relative Paths', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
function debugConfig(adapter: string) {
return {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
debugAdapter: adapter,
program: path.join('foo', 'bar.go'),
cwd: '.',
output: 'debug'
};
}
test('resolve relative paths with workspace root in dlv-dap mode, exec mode does not set __buildDir', () => {
const config = debugConfig('dlv-dap');
config.mode = 'exec';
config.program = path.join('foo', 'bar.exe');
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const { program, cwd, __buildDir } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
workspaceFolder,
config
);
assert.deepStrictEqual(
{ program, cwd, __buildDir },
{
program: path.join(os.tmpdir(), 'foo', 'bar.exe'),
cwd: os.tmpdir(),
__buildDir: undefined
}
);
});
test('program and __buildDir are updated while resolving debug configuration in dlv-dap mode', () => {
const config = debugConfig('dlv-dap');
config.program = path.join('foo', 'bar', 'pkg');
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const {
program,
cwd,
output,
__buildDir
} = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(workspaceFolder, config);
assert.deepStrictEqual(
{ program, cwd, output, __buildDir },
{
program: '.',
cwd: os.tmpdir(),
output: path.join(os.tmpdir(), 'debug'),
__buildDir: path.join(os.tmpdir(), 'foo', 'bar', 'pkg')
}
);
});
test('empty, undefined paths are not affected', () => {
const config = debugConfig('dlv-dap');
config.program = undefined;
config.cwd = '';
delete config.output;
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const { program, cwd, output } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
workspaceFolder,
config
);
assert.deepStrictEqual(
{ program, cwd, output },
{
program: undefined,
cwd: '',
output: undefined
}
);
});
test('relative paths with no workspace root are not expanded', () => {
const config = debugConfig('dlv-dap');
const {
program,
cwd,
output,
__buildDir
} = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(undefined, config);
assert.deepStrictEqual(
{ program, cwd, output, __buildDir },
{
program: '.' + path.sep + 'bar.go',
cwd: '.',
output: 'debug',
__buildDir: 'foo'
}
);
});
test('do not affect relative paths (workspace) in legacy mode', () => {
const config = debugConfig('legacy');
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const { program, cwd, output } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
workspaceFolder,
config
);
assert.deepStrictEqual(
{ program, cwd, output },
{
program: path.join('foo', 'bar.go'),
cwd: '.',
output: 'debug'
}
);
});
test('do not affect relative paths (no workspace) in legacy mode', () => {
const config = debugConfig('legacy');
const { program, cwd, output } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
undefined,
config
);
assert.deepStrictEqual(
{ program, cwd, output },
{
program: path.join('foo', 'bar.go'),
cwd: '.',
output: 'debug'
}
);
});
});
suite('Debug Configuration Auto Mode', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
test('resolve auto to debug with non-test file', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '/path/to/main.go'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.strictEqual(config.mode, 'debug');
assert.strictEqual(config.program, '/path/to/main.go');
});
test('resolve auto to debug with test file', () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '/path/to/main_test.go'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
assert.strictEqual(config.mode, 'test');
assert.strictEqual(config.program, '/path/to');
});
});
suite('Debug Configuration Default DebugAdapter', () => {
const debugConfigProvider = new GoDebugConfigurationProvider();
test("default debugAdapter should be 'dlv-dap'", () => {
const config = {
name: 'Launch',
type: 'go',
request: 'launch',
mode: 'auto',
program: '/path/to/main.go'
};
debugConfigProvider.resolveDebugConfiguration(undefined, config);
const resolvedConfig = config as any;
assert.strictEqual(resolvedConfig['debugAdapter'], 'dlv-dap');
});
test("default debugAdapter for remote mode should be always 'legacy'", () => {
const config = {
name: 'Attach',
type: 'go',
request: 'attach',
mode: 'remote',
program: '/path/to/main_test.go',
cwd: '/path'
};
const want = 'legacy'; // remote mode works only with legacy mode.
debugConfigProvider.resolveDebugConfiguration(undefined, config);
const resolvedConfig = config as any;
assert.strictEqual(resolvedConfig['debugAdapter'], want);
});
test('debugAdapter=dlv-dap should be ignored for remote mode', () => {
const config = {
name: 'Attach',
type: 'go',
request: 'attach',
mode: 'remote',
debugAdapter: 'dlv-dap',
program: '/path/to/main_test.go',
cwd: '/path'
};
const want = 'legacy'; // remote mode works only with legacy mode.
debugConfigProvider.resolveDebugConfiguration(undefined, config);
const resolvedConfig = config as any;
assert.strictEqual(resolvedConfig['debugAdapter'], want);
});
});