Skip to content

Commit 82d2b5d

Browse files
mislavhyangah
authored andcommitted
src/goTest: Add command Test Function At Cursor or Test Previous
This runs `Test Function At Cursor` if a unit test is found at the cursor, otherwise it turns `Test Previous`. Fixes #1508 Change-Id: I91305797dfeccd79c1f5ea14298a60803f24492b GitHub-Last-Rev: 0054155 GitHub-Pull-Request: #1509 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/320769 Trust: Michael Knyszek <mknyszek@google.com> Trust: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
1 parent 505ad8d commit 82d2b5d

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

Diff for: docs/commands.md

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ List all the Go tools being used by this extension along with their locations.
3535

3636
Runs a unit test at the cursor.
3737

38+
### `Go: Test Function At Cursor or Test Previous`
39+
40+
Runs a unit test at the cursor if one is found, otherwise re-runs the last executed test.
41+
3842
### `Go: Subtest At Cursor`
3943

4044
Runs a sub test at the cursor.

Diff for: package.json

+5
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@
201201
"title": "Go: Test Function At Cursor",
202202
"description": "Runs a unit test at the cursor."
203203
},
204+
{
205+
"command": "go.test.cursorOrPrevious",
206+
"title": "Go: Test Function At Cursor or Test Previous",
207+
"description": "Runs a unit test at the cursor if one is found, otherwise re-runs the last executed test."
208+
},
204209
{
205210
"command": "go.subtest.cursor",
206211
"title": "Go: Subtest At Cursor",

Diff for: src/goMain.ts

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
debugPrevious,
6565
subTestAtCursor,
6666
testAtCursor,
67+
testAtCursorOrPrevious,
6768
testCurrentFile,
6869
testCurrentPackage,
6970
testPrevious,
@@ -316,6 +317,13 @@ If you would like additional configuration for diagnostics from gopls, please se
316317
})
317318
);
318319

320+
ctx.subscriptions.push(
321+
vscode.commands.registerCommand('go.test.cursorOrPrevious', (args) => {
322+
const goConfig = getGoConfig();
323+
testAtCursorOrPrevious(goConfig, 'test', args);
324+
})
325+
);
326+
319327
ctx.subscriptions.push(
320328
vscode.commands.registerCommand('go.subtest.cursor', (args) => {
321329
const goConfig = getGoConfig();

Diff for: src/goTest.ts

+53-35
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,68 @@ let lastDebugWorkspaceFolder: vscode.WorkspaceFolder;
3131

3232
export type TestAtCursorCmd = 'debug' | 'test' | 'benchmark';
3333

34-
/**
35-
* Executes the unit test at the primary cursor using `go test`. Output
36-
* is sent to the 'Go' channel.
37-
* @param goConfig Configuration for the Go extension.
38-
* @param cmd Whether the command is test , benchmark or debug.
39-
* @param args
40-
*/
41-
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
34+
class NotFoundError extends Error {}
35+
36+
async function _testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
4237
const editor = vscode.window.activeTextEditor;
4338
if (!editor) {
44-
vscode.window.showInformationMessage('No editor is active.');
45-
return;
39+
throw new NotFoundError('No editor is active.');
4640
}
4741
if (!editor.document.fileName.endsWith('_test.go')) {
48-
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
49-
return;
42+
throw new NotFoundError('No tests found. Current file is not a test file.');
5043
}
5144

5245
const getFunctions = cmd === 'benchmark' ? getBenchmarkFunctions : getTestFunctions;
46+
const testFunctions = await getFunctions(editor.document, null);
47+
// We use functionName if it was provided as argument
48+
// Otherwise find any test function containing the cursor.
49+
const testFunctionName =
50+
args && args.functionName
51+
? args.functionName
52+
: testFunctions.filter((func) => func.range.contains(editor.selection.start)).map((el) => el.name)[0];
53+
if (!testFunctionName) {
54+
throw new NotFoundError('No test function found at cursor.');
55+
}
5356

54-
editor.document.save().then(async () => {
55-
try {
56-
const testFunctions = await getFunctions(editor.document, null);
57-
// We use functionName if it was provided as argument
58-
// Otherwise find any test function containing the cursor.
59-
const testFunctionName =
60-
args && args.functionName
61-
? args.functionName
62-
: testFunctions
63-
.filter((func) => func.range.contains(editor.selection.start))
64-
.map((el) => el.name)[0];
65-
if (!testFunctionName) {
66-
vscode.window.showInformationMessage('No test function found at cursor.');
67-
return;
68-
}
57+
await editor.document.save();
6958

70-
if (cmd === 'debug') {
71-
await debugTestAtCursor(editor, testFunctionName, testFunctions, goConfig);
72-
} else if (cmd === 'benchmark' || cmd === 'test') {
73-
await runTestAtCursor(editor, testFunctionName, testFunctions, goConfig, cmd, args);
74-
} else {
75-
throw new Error('Unsupported command.');
76-
}
77-
} catch (err) {
59+
if (cmd === 'debug') {
60+
return debugTestAtCursor(editor, testFunctionName, testFunctions, goConfig);
61+
} else if (cmd === 'benchmark' || cmd === 'test') {
62+
return runTestAtCursor(editor, testFunctionName, testFunctions, goConfig, cmd, args);
63+
} else {
64+
throw new Error(`Unsupported command: ${cmd}`);
65+
}
66+
}
67+
68+
/**
69+
* Executes the unit test at the primary cursor using `go test`. Output
70+
* is sent to the 'Go' channel.
71+
* @param goConfig Configuration for the Go extension.
72+
* @param cmd Whether the command is test, benchmark, or debug.
73+
* @param args
74+
*/
75+
export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
76+
_testAtCursor(goConfig, cmd, args).catch((err) => {
77+
if (err instanceof NotFoundError) {
78+
vscode.window.showInformationMessage(err.message);
79+
} else {
80+
console.error(err);
81+
}
82+
});
83+
}
84+
85+
/**
86+
* Executes the unit test at the primary cursor if found, otherwise re-runs the previous test.
87+
* @param goConfig Configuration for the Go extension.
88+
* @param cmd Whether the command is test, benchmark, or debug.
89+
* @param args
90+
*/
91+
export function testAtCursorOrPrevious(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
92+
_testAtCursor(goConfig, cmd, args).catch((err) => {
93+
if (err instanceof NotFoundError) {
94+
testPrevious();
95+
} else {
7896
console.error(err);
7997
}
8098
});

0 commit comments

Comments
 (0)