forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage.js
More file actions
171 lines (149 loc) · 5.37 KB
/
Copy pathcoverage.js
File metadata and controls
171 lines (149 loc) · 5.37 KB
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
// @ts-check
import path from 'node:path';
import {writeFile} from '../../tasks/utils.js';
/** @typedef {{text: string; covered: boolean}} CodePart */
/**
* @param {string} code
* @param {{start: number; end: number}[]} ranges
* @returns {CodePart[]}
*/
function splitCode(code, ranges) {
/** @type {CodePart[]} */
const parts = [];
if (ranges.length === 0) {
parts.push({text: code, covered: false});
return;
}
if (ranges[0].start > 0) {
parts.push({text: code.substring(0, ranges[0].start), covered: false});
}
const lastRange = ranges[ranges.length - 1];
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
parts.push({text: code.substring(range.start, range.end), covered: true});
if (range !== lastRange) {
const nextRange = ranges[i + 1];
parts.push({text: code.substring(range.end, nextRange.start), covered: false});
}
}
if (lastRange.end < code.length) {
parts.push({text: code.substring(lastRange.end), covered: false});
}
return parts;
}
function red(/** @type {string} */text) {
return `\x1b[31m${text}\x1b[0m`;
}
function green(/** @type {string} */text) {
return `\x1b[32m${text}\x1b[0m`;
}
/**
* @param {string} code
* @param {{start: number; end: number}[]} ranges
*/
export function logCoverage(code, ranges) {
code = code.substring(0, code.indexOf('//# sourceMappingURL='));
const parts = splitCode(code, ranges);
const message = parts
.map(({text, covered}) => (covered ? green : red)(text))
.join('');
console.log(message);
}
function escapeHTML(/** @type {string} */html) {
return html
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/** @typedef {{name: string; length: number; coverage: number; parts: {text: string; covered: boolean}[]}} FileCoverageInfo */
/**
* @param {string} name
* @param {string} code
* @param {{start: number; end: number}[]} ranges
* @returns {FileCoverageInfo}
*/
function getCoverageInfo(name, code, ranges) {
code = code.substring(0, code.indexOf('//# sourceMappingURL='));
const parts = splitCode(code, ranges);
const length = code.length;
const coverage = parts.filter((p) => p.covered).reduce((sum, p) => sum + p.text.length, 0) / length;
return {name, length, coverage, parts};
}
/**
* @param {string} dir
* @param {FileCoverageInfo} info
* @returns {Promise<void>}
*/
async function generateHTMLCoverageReport(dir, info) {
const {name, coverage, parts} = info;
/** @type {string[]} */
const lines = [];
lines.push('<!DOCTYPE html>');
lines.push('<html>');
lines.push('<head>');
const title = `Coverage report: ${name}`;
lines.push(` <title>${title}</title>`);
lines.push(' <style>');
lines.push(' body { background: #111; color: #ccc; }');
lines.push(' code { background: #222; display: inline-block; white-space: pre-wrap; }');
lines.push(' .uncovered { background: #934; color: white; }');
lines.push(' </style>');
lines.push('</head>');
lines.push('<body>');
lines.push(` <h1>${title}</h1>`);
lines.push(` <h3>Covered ${(coverage * 100).toFixed(0)}%</h3>`);
lines.push(` <code>${parts.map((p) => `<span${p.covered ? '' : ' class="uncovered"'}>${escapeHTML(p.text)}</span>`).join('')}</code>`);
lines.push('</body>');
lines.push('</html>');
await writeFile(path.join(dir, `${name}.html`), lines.join('\n'));
}
/**
* @param {string} dir
* @param {FileCoverageInfo[]} info
* @returns {Promise<void>}
*/
async function generateIndexHTMLCoveragePage(dir, info) {
/** @type {string[]} */
const lines = [];
lines.push('<!DOCTYPE html>');
lines.push('<html>');
lines.push('<head>');
lines.push(` <title>Code coverage reports</title>`);
lines.push(' <style>');
lines.push(' body { background: #111; color: #ccc; }');
lines.push(' a { color: #7ae; }');
lines.push(' </style>');
lines.push('</head>');
lines.push('<body>');
lines.push(` <h1>Code coverage reports</h1>`);
const totalCoverage = info.reduce((sum, i) => sum + i.coverage * i.length, 0);
const totalLength = info.reduce((sum, i) => sum + i.length, 0);
lines.push(` <h3>Total coverage ${(totalCoverage / totalLength * 100).toFixed(0)}%</h3>`);
lines.push(' <table>');
info.forEach((i) => {
lines.push(' <tr>');
lines.push(` <td>${(i.coverage * 100).toFixed(0)}%</td>`);
lines.push(` <td><a href="${i.name}.html">${i.name}</a></td>`);
lines.push(' </tr>');
});
lines.push(' </table>');
lines.push('</body>');
lines.push('</html>');
await writeFile(path.join(dir, 'index.html'), lines.join('\n'));
}
/**
* @param {string} dir
* @param {import('puppeteer-core').CoverageEntry[]} coverage
*/
export async function generateHTMLCoverageReports(dir, coverage) {
const info = coverage
.filter(({url}) => url.startsWith('chrome-extension://'))
.map(({url, text, ranges}) => {
const name = url.replace(/^chrome-extension:\/\/.*?\//, '');
return getCoverageInfo(name, text, ranges);
});
await generateIndexHTMLCoveragePage(dir, info);
await Promise.all(info.map((i) => generateHTMLCoverageReport(dir, i)));
}