This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
/
goCover.ts
307 lines (279 loc) · 10.2 KB
/
goCover.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import path = require('path');
import fs = require('fs');
import rl = require('readline');
import { getTempFilePath } from './util';
import { showTestOutput, goTest, TestConfig } from './testUtils';
import { isModSupported } from './goModules';
let gutterSvgs: { [key: string]: string };
let decorators: {
type: string;
coveredGutterDecorator: vscode.TextEditorDecorationType;
uncoveredGutterDecorator: vscode.TextEditorDecorationType;
coveredHighlightDecorator: vscode.TextEditorDecorationType;
uncoveredHighlightDecorator: vscode.TextEditorDecorationType;
};
let decoratorConfig: {
type: string;
coveredHighlightColor: string;
uncoveredHighlightColor: string;
coveredGutterStyle: string;
uncoveredGutterStyle: string;
};
/**
* Initializes the decorators used for Code coverage.
* @param ctx The extension context
*/
export function initCoverageDecorators(ctx: vscode.ExtensionContext) {
// Initialize gutter svgs
gutterSvgs = {
blockred: ctx.asAbsolutePath('images/gutter-blockred.svg'),
blockgreen: ctx.asAbsolutePath('images/gutter-blockgreen.svg'),
blockblue: ctx.asAbsolutePath('images/gutter-blockblue.svg'),
blockyellow: ctx.asAbsolutePath('images/gutter-blockyellow.svg'),
slashred: ctx.asAbsolutePath('images/gutter-slashred.svg'),
slashgreen: ctx.asAbsolutePath('images/gutter-slashgreen.svg'),
slashblue: ctx.asAbsolutePath('images/gutter-slashblue.svg'),
slashyellow: ctx.asAbsolutePath('images/gutter-slashyellow.svg'),
verticalred: ctx.asAbsolutePath('images/gutter-vertred.svg'),
verticalgreen: ctx.asAbsolutePath('images/gutter-vertgreen.svg'),
verticalblue: ctx.asAbsolutePath('images/gutter-vertblue.svg'),
verticalyellow: ctx.asAbsolutePath('images/gutter-vertyellow.svg')
};
// Update the coverageDecorator in User config, if they are using the old style.
const goConfig = vscode.workspace.getConfiguration('go');
const inspectResult = goConfig.inspect('coverageDecorator');
if (typeof inspectResult.globalValue === 'string') {
goConfig.update('coverageDecorator', { type: inspectResult.globalValue }, vscode.ConfigurationTarget.Global);
}
if (typeof inspectResult.workspaceValue === 'string') {
goConfig.update('coverageDecorator', { type: inspectResult.workspaceValue }, vscode.ConfigurationTarget.Workspace);
}
if (typeof inspectResult.workspaceFolderValue === 'string') {
goConfig.update('coverageDecorator', { type: inspectResult.workspaceValue }, vscode.ConfigurationTarget.WorkspaceFolder);
}
// Update the decorators
updateCodeCoverageDecorators(goConfig.get('coverageDecorator'));
}
/**
* Updates the decorators used for Code coverage.
* @param coverageDecoratorConfig The coverage decorated as configured by the user
*/
export function updateCodeCoverageDecorators(coverageDecoratorConfig: any) {
// These defaults are chosen to be distinguishable in nearly any color scheme (even Red)
// as well as by people who have difficulties with color perception.
decoratorConfig = {
type: 'highlight',
coveredHighlightColor: 'rgba(64,128,128,0.5)',
uncoveredHighlightColor: 'rgba(128,64,64,0.25)',
coveredGutterStyle: 'blockblue',
uncoveredGutterStyle: 'slashyellow'
};
// Update from configuration
if (typeof (coverageDecoratorConfig) === 'string') {
decoratorConfig.type = coverageDecoratorConfig;
} else {
for (let k in coverageDecoratorConfig) {
decoratorConfig[k] = coverageDecoratorConfig[k];
}
}
disposeDecorators();
setDecorators();
}
function setDecorators() {
decorators = {
type: decoratorConfig.type,
coveredGutterDecorator: vscode.window.createTextEditorDecorationType({ gutterIconPath: gutterSvgs[decoratorConfig.coveredGutterStyle] }),
uncoveredGutterDecorator: vscode.window.createTextEditorDecorationType({ gutterIconPath: gutterSvgs[decoratorConfig.uncoveredGutterStyle] }),
coveredHighlightDecorator: vscode.window.createTextEditorDecorationType({ backgroundColor: decoratorConfig.coveredHighlightColor }),
uncoveredHighlightDecorator: vscode.window.createTextEditorDecorationType({ backgroundColor: decoratorConfig.uncoveredHighlightColor })
};
}
/**
* Disposes decorators so that the current coverage is removed from the editor.
*/
function disposeDecorators() {
if (decorators) {
decorators.coveredGutterDecorator.dispose();
decorators.uncoveredGutterDecorator.dispose();
decorators.coveredHighlightDecorator.dispose();
decorators.uncoveredHighlightDecorator.dispose();
}
}
interface CoverageData {
uncoveredRange: vscode.Range[];
coveredRange: vscode.Range[];
}
let coverageFiles: { [key: string]: CoverageData } = {};
let isCoverageApplied: boolean = false;
/**
* Clear the coverage on all files
*/
function clearCoverage() {
coverageFiles = {};
disposeDecorators();
isCoverageApplied = false;
}
/**
* Extract the coverage data from the given cover profile & apply them on the files in the open editors.
* @param coverProfilePath Path to the file that has the cover profile data
* @param packageDirPath Absolute path of the package for which the coverage was calculated
*/
export function applyCodeCoverageToAllEditors(coverProfilePath: string, packageDirPath: string): Promise<void> {
return new Promise((resolve, reject) => {
try {
// Clear existing coverage files
clearCoverage();
let lines = rl.createInterface({
input: fs.createReadStream(coverProfilePath),
output: undefined
});
lines.on('line', function (data: string) {
// go test coverageprofile generates output:
// filename:StartLine.StartColumn,EndLine.EndColumn Hits CoverCount
// The first line will be "mode: set" which will be ignored
let fileRange = data.match(/([^:]+)\:([\d]+)\.([\d]+)\,([\d]+)\.([\d]+)\s([\d]+)\s([\d]+)/);
if (!fileRange) return;
let filePath = path.join(packageDirPath, path.basename(fileRange[1]));
let coverage = getCoverageData(filePath);
let range = new vscode.Range(
// Start Line converted to zero based
parseInt(fileRange[2]) - 1,
// Start Column converted to zero based
parseInt(fileRange[3]) - 1,
// End Line converted to zero based
parseInt(fileRange[4]) - 1,
// End Column converted to zero based
parseInt(fileRange[5]) - 1
);
// If is Covered (CoverCount > 0)
if (parseInt(fileRange[7]) > 0) {
coverage.coveredRange.push(range);
}
// Not Covered
else {
coverage.uncoveredRange.push(range);
}
setCoverageData(filePath, coverage);
});
lines.on('close', () => {
vscode.window.visibleTextEditors.forEach(applyCodeCoverage);
resolve();
});
} catch (e) {
vscode.window.showInformationMessage(e.msg);
reject(e);
}
});
}
/**
* Get the object that holds the coverage data for given file path.
* @param filePath
*/
function getCoverageData(filePath: string): CoverageData {
if (filePath.startsWith('_')) {
filePath = filePath.substr(1);
}
if (process.platform === 'win32') {
const parts = filePath.split('/');
if (parts.length) {
filePath = parts.join(path.sep);
}
}
return coverageFiles[filePath] || { coveredRange: [], uncoveredRange: [] };
}
/**
* Set the object that holds the coverage data for given file path.
* @param filePath
* @param data
*/
function setCoverageData(filePath: string, data: CoverageData) {
if (filePath.startsWith('_')) {
filePath = filePath.substr(1);
}
if (process.platform === 'win32') {
const parts = filePath.split('/');
if (parts.length) {
filePath = parts.join(path.sep);
}
}
coverageFiles[filePath] = data;
}
/**
* Apply the code coverage highlighting in given editor
* @param editor
*/
export function applyCodeCoverage(editor: vscode.TextEditor) {
if (!editor || editor.document.languageId !== 'go' || editor.document.fileName.endsWith('_test.go')) {
return;
}
const cfg = vscode.workspace.getConfiguration('go', editor.document.uri);
const coverageOptions = cfg['coverageOptions'];
setDecorators();
for (let filename in coverageFiles) {
if (editor.document.uri.fsPath.endsWith(filename)) {
isCoverageApplied = true;
const coverageData = coverageFiles[filename];
if (coverageOptions === 'showCoveredCodeOnly' || coverageOptions === 'showBothCoveredAndUncoveredCode') {
editor.setDecorations(decorators.type === 'gutter' ? decorators.coveredGutterDecorator : decorators.coveredHighlightDecorator, coverageData.coveredRange);
}
if (coverageOptions === 'showUncoveredCodeOnly' || coverageOptions === 'showBothCoveredAndUncoveredCode') {
editor.setDecorations(decorators.type === 'gutter' ? decorators.uncoveredGutterDecorator : decorators.uncoveredHighlightDecorator, coverageData.uncoveredRange);
}
}
}
}
/**
* Listener for change in the editor.
* A change in a Go file means the coverage data is stale. Therefore it should be cleared.
* @param e TextDocumentChangeEvent
*/
export function removeCodeCoverageOnFileChange(e: vscode.TextDocumentChangeEvent) {
if (e.document.languageId !== 'go') {
return;
}
if (vscode.window.visibleTextEditors.every(editor => editor.document !== e.document)) {
return;
}
clearCoverage();
}
/**
* If current editor has Code coverage applied, then remove it.
* Else run tests to get the coverage and apply.
*/
export function toggleCoverageCurrentPackage() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (isCoverageApplied) {
clearCoverage();
return;
}
let goConfig = vscode.workspace.getConfiguration('go', editor.document.uri);
let cwd = path.dirname(editor.document.uri.fsPath);
let buildFlags = goConfig['testFlags'] || goConfig['buildFlags'] || [];
let tmpCoverPath = getTempFilePath('go-code-cover');
let args = ['-coverprofile=' + tmpCoverPath, ...buildFlags];
const testConfig: TestConfig = {
goConfig: goConfig,
dir: cwd,
flags: args,
background: true
};
return isModSupported(editor.document.uri).then(isMod => {
testConfig.isMod = isMod;
return goTest(testConfig).then(success => {
if (!success) {
showTestOutput();
return [];
}
return applyCodeCoverageToAllEditors(tmpCoverPath, testConfig.dir);
});
});
}