Skip to content

Commit

Permalink
fix: Properly combine directories in nested summarizer (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage authored and coreyfarrell committed May 3, 2019
1 parent 95ebaf1 commit 50afdbb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
63 changes: 39 additions & 24 deletions packages/istanbul-lib-report/lib/summarizer.js
Expand Up @@ -186,32 +186,48 @@ function toDirParents(list) {
return parentNodeList;
}

function foldIntoParents(nodeList) {
const ret = [];
let i;
let j;

// sort by longest length first
nodeList.sort((a, b) => -1 * Path.compare(a.path, b.path));
function addAllPaths(topPaths, nodeMap, path, node) {
const parentPath = path.parent();
let parent = nodeMap[parentPath.toString()];

if (!parent) {
parent = new ReportNode(parentPath);
nodeMap[parentPath.toString()] = parent;

if (parentPath.hasParent()) {
addAllPaths(topPaths, nodeMap, parentPath, parent);
} else {
topPaths.push(parent);
}
}
parent.addChild(node);
}

for (i = 0; i < nodeList.length; i += 1) {
const first = nodeList[i];
let inserted = false;
function toNested(list) {
const nodeMap = Object.create(null);
const topNodes = [];
list.forEach(o => {
const node = new ReportNode(o.path, o.fileCoverage);

for (j = i + 1; j < nodeList.length; j += 1) {
const second = nodeList[j];
if (second.path.ancestorOf(first.path)) {
second.addChild(first);
inserted = true;
break;
}
}
addAllPaths(topNodes, nodeMap, o.path, node);
});
return topNodes;
}

if (!inserted) {
ret.push(first);
}
function foldIntoOneDir(node, parent) {
const children = node.children;
if (children.length === 1 && !children[0].fileCoverage) {
children[0].parent = parent;
return foldIntoOneDir(children[0], parent);
}
return ret;
for (let i = 0; i < children.length; i++) {
children[i] = foldIntoOneDir(children[i], node);
}
return node;
}

function foldInOneDirNodes(list) {
return list.map(node => foldIntoOneDir(node));
}

function createRoot() {
Expand All @@ -220,8 +236,7 @@ function createRoot() {

function createNestedSummary(coverageMap) {
const flattened = toInitialList(coverageMap);
const dirParents = toDirParents(flattened.list);
const topNodes = foldIntoParents(dirParents);
const topNodes = foldInOneDirNodes(toNested(flattened.list));

if (topNodes.length === 0) {
return treeFor(new ReportNode(new Path([])));
Expand Down
35 changes: 35 additions & 0 deletions packages/istanbul-lib-report/test/summarizer.test.js
Expand Up @@ -94,6 +94,24 @@ function threeDirMap() {
return coverage.createCoverageMap(map);
}

function multiDirMap() {
const files = [
'lib1/sub/file3.js',
'lib1/file4.js',
'lib2/sub1/file2.js',
'lib2/sub2/file1.js'
];
let count = 0;
const map = {};
files.forEach(f => {
const filePath = f;
const fc = makeCoverage(filePath, 4, count);
count += 1;
map[filePath] = fc;
});
return coverage.createCoverageMap(map);
}

function getStructure(tree, localNames) {
const meth = localNames ? 'getRelativeName' : 'getQualifiedName';
const visitor = {
Expand Down Expand Up @@ -403,6 +421,23 @@ describe('summarizer', () => {
'f:lib2/file4.js'
]);
});
it('supports directory in directory', () => {
const map = multiDirMap();
const tree = fn(map);
const nodes = getStructure(tree);
assert.deepEqual(nodes, [
'g:',
'g:lib1',
'f:lib1/file4.js',
'g:lib1/sub',
'f:lib1/sub/file3.js',
'g:lib2',
'g:lib2/sub1',
'f:lib2/sub1/file2.js',
'g:lib2/sub2',
'f:lib2/sub2/file1.js'
]);
});
});

describe('report node properties', () => {
Expand Down

0 comments on commit 50afdbb

Please sign in to comment.