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

new: support for showing coverage on single function test. closes #1637 #1638

Merged
merged 4 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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};
}