| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
const fs = require('fs'); |
| 7 |
const path = require('path'); |
| 8 |
const html = require('html-escaper'); |
| 9 |
const { ReportBase } = require('istanbul-lib-report'); |
| 10 |
const annotator = require('./annotator'); |
| 11 |
|
| 12 |
function htmlHead(details) { |
| 13 |
return ` |
| 14 |
<head> |
| 15 |
<title>Code coverage report for ${html.escape(details.entity)}</title> |
| 16 |
<meta charset="utf-8" /> |
| 17 |
<link rel="stylesheet" href="${html.escape(details.prettify.css)}" /> |
| 18 |
<link rel="stylesheet" href="${html.escape(details.base.css)}" /> |
| 19 |
<link rel="shortcut icon" type="image/x-icon" href="${html.escape( |
| 20 |
details.favicon |
| 21 |
)}" /> |
| 22 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 23 |
<style type='text/css'> |
| 24 |
.coverage-summary .sorter { |
| 25 |
background-image: url(${html.escape(details.sorter.image)}); |
| 26 |
} |
| 27 |
</style> |
| 28 |
</head> |
| 29 |
`; |
| 30 |
} |
| 31 |
|
| 32 |
function headerTemplate(details) { |
| 33 |
function metricsTemplate({ pct, covered, total }, kind) { |
| 34 |
return ` |
| 35 |
<div class='fl pad1y space-right2'> |
| 36 |
<span class="strong">${pct}% </span> |
| 37 |
<span class="quiet">${kind}</span> |
| 38 |
<span class='fraction'>${covered}/${total}</span> |
| 39 |
</div> |
| 40 |
`; |
| 41 |
} |
| 42 |
|
| 43 |
function skipTemplate(metrics) { |
| 44 |
const statements = metrics.statements.skipped; |
| 45 |
const branches = metrics.branches.skipped; |
| 46 |
const functions = metrics.functions.skipped; |
| 47 |
|
| 48 |
const countLabel = (c, label, plural) => |
| 49 |
c === 0 ? [] : `${c} ${label}${c === 1 ? '' : plural}`; |
| 50 |
const skips = [].concat( |
| 51 |
countLabel(statements, 'statement', 's'), |
| 52 |
countLabel(functions, 'function', 's'), |
| 53 |
countLabel(branches, 'branch', 'es') |
| 54 |
); |
| 55 |
|
| 56 |
if (skips.length === 0) { |
| 57 |
return ''; |
| 58 |
} |
| 59 |
|
| 60 |
return ` |
| 61 |
<div class='fl pad1y'> |
| 62 |
<span class="strong">${skips.join(', ')}</span> |
| 63 |
<span class="quiet">Ignored</span> |
| 64 |
</div> |
| 65 |
`; |
| 66 |
} |
| 67 |
|
| 68 |
return ` |
| 69 |
<!doctype html> |
| 70 |
<html lang="en"> |
| 71 |
${htmlHead(details)} |
| 72 |
<body> |
| 73 |
<div class='wrapper'> |
| 74 |
<div class='pad1'> |
| 75 |
<h1>${details.pathHtml}</h1> |
| 76 |
<div class='clearfix'> |
| 77 |
${metricsTemplate(details.metrics.statements, 'Statements')} |
| 78 |
${metricsTemplate(details.metrics.branches, 'Branches')} |
| 79 |
${metricsTemplate(details.metrics.functions, 'Functions')} |
| 80 |
${metricsTemplate(details.metrics.lines, 'Lines')} |
| 81 |
${skipTemplate(details.metrics)} |
| 82 |
</div> |
| 83 |
<p class="quiet"> |
| 84 |
Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block. |
| 85 |
</p> |
| 86 |
<template id="filterTemplate"> |
| 87 |
<div class="quiet"> |
| 88 |
Filter: |
| 89 |
<input oninput="onInput()" type="search" id="fileSearch"> |
| 90 |
</div> |
| 91 |
</template> |
| 92 |
</div> |
| 93 |
<div class='status-line ${details.reportClass}'></div> |
| 94 |
`; |
| 95 |
} |
| 96 |
|
| 97 |
function footerTemplate(details) { |
| 98 |
return ` |
| 99 |
<div class='push'></div><!-- for sticky footer --> |
| 100 |
</div><!-- /wrapper --> |
| 101 |
<div class='footer quiet pad2 space-top1 center small'> |
| 102 |
Code coverage generated by |
| 103 |
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a> |
| 104 |
at ${html.escape(details.datetime)} |
| 105 |
</div> |
| 106 |
<script src="${html.escape(details.prettify.js)}"></script> |
| 107 |
<script> |
| 108 |
window.onload = function () { |
| 109 |
prettyPrint(); |
| 110 |
}; |
| 111 |
</script> |
| 112 |
<script src="${html.escape(details.sorter.js)}"></script> |
| 113 |
<script src="${html.escape(details.blockNavigation.js)}"></script> |
| 114 |
</body> |
| 115 |
</html> |
| 116 |
`; |
| 117 |
} |
| 118 |
|
| 119 |
function detailTemplate(data) { |
| 120 |
const lineNumbers = new Array(data.maxLines).fill().map((_, i) => i + 1); |
| 121 |
const lineLink = num => |
| 122 |
`<a name='L${num}'></a><a href='#L${num}'>${num}</a>`; |
| 123 |
const lineCount = line => |
| 124 |
`<span class="cline-any cline-${line.covered}">${line.hits}</span>`; |
| 125 |
|
| 126 |
|
| 127 |
return [ |
| 128 |
'<tr>', |
| 129 |
`<td class="line-count quiet">${lineNumbers |
| 130 |
.map(lineLink) |
| 131 |
.join('\n')}</td>`, |
| 132 |
`<td class="line-coverage quiet">${data.lineCoverage |
| 133 |
.map(lineCount) |
| 134 |
.join('\n')}</td>`, |
| 135 |
`<td class="text"><pre class="prettyprint lang-js">${data.annotatedCode.join( |
| 136 |
'\n' |
| 137 |
)}</pre></td>`, |
| 138 |
'</tr>' |
| 139 |
].join(''); |
| 140 |
} |
| 141 |
const summaryTableHeader = [ |
| 142 |
'<div class="pad1">', |
| 143 |
'<table class="coverage-summary">', |
| 144 |
'<thead>', |
| 145 |
'<tr>', |
| 146 |
' <th data-col="file" data-fmt="html" data-html="true" class="file">File</th>', |
| 147 |
' <th data-col="pic" data-type="number" data-fmt="html" data-html="true" class="pic"></th>', |
| 148 |
' <th data-col="statements" data-type="number" data-fmt="pct" class="pct">Statements</th>', |
| 149 |
' <th data-col="statements_raw" data-type="number" data-fmt="html" class="abs"></th>', |
| 150 |
' <th data-col="branches" data-type="number" data-fmt="pct" class="pct">Branches</th>', |
| 151 |
' <th data-col="branches_raw" data-type="number" data-fmt="html" class="abs"></th>', |
| 152 |
' <th data-col="functions" data-type="number" data-fmt="pct" class="pct">Functions</th>', |
| 153 |
' <th data-col="functions_raw" data-type="number" data-fmt="html" class="abs"></th>', |
| 154 |
' <th data-col="lines" data-type="number" data-fmt="pct" class="pct">Lines</th>', |
| 155 |
' <th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>', |
| 156 |
'</tr>', |
| 157 |
'</thead>', |
| 158 |
'<tbody>' |
| 159 |
].join('\n'); |
| 160 |
|
| 161 |
function summaryLineTemplate(details) { |
| 162 |
const { reportClasses, metrics, file, output } = details; |
| 163 |
const percentGraph = pct => { |
| 164 |
if (!isFinite(pct)) { |
| 165 |
return ''; |
| 166 |
} |
| 167 |
|
| 168 |
const cls = ['cover-fill']; |
| 169 |
if (pct === 100) { |
| 170 |
cls.push('cover-full'); |
| 171 |
} |
| 172 |
|
| 173 |
pct = Math.floor(pct); |
| 174 |
return [ |
| 175 |
`<div class="${cls.join(' ')}" style="width: ${pct}%"></div>`, |
| 176 |
`<div class="cover-empty" style="width: ${100 - pct}%"></div>` |
| 177 |
].join(''); |
| 178 |
}; |
| 179 |
const summaryType = (type, showGraph = false) => { |
| 180 |
const info = metrics[type]; |
| 181 |
const reportClass = reportClasses[type]; |
| 182 |
const result = [ |
| 183 |
`<td data-value="${info.pct}" class="pct ${reportClass}">${info.pct}%</td>`, |
| 184 |
`<td data-value="${info.total}" class="abs ${reportClass}">${info.covered}/${info.total}</td>` |
| 185 |
]; |
| 186 |
if (showGraph) { |
| 187 |
result.unshift( |
| 188 |
`<td data-value="${info.pct}" class="pic ${reportClass}">`, |
| 189 |
`<div class="chart">${percentGraph(info.pct)}</div>`, |
| 190 |
`</td>` |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
return result; |
| 195 |
}; |
| 196 |
|
| 197 |
return [] |
| 198 |
.concat( |
| 199 |
'<tr>', |
| 200 |
`<td class="file ${ |
| 201 |
reportClasses.statements |
| 202 |
}" data-value="${html.escape(file)}"><a href="${html.escape( |
| 203 |
output |
| 204 |
)}">${html.escape(file)}</a></td>`, |
| 205 |
summaryType('statements', true), |
| 206 |
summaryType('branches'), |
| 207 |
summaryType('functions'), |
| 208 |
summaryType('lines'), |
| 209 |
'</tr>\n' |
| 210 |
) |
| 211 |
.join('\n\t'); |
| 212 |
} |
| 213 |
|
| 214 |
const summaryTableFooter = ['</tbody>', '</table>', '</div>'].join('\n'); |
| 215 |
const emptyClasses = { |
| 216 |
statements: 'empty', |
| 217 |
lines: 'empty', |
| 218 |
functions: 'empty', |
| 219 |
branches: 'empty' |
| 220 |
}; |
| 221 |
|
| 222 |
const standardLinkMapper = { |
| 223 |
getPath(node) { |
| 224 |
if (typeof node === 'string') { |
| 225 |
return node; |
| 226 |
} |
| 227 |
let filePath = node.getQualifiedName(); |
| 228 |
if (node.isSummary()) { |
| 229 |
if (filePath !== '') { |
| 230 |
filePath += '/index.html'; |
| 231 |
} else { |
| 232 |
filePath = 'index.html'; |
| 233 |
} |
| 234 |
} else { |
| 235 |
filePath += '.html'; |
| 236 |
} |
| 237 |
return filePath; |
| 238 |
}, |
| 239 |
|
| 240 |
relativePath(source, target) { |
| 241 |
const targetPath = this.getPath(target); |
| 242 |
const sourcePath = path.dirname(this.getPath(source)); |
| 243 |
return path.posix.relative(sourcePath, targetPath); |
| 244 |
}, |
| 245 |
|
| 246 |
assetPath(node, name) { |
| 247 |
return this.relativePath(this.getPath(node), name); |
| 248 |
} |
| 249 |
}; |
| 250 |
|
| 251 |
function fixPct(metrics) { |
| 252 |
Object.keys(emptyClasses).forEach(key => { |
| 253 |
metrics[key].pct = 0; |
| 254 |
}); |
| 255 |
return metrics; |
| 256 |
} |
| 257 |
|
| 258 |
class HtmlReport extends ReportBase { |
| 259 |
constructor(opts) { |
| 260 |
super(); |
| 261 |
|
| 262 |
this.verbose = opts.verbose; |
| 263 |
this.linkMapper = opts.linkMapper || standardLinkMapper; |
| 264 |
this.subdir = opts.subdir || ''; |
| 265 |
this.date = Date(); |
| 266 |
this.skipEmpty = opts.skipEmpty; |
| 267 |
} |
| 268 |
|
| 269 |
getBreadcrumbHtml(node) { |
| 270 |
let parent = node.getParent(); |
| 271 |
const nodePath = []; |
| 272 |
|
| 273 |
while (parent) { |
| 274 |
nodePath.push(parent); |
| 275 |
parent = parent.getParent(); |
| 276 |
} |
| 277 |
|
| 278 |
const linkPath = nodePath.map(ancestor => { |
| 279 |
const target = this.linkMapper.relativePath(node, ancestor); |
| 280 |
const name = ancestor.getRelativeName() || 'All files'; |
| 281 |
return '<a href="' + target + '">' + name + '</a>'; |
| 282 |
}); |
| 283 |
|
| 284 |
linkPath.reverse(); |
| 285 |
return linkPath.length > 0 |
| 286 |
? linkPath.join(' / ') + ' ' + node.getRelativeName() |
| 287 |
: 'All files'; |
| 288 |
} |
| 289 |
|
| 290 |
fillTemplate(node, templateData, context) { |
| 291 |
const linkMapper = this.linkMapper; |
| 292 |
const summary = node.getCoverageSummary(); |
| 293 |
templateData.entity = node.getQualifiedName() || 'All files'; |
| 294 |
templateData.metrics = summary; |
| 295 |
templateData.reportClass = context.classForPercent( |
| 296 |
'statements', |
| 297 |
summary.statements.pct |
| 298 |
); |
| 299 |
templateData.pathHtml = this.getBreadcrumbHtml(node); |
| 300 |
templateData.base = { |
| 301 |
css: linkMapper.assetPath(node, 'base.css') |
| 302 |
}; |
| 303 |
templateData.sorter = { |
| 304 |
js: linkMapper.assetPath(node, 'sorter.js'), |
| 305 |
image: linkMapper.assetPath(node, 'sort-arrow-sprite.png') |
| 306 |
}; |
| 307 |
templateData.blockNavigation = { |
| 308 |
js: linkMapper.assetPath(node, 'block-navigation.js') |
| 309 |
}; |
| 310 |
templateData.prettify = { |
| 311 |
js: linkMapper.assetPath(node, 'prettify.js'), |
| 312 |
css: linkMapper.assetPath(node, 'prettify.css') |
| 313 |
}; |
| 314 |
templateData.favicon = linkMapper.assetPath(node, 'favicon.png'); |
| 315 |
} |
| 316 |
|
| 317 |
getTemplateData() { |
| 318 |
return { datetime: this.date }; |
| 319 |
} |
| 320 |
|
| 321 |
getWriter(context) { |
| 322 |
if (!this.subdir) { |
| 323 |
return context.writer; |
| 324 |
} |
| 325 |
return context.writer.writerForDir(this.subdir); |
| 326 |
} |
| 327 |
|
| 328 |
onStart(root, context) { |
| 329 |
const assetHeaders = { |
| 330 |
'.js': '/* eslint-disable */\n' |
| 331 |
}; |
| 332 |
|
| 333 |
['.', 'vendor'].forEach(subdir => { |
| 334 |
const writer = this.getWriter(context); |
| 335 |
const srcDir = path.resolve(__dirname, 'assets', subdir); |
| 336 |
fs.readdirSync(srcDir).forEach(f => { |
| 337 |
const resolvedSource = path.resolve(srcDir, f); |
| 338 |
const resolvedDestination = '.'; |
| 339 |
const stat = fs.statSync(resolvedSource); |
| 340 |
let dest; |
| 341 |
|
| 342 |
if (stat.isFile()) { |
| 343 |
dest = resolvedDestination + '/' + f; |
| 344 |
if (this.verbose) { |
| 345 |
console.log('Write asset: ' + dest); |
| 346 |
} |
| 347 |
writer.copyFile( |
| 348 |
resolvedSource, |
| 349 |
dest, |
| 350 |
assetHeaders[path.extname(f)] |
| 351 |
); |
| 352 |
} |
| 353 |
}); |
| 354 |
}); |
| 355 |
} |
| 356 |
|
| 357 |
onSummary(node, context) { |
| 358 |
const linkMapper = this.linkMapper; |
| 359 |
const templateData = this.getTemplateData(); |
| 360 |
const children = node.getChildren(); |
| 361 |
const skipEmpty = this.skipEmpty; |
| 362 |
|
| 363 |
this.fillTemplate(node, templateData, context); |
| 364 |
const cw = this.getWriter(context).writeFile(linkMapper.getPath(node)); |
| 365 |
cw.write(headerTemplate(templateData)); |
| 366 |
cw.write(summaryTableHeader); |
| 367 |
children.forEach(child => { |
| 368 |
const metrics = child.getCoverageSummary(); |
| 369 |
const isEmpty = metrics.isEmpty(); |
| 370 |
if (skipEmpty && isEmpty) { |
| 371 |
return; |
| 372 |
} |
| 373 |
const reportClasses = isEmpty |
| 374 |
? emptyClasses |
| 375 |
: { |
| 376 |
statements: context.classForPercent( |
| 377 |
'statements', |
| 378 |
metrics.statements.pct |
| 379 |
), |
| 380 |
lines: context.classForPercent( |
| 381 |
'lines', |
| 382 |
metrics.lines.pct |
| 383 |
), |
| 384 |
functions: context.classForPercent( |
| 385 |
'functions', |
| 386 |
metrics.functions.pct |
| 387 |
), |
| 388 |
branches: context.classForPercent( |
| 389 |
'branches', |
| 390 |
metrics.branches.pct |
| 391 |
) |
| 392 |
}; |
| 393 |
const data = { |
| 394 |
metrics: isEmpty ? fixPct(metrics) : metrics, |
| 395 |
reportClasses, |
| 396 |
file: child.getRelativeName(), |
| 397 |
output: linkMapper.relativePath(node, child) |
| 398 |
}; |
| 399 |
cw.write(summaryLineTemplate(data) + '\n'); |
| 400 |
}); |
| 401 |
cw.write(summaryTableFooter); |
| 402 |
cw.write(footerTemplate(templateData)); |
| 403 |
cw.close(); |
| 404 |
} |
| 405 |
|
| 406 |
onDetail(node, context) { |
| 407 |
const linkMapper = this.linkMapper; |
| 408 |
const templateData = this.getTemplateData(); |
| 409 |
|
| 410 |
this.fillTemplate(node, templateData, context); |
| 411 |
const cw = this.getWriter(context).writeFile(linkMapper.getPath(node)); |
| 412 |
cw.write(headerTemplate(templateData)); |
| 413 |
cw.write('<pre><table class="coverage">\n'); |
| 414 |
cw.write(detailTemplate(annotator(node.getFileCoverage(), context))); |
| 415 |
cw.write('</table></pre>\n'); |
| 416 |
cw.write(footerTemplate(templateData)); |
| 417 |
cw.close(); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
module.exports = HtmlReport; |