Feature: Load and apply a custom coverprofile (#2361)
diff --git a/package.json b/package.json
index ae0ced7..a553f05 100644
--- a/package.json
+++ b/package.json
@@ -306,6 +306,11 @@
"description": "Cancels running tests."
},
{
+ "command": "go.apply.coverprofile",
+ "title": "Go: Apply Cover Profile",
+ "description": "Applies existing cover profile."
+ },
+ {
"command": "go.godoctor.extract",
"title": "Go: Extract to function",
"description": "Extract to function using godoctor."
diff --git a/src/goMain.ts b/src/goMain.ts
index 3377c39..29a225b 100644
--- a/src/goMain.ts
+++ b/src/goMain.ts
@@ -5,6 +5,7 @@
'use strict';
+import * as path from 'path';
import vscode = require('vscode');
import { browsePackages } from './goBrowsePackage';
import { buildCode } from './goBuild';
@@ -12,6 +13,7 @@
import { GoCodeActionProvider } from './goCodeAction';
import {
applyCodeCoverage,
+ applyCodeCoverageToAllEditors,
initCoverageDecorators,
removeCodeCoverageOnFileChange,
toggleCoverageCurrentPackage,
@@ -37,7 +39,7 @@
import { GO_MODE } from './goMode';
import { addTags, removeTags } from './goModifytags';
import { GO111MODULE, isModSupported } from './goModules';
-import { clearCacheForTools } from './goPath';
+import { clearCacheForTools, fileExists } from './goPath';
import { playgroundCommand } from './goPlayground';
import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
@@ -463,6 +465,25 @@
ctx.subscriptions.push(vscode.commands.registerCommand('go.install.package', installCurrentPackage));
+ ctx.subscriptions.push(vscode.commands.registerCommand('go.apply.coverprofile', () => {
+ if (!vscode.window.activeTextEditor || !vscode.window.activeTextEditor.document.fileName.endsWith('.go')) {
+ vscode.window.showErrorMessage('Cannot apply coverage profile when no Go file is open.');
+ return;
+ }
+ vscode.window.showInputBox({
+ prompt: 'Enter the path to the coverage profile for current package'
+ }).then((coverProfilePath) => {
+ if (!coverProfilePath) {
+ return;
+ }
+ if (!fileExists(coverProfilePath)) {
+ vscode.window.showErrorMessage(`Cannot find the file ${coverProfilePath}`);
+ return;
+ }
+ applyCodeCoverageToAllEditors(coverProfilePath, path.dirname(vscode.window.activeTextEditor.document.fileName));
+ });
+ }));
+
vscode.languages.setLanguageConfiguration(GO_MODE.language, {
wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\#\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s]+)/g
});
diff --git a/src/goPath.ts b/src/goPath.ts
index d211ef1..caf40bb 100644
--- a/src/goPath.ts
+++ b/src/goPath.ts
@@ -87,7 +87,7 @@
return toolName;
}
-function fileExists(filePath: string): boolean {
+export function fileExists(filePath: string): boolean {
let exists = true;
try {
exists = fs.statSync(filePath).isFile();