From d464a1762501fe8f07588ba64fea53e4f3b8433c Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 2 May 2018 14:28:56 -0700 Subject: [PATCH] new: support for showing coverage on single function test. closes #1637 (#1638) * new: support for showing coverage on single function test. closes #1637 * fixed: lint * new: added new coverOnTestFunction conf * fixed: rename conf flag to desired name --- package.json | 5 +++++ src/goTest.ts | 36 +++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 6c0254057..5b0cfb416 100644 --- a/package.json +++ b/package.json @@ -571,6 +571,11 @@ "default": true, "description": "If true, shows test coverage when Go: Test Package command is run." }, + "go.coverOnSingleTest": { + "type": "boolean", + "default": false, + "description": "If true, shows test coverage when Go: Test Function at cursor command is run." + }, "go.coverageOptions": { "type": "string", "enum": [ diff --git a/src/goTest.ts b/src/goTest.ts index 755e70455..2f18a5c2f 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -34,6 +34,8 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions; + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnSingleTest', args); + editor.document.save().then(() => { return getFunctions(editor.document, null).then(testFunctions => { let testFunctionName: string; @@ -60,9 +62,10 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar const testConfig = { goConfig: goConfig, dir: path.dirname(editor.document.fileName), - flags: getTestFlags(goConfig, args), + flags: testFlags, functions: [testFunctionName], - isBenchmark: isBenchmark + isBenchmark: isBenchmark, + showTestCoverage: true }; // Remember this config as the last executed test. @@ -70,7 +73,11 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar return goTest(testConfig); }); - }).then(null, err => { + }).then(success => { + if (success && tmpCoverPath) { + return getCoverage(tmpCoverPath); + } + }, err => { console.error(err); }); } @@ -87,12 +94,7 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args return; } - let tmpCoverPath = ''; - let testFlags = getTestFlags(goConfig, args) || []; - if (goConfig['coverOnTestPackage'] === true) { - tmpCoverPath = path.normalize(path.join(os.tmpdir(), 'go-code-cover')); - testFlags.push('-coverprofile=' + tmpCoverPath); - } + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestPackage', args); const testConfig = { goConfig: goConfig, @@ -188,6 +190,18 @@ export function testPrevious() { }); } +/** + * Computes the tmp coverage path and needed flags. + * + * @param goConfig Configuration for the Go extension. + */ +function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string, args: any): { tmpCoverPath: string, testFlags: string[] } { + let tmpCoverPath = ''; + let testFlags = getTestFlags(goConfig, args) || []; + if (goConfig[confFlag] === true) { + tmpCoverPath = path.normalize(path.join(os.tmpdir(), 'go-code-cover')); + testFlags.push('-coverprofile=' + tmpCoverPath); + } - - + return {tmpCoverPath, testFlags}; +}