Skip to content

Commit

Permalink
fix: Ignore insignificant lines when coalesce (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
piranna committed Apr 1, 2020
1 parent df24342 commit d7d7cfa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 66 deletions.
32 changes: 19 additions & 13 deletions packages/istanbul-reports/lib/text/index.js
Expand Up @@ -67,33 +67,39 @@ function nodeMissing(node) {
const isEmpty = metrics.isEmpty();
const lines = isEmpty ? 0 : metrics.lines.pct;

let missingLines;
let coveredLines;

const fileCoverage = node.getFileCoverage();
if (lines === 100) {
const branches = fileCoverage.getBranchCoverageByLine();
missingLines = Object.keys(branches).filter(
key => branches[key].coverage < 100
);
coveredLines = Object.entries(branches).map(([key, { coverage }]) => [
key,
coverage === 100
]);
} else {
missingLines = fileCoverage.getUncoveredLines();
coveredLines = Object.entries(fileCoverage.getLineCoverage());
}

const ranges = missingLines
.reduce((acum, line) => {
line = parseInt(line);
const range = acum[acum.length - 1];
if (range && range[range.length - 1] === line - 1) range.push(line);
else acum.push([line]);
let newRange = true;
const ranges = coveredLines
.reduce((acum, [line, hit]) => {
if (hit) newRange = true;
else {
line = parseInt(line);
if (newRange) {
acum.push([line]);
newRange = false;
} else acum[acum.length - 1][1] = line;
}

return acum;
}, [])
.map(range => {
const { length } = range;

if (length <= 2) return range;
if (length === 1) return range[0];

return `${range[0]}-${range[length - 1]}`;
return `${range[0]}-${range[1]}`;
});

return [].concat(...ranges).join(',');
Expand Down
30 changes: 15 additions & 15 deletions packages/istanbul-reports/test/fixtures/specs/coalescence.json
Expand Up @@ -3,17 +3,17 @@
"opts": {
"maxCols": 80
},
"textReportExpected": "----------|---------|----------|---------|---------|----------------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|----------------------------\n\u001b[31;1mAll files\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[31;1m index.js\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[31;1m...21-24,28,29,32,33,35-37\u001b[0m \n----------|---------|----------|---------|---------|----------------------------\n",
"textReportExpected": "----------|---------|----------|---------|---------|----------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|----------------------\n\u001b[31;1mAll files\u001b[0m | \u001b[31;1m 30\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 33.33\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[31;1m index.js\u001b[0m | \u001b[31;1m 30\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 33.33\u001b[0m | \u001b[31;1m2,4,9,21-28,32,35-37\u001b[0m \n----------|---------|----------|---------|---------|----------------------\n",
"htmlSpaFiles": ["index.js.html", "index.html"],
"htmlSpaCoverageData": {
"file": "",
"isEmpty": false,
"metrics": {
"statements": {
"total": 20,
"covered": 0,
"covered": 6,
"skipped": 0,
"pct": 0,
"pct": 30,
"classForPercent": "low"
},
"branches": {
Expand All @@ -32,9 +32,9 @@
},
"lines": {
"total": 18,
"covered": 0,
"covered": 6,
"skipped": 0,
"pct": 0,
"pct": 33.33,
"classForPercent": "low"
}
},
Expand All @@ -45,9 +45,9 @@
"metrics": {
"statements": {
"total": 20,
"covered": 0,
"covered": 6,
"skipped": 0,
"pct": 0,
"pct": 30,
"classForPercent": "low"
},
"branches": {
Expand All @@ -66,9 +66,9 @@
},
"lines": {
"total": 18,
"covered": 0,
"covered": 6,
"skipped": 0,
"pct": 0,
"pct": 33.33,
"classForPercent": "low"
}
},
Expand Down Expand Up @@ -284,23 +284,23 @@
"fnMap": {},
"branchMap": {},
"s": {
"0": 0,
"0": 1,
"1": 0,
"2": 0,
"2": 1,
"3": 0,
"4": 0,
"4": 1,
"5": 0,
"6": 0,
"6": 1,
"7": 0,
"8": 0,
"9": 0,
"10": 0,
"11": 0,
"12": 0,
"13": 0,
"14": 0,
"14": 1,
"15": 0,
"16": 0,
"16": 1,
"17": 0,
"18": 0,
"19": 0
Expand Down
76 changes: 38 additions & 38 deletions packages/istanbul-reports/test/fixtures/specs/maxcols.json
Expand Up @@ -3,17 +3,17 @@
"opts": {
"maxCols": 80
},
"textReportExpected": "----------|---------|----------|---------|---------|----------------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|----------------------------\n\u001b[31;1mAll files\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[31;1m index.js\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 0\u001b[0m | \u001b[31;1m...25,28,29,32,33,35,38,39\u001b[0m \n----------|---------|----------|---------|---------|----------------------------\n",
"textReportExpected": "----------|---------|----------|---------|---------|----------------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|----------------------------\n\u001b[31;1mAll files\u001b[0m | \u001b[31;1m 40\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 40\u001b[0m | \u001b[31;1m \u001b[0m \n\u001b[31;1m index.js\u001b[0m | \u001b[31;1m 40\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[32;1m 100\u001b[0m | \u001b[31;1m 40\u001b[0m | \u001b[31;1m...11,15,19,23,27,31,35,39\u001b[0m \n----------|---------|----------|---------|---------|----------------------------\n",
"htmlSpaFiles": ["index.js.html", "index.html"],
"htmlSpaCoverageData": {
"file": "",
"isEmpty": false,
"metrics": {
"statements": {
"total": 20,
"covered": 0,
"covered": 8,
"skipped": 0,
"pct": 0,
"pct": 40,
"classForPercent": "low"
},
"branches": {
Expand All @@ -31,10 +31,10 @@
"classForPercent": "empty"
},
"lines": {
"total": 18,
"covered": 0,
"total": 20,
"covered": 8,
"skipped": 0,
"pct": 0,
"pct": 40,
"classForPercent": "low"
}
},
Expand All @@ -45,9 +45,9 @@
"metrics": {
"statements": {
"total": 20,
"covered": 0,
"covered": 8,
"skipped": 0,
"pct": 0,
"pct": 40,
"classForPercent": "low"
},
"branches": {
Expand All @@ -65,10 +65,10 @@
"classForPercent": "empty"
},
"lines": {
"total": 18,
"covered": 0,
"total": 20,
"covered": 8,
"skipped": 0,
"pct": 0,
"pct": 40,
"classForPercent": "low"
}
},
Expand Down Expand Up @@ -132,11 +132,11 @@
},
"5": {
"start": {
"line": 9,
"line": 11,
"column": 2
},
"end": {
"line": 9,
"line": 11,
"column": 11
}
},
Expand All @@ -146,57 +146,57 @@
"column": 2
},
"end": {
"line": 19,
"line": 13,
"column": 10
}
},
"7": {
"start": {
"line": 21,
"line": 15,
"column": 2
},
"end": {
"line": 21,
"line": 15,
"column": 81
}
},
"8": {
"start": {
"line": 21,
"line": 17,
"column": 40
},
"end": {
"line": 21,
"line": 17,
"column": 81
}
},
"9": {
"start": {
"line": 22,
"line": 19,
"column": 2
},
"end": {
"line": 22,
"line": 19,
"column": 69
}
},
"10": {
"start": {
"line": 22,
"line": 21,
"column": 40
},
"end": {
"line": 22,
"line": 21,
"column": 69
}
},
"11": {
"start": {
"line": 24,
"line": 23,
"column": 2
},
"end": {
"line": 26,
"line": 23,
"column": 3
}
},
Expand All @@ -212,11 +212,11 @@
},
"13": {
"start": {
"line": 28,
"line": 27,
"column": 2
},
"end": {
"line": 30,
"line": 27,
"column": 3
}
},
Expand All @@ -232,11 +232,11 @@
},
"15": {
"start": {
"line": 32,
"line": 31,
"column": 2
},
"end": {
"line": 36,
"line": 31,
"column": 3
}
},
Expand All @@ -262,11 +262,11 @@
},
"18": {
"start": {
"line": 38,
"line": 37,
"column": 2
},
"end": {
"line": 40,
"line": 37,
"column": 3
}
},
Expand All @@ -288,21 +288,21 @@
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"4": 1,
"5": 0,
"6": 0,
"6": 1,
"7": 0,
"8": 0,
"8": 1,
"9": 0,
"10": 0,
"10": 1,
"11": 0,
"12": 0,
"12": 1,
"13": 0,
"14": 0,
"14": 1,
"15": 0,
"16": 0,
"16": 1,
"17": 0,
"18": 0,
"18": 1,
"19": 0
},
"f": {},
Expand Down

0 comments on commit d7d7cfa

Please sign in to comment.