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
385 lines (349 loc) · 11.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import fs = require('fs');
import path = require('path');
import rl = require('readline');
import vscode = require('vscode');
import { isModSupported } from './goModules';
import { getTestFlags, goTest, showTestOutput, TestConfig } from './testUtils';
import { getGoConfig, getTempFilePath } from './util';
let gutterSvgs: { [key: string]: string };
let decorators: {
type: string;
coveredGutterDecorator: vscode.TextEditorDecorationType;
uncoveredGutterDecorator: vscode.TextEditorDecorationType;
coveredHighlightDecorator: vscode.TextEditorDecorationType;
uncoveredHighlightDecorator: vscode.TextEditorDecorationType;
};
let decoratorConfig: {
[key: string]: any;
type: string;
coveredHighlightColor: string;
uncoveredHighlightColor: string;
coveredGutterStyle: string;
uncoveredGutterStyle: string;
};
// a list of modified, unsaved go files with actual code edits (rather than comment edits)
let modifiedFiles: {
[key: string]: boolean;
} = {};
/**
* 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 = getGoConfig();
const inspectResult = goConfig.inspect('coverageDecorator');
if (inspectResult) {
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 (const k in coverageDecoratorConfig) {
if (coverageDecoratorConfig.hasOwnProperty(k)) {
decoratorConfig[k] = coverageDecoratorConfig[k];
}
}
}
setDecorators();
vscode.window.visibleTextEditors.forEach(applyCodeCoverage);
}
function setDecorators() {
disposeDecorators();
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();
const lines = rl.createInterface({
input: fs.createReadStream(coverProfilePath),
output: undefined
});
lines.on('line', (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
const fileRange = data.match(/([^:]+)\:([\d]+)\.([\d]+)\,([\d]+)\.([\d]+)\s([\d]+)\s([\d]+)/);
if (!fileRange) {
return;
}
const filePath = path.join(packageDirPath, path.basename(fileRange[1]));
const coverage = getCoverageData(filePath);
const range = new vscode.Range(
// Start Line converted to zero based
parseInt(fileRange[2], 10) - 1,
// Start Column converted to zero based
parseInt(fileRange[3], 10) - 1,
// End Line converted to zero based
parseInt(fileRange[4], 10) - 1,
// End Column converted to zero based
parseInt(fileRange[5], 10) - 1
);
// If is Covered (CoverCount > 0)
if (parseInt(fileRange[7], 10) > 0) {
coverage.coveredRange.push(range);
} else {
coverage.uncoveredRange.push(range);
}
setCoverageData(filePath, coverage);
});
lines.on('close', () => {
setDecorators();
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 = getGoConfig(editor.document.uri);
const coverageOptions = cfg['coverageOptions'];
for (const 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 file save that clears potential stale coverage data.
* Local cache tracks files with changes outside of comments to determine
* files for which the save event can cause stale coverage data.
* @param e TextDocument
*/
export function removeCodeCoverageOnFileSave(e: vscode.TextDocument) {
if (e.languageId !== 'go' || !isCoverageApplied) {
return;
}
if (vscode.window.visibleTextEditors.every((editor) => editor.document !== e)) {
return;
}
if (modifiedFiles[e.fileName]) {
clearCoverage();
modifiedFiles = {}; // reset the list of modified files
}
}
/**
* Listener for file change that tracks files with changes outside of comments
* to determine files for which an eventual save can cause stale coverage data.
* @param e TextDocumentChangeEvent
*/
export function trackCodeCoverageRemovalOnFileChange(e: vscode.TextDocumentChangeEvent) {
if (e.document.languageId !== 'go' || !e.contentChanges.length || !isCoverageApplied) {
return;
}
if (vscode.window.visibleTextEditors.every((editor) => editor.document !== e.document)) {
return;
}
if (isPartOfComment(e)) {
return;
}
modifiedFiles[e.document.fileName] = true;
}
/**
* If current editor has Code coverage applied, then remove it.
* Else run tests to get the coverage and apply.
*/
export async function toggleCoverageCurrentPackage() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (isCoverageApplied) {
clearCoverage();
return;
}
const goConfig = getGoConfig();
const cwd = path.dirname(editor.document.uri.fsPath);
const testFlags = getTestFlags(goConfig);
const isMod = await isModSupported(editor.document.uri);
const testConfig: TestConfig = {
goConfig,
dir: cwd,
flags: testFlags,
background: true,
isMod,
applyCodeCoverage: true
};
return goTest(testConfig).then((success) => {
if (!success) {
showTestOutput();
}
});
}
export function isPartOfComment(e: vscode.TextDocumentChangeEvent): boolean {
return e.contentChanges.every((change) => {
// We cannot be sure with using just regex on individual lines whether a multi line change is part of a comment or not
// So play it safe and treat it as not a comment
if (!change.range.isSingleLine || change.text.includes('\n')) {
return false;
}
const text = e.document.lineAt(change.range.start).text;
const idx = text.search('//');
return idx > -1 && idx <= change.range.start.character;
});
}