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

Feature: Load and apply a custom coverprofile #2361

Merged
merged 12 commits into from
Jan 26, 2020
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@
"title": "Go: Cancel Running Tests",
"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",
Expand Down
23 changes: 22 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

'use strict';

import * as path from 'path';
import vscode = require('vscode');
import { browsePackages } from './goBrowsePackage';
import { buildCode } from './goBuild';
import { check, notifyIfGeneratedFile, removeTestStatus } from './goCheck';
import { GoCodeActionProvider } from './goCodeAction';
import {
applyCodeCoverage,
applyCodeCoverageToAllEditors,
initCoverageDecorators,
removeCodeCoverageOnFileChange,
toggleCoverageCurrentPackage,
Expand All @@ -37,7 +39,7 @@ import { lintCode } from './goLint';
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';
Expand Down Expand Up @@ -463,6 +465,25 @@ export function activate(ctx: vscode.ExtensionContext): void {

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
});
Expand Down
2 changes: 1 addition & 1 deletion src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function correctBinname(toolName: string) {
return toolName;
}

function fileExists(filePath: string): boolean {
export function fileExists(filePath: string): boolean {
let exists = true;
try {
exists = fs.statSync(filePath).isFile();
Expand Down