Skip to content

Commit

Permalink
Add command Test Function At Cursor or Test Previous
Browse files Browse the repository at this point in the history
This runs `Test Function At Cursor` if a unit test is found at the
cursor, otherwise it turns `Test Previous`.
  • Loading branch information
mislav committed May 19, 2021
1 parent cf9b1c5 commit 0e3f90f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ List all the Go tools being used by this extension along with their locations.

Runs a unit test at the cursor.

### `Go: Test Function At Cursor or Test Previous`

Runs a unit test at the cursor if one is found; otherwise re-runs the last executed test.

### `Go: Subtest At Cursor`

Runs a sub test at the cursor.
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@
"title": "Go: Test Function At Cursor",
"description": "Runs a unit test at the cursor."
},
{
"command": "go.test.cursorOrPrevious",
"title": "Go: Test Function At Cursor or Test Previous",
"description": "Runs a unit test at the cursor if one is found, otherwise re-runs the last executed test."
},
{
"command": "go.subtest.cursor",
"title": "Go: Subtest At Cursor",
Expand Down
8 changes: 8 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
debugPrevious,
subTestAtCursor,
testAtCursor,
testAtCursorOrPrevious,
testCurrentFile,
testCurrentPackage,
testPrevious,
Expand Down Expand Up @@ -316,6 +317,13 @@ If you would like additional configuration for diagnostics from gopls, please se
})
);

ctx.subscriptions.push(
vscode.commands.registerCommand('go.test.cursorOrPrevious', (args) => {
const goConfig = getGoConfig();
testAtCursorOrPrevious(goConfig, 'test', args);
})
);

ctx.subscriptions.push(
vscode.commands.registerCommand('go.subtest.cursor', (args) => {
const goConfig = getGoConfig();
Expand Down
18 changes: 18 additions & 0 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestA
});
}

/**
* Executes the unit test at the primary cursor if found, otherwise re-runs the previous test.
* @param goConfig Configuration for the Go extension.
* @param cmd Whether the command is test, benchmark, or debug.
* @param args
*/
export function testAtCursorOrPrevious(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) {
_testAtCursor(goConfig, cmd, args).catch((err) => {
if (err instanceof NotFoundError) {
testPrevious();
} else if (err instanceof InformationError) {
vscode.window.showInformationMessage(err.message);
} else {
console.error(err);
}
});
}

/**
* Runs the test at cursor.
*/
Expand Down

0 comments on commit 0e3f90f

Please sign in to comment.