Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/unit/coverage.js
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
const minimatch = require('minimatch');
7
const fs = require('fs');
8
const path = require('path');
9
const iLibInstrument = require('istanbul-lib-instrument');
10
const iLibCoverage = require('istanbul-lib-coverage');
11
const iLibSourceMaps = require('istanbul-lib-source-maps');
12
const iLibReport = require('istanbul-lib-report');
13
const iReports = require('istanbul-reports');
14
15
const REPO_PATH = toUpperDriveLetter(path.join(__dirname, '../../'));
16
17
exports.initialize = function (loaderConfig) {
18
const instrumenter = iLibInstrument.createInstrumenter();
19
loaderConfig.nodeInstrumenter = function (contents, source) {
20
if (minimatch(source, '**/test/**')) {
21
// tests don't get instrumented
22
return contents;
23
}
24
// Try to find a .map file
25
let map = undefined;
26
try {
27
map = JSON.parse(fs.readFileSync(`${source}.map`).toString());
28
} catch (err) {
29
// missing source map...
30
}
31
try {
32
return instrumenter.instrumentSync(contents, source, map);
33
} catch (e) {
34
console.error(`Error instrumenting ${source}: ${e}`);
35
throw e;
36
}
37
};
38
};
39
40
exports.createReport = function (isSingle, coveragePath, formats) {
41
const mapStore = iLibSourceMaps.createSourceMapStore();
42
const coverageMap = iLibCoverage.createCoverageMap(global.__coverage__);
43
return mapStore.transformCoverage(coverageMap).then((transformed) => {
44
// Paths come out all broken
45
const newData = Object.create(null);
46
Object.keys(transformed.data).forEach((file) => {
47
const entry = transformed.data[file];
48
const fixedPath = fixPath(entry.path);
49
entry.data.path = fixedPath;
50
newData[fixedPath] = entry;
51
});
52
transformed.data = newData;
53
54
const context = iLibReport.createContext({
55
dir: coveragePath || path.join(REPO_PATH, `.build/coverage${isSingle ? '-single' : ''}`),
56
coverageMap: transformed
57
});
58
const tree = context.getTree('flat');
59
60
const reports = [];
61
if (formats) {
62
if (typeof formats === 'string') {
63
formats = [formats];
64
}
65
formats.forEach(format => {
66
reports.push(iReports.create(format));
67
});
68
} else if (isSingle) {
69
reports.push(iReports.create('lcovonly'));
70
} else {
71
reports.push(iReports.create('json'));
72
reports.push(iReports.create('lcov'));
73
reports.push(iReports.create('html'));
74
}
75
reports.forEach(report => tree.visit(report, context));
76
});
77
};
78
79
function toUpperDriveLetter(str) {
80
if (/^[a-z]:/.test(str)) {
81
return str.charAt(0).toUpperCase() + str.substr(1);
82
}
83
return str;
84
}
85
86
function toLowerDriveLetter(str) {
87
if (/^[A-Z]:/.test(str)) {
88
return str.charAt(0).toLowerCase() + str.substr(1);
89
}
90
return str;
91
}
92
93
function fixPath(brokenPath) {
94
const startIndex = brokenPath.lastIndexOf(REPO_PATH);
95
if (startIndex === -1) {
96
return toLowerDriveLetter(brokenPath);
97
}
98
return toLowerDriveLetter(brokenPath.substr(startIndex));
99
}
100
101