index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. /*jshint maxlen: 300 */
  6. var fs = require('fs'),
  7. path = require('path'),
  8. handlebars = require('handlebars').create(),
  9. annotator = require('./annotator'),
  10. helpers = require('./helpers'),
  11. templateFor = function (name) {
  12. return handlebars.compile(fs.readFileSync(path.resolve(__dirname, 'templates', name + '.txt'), 'utf8'));
  13. },
  14. headerTemplate = templateFor('head'),
  15. footerTemplate = templateFor('foot'),
  16. detailTemplate = handlebars.compile([
  17. '<tr>',
  18. '<td class="line-count quiet">{{#show_lines}}{{maxLines}}{{/show_lines}}</td>',
  19. '<td class="line-coverage quiet">{{#show_line_execution_counts lineCoverage}}{{maxLines}}{{/show_line_execution_counts}}</td>',
  20. '<td class="text"><pre class="prettyprint lang-js">{{#show_code annotatedCode}}{{/show_code}}</pre></td>',
  21. '</tr>\n'
  22. ].join('')),
  23. summaryTableHeader = [
  24. '<div class="pad1">',
  25. '<table class="coverage-summary">',
  26. '<thead>',
  27. '<tr>',
  28. ' <th data-col="file" data-fmt="html" data-html="true" class="file">File</th>',
  29. ' <th data-col="pic" data-type="number" data-fmt="html" data-html="true" class="pic"></th>',
  30. ' <th data-col="statements" data-type="number" data-fmt="pct" class="pct">Statements</th>',
  31. ' <th data-col="statements_raw" data-type="number" data-fmt="html" class="abs"></th>',
  32. ' <th data-col="branches" data-type="number" data-fmt="pct" class="pct">Branches</th>',
  33. ' <th data-col="branches_raw" data-type="number" data-fmt="html" class="abs"></th>',
  34. ' <th data-col="functions" data-type="number" data-fmt="pct" class="pct">Functions</th>',
  35. ' <th data-col="functions_raw" data-type="number" data-fmt="html" class="abs"></th>',
  36. ' <th data-col="lines" data-type="number" data-fmt="pct" class="pct">Lines</th>',
  37. ' <th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>',
  38. '</tr>',
  39. '</thead>',
  40. '<tbody>'
  41. ].join('\n'),
  42. summaryLineTemplate = handlebars.compile([
  43. '<tr>',
  44. '<td class="file {{reportClasses.statements}}" data-value="{{file}}"><a href="{{output}}">{{file}}</a></td>',
  45. '<td data-value="{{metrics.statements.pct}}" class="pic {{reportClasses.statements}}"><div class="chart">{{#show_picture}}{{metrics.statements.pct}}{{/show_picture}}</div></td>',
  46. '<td data-value="{{metrics.statements.pct}}" class="pct {{reportClasses.statements}}">{{metrics.statements.pct}}%</td>',
  47. '<td data-value="{{metrics.statements.total}}" class="abs {{reportClasses.statements}}">{{metrics.statements.covered}}/{{metrics.statements.total}}</td>',
  48. '<td data-value="{{metrics.branches.pct}}" class="pct {{reportClasses.branches}}">{{metrics.branches.pct}}%</td>',
  49. '<td data-value="{{metrics.branches.total}}" class="abs {{reportClasses.branches}}">{{metrics.branches.covered}}/{{metrics.branches.total}}</td>',
  50. '<td data-value="{{metrics.functions.pct}}" class="pct {{reportClasses.functions}}">{{metrics.functions.pct}}%</td>',
  51. '<td data-value="{{metrics.functions.total}}" class="abs {{reportClasses.functions}}">{{metrics.functions.covered}}/{{metrics.functions.total}}</td>',
  52. '<td data-value="{{metrics.lines.pct}}" class="pct {{reportClasses.lines}}">{{metrics.lines.pct}}%</td>',
  53. '<td data-value="{{metrics.lines.total}}" class="abs {{reportClasses.lines}}">{{metrics.lines.covered}}/{{metrics.lines.total}}</td>',
  54. '</tr>\n'
  55. ].join('\n\t')),
  56. summaryTableFooter = [
  57. '</tbody>',
  58. '</table>',
  59. '</div>'
  60. ].join('\n');
  61. helpers.registerHelpers(handlebars);
  62. var standardLinkMapper = {
  63. getPath: function (node) {
  64. if (typeof node === 'string') {
  65. return node;
  66. }
  67. var filePath = node.getQualifiedName();
  68. if (node.isSummary()) {
  69. if (filePath !== '') {
  70. filePath += "/index.html";
  71. } else {
  72. filePath = "index.html";
  73. }
  74. } else {
  75. filePath += ".html";
  76. }
  77. return filePath;
  78. },
  79. relativePath: function (source, target) {
  80. var targetPath = this.getPath(target),
  81. sourcePath = path.dirname(this.getPath(source));
  82. return path.relative(sourcePath, targetPath);
  83. },
  84. assetPath: function (node, name) {
  85. return this.relativePath(this.getPath(node), name);
  86. }
  87. };
  88. function getBreadcrumbHtml(node, linkMapper) {
  89. var parent = node.getParent(),
  90. nodePath = [],
  91. linkPath;
  92. while (parent) {
  93. nodePath.push(parent);
  94. parent = parent.getParent();
  95. }
  96. linkPath = nodePath.map(function (ancestor) {
  97. var target = linkMapper.relativePath(node, ancestor),
  98. name = ancestor.getRelativeName() || 'All files';
  99. return '<a href="' + target + '">' + name + '</a>';
  100. });
  101. linkPath.reverse();
  102. return linkPath.length > 0 ? linkPath.join(' / ') + ' ' +
  103. node.getRelativeName() : 'All files';
  104. }
  105. function fillTemplate(node, templateData, linkMapper, context) {
  106. var summary = node.getCoverageSummary();
  107. templateData.entity = node.getQualifiedName() || 'All files';
  108. templateData.metrics = summary;
  109. templateData.reportClass = context.classForPercent('statements', summary.statements.pct);
  110. templateData.pathHtml = getBreadcrumbHtml(node, linkMapper);
  111. templateData.base = {
  112. css: linkMapper.assetPath(node, 'base.css')
  113. };
  114. templateData.sorter = {
  115. js: linkMapper.assetPath(node, 'sorter.js'),
  116. image: linkMapper.assetPath(node, 'sort-arrow-sprite.png')
  117. };
  118. templateData.prettify = {
  119. js: linkMapper.assetPath(node, 'prettify.js'),
  120. css: linkMapper.assetPath(node, 'prettify.css')
  121. };
  122. }
  123. function HtmlReport(opts) {
  124. this.verbose = opts.verbose;
  125. this.linkMapper = opts.linkMapper || standardLinkMapper;
  126. this.subdir = opts.subdir || '';
  127. this.date = Date();
  128. }
  129. HtmlReport.prototype.getTemplateData = function () {
  130. return { datetime: this.date };
  131. };
  132. HtmlReport.prototype.getWriter = function (context) {
  133. if (!this.subdir) {
  134. return context.writer;
  135. }
  136. return context.writer.writerForDir(this.subdir);
  137. };
  138. HtmlReport.prototype.onStart = function (root, context) {
  139. var that = this,
  140. copyAssets = function (subdir, writer) {
  141. var srcDir = path.resolve(__dirname, 'assets', subdir);
  142. fs.readdirSync(srcDir).forEach(function (f) {
  143. var resolvedSource = path.resolve(srcDir, f),
  144. resolvedDestination = '.',
  145. stat = fs.statSync(resolvedSource),
  146. dest;
  147. if (stat.isFile()) {
  148. dest = resolvedDestination + '/' + f;
  149. if (this.verbose) {
  150. console.log('Write asset: ' + dest);
  151. }
  152. writer.copyFile(resolvedSource, dest);
  153. }
  154. });
  155. };
  156. ['.', 'vendor'].forEach(function (subdir) {
  157. copyAssets(subdir, that.getWriter(context));
  158. });
  159. };
  160. HtmlReport.prototype.onSummary = function (node, context) {
  161. var linkMapper = this.linkMapper,
  162. templateData = this.getTemplateData(),
  163. children = node.getChildren(),
  164. cw;
  165. fillTemplate(node, templateData, linkMapper, context);
  166. cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
  167. cw.write(headerTemplate(templateData));
  168. cw.write(summaryTableHeader);
  169. children.forEach(function (child) {
  170. var metrics = child.getCoverageSummary(),
  171. reportClasses = {
  172. statements: context.classForPercent('statements', metrics.statements.pct),
  173. lines: context.classForPercent('lines', metrics.lines.pct),
  174. functions: context.classForPercent('functions', metrics.functions.pct),
  175. branches: context.classForPercent('branches', metrics.branches.pct)
  176. },
  177. data = {
  178. metrics: metrics,
  179. reportClasses: reportClasses,
  180. file: child.getRelativeName(),
  181. output: linkMapper.relativePath(node, child)
  182. };
  183. cw.write(summaryLineTemplate(data) + '\n');
  184. });
  185. cw.write(summaryTableFooter);
  186. cw.write(footerTemplate(templateData));
  187. cw.close();
  188. };
  189. HtmlReport.prototype.onDetail = function (node, context) {
  190. var linkMapper = this.linkMapper,
  191. templateData = this.getTemplateData(),
  192. cw;
  193. fillTemplate(node, templateData, linkMapper, context);
  194. cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
  195. cw.write(headerTemplate(templateData));
  196. cw.write('<pre><table class="coverage">\n');
  197. cw.write(detailTemplate(annotator.annotateSourceCode(node.getFileCoverage(), context)));
  198. cw.write('</table></pre>\n');
  199. cw.write(footerTemplate(templateData));
  200. cw.close();
  201. };
  202. module.exports = HtmlReport;