Permalink
Browse files

Clean up html report to provide a nicer interface for middleware hacks

  • Loading branch information...
gotwarlost committed Nov 5, 2012
1 parent e1806b7 commit 4653aecd10fff8cc74e602577b29905bac5ce98a
Showing with 80 additions and 72 deletions.
  1. +80 −72 lib/report/html.js
View
@@ -136,10 +136,9 @@ function customEscape(text) {
}
handlebars.registerHelper('show_code', function (context /*, opts */) {
- var structured = context,
- array = [];
+ var array = [];
- structured.forEach(function (item) {
+ context.forEach(function (item) {
array.push(customEscape(item.text) || ' ');
});
return array.join('\n');
@@ -308,6 +307,9 @@ function HtmlReport(opts) {
this.opts = opts || {};
this.opts.dir = this.opts.dir || path.resolve(process.cwd(), 'html-report');
this.opts.sourceStore = this.opts.sourceStore || Store.create('fslookup');
+ this.opts.linkMapper = this.opts.linkMapper || this.standardLinkMapper();
+ this.opts.writer = this.opts.writer || null;
+ this.opts.templateData = { datetime: Date() };
}
HtmlReport.TYPE = 'html';
@@ -335,25 +337,33 @@ Report.mix(HtmlReport, {
node.displayShortName() : '';
},
- writeDetailPage: function (writer, node, linkMapper, templateData, fileCoverage, metrics) {
+ fillTemplate: function (node, templateData) {
+ var opts = this.opts,
+ linkMapper = opts.linkMapper;
+
+ templateData.entity = node.name || 'All files';
+ templateData.metrics = node.metrics;
+ templateData.reportClass = getReportClass(node.metrics.statements);
+ templateData.pathHtml = pathTemplate({ html: this.getPathHtml(node, linkMapper) });
+ templateData.prettify = {
+ js: linkMapper.asset(node, 'prettify.js'),
+ css: linkMapper.asset(node, 'prettify.css')
+ };
+ },
+ writeDetailPage: function (writer, node, fileCoverage) {
var opts = this.opts,
sourceStore = opts.sourceStore,
+ templateData = opts.templateData,
sourceText = fileCoverage.code && Array.isArray(fileCoverage.code) ?
- fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path),
+ fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path),
code = sourceText.split(/\r?\n/),
count = 0,
structured = code.map(function (str) { count += 1; return { line: count, covered: null, text: new InsertionText(str, true) }; }),
context;
structured.unshift({ line: 0, covered: null, text: new InsertionText("") });
- templateData.metrics = metrics;
- templateData.reportClass = getReportClass(templateData.metrics.statements);
- templateData.pathHtml = pathTemplate({ html: this.getPathHtml(node, linkMapper) });
- templateData.prettify = {
- js: linkMapper.asset(node, 'prettify.js'),
- css: linkMapper.asset(node, 'prettify.css')
- };
+ this.fillTemplate(node, templateData);
writer.write(headerTemplate(templateData));
writer.write('<pre><table class="coverage">\n');
@@ -375,17 +385,16 @@ Report.mix(HtmlReport, {
writer.write(footerTemplate(templateData));
},
- writeIndexPage: function (writer, node, linkMapper, templateData) {
- var children = Array.prototype.slice.apply(node.children);
+ writeIndexPage: function (writer, node) {
+ var linkMapper = this.opts.linkMapper,
+ templateData = this.opts.templateData,
+ children = Array.prototype.slice.apply(node.children);
children.sort(function (a, b) {
return a.name < b.name ? -1 : 1;
});
- templateData.metrics = node.metrics;
- templateData.reportClass = getReportClass(node.metrics.statements);
- templateData.pathHtml = pathTemplate({ html: this.getPathHtml(node, linkMapper) });
-
+ this.fillTemplate(node, templateData);
writer.write(headerTemplate(templateData));
writer.write(summaryTableHeader);
children.forEach(function (child) {
@@ -408,94 +417,93 @@ Report.mix(HtmlReport, {
writer.write(footerTemplate(templateData));
},
- writeFiles: function (writer, node, dir, linkMapper, templateData, collector) {
+ writeFiles: function (writer, node, dir, collector) {
var that = this,
indexFile = path.resolve(dir, 'index.html'),
childFile;
- templateData.entity = node.name || 'All files';
if (this.opts.verbose) { console.error('Writing ' + indexFile); }
writer.writeFile(indexFile, function () {
- that.writeIndexPage(writer, node, linkMapper, templateData);
+ that.writeIndexPage(writer, node);
});
node.children.forEach(function (child) {
if (child.kind === 'dir') {
- that.writeFiles(writer, child, path.resolve(dir, child.relativeName), linkMapper, templateData, collector);
+ that.writeFiles(writer, child, path.resolve(dir, child.relativeName), collector);
} else {
childFile = path.resolve(dir, child.relativeName + '.html');
if (that.opts.verbose) { console.error('Writing ' + childFile); }
- templateData.entity = child.name;
writer.writeFile(childFile, function () {
- that.writeDetailPage(writer, child, linkMapper, templateData, collector.fileCoverageFor(child.fullPath()), child.metrics);
+ that.writeDetailPage(writer, child, collector.fileCoverageFor(child.fullPath()));
});
}
});
},
- writeReport: function (collector, sync) {
- var opts = this.opts,
- dir = opts.dir,
- dt = new Date().toString(),
- summarizer = new TreeSummarizer(),
- linkMapper = {
- fromParent: function (node) {
- var i = 0,
- relativeName = node.relativeName,
- ch;
- if (SEP !== '/') {
- relativeName = '';
- for (i = 0; i < node.relativeName.length; i += 1) {
- ch = node.relativeName.charAt(i);
- if (ch === SEP) {
- relativeName += '/';
- } else {
- relativeName += ch;
- }
+ standardLinkMapper: function () {
+ return {
+ fromParent: function (node) {
+ var i = 0,
+ relativeName = node.relativeName,
+ ch;
+ if (SEP !== '/') {
+ relativeName = '';
+ for (i = 0; i < node.relativeName.length; i += 1) {
+ ch = node.relativeName.charAt(i);
+ if (ch === SEP) {
+ relativeName += '/';
+ } else {
+ relativeName += ch;
}
}
- return node.kind === 'dir' ? relativeName + 'index.html' : relativeName + '.html';
- },
- ancestorHref: function (node, num) {
- var href = '',
- separated,
- levels,
- i,
- j;
- for (i = 0; i < num; i += 1) {
- separated = node.relativeName.split(SEP);
- levels = separated.length - 1;
- for (j = 0; j < levels; j += 1) {
- href += '../';
- }
- node = node.parent;
+ }
+ return node.kind === 'dir' ? relativeName + 'index.html' : relativeName + '.html';
+ },
+ ancestorHref: function (node, num) {
+ var href = '',
+ separated,
+ levels,
+ i,
+ j;
+ for (i = 0; i < num; i += 1) {
+ separated = node.relativeName.split(SEP);
+ levels = separated.length - 1;
+ for (j = 0; j < levels; j += 1) {
+ href += '../';
}
- return href;
- },
- ancestor: function (node, num) {
- return this.ancestorHref(node, num) + 'index.html';
- },
- asset: function (node, name) {
- var i = 0,
- parent = node.parent;
- while (parent) { i += 1; parent = parent.parent; }
- return this.ancestorHref(node, i) + name;
+ node = node.parent;
}
+ return href;
+ },
+ ancestor: function (node, num) {
+ return this.ancestorHref(node, num) + 'index.html';
},
- writer = new FileWriter(sync),
- that = this,
+ asset: function (node, name) {
+ var i = 0,
+ parent = node.parent;
+ while (parent) { i += 1; parent = parent.parent; }
+ return this.ancestorHref(node, i) + name;
+ }
+ };
+ },
+
+ writeReport: function (collector, sync) {
+ var opts = this.opts,
+ dir = opts.dir,
+ summarizer = new TreeSummarizer(),
+ writer = opts.writer || new FileWriter(sync),
tree;
collector.files().forEach(function (key) {
summarizer.addFileCoverageSummary(key, utils.summarizeFileCoverage(collector.fileCoverageFor(key)));
});
tree = summarizer.getTreeSummary();
fs.readdirSync(path.resolve(__dirname, '..', 'vendor')).forEach(function (f) {
- if (that.opts.verbose) {
+ if (opts.verbose) {
console.log('Write asset: ' + path.resolve(dir, f));
}
writer.copyFile(path.resolve(__dirname, '..', 'vendor', f), path.resolve(dir, f));
});
//console.log(JSON.stringify(tree.root, undefined, 4));
- this.writeFiles(writer, tree.root, dir, linkMapper, { datetime: dt }, collector);
+ this.writeFiles(writer, tree.root, dir, collector);
}
});

0 comments on commit 4653aec

Please sign in to comment.