-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support reporting coverage for tests
If `coverage` options is set, template files will be instrumented before execution (can be enabled per-technology). If templates are built with source map, there is an option to show coverage for original files. There is also an option to filter out some sources from report. By default, all content of `node_modules` and `libs` will be omitted.
- Loading branch information
Sergey Tatarintsev
committed
Nov 10, 2014
1 parent
9d7a952
commit a4dbf9e
Showing
9 changed files
with
277 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
var _ = require('lodash'); | ||
|
||
function CoverageObject() { | ||
this._indices = { | ||
}; | ||
this.coverage = { | ||
}; | ||
} | ||
|
||
CoverageObject.prototype = { | ||
constructor: CoverageObject, | ||
|
||
addStatement: function (start, end, counter) { | ||
var sourceName = start.source, | ||
sourceObj = this._getFileObject(sourceName), | ||
idx = this._indices[sourceName].s++; | ||
sourceObj.statementMap[idx] = { | ||
start: getLocation(start), | ||
end: getLocation(end) | ||
}; | ||
sourceObj.s[idx] = counter; | ||
}, | ||
|
||
addFunction: function (name, start, end, counter) { | ||
var sourceName = start.source, | ||
sourceObj = this._getFileObject(sourceName), | ||
idx = this._indices[sourceName].f++; | ||
sourceObj.fnMap[idx] = { | ||
name: name, | ||
line: start.line, | ||
loc: { | ||
start: getLocation(start), | ||
end: getLocation(end) | ||
} | ||
}; | ||
|
||
sourceObj.f[idx] = counter; | ||
}, | ||
|
||
addBranch: function (type, locations, counter) { | ||
var sourceName = locations[0].start.source, | ||
sourceObj = this._getFileObject(sourceName), | ||
idx = this._indices[sourceName].b++; | ||
sourceObj.branchMap[idx] = { | ||
line: locations[0].start.line, | ||
type: type, | ||
locations: locations.map(function (loc) { | ||
return { | ||
start: getLocation(loc.start), | ||
end: getLocation(loc.end) | ||
}; | ||
}) | ||
}; | ||
sourceObj.b[idx] = counter; | ||
}, | ||
|
||
_getFileObject: function (sourceName) { | ||
var fileObject = this.coverage[sourceName]; | ||
if (!fileObject) { | ||
fileObject = this.coverage[sourceName] = { | ||
path: sourceName, | ||
s: {}, | ||
b: {}, | ||
f: {}, | ||
statementMap: {}, | ||
fnMap: {}, | ||
branchMap: {} | ||
}; | ||
|
||
this._indices[sourceName] = { | ||
s: 1, | ||
b: 1, | ||
f: 1 | ||
}; | ||
} | ||
return fileObject; | ||
} | ||
|
||
}; | ||
|
||
function getLocation(soureMapLocation) { | ||
return _.pick(soureMapLocation, 'line', 'column'); | ||
} | ||
|
||
module.exports = CoverageObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var Instrumenter = require('istanbul').Instrumenter, | ||
fs = require('vow-fs'); | ||
|
||
module.exports = require('enb/lib/build-flow').create() | ||
.name('istanbul') | ||
.target('target', '?.instrumented.js') | ||
.useSourceFilename('source', '?.js') | ||
.builder(function (source) { | ||
return fs.read(source, 'utf8') | ||
.then(function (content) { | ||
var instrumenter = new Instrumenter(); | ||
return instrumenter.instrumentSync(content, source); | ||
}); | ||
}) | ||
.createTech(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
var vow = require('vow'), | ||
vowFs = require('vow-fs'), | ||
_ = require('lodash'), | ||
istanbul = require('istanbul'), | ||
SourceLocator = require('enb-source-map/lib/source-locator'), | ||
|
||
CoverageObject = require('./coverage-object'); | ||
|
||
function unmapCoverageObject(sourceObject) { | ||
return vow.all(Object.keys(sourceObject).map(function (fileName) { | ||
return vowFs.read(fileName, 'utf8').then(function (source) { | ||
return unmapFile(fileName, sourceObject[fileName], source); | ||
}); | ||
})) | ||
.then(function (coverageObjects) { | ||
return coverageObjects.reduce(istanbul.utils.mergeFileCoverage); | ||
}) | ||
.then(function (totalCoverage) { | ||
return _.omit(totalCoverage, function (value, fileName) { | ||
return fileName in sourceObject; | ||
}); | ||
}); | ||
} | ||
|
||
function unmapFile(fileName, fileObject, source) { | ||
var locator = new SourceLocator(fileName, source), | ||
result = new CoverageObject(); | ||
|
||
Object.keys(fileObject.statementMap).forEach(function (key) { | ||
var statement = fileObject.statementMap[key], | ||
originalStart = locator.locate(statement.start.line, statement.start.column), | ||
originalEnd = locator.locate(statement.end.line, statement.end.column); | ||
result.addStatement(originalStart, originalEnd, fileObject.s[key]); | ||
}); | ||
|
||
Object.keys(fileObject.fnMap).forEach(function (key) { | ||
var fn = fileObject.fnMap[key], | ||
loc = fn.loc, | ||
originalStart = locator.locate(loc.start.line, loc.start.column), | ||
originalEnd = locator.locate(loc.end.line, loc.end.column); | ||
result.addFunction(fn.name, originalStart, originalEnd, fileObject.f[key]); | ||
}); | ||
|
||
Object.keys(fileObject.branchMap).forEach(function (key) { | ||
var branch = fileObject.branchMap[key], | ||
locations = branch.locations.map(function (loc) { | ||
return { | ||
start: locator.locate(loc.start.line, loc.start.column), | ||
end: locator.locate(loc.end.line, loc.end.column) | ||
}; | ||
}); | ||
result.addBranch( | ||
branch.type, | ||
locations, | ||
fileObject.b[key] | ||
); | ||
}); | ||
|
||
return result.coverage; | ||
} | ||
|
||
module.exports = unmapCoverageObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.instrumentedTarget = function instrumentedTarget(target) { | ||
return target + '.instr.js'; | ||
}; |
Oops, something went wrong.