'use strict'; /* Copyright 2012-2015, Yahoo Inc. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ const fs = require('fs'); const path = require('path'); const html = require('html-escaper'); const { ReportBase } = require('istanbul-lib-report'); const annotator = require('./annotator'); function htmlHead(details) { return ` Code coverage report for ${html.escape(details.entity)} `; } function headerTemplate(details) { function metricsTemplate({ pct, covered, total }, kind) { return `
${pct}% ${kind} ${covered}/${total}
`; } function skipTemplate(metrics) { const statements = metrics.statements.skipped; const branches = metrics.branches.skipped; const functions = metrics.functions.skipped; const countLabel = (c, label, plural) => c === 0 ? [] : `${c} ${label}${c === 1 ? '' : plural}`; const skips = [].concat( countLabel(statements, 'statement', 's'), countLabel(functions, 'function', 's'), countLabel(branches, 'branch', 'es') ); if (skips.length === 0) { return ''; } return `
${skips.join(', ')} Ignored     
`; } return ` ${htmlHead(details)}

${details.pathHtml}

${metricsTemplate(details.metrics.statements, 'Statements')} ${metricsTemplate(details.metrics.branches, 'Branches')} ${metricsTemplate(details.metrics.functions, 'Functions')} ${metricsTemplate(details.metrics.lines, 'Lines')} ${skipTemplate(details.metrics)}

Press n or j to go to the next uncovered block, b, p or k for the previous block.

`; } function footerTemplate(details) { return `
`; } function detailTemplate(data) { const lineNumbers = new Array(data.maxLines).fill().map((_, i) => i + 1); const lineLink = num => `${num}`; const lineCount = line => `${line.hits}`; /* This is rendered in a `
`, need control of all whitespace. */
    return [
        '',
        `${lineNumbers
            .map(lineLink)
            .join('\n')}`,
        `${data.lineCoverage
            .map(lineCount)
            .join('\n')}`,
        `
${data.annotatedCode.join(
            '\n'
        )}
`, '' ].join(''); } const summaryTableHeader = [ '
', '', '', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', '' ].join('\n'); function summaryLineTemplate(details) { const { reportClasses, metrics, file, output } = details; const percentGraph = pct => { if (!isFinite(pct)) { return ''; } const cls = ['cover-fill']; if (pct === 100) { cls.push('cover-full'); } pct = Math.floor(pct); return [ `
`, `
` ].join(''); }; const summaryType = (type, showGraph = false) => { const info = metrics[type]; const reportClass = reportClasses[type]; const result = [ ``, `` ]; if (showGraph) { result.unshift( `` ); } return result; }; return [] .concat( '', ``, summaryType('statements', true), summaryType('branches'), summaryType('functions'), summaryType('lines'), '\n' ) .join('\n\t'); } const summaryTableFooter = ['', '
FileStatementsBranchesFunctionsLines
${info.pct}%${info.covered}/${info.total}`, `
${percentGraph(info.pct)}
`, `
${html.escape(file)}
', '
'].join('\n'); const emptyClasses = { statements: 'empty', lines: 'empty', functions: 'empty', branches: 'empty' }; const standardLinkMapper = { getPath(node) { if (typeof node === 'string') { return node; } let filePath = node.getQualifiedName(); if (node.isSummary()) { if (filePath !== '') { filePath += '/index.html'; } else { filePath = 'index.html'; } } else { filePath += '.html'; } return filePath; }, relativePath(source, target) { const targetPath = this.getPath(target); const sourcePath = path.dirname(this.getPath(source)); return path.posix.relative(sourcePath, targetPath); }, assetPath(node, name) { return this.relativePath(this.getPath(node), name); } }; function fixPct(metrics) { Object.keys(emptyClasses).forEach(key => { metrics[key].pct = 0; }); return metrics; } class HtmlReport extends ReportBase { constructor(opts) { super(); this.verbose = opts.verbose; this.linkMapper = opts.linkMapper || standardLinkMapper; this.subdir = opts.subdir || ''; this.date = Date(); this.skipEmpty = opts.skipEmpty; } getBreadcrumbHtml(node) { let parent = node.getParent(); const nodePath = []; while (parent) { nodePath.push(parent); parent = parent.getParent(); } const linkPath = nodePath.map(ancestor => { const target = this.linkMapper.relativePath(node, ancestor); const name = ancestor.getRelativeName() || 'All files'; return '' + name + ''; }); linkPath.reverse(); return linkPath.length > 0 ? linkPath.join(' / ') + ' ' + node.getRelativeName() : 'All files'; } fillTemplate(node, templateData, context) { const linkMapper = this.linkMapper; const summary = node.getCoverageSummary(); templateData.entity = node.getQualifiedName() || 'All files'; templateData.metrics = summary; templateData.reportClass = context.classForPercent( 'statements', summary.statements.pct ); templateData.pathHtml = this.getBreadcrumbHtml(node); templateData.base = { css: linkMapper.assetPath(node, 'base.css') }; templateData.sorter = { js: linkMapper.assetPath(node, 'sorter.js'), image: linkMapper.assetPath(node, 'sort-arrow-sprite.png') }; templateData.blockNavigation = { js: linkMapper.assetPath(node, 'block-navigation.js') }; templateData.prettify = { js: linkMapper.assetPath(node, 'prettify.js'), css: linkMapper.assetPath(node, 'prettify.css') }; templateData.favicon = linkMapper.assetPath(node, 'favicon.png'); } getTemplateData() { return { datetime: this.date }; } getWriter(context) { if (!this.subdir) { return context.writer; } return context.writer.writerForDir(this.subdir); } onStart(root, context) { const assetHeaders = { '.js': '/* eslint-disable */\n' }; ['.', 'vendor'].forEach(subdir => { const writer = this.getWriter(context); const srcDir = path.resolve(__dirname, 'assets', subdir); fs.readdirSync(srcDir).forEach(f => { const resolvedSource = path.resolve(srcDir, f); const resolvedDestination = '.'; const stat = fs.statSync(resolvedSource); let dest; if (stat.isFile()) { dest = resolvedDestination + '/' + f; if (this.verbose) { console.log('Write asset: ' + dest); } writer.copyFile( resolvedSource, dest, assetHeaders[path.extname(f)] ); } }); }); } onSummary(node, context) { const linkMapper = this.linkMapper; const templateData = this.getTemplateData(); const children = node.getChildren(); const skipEmpty = this.skipEmpty; this.fillTemplate(node, templateData, context); const cw = this.getWriter(context).writeFile(linkMapper.getPath(node)); cw.write(headerTemplate(templateData)); cw.write(summaryTableHeader); children.forEach(child => { const metrics = child.getCoverageSummary(); const isEmpty = metrics.isEmpty(); if (skipEmpty && isEmpty) { return; } const reportClasses = isEmpty ? emptyClasses : { statements: context.classForPercent( 'statements', metrics.statements.pct ), lines: context.classForPercent( 'lines', metrics.lines.pct ), functions: context.classForPercent( 'functions', metrics.functions.pct ), branches: context.classForPercent( 'branches', metrics.branches.pct ) }; const data = { metrics: isEmpty ? fixPct(metrics) : metrics, reportClasses, file: child.getRelativeName(), output: linkMapper.relativePath(node, child) }; cw.write(summaryLineTemplate(data) + '\n'); }); cw.write(summaryTableFooter); cw.write(footerTemplate(templateData)); cw.close(); } onDetail(node, context) { const linkMapper = this.linkMapper; const templateData = this.getTemplateData(); this.fillTemplate(node, templateData, context); const cw = this.getWriter(context).writeFile(linkMapper.getPath(node)); cw.write(headerTemplate(templateData)); cw.write('
\n');
        cw.write(detailTemplate(annotator(node.getFileCoverage(), context)));
        cw.write('
\n'); cw.write(footerTemplate(templateData)); cw.close(); } } module.exports = HtmlReport;