Skip to content

Commit

Permalink
Merge 6b80ddd into 89e338f
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Oct 30, 2016
2 parents 89e338f + 6b80ddd commit 8773e29
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/command/report.js
Expand Up @@ -43,6 +43,7 @@ Command.mix(ReportCommand, {
formatOption('--config <path-to-config>', 'the configuration file to use, defaults to .istanbul.yml'),
formatOption('--root <input-directory>', 'The input root directory for finding coverage files'),
formatOption('--dir <report-directory>', 'The output directory where files will be written. This defaults to ./coverage/'),
formatOption('--nested', 'Nest the report by directories'),
formatOption('--include <glob>', 'The glob pattern to select one or more coverage files, defaults to **/coverage*.json'),
formatOption('--verbose, -v', 'verbose mode')
].join('\n\n'));
Expand All @@ -67,6 +68,7 @@ Command.mix(ReportCommand, {
config: path,
root: path,
dir: path,
nested: Boolean,
include: String,
verbose: Boolean
},
Expand All @@ -76,6 +78,7 @@ Command.mix(ReportCommand, {
collector = new Collector(),
config = configuration.loadFile(opts.config, {
verbose: opts.verbose,
nested: opts.nested,
reporting: {
dir: opts.dir
}
Expand Down
2 changes: 1 addition & 1 deletion lib/report/clover.js
Expand Up @@ -203,7 +203,7 @@ Report.mix(CloverReport, {
return { file: 'clover.xml' };
},
writeReport: function (collector, sync) {
var summarizer = new TreeSummarizer(),
var summarizer = new TreeSummarizer(this.opts.nested),
outputFile = path.join(this.dir, this.file),
writer = this.opts.writer || new FileWriter(sync),
projectRoot = this.projectRoot,
Expand Down
2 changes: 1 addition & 1 deletion lib/report/cobertura.js
Expand Up @@ -197,7 +197,7 @@ Report.mix(CoberturaReport, {
return { file: 'cobertura-coverage.xml' };
},
writeReport: function (collector, sync) {
var summarizer = new TreeSummarizer(),
var summarizer = new TreeSummarizer(this.opts.nested),
outputFile = path.join(this.dir, this.file),
writer = this.opts.writer || new FileWriter(sync),
projectRoot = this.projectRoot,
Expand Down
2 changes: 1 addition & 1 deletion lib/report/html.js
Expand Up @@ -534,7 +534,7 @@ Report.mix(HtmlReport, {
writeReport: function (collector, sync) {
var opts = this.opts,
dir = opts.dir,
summarizer = new TreeSummarizer(),
summarizer = new TreeSummarizer(opts.nested),
writer = opts.writer || new FileWriter(sync),
that = this,
tree,
Expand Down
3 changes: 2 additions & 1 deletion lib/report/text.js
Expand Up @@ -42,6 +42,7 @@ function TextReport(opts) {
this.file = opts.file;
this.summary = opts.summary;
this.maxCols = opts.maxCols || 0;
this.nested = !!opts.nested;
this.watermarks = opts.watermarks || defaults.watermarks();
}

Expand Down Expand Up @@ -196,7 +197,7 @@ Report.mix(TextReport, {
return { file: null, maxCols: 0 };
},
writeReport: function (collector /*, sync */) {
var summarizer = new TreeSummarizer(),
var summarizer = new TreeSummarizer(this.nested),
tree,
root,
nameWidth,
Expand Down
1 change: 1 addition & 0 deletions lib/reporter.js
Expand Up @@ -51,6 +51,7 @@ Reporter.prototype = {
rptConfig = config.reporting.reportConfig()[fmt] || {};
rptConfig.verbose = config.verbose;
rptConfig.dir = this.dir;
rptConfig.nested = config.nested;
rptConfig.watermarks = config.reporting.watermarks();
try {
this.reports[fmt] = Report.create(fmt, rptConfig);
Expand Down
60 changes: 45 additions & 15 deletions lib/util/tree-summarizer.js
Expand Up @@ -69,16 +69,17 @@ Node.prototype = {
}
};

function TreeSummary(summaryMap, commonPrefix) {
function TreeSummary(summaryMap, commonPrefix, nest) {
this.prefix = commonPrefix;
this.convertToTree(summaryMap, commonPrefix);
this.nest = !!nest;
this.convertToTree(summaryMap, commonPrefix, nest);
}

TreeSummary.prototype = {
getNode: function (shortName) {
return this.map[shortName];
},
convertToTree: function (summaryMap, arrayPrefix) {
convertToTree: function (summaryMap, arrayPrefix, nest) {
var nodes = [],
rootPath = arrayPrefix.join(SEP) + SEP,
root = new Node(rootPath, 'dir'),
Expand All @@ -91,8 +92,9 @@ TreeSummary.prototype = {
Object.keys(summaryMap).forEach(function (key) {
var metrics = summaryMap[key],
node,
parentPath,
parent;
parent,
parentPath;

node = new Node(key, 'file', metrics);
seen[key] = node;
nodes.push(node);
Expand All @@ -101,7 +103,29 @@ TreeSummary.prototype = {
parentPath = SEP + '__root__' + SEP;
}
parent = seen[parentPath];
if (!parent) {

if (nest) {
var parentsPaths = parentPath.split(SEP),
prevParent,
lastParent = root,
fullPath = '',
lastParentPath;

while (!!(lastParentPath = parentsPaths.shift())) {
lastParentPath = lastParentPath + SEP;
fullPath += lastParentPath;
prevParent = lastParent;
lastParent = seen[fullPath];
if (!lastParent) {
lastParent = new Node(lastParentPath, 'dir');
prevParent.addChild(lastParent);
seen[fullPath] = prevParent = lastParent;
}
}
node.name = node.name.replace(fullPath, '');

parent = lastParent;
} else if (!parent) {
parent = new Node(parentPath, 'dir');
root.addChild(parent);
seen[parentPath] = parent;
Expand Down Expand Up @@ -140,23 +164,28 @@ TreeSummary.prototype = {
if (node.name.charAt(0) === SEP) {
node.name = node.name.substring(1);
}
if (parent) {
if (parent.name !== '__root__' + SEP) {
node.relativeName = node.name.substring(parent.name.length);
if (this.nest) {
node.relativeName = node.name;
} else {
if (parent) {
if (parent.name !== '__root__' + SEP) {
node.relativeName = node.name.substring(parent.name.length);
} else {
node.relativeName = node.name;
}
} else {
node.relativeName = node.name;
node.relativeName = node.name.substring(prefix.length);
}
} else {
node.relativeName = node.name.substring(prefix.length);
}

node.children.forEach(function (child) {
that.fixupNodes(child, prefix, node);
});
},
calculateMetrics: function (entry) {
var that = this,
fileChildren;
if (entry.kind !== 'dir') {return; }
if (entry.kind !== 'dir') { return; }
entry.children.forEach(function (child) {
that.calculateMetrics(child);
});
Expand Down Expand Up @@ -196,8 +225,9 @@ TreeSummary.prototype = {
}
};

function TreeSummarizer() {
function TreeSummarizer(nest) {
this.summaryMap = {};
this.nest = !!nest;
}

TreeSummarizer.prototype = {
Expand All @@ -206,7 +236,7 @@ TreeSummarizer.prototype = {
},
getTreeSummary: function () {
var commonArrayPrefix = findCommonArrayPrefix(Object.keys(this.summaryMap));
return new TreeSummary(this.summaryMap, commonArrayPrefix);
return new TreeSummary(this.summaryMap, commonArrayPrefix, this.nest);
}
};

Expand Down

0 comments on commit 8773e29

Please sign in to comment.