From 2b1cf62d0cce892467f21c632dbfb137f35765a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 18 May 2021 12:00:11 +0200 Subject: [PATCH 1/2] src/goTest: enable handling the "not found" case in testAtCursor --- src/goTest.ts | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/src/goTest.ts b/src/goTest.ts index e25c9d7b19..0e5cfbea1e 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -31,50 +31,53 @@ let lastDebugWorkspaceFolder: vscode.WorkspaceFolder; export type TestAtCursorCmd = 'debug' | 'test' | 'benchmark'; -/** - * Executes the unit test at the primary cursor using `go test`. Output - * is sent to the 'Go' channel. - * @param goConfig Configuration for the Go extension. - * @param cmd Whether the command is test , benchmark or debug. - * @param args - */ -export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) { +class InformationError extends Error {} +class NotFoundError extends InformationError {} + +async function _testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) { const editor = vscode.window.activeTextEditor; if (!editor) { - vscode.window.showInformationMessage('No editor is active.'); - return; + throw new InformationError('No editor is active.'); } if (!editor.document.fileName.endsWith('_test.go')) { - vscode.window.showInformationMessage('No tests found. Current file is not a test file.'); - return; + throw new NotFoundError('No tests found. Current file is not a test file.'); } const getFunctions = cmd === 'benchmark' ? getBenchmarkFunctions : getTestFunctions; + const testFunctions = await getFunctions(editor.document, null); + // We use functionName if it was provided as argument + // Otherwise find any test function containing the cursor. + const testFunctionName = + args && args.functionName + ? args.functionName + : testFunctions.filter((func) => func.range.contains(editor.selection.start)).map((el) => el.name)[0]; + if (!testFunctionName) { + throw new NotFoundError('No test function found at cursor.'); + } - editor.document.save().then(async () => { - try { - const testFunctions = await getFunctions(editor.document, null); - // We use functionName if it was provided as argument - // Otherwise find any test function containing the cursor. - const testFunctionName = - args && args.functionName - ? args.functionName - : testFunctions - .filter((func) => func.range.contains(editor.selection.start)) - .map((el) => el.name)[0]; - if (!testFunctionName) { - vscode.window.showInformationMessage('No test function found at cursor.'); - return; - } + await editor.document.save(); - if (cmd === 'debug') { - await debugTestAtCursor(editor, testFunctionName, testFunctions, goConfig); - } else if (cmd === 'benchmark' || cmd === 'test') { - await runTestAtCursor(editor, testFunctionName, testFunctions, goConfig, cmd, args); - } else { - throw new Error('Unsupported command.'); - } - } catch (err) { + if (cmd === 'debug') { + return debugTestAtCursor(editor, testFunctionName, testFunctions, goConfig); + } else if (cmd === 'benchmark' || cmd === 'test') { + return runTestAtCursor(editor, testFunctionName, testFunctions, goConfig, cmd, args); + } else { + throw new Error(`Unsupported command: ${cmd}`); + } +} + +/** + * Executes the unit test at the primary cursor using `go test`. Output + * is sent to the 'Go' channel. + * @param goConfig Configuration for the Go extension. + * @param cmd Whether the command is test, benchmark, or debug. + * @param args + */ +export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) { + _testAtCursor(goConfig, cmd, args).catch((err) => { + if (err instanceof InformationError) { + vscode.window.showInformationMessage(err.message); + } else { console.error(err); } }); From 005415566932b373a994d7d39730f6996c97dab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 18 May 2021 12:00:57 +0200 Subject: [PATCH 2/2] 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`. --- docs/commands.md | 4 ++++ package.json | 5 +++++ src/goMain.ts | 8 ++++++++ src/goTest.ts | 23 +++++++++++++++++++---- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 6486617fec..23e0821ffd 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -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. diff --git a/package.json b/package.json index faab923148..c620403457 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/goMain.ts b/src/goMain.ts index d03f8633a9..b215f0759e 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -64,6 +64,7 @@ import { debugPrevious, subTestAtCursor, testAtCursor, + testAtCursorOrPrevious, testCurrentFile, testCurrentPackage, testPrevious, @@ -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(); diff --git a/src/goTest.ts b/src/goTest.ts index 0e5cfbea1e..bbf95c85ae 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -31,13 +31,12 @@ let lastDebugWorkspaceFolder: vscode.WorkspaceFolder; export type TestAtCursorCmd = 'debug' | 'test' | 'benchmark'; -class InformationError extends Error {} -class NotFoundError extends InformationError {} +class NotFoundError extends Error {} async function _testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) { const editor = vscode.window.activeTextEditor; if (!editor) { - throw new InformationError('No editor is active.'); + throw new NotFoundError('No editor is active.'); } if (!editor.document.fileName.endsWith('_test.go')) { throw new NotFoundError('No tests found. Current file is not a test file.'); @@ -75,7 +74,7 @@ async function _testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestA */ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, cmd: TestAtCursorCmd, args: any) { _testAtCursor(goConfig, cmd, args).catch((err) => { - if (err instanceof InformationError) { + if (err instanceof NotFoundError) { vscode.window.showInformationMessage(err.message); } else { console.error(err); @@ -83,6 +82,22 @@ 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 { + console.error(err); + } + }); +} + /** * Runs the test at cursor. */