Skip to content

Commit

Permalink
feat: coverage information is now returned for process tree (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored and bcoe committed Oct 30, 2016
1 parent ac31339 commit 92dedda
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
14 changes: 6 additions & 8 deletions index.js
Expand Up @@ -413,7 +413,7 @@ function coverageFinder () {
NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {
var map = libCoverage.createCoverageMap({})

this._loadReports().forEach(function (report) {
this.loadReports().forEach(function (report) {
map.merge(report)
})

Expand All @@ -439,7 +439,9 @@ NYC.prototype.report = function () {
}

NYC.prototype.showProcessTree = function () {
console.log(this._loadProcessInfoTree().render())
var processTree = ProcessInfo.buildProcessTree(this._loadProcessInfos())

console.log(processTree.render(this))
}

NYC.prototype.checkCoverage = function (thresholds) {
Expand All @@ -459,10 +461,6 @@ NYC.prototype.checkCoverage = function (thresholds) {
if (/^v0\.(1[0-1]\.|[0-9]\.)/.test(process.version) && process.exitCode !== 0) process.exit(process.exitCode)
}

NYC.prototype._loadProcessInfoTree = function () {
return ProcessInfo.buildProcessTree(this._loadProcessInfos())
}

NYC.prototype._loadProcessInfos = function () {
var _this = this
var files = fs.readdirSync(this.processInfoDirectory())
Expand All @@ -479,9 +477,9 @@ NYC.prototype._loadProcessInfos = function () {
})
}

NYC.prototype._loadReports = function () {
NYC.prototype.loadReports = function (filenames) {
var _this = this
var files = fs.readdirSync(this.tempDirectory())
var files = filenames || fs.readdirSync(this.tempDirectory())

var cacheDir = _this.cacheDirectory

Expand Down
39 changes: 37 additions & 2 deletions lib/process.js
@@ -1,5 +1,6 @@
'use strict'
var archy = require('archy')
var libCoverage = require('istanbul-lib-coverage')

function ProcessInfo (defaults) {
defaults = defaults || {}
Expand All @@ -14,6 +15,8 @@ function ProcessInfo (defaults) {
this.coverageFilename = null
this.nodes = [] // list of children, filled by buildProcessTree()

this._coverageMap = null

for (var key in defaults) {
this[key] = defaults[key]
}
Expand All @@ -25,7 +28,11 @@ Object.defineProperty(ProcessInfo.prototype, 'label', {
return this._label
}

return this.argv.join(' ')
var covInfo = ''
if (this._coverageMap) {
covInfo = '\n ' + this._coverageMap.getCoverageSummary().lines.pct + ' % Lines'
}
return this.argv.join(' ') + covInfo
}
})

Expand Down Expand Up @@ -57,7 +64,35 @@ ProcessInfo.buildProcessTree = function (infos) {
return treeRoot
}

ProcessInfo.prototype.render = function () {
ProcessInfo.prototype.getCoverageMap = function (merger) {
if (this._coverageMap) {
return this._coverageMap
}

var childMaps = this.nodes.map(function (child) {
return child.getCoverageMap(merger)
})

this._coverageMap = merger([this.coverageFilename], childMaps)

return this._coverageMap
}

ProcessInfo.prototype.render = function (nyc) {
this.getCoverageMap(function (filenames, maps) {
var map = libCoverage.createCoverageMap({})

nyc.loadReports(filenames).forEach(function (report) {
map.merge(report)
})

maps.forEach(function (otherMap) {
map.merge(otherMap)
})

return map
})

return archy(this)
}

Expand Down
11 changes: 10 additions & 1 deletion test/src/nyc-bin.js
Expand Up @@ -580,14 +580,23 @@ describe('the nyc cli', function () {
stdout.should.match(new RegExp(
'nyc\n' +
'└─┬.*selfspawn-fibonacci.js 5\n' +
' │.* % Lines\n' +
' ├─┬.*selfspawn-fibonacci.js 4\n' +
' │ │.* % Lines\n' +
' │ ├─┬.*selfspawn-fibonacci.js 3\n' +
' │ │ │.* % Lines\n' +
' │ │ ├──.*selfspawn-fibonacci.js 2\n' +
' │ │ │.* % Lines\n' +
' │ │ └──.*selfspawn-fibonacci.js 1\n' +
' │ │ .* % Lines\n' +
' │ └──.*selfspawn-fibonacci.js 2\n' +
' │ .* % Lines\n' +
' └─┬.*selfspawn-fibonacci.js 3\n' +
' │.* % Lines\n' +
' ├──.*selfspawn-fibonacci.js 2\n' +
' └──.*selfspawn-fibonacci.js 1\n'
' │.* % Lines\n' +
' └──.*selfspawn-fibonacci.js 1\n' +
' .* % Lines\n'
))
done()
})
Expand Down
14 changes: 7 additions & 7 deletions test/src/nyc-test.js
Expand Up @@ -248,7 +248,7 @@ describe('nyc', function () {
})

proc.on('close', function () {
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return report[path.join(fixtures, signal + '.js')]
})
reports.length.should.equal(1)
Expand All @@ -271,7 +271,7 @@ describe('nyc', function () {
nyc.wrap()
nyc.reset()

var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return report['./test/fixtures/not-loaded.js']
})
reports.length.should.equal(0)
Expand Down Expand Up @@ -308,7 +308,7 @@ describe('nyc', function () {
nyc.addAllFiles()

var notLoadedPath = path.join(fixtures, './not-loaded.js')
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[notLoadedPath]
})
var report = reports[0][notLoadedPath]
Expand All @@ -327,7 +327,7 @@ describe('nyc', function () {

var notLoadedPath1 = path.join(cwd, './not-loaded.es6')
var notLoadedPath2 = path.join(cwd, './not-loaded.js')
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
var apr = ap(report)
return apr[notLoadedPath1] || apr[notLoadedPath2]
})
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('nyc', function () {
nyc.writeCoverageFile()

var notLoadedPath = path.join(fixtures, './not-loaded.js')
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return report[notLoadedPath]
})
var report = reports[0][notLoadedPath]
Expand All @@ -379,7 +379,7 @@ describe('nyc', function () {
nyc.addAllFiles()

var needsTranspilePath = path.join(fixtures, './needs-transpile.js')
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[needsTranspilePath]
})
var report = reports[0][needsTranspilePath]
Expand Down Expand Up @@ -408,7 +408,7 @@ describe('nyc', function () {
nyc.addAllFiles()

var needsTranspilePath = path.join(fixtures, './needs-transpile.whatever')
var reports = _.filter(nyc._loadReports(), function (report) {
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[needsTranspilePath]
})
var report = reports[0][needsTranspilePath]
Expand Down

0 comments on commit 92dedda

Please sign in to comment.