-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathcodelens.test.ts
More file actions
368 lines (316 loc) · 14.3 KB
/
codelens.test.ts
File metadata and controls
368 lines (316 loc) · 14.3 KB
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
/*---------------------------------------------------------
* Copyright 2020 The Go Authors. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import assert from 'assert';
import path = require('path');
import sinon = require('sinon');
import vscode = require('vscode');
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
import { GoRunTestCodeLensProvider } from '../../src/goRunTestCodelens';
import { subTestAtCursor, testAtCursor } from '../../src/goTest';
import { MockExtensionContext } from '../mocks/MockContext';
import { Env } from './goplsTestEnv.utils';
import * as testUtils from '../../src/testUtils';
import * as config from '../../src/config';
import { MockCfg } from '../mocks/MockCfg';
suite('Code lenses for testing and benchmarking', function () {
this.timeout(20000);
let document: vscode.TextDocument;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ctx = new MockExtensionContext() as any;
const cancellationTokenSource = new vscode.CancellationTokenSource();
const projectDir = path.join(__dirname, '..', '..', '..');
const testdataDir = path.join(projectDir, 'test', 'testdata', 'codelens');
const env = new Env();
this.afterEach(function () {
// Note: this shouldn't use () => {...}. Arrow functions do not have 'this'.
// I don't know why but this.currentTest.state does not have the expected value when
// used with teardown.
env.flushTrace(this.currentTest?.state === 'failed');
sinon.restore();
});
// updaetGoVarsFromConfig mutates env vars. Cache the value
// so we can restore it in suiteTeardown.
const prevEnv = Object.assign({}, process.env);
suiteSetup(async () => {
await updateGoVarsFromConfig({});
const uri = vscode.Uri.file(path.join(testdataDir, 'codelens_test.go'));
await env.startGopls(uri.fsPath);
document = await vscode.workspace.openTextDocument(uri);
});
suiteTeardown(async () => {
await env.teardown();
process.env = prevEnv;
});
test('Subtests - runs a test with cursor on t.Run line', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(7, 4, 7, 4);
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, true);
});
test('Subtests - runs a test with cursor within t.Run function', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(8, 4, 8, 4);
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, true);
});
test('Subtests - returns false for a failing test', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(11, 4, 11, 4);
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, false);
});
test('Subtests - does nothing for a dynamically defined subtest', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(17, 4, 17, 4);
sinon.stub(vscode.window, 'showInputBox').onFirstCall().resolves(undefined);
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, undefined);
});
test('Subtests - runs a test with curson on t.Run line and dynamic test name is passed in input box', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(17, 4, 17, 4);
sinon.stub(vscode.window, 'showInputBox').onFirstCall().resolves('dynamic test name');
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, false);
});
test('Subtests - does nothing when cursor outside of a test function', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(5, 0, 5, 0);
const result = await subTestAtCursor('test')(ctx, env.goCtx)([]);
assert.equal(result, undefined);
});
test('Subtests - does nothing when no test function covers the cursor and a function name is passed in', async () => {
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(5, 0, 5, 0);
const result = await subTestAtCursor('test')(ctx, env.goCtx)({ functionName: 'TestMyFunction' });
assert.equal(result, undefined);
});
test('Test codelenses', async () => {
const codeLensProvider = new GoRunTestCodeLensProvider(env.goCtx);
const codeLenses = await codeLensProvider.provideCodeLenses(document, cancellationTokenSource.token);
assert.equal(codeLenses.length, 8);
const wantCommands = [
'go.test.package',
'go.test.file',
'go.test.cursor',
'go.debug.cursor',
'go.subtest.cursor',
'go.debug.subtest.cursor',
'go.subtest.cursor',
'go.debug.subtest.cursor'
];
for (let i = 0; i < codeLenses.length; i++) {
assert.equal(codeLenses[i].command?.command, wantCommands[i]);
}
});
test('Benchmark codelenses', async () => {
const codeLensProvider = new GoRunTestCodeLensProvider(env.goCtx);
const uri = vscode.Uri.file(path.join(testdataDir, 'codelens_benchmark_test.go'));
const benchmarkDocument = await vscode.workspace.openTextDocument(uri);
const codeLenses = await codeLensProvider.provideCodeLenses(benchmarkDocument, cancellationTokenSource.token);
assert.equal(codeLenses.length, 6);
const wantCommands = [
'go.test.package',
'go.test.file',
'go.benchmark.package',
'go.benchmark.file',
'go.benchmark.cursor',
'go.debug.cursor'
];
for (let i = 0; i < codeLenses.length; i++) {
assert.equal(codeLenses[i].command?.command, wantCommands[i]);
}
});
test('Test codelenses include only valid test function names', async () => {
const codeLensProvider = new GoRunTestCodeLensProvider(env.goCtx);
const uri = vscode.Uri.file(path.join(testdataDir, 'testnames', 'testnames_test.go'));
const benchmarkDocument = await vscode.workspace.openTextDocument(uri);
const codeLenses = await codeLensProvider.provideCodeLenses(benchmarkDocument, cancellationTokenSource.token);
assert.equal(codeLenses.length, 20, JSON.stringify(codeLenses, null, 2));
const found = [] as string[];
for (let i = 0; i < codeLenses.length; i++) {
const lens = codeLenses[i];
if (lens.command?.command === 'go.test.cursor') {
found.push(lens.command.arguments?.[0].functionName);
}
}
found.sort();
// Results should match `go test -list`.
assert.deepStrictEqual(found, [
'Example',
'ExampleFunction',
'Test',
'Test1Function',
'TestFunction',
'TestMain',
'Test_foobar',
'TestΣυνάρτηση',
'Test함수'
]);
});
test('Test codelenses include valid fuzz function names', async () => {
const codeLensProvider = new GoRunTestCodeLensProvider(env.goCtx);
const uri = vscode.Uri.file(path.join(testdataDir, 'codelens_go118_test.go'));
const testDocument = await vscode.workspace.openTextDocument(uri);
const codeLenses = await codeLensProvider.provideCodeLenses(testDocument, cancellationTokenSource.token);
assert.equal(codeLenses.length, 8, JSON.stringify(codeLenses, null, 2));
const found = [] as string[];
for (let i = 0; i < codeLenses.length; i++) {
const lens = codeLenses[i];
if (lens.command?.command === 'go.test.cursor') {
found.push(lens.command.arguments?.[0].functionName);
}
}
found.sort();
// Results should match `go test -list`.
assert.deepStrictEqual(found, ['Fuzz', 'FuzzFunc', 'TestGo118']);
});
test('Test codelenses skip TestMain', async () => {
const codeLensProvider = new GoRunTestCodeLensProvider(env.goCtx);
const uri = vscode.Uri.file(path.join(testdataDir, 'testmain/testmain_test.go'));
const testDocument = await vscode.workspace.openTextDocument(uri);
const codeLenses = await codeLensProvider.provideCodeLenses(testDocument, cancellationTokenSource.token);
assert.equal(codeLenses.length, 4, JSON.stringify(codeLenses, null, 2));
const found = [] as string[];
for (let i = 0; i < codeLenses.length; i++) {
const lens = codeLenses[i];
if (lens.command?.command === 'go.test.cursor') {
found.push(lens.command.arguments?.[0].functionName);
}
}
found.sort();
// Results should match `go test -list`.
assert.deepStrictEqual(found, ['TestNotMain']);
});
test('Debug - debugs a test with cursor on t.Run line', async () => {
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(document);
editor.selection = new vscode.Selection(7, 4, 7, 4);
const result = await subTestAtCursor('debug')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
gotConfig.program = '';
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
name: 'Debug Test',
type: 'go',
request: 'launch',
args: ['-test.run', '^TestSample$/^sample_test_passing$'],
env: {},
sessionID: undefined,
mode: 'test',
envFile: null,
program: ''
});
});
});
suite('Code lenses with stretchr/testify/suite', function () {
this.timeout(20000); // Gopls needs to load modules from the internet for this test.
const ctx = MockExtensionContext.new();
const testdataDir = path.join(__dirname, '..', '..', '..', 'test', 'testdata', 'stretchrTestSuite');
const env = new Env();
this.afterEach(function () {
// Note: this shouldn't use () => {...}. Arrow functions do not have 'this'.
// I don't know why but this.currentTest.state does not have the expected value when
// used with teardown.
env.flushTrace(this.currentTest?.state === 'failed');
ctx.teardown();
sinon.restore();
});
suiteSetup(async () => {
await updateGoVarsFromConfig({});
await env.startGopls(undefined, undefined, testdataDir);
});
suiteTeardown(async () => {
await env.teardown();
});
test('Run test at cursor', async () => {
const goTestStub = sinon.stub(testUtils, 'goTest').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(vscode.Uri.file(path.join(testdataDir, 'suite_test.go')));
editor.selection = new vscode.Selection(25, 4, 25, 4);
const result = await testAtCursor('test')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(goTestStub.callCount, 1, 'expected one call to goTest');
const gotConfig = goTestStub.getCall(0).args[0];
assert.deepStrictEqual(gotConfig.functions, ['(*ExampleTestSuite).TestExample', 'TestExampleTestSuite']);
});
test('Run test at cursor in different file than test suite definition', async () => {
const goTestStub = sinon.stub(testUtils, 'goTest').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(
vscode.Uri.file(path.join(testdataDir, 'another_suite_test.go'))
);
editor.selection = new vscode.Selection(3, 4, 3, 4);
const result = await testAtCursor('test')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(goTestStub.callCount, 1, 'expected one call to goTest');
const gotConfig = goTestStub.getCall(0).args[0];
assert.deepStrictEqual(gotConfig.functions, [
'(*ExampleTestSuite).TestExampleInAnotherFile',
'TestExampleTestSuite'
]);
});
test('Debug test at cursor', async () => {
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(vscode.Uri.file(path.join(testdataDir, 'suite_test.go')));
editor.selection = new vscode.Selection(25, 4, 25, 4);
const result = await testAtCursor('debug')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
gotConfig.program = '';
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
name: 'Debug Test',
type: 'go',
request: 'launch',
args: ['-test.run', '^TestExampleTestSuite$/^TestExample$'],
env: {},
sessionID: undefined,
mode: 'test',
envFile: null,
program: ''
});
});
test('Debug test at cursor in different file than test suite definition', async () => {
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(
vscode.Uri.file(path.join(testdataDir, 'another_suite_test.go'))
);
editor.selection = new vscode.Selection(3, 4, 3, 4);
const result = await testAtCursor('debug')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
gotConfig.program = '';
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
name: 'Debug Test',
type: 'go',
request: 'launch',
args: ['-test.run', '^TestExampleTestSuite$/^TestExampleInAnotherFile$'],
env: {},
sessionID: undefined,
mode: 'test',
envFile: null,
program: ''
});
});
// Regression test for golang/vscode-go#3933: build flag values that contain spaces
// (e.g. -ldflags "-X k=v") must be passed as an array so delve sees each flag as a
// separate argument. Joining them into a single string corrupts the value.
test('Debug test at cursor preserves buildFlags as array', async () => {
const goConfig = new MockCfg({
buildFlags: ['-ldflags', '-X github.com/org/pkg/info.version=v25.8.0']
});
sinon.stub(config, 'getGoConfig').returns(goConfig);
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
const editor = await vscode.window.showTextDocument(vscode.Uri.file(path.join(testdataDir, 'suite_test.go')));
editor.selection = new vscode.Selection(25, 4, 25, 4);
const result = await testAtCursor('debug')(ctx, env.goCtx)([]);
assert.strictEqual(result, true);
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
assert.deepStrictEqual(gotConfig.buildFlags, ['-ldflags', '-X github.com/org/pkg/info.version=v25.8.0']);
});
});