Skip to content

Commit b1e7ab7

Browse files
committed
test/gopls: add tests covering stretchr test suites
1 parent e79bcf8 commit b1e7ab7

File tree

6 files changed

+176
-5
lines changed

6 files changed

+176
-5
lines changed

Diff for: extension/src/goTest.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async function _subTestAtCursor(
164164
export function testAtCursor(cmd: TestAtCursorCmd): CommandFactory {
165165
return (ctx, goCtx) => (args: any) => {
166166
const goConfig = getGoConfig();
167-
_testAtCursor(goCtx, goConfig, cmd, args).catch((err) => {
167+
return _testAtCursor(goCtx, goConfig, cmd, args).catch((err) => {
168168
if (err instanceof NotFoundError) {
169169
vscode.window.showInformationMessage(err.message);
170170
} else {
@@ -299,9 +299,14 @@ export async function debugTestAtCursor(
299299
sessionID
300300
};
301301
lastDebugConfig = debugConfig;
302+
console.log('✅ debugTestAtCursor', lastDebugConfig, debugConfig);
302303
lastDebugWorkspaceFolder = workspaceFolder;
303304
vscode.commands.executeCommand('workbench.debug.action.focusRepl');
304-
return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
305+
console.log('after execute command');
306+
console.log('workspace', workspaceFolder);
307+
const result = await vscode.debug.startDebugging(workspaceFolder, debugConfig);
308+
console.log('after debugging:', result);
309+
return result;
305310
}
306311

307312
/**

Diff for: extension/test/gopls/codelens.test.ts

+139-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import sinon = require('sinon');
1111
import vscode = require('vscode');
1212
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
1313
import { GoRunTestCodeLensProvider } from '../../src/goRunTestCodelens';
14-
import { subTestAtCursor } from '../../src/goTest';
14+
import { subTestAtCursor, testAtCursor } from '../../src/goTest';
1515
import { MockExtensionContext } from '../mocks/MockContext';
1616
import { Env } from './goplsTestEnv.utils';
17+
import * as testUtils from '../../src/testUtils';
1718

1819
suite('Code lenses for testing and benchmarking', function () {
1920
this.timeout(20000);
@@ -200,4 +201,141 @@ suite('Code lenses for testing and benchmarking', function () {
200201
// Results should match `go test -list`.
201202
assert.deepStrictEqual(found, ['TestNotMain']);
202203
});
204+
205+
test('Debug - debugs a test with cursor on t.Run line', async () => {
206+
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
207+
208+
const editor = await vscode.window.showTextDocument(document);
209+
editor.selection = new vscode.Selection(7, 4, 7, 4);
210+
const result = await subTestAtCursor('debug')(ctx, env.goCtx)([]);
211+
assert.strictEqual(result, true);
212+
213+
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
214+
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
215+
gotConfig.program = '';
216+
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
217+
name: 'Debug Test',
218+
type: 'go',
219+
request: 'launch',
220+
args: ['-test.run', '^TestSample$/^sample_test_passing$'],
221+
buildFlags: '',
222+
env: {},
223+
sessionID: undefined,
224+
mode: 'test',
225+
envFile: null,
226+
program: ''
227+
});
228+
});
229+
});
230+
231+
suite('Code lenses with stretchr/testify/suite', function () {
232+
const ctx = MockExtensionContext.new();
233+
234+
const testdataDir = path.join(__dirname, '..', '..', '..', 'test', 'testdata', 'stretchrTestSuite');
235+
const env = new Env();
236+
237+
this.afterEach(async function () {
238+
// Note: this shouldn't use () => {...}. Arrow functions do not have 'this'.
239+
// I don't know why but this.currentTest.state does not have the expected value when
240+
// used with teardown.
241+
env.flushTrace(this.currentTest?.state === 'failed');
242+
ctx.teardown();
243+
sinon.restore();
244+
});
245+
246+
suiteSetup(async () => {
247+
await updateGoVarsFromConfig({});
248+
await env.startGopls(undefined, undefined, testdataDir);
249+
});
250+
251+
suiteTeardown(async () => {
252+
await env.teardown();
253+
});
254+
255+
test('Run test at cursor', async () => {
256+
const goTestStub = sinon.stub(testUtils, 'goTest').returns(Promise.resolve(true));
257+
258+
const editor = await vscode.window.showTextDocument(vscode.Uri.file(path.join(testdataDir, 'suite_test.go')));
259+
editor.selection = new vscode.Selection(25, 4, 25, 4);
260+
261+
const result = await testAtCursor('test')(ctx, env.goCtx)([]);
262+
assert.strictEqual(result, true);
263+
264+
assert.strictEqual(goTestStub.callCount, 1, 'expected one call to goTest');
265+
const gotConfig = goTestStub.getCall(0).args[0];
266+
assert.deepStrictEqual(gotConfig.functions, ['(*ExampleTestSuite).TestExample', 'TestExampleTestSuite']);
267+
});
268+
269+
test('Run test at cursor in different file than test suite definition', async () => {
270+
const goTestStub = sinon.stub(testUtils, 'goTest').returns(Promise.resolve(true));
271+
272+
const editor = await vscode.window.showTextDocument(
273+
vscode.Uri.file(path.join(testdataDir, 'another_suite_test.go'))
274+
);
275+
editor.selection = new vscode.Selection(3, 4, 3, 4);
276+
277+
const result = await testAtCursor('test')(ctx, env.goCtx)([]);
278+
assert.strictEqual(result, true);
279+
280+
assert.strictEqual(goTestStub.callCount, 1, 'expected one call to goTest');
281+
const gotConfig = goTestStub.getCall(0).args[0];
282+
assert.deepStrictEqual(gotConfig.functions, [
283+
'(*ExampleTestSuite).TestExampleInAnotherFile',
284+
'TestExampleTestSuite'
285+
]);
286+
});
287+
288+
test('Debug test at cursor', async () => {
289+
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
290+
291+
const editor = await vscode.window.showTextDocument(vscode.Uri.file(path.join(testdataDir, 'suite_test.go')));
292+
editor.selection = new vscode.Selection(25, 4, 25, 4);
293+
294+
const result = await testAtCursor('debug')(ctx, env.goCtx)([]);
295+
assert.strictEqual(result, true);
296+
297+
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
298+
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
299+
gotConfig.program = '';
300+
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
301+
name: 'Debug Test',
302+
type: 'go',
303+
request: 'launch',
304+
args: ['-test.run', '^TestExampleTestSuite$', '-testify.m', '^TestExample$'],
305+
buildFlags: '',
306+
env: {},
307+
sessionID: undefined,
308+
mode: 'test',
309+
envFile: null,
310+
program: ''
311+
});
312+
});
313+
314+
test('Debug test at cursor in different file than test suite definition', async () => {
315+
const startDebuggingStub = sinon.stub(vscode.debug, 'startDebugging').returns(Promise.resolve(true));
316+
317+
const editor = await vscode.window.showTextDocument(
318+
vscode.Uri.file(path.join(testdataDir, 'another_suite_test.go'))
319+
);
320+
editor.selection = new vscode.Selection(3, 4, 3, 4);
321+
322+
const result = await testAtCursor('debug')(ctx, env.goCtx)([]);
323+
assert.strictEqual(result, true);
324+
325+
assert.strictEqual(startDebuggingStub.callCount, 1, 'expected one call to startDebugging');
326+
const gotConfig = startDebuggingStub.getCall(0).args[1] as vscode.DebugConfiguration;
327+
gotConfig.program = '';
328+
assert.deepStrictEqual<vscode.DebugConfiguration>(gotConfig, {
329+
name: 'Debug Test',
330+
type: 'go',
331+
request: 'launch',
332+
args: ['-test.run', '^TestExampleTestSuite$', '-testify.m', '^TestExampleInAnotherFile$'],
333+
buildFlags: '',
334+
env: {},
335+
sessionID: undefined,
336+
mode: 'test',
337+
envFile: null,
338+
program: ''
339+
});
340+
});
203341
});

Diff for: extension/test/gopls/goTest.run.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import { GoTestExplorer } from '../../src/goTest/explore';
88
import { MockExtensionContext } from '../mocks/MockContext';
99
import { GoTest } from '../../src/goTest/utils';
1010
import { Env } from './goplsTestEnv.utils';
11+
import { updateGoVarsFromConfig } from '../../src/goInstallTools';
1112

1213
suite('Go Test Runner', () => {
1314
const fixtureDir = path.join(__dirname, '..', '..', '..', 'test', 'testdata');
1415

1516
let testExplorer: GoTestExplorer;
1617

18+
suiteSetup(async () => {
19+
await updateGoVarsFromConfig({});
20+
});
21+
1722
suite('parseOutput', () => {
1823
const ctx = MockExtensionContext.new();
1924
suiteSetup(async () => {

Diff for: extension/test/integration/test.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ suite('Test Go Test Args', () => {
9797
flags: ['-run', 'TestC']
9898
});
9999
});
100+
test('use -testify.m for methods', () => {
101+
runTest({
102+
expectedArgs:
103+
'test -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$ ./...',
104+
expectedOutArgs:
105+
'test -timeout 30s -run ^TestExampleTestSuite$ -testify.m ^(TestExample|TestAnotherExample)$ ./...',
106+
functions: [
107+
'(*ExampleTestSuite).TestExample',
108+
'(*ExampleTestSuite).TestAnotherExample',
109+
'TestExampleTestSuite'
110+
]
111+
});
112+
});
100113
});
101114

102115
suite('Test Go Test', function () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main_test
2+
3+
func (suite *ExampleTestSuite) TestExampleInAnotherFile() {
4+
if suite.VariableThatShouldStartAtFive != 5 {
5+
suite.T().Fatalf("%d != %d", 5, suite.VariableThatShouldStartAtFive)
6+
}
7+
}

Diff for: extension/test/testdata/stretchrTestSuite/go.mod

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
module example/a
22

3-
go 1.16
3+
go 1.21
4+
5+
require github.com/stretchr/testify v1.7.0
46

57
require (
68
github.com/davecgh/go-spew v1.1.1 // indirect
79
github.com/kr/pretty v0.1.0 // indirect
8-
github.com/stretchr/testify v1.7.0
10+
github.com/pmezard/go-difflib v1.0.0 // indirect
911
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1013
)

0 commit comments

Comments
 (0)