| 1 |
/* |
| 2 |
Copyright 2012-2015, Yahoo Inc. |
| 3 |
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. |
| 4 |
*/ |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
const percent = require('./percent'); |
| 8 |
const dataProperties = require('./data-properties'); |
| 9 |
const { CoverageSummary } = require('./coverage-summary'); |
| 10 |
|
| 11 |
// returns a data object that represents empty coverage |
| 12 |
function emptyCoverage(filePath, reportLogic) { |
| 13 |
const cov = { |
| 14 |
path: filePath, |
| 15 |
statementMap: {}, |
| 16 |
fnMap: {}, |
| 17 |
branchMap: {}, |
| 18 |
s: {}, |
| 19 |
f: {}, |
| 20 |
b: {} |
| 21 |
}; |
| 22 |
if (reportLogic) cov.bT = {}; |
| 23 |
return cov; |
| 24 |
} |
| 25 |
|
| 26 |
// asserts that a data object "looks like" a coverage object |
| 27 |
function assertValidObject(obj) { |
| 28 |
const valid = |
| 29 |
obj && |
| 30 |
obj.path && |
| 31 |
obj.statementMap && |
| 32 |
obj.fnMap && |
| 33 |
obj.branchMap && |
| 34 |
obj.s && |
| 35 |
obj.f && |
| 36 |
obj.b; |
| 37 |
if (!valid) { |
| 38 |
throw new Error( |
| 39 |
'Invalid file coverage object, missing keys, found:' + |
| 40 |
Object.keys(obj).join(',') |
| 41 |
); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
const keyFromLoc = ({ start, end }) => |
| 46 |
`${start.line}|${start.column}|${end.line}|${end.column}`; |
| 47 |
|
| 48 |
const mergeProp = (aHits, aMap, bHits, bMap, itemKey = keyFromLoc) => { |
| 49 |
const aItems = {}; |
| 50 |
for (const [key, itemHits] of Object.entries(aHits)) { |
| 51 |
const item = aMap[key]; |
| 52 |
aItems[itemKey(item)] = [itemHits, item]; |
| 53 |
} |
| 54 |
for (const [key, bItemHits] of Object.entries(bHits)) { |
| 55 |
const bItem = bMap[key]; |
| 56 |
const k = itemKey(bItem); |
| 57 |
|
| 58 |
if (aItems[k]) { |
| 59 |
const aPair = aItems[k]; |
| 60 |
if (bItemHits.forEach) { |
| 61 |
// should this throw an exception if aPair[0] is not an array? |
| 62 |
bItemHits.forEach((hits, h) => { |
| 63 |
if (aPair[0][h] !== undefined) aPair[0][h] += hits; |
| 64 |
else aPair[0][h] = hits; |
| 65 |
}); |
| 66 |
} else { |
| 67 |
aPair[0] += bItemHits; |
| 68 |
} |
| 69 |
} else { |
| 70 |
aItems[k] = [bItemHits, bItem]; |
| 71 |
} |
| 72 |
} |
| 73 |
const hits = {}; |
| 74 |
const map = {}; |
| 75 |
|
| 76 |
Object.values(aItems).forEach(([itemHits, item], i) => { |
| 77 |
hits[i] = itemHits; |
| 78 |
map[i] = item; |
| 79 |
}); |
| 80 |
|
| 81 |
return [hits, map]; |
| 82 |
}; |
| 83 |
|
| 84 |
/** |
| 85 |
* provides a read-only view of coverage for a single file. |
| 86 |
* The deep structure of this object is documented elsewhere. It has the following |
| 87 |
* properties: |
| 88 |
* |
| 89 |
* * `path` - the file path for which coverage is being tracked |
| 90 |
* * `statementMap` - map of statement locations keyed by statement index |
| 91 |
* * `fnMap` - map of function metadata keyed by function index |
| 92 |
* * `branchMap` - map of branch metadata keyed by branch index |
| 93 |
* * `s` - hit counts for statements |
| 94 |
* * `f` - hit count for functions |
| 95 |
* * `b` - hit count for branches |
| 96 |
*/ |
| 97 |
class FileCoverage { |
| 98 |
/** |
| 99 |
* @constructor |
| 100 |
* @param {Object|FileCoverage|String} pathOrObj is a string that initializes |
| 101 |
* and empty coverage object with the specified file path or a data object that |
| 102 |
* has all the required properties for a file coverage object. |
| 103 |
*/ |
| 104 |
constructor(pathOrObj, reportLogic = false) { |
| 105 |
if (!pathOrObj) { |
| 106 |
throw new Error( |
| 107 |
'Coverage must be initialized with a path or an object' |
| 108 |
); |
| 109 |
} |
| 110 |
if (typeof pathOrObj === 'string') { |
| 111 |
this.data = emptyCoverage(pathOrObj, reportLogic); |
| 112 |
} else if (pathOrObj instanceof FileCoverage) { |
| 113 |
this.data = pathOrObj.data; |
| 114 |
} else if (typeof pathOrObj === 'object') { |
| 115 |
this.data = pathOrObj; |
| 116 |
} else { |
| 117 |
throw new Error('Invalid argument to coverage constructor'); |
| 118 |
} |
| 119 |
assertValidObject(this.data); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* returns computed line coverage from statement coverage. |
| 124 |
* This is a map of hits keyed by line number in the source. |
| 125 |
*/ |
| 126 |
getLineCoverage() { |
| 127 |
const statementMap = this.data.statementMap; |
| 128 |
const statements = this.data.s; |
| 129 |
const lineMap = Object.create(null); |
| 130 |
|
| 131 |
Object.entries(statements).forEach(([st, count]) => { |
| 132 |
/* istanbul ignore if: is this even possible? */ |
| 133 |
if (!statementMap[st]) { |
| 134 |
return; |
| 135 |
} |
| 136 |
const { line } = statementMap[st].start; |
| 137 |
const prevVal = lineMap[line]; |
| 138 |
if (prevVal === undefined || prevVal < count) { |
| 139 |
lineMap[line] = count; |
| 140 |
} |
| 141 |
}); |
| 142 |
return lineMap; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* returns an array of uncovered line numbers. |
| 147 |
* @returns {Array} an array of line numbers for which no hits have been |
| 148 |
* collected. |
| 149 |
*/ |
| 150 |
getUncoveredLines() { |
| 151 |
const lc = this.getLineCoverage(); |
| 152 |
const ret = []; |
| 153 |
Object.entries(lc).forEach(([l, hits]) => { |
| 154 |
if (hits === 0) { |
| 155 |
ret.push(l); |
| 156 |
} |
| 157 |
}); |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* returns a map of branch coverage by source line number. |
| 163 |
* @returns {Object} an object keyed by line number. Each object |
| 164 |
* has a `covered`, `total` and `coverage` (percentage) property. |
| 165 |
*/ |
| 166 |
getBranchCoverageByLine() { |
| 167 |
const branchMap = this.branchMap; |
| 168 |
const branches = this.b; |
| 169 |
const ret = {}; |
| 170 |
Object.entries(branchMap).forEach(([k, map]) => { |
| 171 |
const line = map.line || map.loc.start.line; |
| 172 |
const branchData = branches[k]; |
| 173 |
ret[line] = ret[line] || []; |
| 174 |
ret[line].push(...branchData); |
| 175 |
}); |
| 176 |
Object.entries(ret).forEach(([k, dataArray]) => { |
| 177 |
const covered = dataArray.filter(item => item > 0); |
| 178 |
const coverage = (covered.length / dataArray.length) * 100; |
| 179 |
ret[k] = { |
| 180 |
covered: covered.length, |
| 181 |
total: dataArray.length, |
| 182 |
coverage |
| 183 |
}; |
| 184 |
}); |
| 185 |
return ret; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* return a JSON-serializable POJO for this file coverage object |
| 190 |
*/ |
| 191 |
toJSON() { |
| 192 |
return this.data; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* merges a second coverage object into this one, updating hit counts |
| 197 |
* @param {FileCoverage} other - the coverage object to be merged into this one. |
| 198 |
* Note that the other object should have the same structure as this one (same file). |
| 199 |
*/ |
| 200 |
merge(other) { |
| 201 |
if (other.all === true) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (this.all === true) { |
| 206 |
this.data = other.data; |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
let [hits, map] = mergeProp( |
| 211 |
this.s, |
| 212 |
this.statementMap, |
| 213 |
other.s, |
| 214 |
other.statementMap |
| 215 |
); |
| 216 |
this.data.s = hits; |
| 217 |
this.data.statementMap = map; |
| 218 |
|
| 219 |
const keyFromLocProp = x => keyFromLoc(x.loc); |
| 220 |
const keyFromLocationsProp = x => keyFromLoc(x.locations[0]); |
| 221 |
|
| 222 |
[hits, map] = mergeProp( |
| 223 |
this.f, |
| 224 |
this.fnMap, |
| 225 |
other.f, |
| 226 |
other.fnMap, |
| 227 |
keyFromLocProp |
| 228 |
); |
| 229 |
this.data.f = hits; |
| 230 |
this.data.fnMap = map; |
| 231 |
|
| 232 |
[hits, map] = mergeProp( |
| 233 |
this.b, |
| 234 |
this.branchMap, |
| 235 |
other.b, |
| 236 |
other.branchMap, |
| 237 |
keyFromLocationsProp |
| 238 |
); |
| 239 |
this.data.b = hits; |
| 240 |
this.data.branchMap = map; |
| 241 |
|
| 242 |
// Tracking additional information about branch truthiness |
| 243 |
// can be optionally enabled: |
| 244 |
if (this.bT && other.bT) { |
| 245 |
[hits, map] = mergeProp( |
| 246 |
this.bT, |
| 247 |
this.branchMap, |
| 248 |
other.bT, |
| 249 |
other.branchMap, |
| 250 |
keyFromLocationsProp |
| 251 |
); |
| 252 |
this.data.bT = hits; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
computeSimpleTotals(property) { |
| 257 |
let stats = this[property]; |
| 258 |
|
| 259 |
if (typeof stats === 'function') { |
| 260 |
stats = stats.call(this); |
| 261 |
} |
| 262 |
|
| 263 |
const ret = { |
| 264 |
total: Object.keys(stats).length, |
| 265 |
covered: Object.values(stats).filter(v => !!v).length, |
| 266 |
skipped: 0 |
| 267 |
}; |
| 268 |
ret.pct = percent(ret.covered, ret.total); |
| 269 |
return ret; |
| 270 |
} |
| 271 |
|
| 272 |
computeBranchTotals(property) { |
| 273 |
const stats = this[property]; |
| 274 |
const ret = { total: 0, covered: 0, skipped: 0 }; |
| 275 |
|
| 276 |
Object.values(stats).forEach(branches => { |
| 277 |
ret.covered += branches.filter(hits => hits > 0).length; |
| 278 |
ret.total += branches.length; |
| 279 |
}); |
| 280 |
ret.pct = percent(ret.covered, ret.total); |
| 281 |
return ret; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* resets hit counts for all statements, functions and branches |
| 286 |
* in this coverage object resulting in zero coverage. |
| 287 |
*/ |
| 288 |
resetHits() { |
| 289 |
const statements = this.s; |
| 290 |
const functions = this.f; |
| 291 |
const branches = this.b; |
| 292 |
const branchesTrue = this.bT; |
| 293 |
Object.keys(statements).forEach(s => { |
| 294 |
statements[s] = 0; |
| 295 |
}); |
| 296 |
Object.keys(functions).forEach(f => { |
| 297 |
functions[f] = 0; |
| 298 |
}); |
| 299 |
Object.keys(branches).forEach(b => { |
| 300 |
branches[b].fill(0); |
| 301 |
}); |
| 302 |
// Tracking additional information about branch truthiness |
| 303 |
// can be optionally enabled: |
| 304 |
if (branchesTrue) { |
| 305 |
Object.keys(branchesTrue).forEach(bT => { |
| 306 |
branchesTrue[bT].fill(0); |
| 307 |
}); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* returns a CoverageSummary for this file coverage object |
| 313 |
* @returns {CoverageSummary} |
| 314 |
*/ |
| 315 |
toSummary() { |
| 316 |
const ret = {}; |
| 317 |
ret.lines = this.computeSimpleTotals('getLineCoverage'); |
| 318 |
ret.functions = this.computeSimpleTotals('f', 'fnMap'); |
| 319 |
ret.statements = this.computeSimpleTotals('s', 'statementMap'); |
| 320 |
ret.branches = this.computeBranchTotals('b'); |
| 321 |
// Tracking additional information about branch truthiness |
| 322 |
// can be optionally enabled: |
| 323 |
if (this['bt']) { |
| 324 |
ret.branchesTrue = this.computeBranchTotals('bT'); |
| 325 |
} |
| 326 |
return new CoverageSummary(ret); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// expose coverage data attributes |
| 331 |
dataProperties(FileCoverage, [ |
| 332 |
'path', |
| 333 |
'statementMap', |
| 334 |
'fnMap', |
| 335 |
'branchMap', |
| 336 |
's', |
| 337 |
'f', |
| 338 |
'b', |
| 339 |
'bT', |
| 340 |
'all' |
| 341 |
]); |
| 342 |
|
| 343 |
module.exports = { |
| 344 |
FileCoverage |
| 345 |
}; |