Skip to content

Commit 92dedda

Browse files
addaleaxbcoe
authored andcommitted
feat: coverage information is now returned for process tree (#416)
1 parent ac31339 commit 92dedda

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ function coverageFinder () {
413413
NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {
414414
var map = libCoverage.createCoverageMap({})
415415

416-
this._loadReports().forEach(function (report) {
416+
this.loadReports().forEach(function (report) {
417417
map.merge(report)
418418
})
419419

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

441441
NYC.prototype.showProcessTree = function () {
442-
console.log(this._loadProcessInfoTree().render())
442+
var processTree = ProcessInfo.buildProcessTree(this._loadProcessInfos())
443+
444+
console.log(processTree.render(this))
443445
}
444446

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

462-
NYC.prototype._loadProcessInfoTree = function () {
463-
return ProcessInfo.buildProcessTree(this._loadProcessInfos())
464-
}
465-
466464
NYC.prototype._loadProcessInfos = function () {
467465
var _this = this
468466
var files = fs.readdirSync(this.processInfoDirectory())
@@ -479,9 +477,9 @@ NYC.prototype._loadProcessInfos = function () {
479477
})
480478
}
481479

482-
NYC.prototype._loadReports = function () {
480+
NYC.prototype.loadReports = function (filenames) {
483481
var _this = this
484-
var files = fs.readdirSync(this.tempDirectory())
482+
var files = filenames || fs.readdirSync(this.tempDirectory())
485483

486484
var cacheDir = _this.cacheDirectory
487485

lib/process.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22
var archy = require('archy')
3+
var libCoverage = require('istanbul-lib-coverage')
34

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

18+
this._coverageMap = null
19+
1720
for (var key in defaults) {
1821
this[key] = defaults[key]
1922
}
@@ -25,7 +28,11 @@ Object.defineProperty(ProcessInfo.prototype, 'label', {
2528
return this._label
2629
}
2730

28-
return this.argv.join(' ')
31+
var covInfo = ''
32+
if (this._coverageMap) {
33+
covInfo = '\n ' + this._coverageMap.getCoverageSummary().lines.pct + ' % Lines'
34+
}
35+
return this.argv.join(' ') + covInfo
2936
}
3037
})
3138

@@ -57,7 +64,35 @@ ProcessInfo.buildProcessTree = function (infos) {
5764
return treeRoot
5865
}
5966

60-
ProcessInfo.prototype.render = function () {
67+
ProcessInfo.prototype.getCoverageMap = function (merger) {
68+
if (this._coverageMap) {
69+
return this._coverageMap
70+
}
71+
72+
var childMaps = this.nodes.map(function (child) {
73+
return child.getCoverageMap(merger)
74+
})
75+
76+
this._coverageMap = merger([this.coverageFilename], childMaps)
77+
78+
return this._coverageMap
79+
}
80+
81+
ProcessInfo.prototype.render = function (nyc) {
82+
this.getCoverageMap(function (filenames, maps) {
83+
var map = libCoverage.createCoverageMap({})
84+
85+
nyc.loadReports(filenames).forEach(function (report) {
86+
map.merge(report)
87+
})
88+
89+
maps.forEach(function (otherMap) {
90+
map.merge(otherMap)
91+
})
92+
93+
return map
94+
})
95+
6196
return archy(this)
6297
}
6398

test/src/nyc-bin.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,23 @@ describe('the nyc cli', function () {
580580
stdout.should.match(new RegExp(
581581
'nyc\n' +
582582
'└─┬.*selfspawn-fibonacci.js 5\n' +
583+
' │.* % Lines\n' +
583584
' ├─┬.*selfspawn-fibonacci.js 4\n' +
585+
' │ │.* % Lines\n' +
584586
' │ ├─┬.*selfspawn-fibonacci.js 3\n' +
587+
' │ │ │.* % Lines\n' +
585588
' │ │ ├──.*selfspawn-fibonacci.js 2\n' +
589+
' │ │ │.* % Lines\n' +
586590
' │ │ └──.*selfspawn-fibonacci.js 1\n' +
591+
' │ │ .* % Lines\n' +
587592
' │ └──.*selfspawn-fibonacci.js 2\n' +
593+
' │ .* % Lines\n' +
588594
' └─┬.*selfspawn-fibonacci.js 3\n' +
595+
' │.* % Lines\n' +
589596
' ├──.*selfspawn-fibonacci.js 2\n' +
590-
' └──.*selfspawn-fibonacci.js 1\n'
597+
' │.* % Lines\n' +
598+
' └──.*selfspawn-fibonacci.js 1\n' +
599+
' .* % Lines\n'
591600
))
592601
done()
593602
})

test/src/nyc-test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('nyc', function () {
248248
})
249249

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

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

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

328328
var notLoadedPath1 = path.join(cwd, './not-loaded.es6')
329329
var notLoadedPath2 = path.join(cwd, './not-loaded.js')
330-
var reports = _.filter(nyc._loadReports(), function (report) {
330+
var reports = _.filter(nyc.loadReports(), function (report) {
331331
var apr = ap(report)
332332
return apr[notLoadedPath1] || apr[notLoadedPath2]
333333
})
@@ -355,7 +355,7 @@ describe('nyc', function () {
355355
nyc.writeCoverageFile()
356356

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

381381
var needsTranspilePath = path.join(fixtures, './needs-transpile.js')
382-
var reports = _.filter(nyc._loadReports(), function (report) {
382+
var reports = _.filter(nyc.loadReports(), function (report) {
383383
return ap(report)[needsTranspilePath]
384384
})
385385
var report = reports[0][needsTranspilePath]
@@ -408,7 +408,7 @@ describe('nyc', function () {
408408
nyc.addAllFiles()
409409

410410
var needsTranspilePath = path.join(fixtures, './needs-transpile.whatever')
411-
var reports = _.filter(nyc._loadReports(), function (report) {
411+
var reports = _.filter(nyc.loadReports(), function (report) {
412412
return ap(report)[needsTranspilePath]
413413
})
414414
var report = reports[0][needsTranspilePath]

0 commit comments

Comments
 (0)