Skip to content

Commit

Permalink
Merge 4708152 into f556b46
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jul 4, 2015
2 parents f556b46 + 4708152 commit d5bd818
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/config.js
Expand Up @@ -247,6 +247,13 @@ function ReportingOptions(config) {
* @method dir
* @return {String} the directory under which reports should be generated.
*/
/**
* returns the directory in which the `head.txt` and `foot.txt` HTML templates
* will be read from.
*
* @method templateDir
* @return {String} the directory in which the templates will be read from.
*/
/**
* returns an object that has keys that are report format names and values that are objects
* containing detailed configuration for each format. Running `istanbul help config`
Expand All @@ -255,7 +262,7 @@ function ReportingOptions(config) {
* @method reportConfig
* @return {Object} detailed report configuration per report format.
*/
addMethods(ReportingOptions, 'print', 'reports', 'dir', 'reportConfig');
addMethods(ReportingOptions, 'print', 'reports', 'dir', 'reportConfig', 'templateDir');

function isInvalidMark(v, key) {
var prefix = 'Watermark for [' + key + '] :';
Expand Down
16 changes: 9 additions & 7 deletions lib/report/html.js
Expand Up @@ -15,9 +15,7 @@ var handlebars = require('handlebars'),
InsertionText = require('../util/insertion-text'),
TreeSummarizer = require('../util/tree-summarizer'),
utils = require('../object-utils'),
templateFor = function (name) { return handlebars.compile(fs.readFileSync(path.resolve(__dirname, 'templates', name + '.txt'), 'utf8')); },
headerTemplate = templateFor('head'),
footerTemplate = templateFor('foot'),
templateFor = function (name, dirPath) { return handlebars.compile(fs.readFileSync(path.resolve(dirPath, name + '.txt'), 'utf8')); },
pathTemplate = handlebars.compile('<div class="path">{{{html}}}</div>'),
detailTemplate = handlebars.compile([
'<tr>',
Expand Down Expand Up @@ -347,6 +345,10 @@ function HtmlReport(opts) {
this.opts.writer = this.opts.writer || null;
this.opts.templateData = { datetime: Date() };
this.opts.watermarks = this.opts.watermarks || defaults.watermarks();
this.opts.templateDir = this.opts.templateDir || path.join(__dirname, 'templates');

this.headerTemplate = templateFor('head', this.opts.templateDir);
this.footerTemplate = templateFor('foot', this.opts.templateDir);
}

HtmlReport.TYPE = 'html';
Expand Down Expand Up @@ -412,7 +414,7 @@ Report.mix(HtmlReport, {
structured.unshift({ line: 0, covered: null, text: new InsertionText("") });

this.fillTemplate(node, templateData);
writer.write(headerTemplate(templateData));
writer.write(this.headerTemplate(templateData));
writer.write('<pre><table class="coverage">\n');

annotateLines(fileCoverage, structured);
Expand All @@ -430,7 +432,7 @@ Report.mix(HtmlReport, {
};
writer.write(detailTemplate(context));
writer.write('</table></pre>\n');
writer.write(footerTemplate(templateData));
writer.write(this.footerTemplate(templateData));
},

writeIndexPage: function (writer, node) {
Expand All @@ -444,7 +446,7 @@ Report.mix(HtmlReport, {
});

this.fillTemplate(node, templateData);
writer.write(headerTemplate(templateData));
writer.write(this.headerTemplate(templateData));
writer.write(summaryTableHeader);
children.forEach(function (child) {
var metrics = child.metrics,
Expand All @@ -463,7 +465,7 @@ Report.mix(HtmlReport, {
writer.write(summaryLineTemplate(data) + '\n');
});
writer.write(summaryTableFooter);
writer.write(footerTemplate(templateData));
writer.write(this.footerTemplate(templateData));
},

writeFiles: function (writer, node, dir, collector) {
Expand Down
2 changes: 2 additions & 0 deletions test/cli/custom-templates/foot.txt
@@ -0,0 +1,2 @@
</body>
</html>
5 changes: 5 additions & 0 deletions test/cli/custom-templates/head.txt
@@ -0,0 +1,5 @@
<!doctype html>
<html lang="en">
<head>
</head>
<body>
17 changes: 16 additions & 1 deletion test/cli/test-html-report.js
Expand Up @@ -221,6 +221,21 @@ module.exports = {
test.done();
},

"should use custom templates if specified" : function(test) {
var customTemplateDir = path.join(__dirname, 'custom-templates'),
reporter = new Reporter({
dir: OUTPUT_DIR,
verbose: true,
templateDir: customTemplateDir
}),
head = fs.readFileSync(path.join(customTemplateDir, 'head.txt'), 'utf8'),
foot = fs.readFileSync(path.join(customTemplateDir, 'foot.txt'), 'utf8');

test.ok(head === reporter.headerTemplate({}));
test.ok(foot === reporter.footerTemplate({}));
test.done();
},

"should generate proper ancestorHref when path separator is \\" : function(test) {
var pathSep = path.sep,
reporter = new Reporter(),
Expand Down Expand Up @@ -321,4 +336,4 @@ module.exports = {
path.sep = pathSep;
test.done();
}
};
};

0 comments on commit d5bd818

Please sign in to comment.