From 264fb54bfcd86e12c13749cdd15f5a92a05e9bcf Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Thu, 19 Apr 2018 17:58:18 -0700 Subject: [PATCH 1/4] new: support for showing coverage on single function test. closes #1637 --- src/goTest.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/goTest.ts b/src/goTest.ts index 755e70455..5afb66107 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, 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, 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, args: any): { tmpCoverPath: string, testFlags: string[] } { + 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); + } - - + return {tmpCoverPath, testFlags} +} From e036073e1e0c4532d32ab5a6c8c3b94338da566d Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Sat, 28 Apr 2018 16:48:49 -0700 Subject: [PATCH 2/4] fixed: lint --- src/goTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/goTest.ts b/src/goTest.ts index 5afb66107..d31958cbe 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -34,7 +34,7 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions; - const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args) + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args); editor.document.save().then(() => { return getFunctions(editor.document, null).then(testFunctions => { @@ -94,7 +94,7 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args return; } - const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args) + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args); const testConfig = { goConfig: goConfig, @@ -203,5 +203,5 @@ function makeCoverData(goConfig: vscode.WorkspaceConfiguration, args: any): { tm testFlags.push('-coverprofile=' + tmpCoverPath); } - return {tmpCoverPath, testFlags} + return {tmpCoverPath, testFlags}; } From d1c6c491a7403312f220b3fceba8ec4162635970 Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Wed, 2 May 2018 10:29:18 -0700 Subject: [PATCH 3/4] new: added new coverOnTestFunction conf --- package.json | 5 +++++ src/goTest.ts | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 6c0254057..e31ab6736 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.coverOnTestFunction": { + "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 d31958cbe..040afcfff 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -34,7 +34,7 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions; - const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args); + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestFunction', args); editor.document.save().then(() => { return getFunctions(editor.document, null).then(testFunctions => { @@ -94,7 +94,7 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args return; } - const {tmpCoverPath, testFlags } = makeCoverData(goConfig, args); + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestPackage', args); const testConfig = { goConfig: goConfig, @@ -195,10 +195,10 @@ export function testPrevious() { * * @param goConfig Configuration for the Go extension. */ -function makeCoverData(goConfig: vscode.WorkspaceConfiguration, args: any): { tmpCoverPath: string, testFlags: string[] } { +function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string, args: any): { tmpCoverPath: string, testFlags: string[] } { let tmpCoverPath = ''; let testFlags = getTestFlags(goConfig, args) || []; - if (goConfig['coverOnTestPackage'] === true) { + if (goConfig[confFlag] === true) { tmpCoverPath = path.normalize(path.join(os.tmpdir(), 'go-code-cover')); testFlags.push('-coverprofile=' + tmpCoverPath); } From bc81d3863696227edd923f0d314449759c70770d Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Wed, 2 May 2018 10:30:34 -0700 Subject: [PATCH 4/4] fixed: rename conf flag to desired name --- package.json | 2 +- src/goTest.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e31ab6736..5b0cfb416 100644 --- a/package.json +++ b/package.json @@ -571,7 +571,7 @@ "default": true, "description": "If true, shows test coverage when Go: Test Package command is run." }, - "go.coverOnTestFunction": { + "go.coverOnSingleTest": { "type": "boolean", "default": false, "description": "If true, shows test coverage when Go: Test Function at cursor command is run." diff --git a/src/goTest.ts b/src/goTest.ts index 040afcfff..2f18a5c2f 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -34,7 +34,7 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, isBenchmar const getFunctions = isBenchmark ? getBenchmarkFunctions : getTestFunctions; - const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnTestFunction', args); + const {tmpCoverPath, testFlags } = makeCoverData(goConfig, 'coverOnSingleTest', args); editor.document.save().then(() => { return getFunctions(editor.document, null).then(testFunctions => {