Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement coverage details for process tree #416

Merged
merged 2 commits into from
Oct 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool! it works because we already output the coverage for each subprocess to a separate file named after the pid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep – the coverage filename is included in the file containing the process info. I thought about magically inferring the filename from the PID, but I wanted to keep the flexibility – mostly because I’ve been hearing Bad Things™ about assuming uniqueness of PIDs on Windows. 😄

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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