Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
new: support for showing coverage on single function test. closes #1637
Browse files Browse the repository at this point in the history
… (#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
  • Loading branch information
primalmotion authored and ramya-rao-a committed May 2, 2018
1 parent 70bde3e commit d464a17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
36 changes: 25 additions & 11 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,17 +62,22 @@ 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.
lastTestConfig = testConfig;

return goTest(testConfig);
});
}).then(null, err => {
}).then(success => {
if (success && tmpCoverPath) {
return getCoverage(tmpCoverPath);
}
}, err => {
console.error(err);
});
}
Expand All @@ -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,
Expand Down Expand Up @@ -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};
}

0 comments on commit d464a17

Please sign in to comment.