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: merge together multiple istanbul format reports #840

Merged
merged 3 commits into from
May 14, 2018
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
10 changes: 4 additions & 6 deletions bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ const config = configUtil.loadConfig(yargs.parse(instrumenterArgs))
configUtil.addCommandsAndHelp(yargs)
const argv = yargs.config(config).parse(instrumenterArgs)

if (argv._[0] === 'report') {
// look in lib/commands/report.js for logic.
} else if (argv._[0] === 'check-coverage') {
// look in lib/commands/check-coverage.js for logic.
} else if (argv._[0] === 'instrument') {
// look in lib/commands/instrument.js for logic.
if ([
'check-coverage', 'report', 'instrument', 'merge'
].indexOf(argv._[0]) !== -1) {
// look in lib/commands for logic.
} else if (argv._.length) {
// if instrument is set to false,
// enable a noop instrumenter.
Expand Down
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,13 @@ function coverageFinder () {
return coverage
}

NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {
NYC.prototype.getCoverageMapFromAllCoverageFiles = function (baseDirectory) {
var _this = this
var map = libCoverage.createCoverageMap({})

this.eachReport(function (report) {
this.eachReport(undefined, (report) => {
map.merge(report)
})
}, baseDirectory)
// depending on whether source-code is pre-instrumented
// or instrumented using a JIT plugin like babel-require
// you may opt to exclude files after applying
Expand All @@ -443,7 +443,7 @@ NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {

NYC.prototype.report = function () {
var tree
var map = this._getCoverageMapFromAllCoverageFiles()
var map = this.getCoverageMapFromAllCoverageFiles()
var context = libReport.createContext({
dir: this.reportDirectory(),
watermarks: this.config.watermarks
Expand All @@ -469,7 +469,7 @@ NYC.prototype.showProcessTree = function () {
}

NYC.prototype.checkCoverage = function (thresholds, perFile) {
var map = this._getCoverageMapFromAllCoverageFiles()
var map = this.getCoverageMapFromAllCoverageFiles()
var nyc = this

if (perFile) {
Expand Down Expand Up @@ -516,20 +516,22 @@ NYC.prototype._loadProcessInfos = function () {
})
}

NYC.prototype.eachReport = function (filenames, iterator) {
NYC.prototype.eachReport = function (filenames, iterator, baseDirectory) {
baseDirectory = baseDirectory || this.tempDirectory()

if (typeof filenames === 'function') {
iterator = filenames
filenames = undefined
}

var _this = this
var files = filenames || fs.readdirSync(this.tempDirectory())
var files = filenames || fs.readdirSync(baseDirectory)

files.forEach(function (f) {
var report
try {
report = JSON.parse(fs.readFileSync(
path.resolve(_this.tempDirectory(), f),
path.resolve(baseDirectory, f),
'utf-8'
))

Expand All @@ -545,7 +547,7 @@ NYC.prototype.eachReport = function (filenames, iterator) {
NYC.prototype.loadReports = function (filenames) {
var reports = []

this.eachReport(filenames, function (report) {
this.eachReport(filenames, (report) => {
reports.push(report)
})

Expand Down
51 changes: 51 additions & 0 deletions lib/commands/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'
const fs = require('fs')

var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}

exports.command = 'merge <input-directory> [output-file]'

exports.describe = 'merge istanbul format coverage output in a given folder'

exports.builder = function (yargs) {
return yargs
.positional('input-directory', {
describe: 'directory containing multiple istanbul coverage files',
type: 'text',
default: './.nyc_output'
})
.positional('output-file', {
describe: 'file to output combined istanbul format coverage to',
type: 'text',
default: 'coverage.json'
})
.option('temp-directory', {
describe: 'directory to read raw coverage information from',
default: './.nyc_output'
})
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')
}

exports.handler = function (argv) {
process.env.NYC_CWD = process.cwd()
const nyc = new NYC(argv)
let inputStat
try {
inputStat = fs.statSync(argv.inputDirectory)
if (!inputStat.isDirectory()) {
console.error(`${argv.inputDirectory} was not a directory`)
process.exit(1)
}
} catch (err) {
console.error(`failed access input directory ${argv.inputDirectory} with error:\n\n${err.message}`)
process.exit(1)
}
const map = nyc.getCoverageMapFromAllCoverageFiles(argv.inputDirectory)
fs.writeFileSync(argv.outputFile, JSON.stringify(map, null, 2), 'utf8')
console.info(`coverage files in ${argv.inputDirectory} merged into ${argv.outputFile}`)
}
1 change: 1 addition & 0 deletions lib/config-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Config.addCommandsAndHelp = function (yargs) {
.command(require('../lib/commands/check-coverage'))
.command(require('../lib/commands/instrument'))
.command(require('../lib/commands/report'))
.command(require('../lib/commands/merge'))
}

module.exports = Config
3 changes: 2 additions & 1 deletion lib/process-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const parser = require('yargs-parser')
const commands = [
'report',
'check-coverage',
'instrument'
'instrument',
'merge'
]

module.exports = {
Expand Down
200 changes: 200 additions & 0 deletions test/fixtures/cli/merge-input/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"/private/tmp/contrived/library.js": {
"path": "/private/tmp/contrived/library.js",
"statementMap": {
"0": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 15,
"column": 1
}
},
"1": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 16
}
},
"2": {
"start": {
"line": 6,
"column": 4
},
"end": {
"line": 6,
"column": 16
}
},
"3": {
"start": {
"line": 9,
"column": 4
},
"end": {
"line": 13,
"column": 5
}
},
"4": {
"start": {
"line": 10,
"column": 6
},
"end": {
"line": 10,
"column": 14
}
},
"5": {
"start": {
"line": 12,
"column": 6
},
"end": {
"line": 12,
"column": 14
}
}
},
"fnMap": {
"0": {
"name": "(anonymous_0)",
"decl": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 12
}
},
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 4,
"column": 3
}
},
"line": 2
},
"1": {
"name": "(anonymous_1)",
"decl": {
"start": {
"line": 5,
"column": 11
},
"end": {
"line": 5,
"column": 12
}
},
"loc": {
"start": {
"line": 5,
"column": 21
},
"end": {
"line": 7,
"column": 3
}
},
"line": 5
},
"2": {
"name": "(anonymous_2)",
"decl": {
"start": {
"line": 8,
"column": 11
},
"end": {
"line": 8,
"column": 12
}
},
"loc": {
"start": {
"line": 8,
"column": 18
},
"end": {
"line": 14,
"column": 3
}
},
"line": 8
}
},
"branchMap": {
"0": {
"loc": {
"start": {
"line": 9,
"column": 4
},
"end": {
"line": 13,
"column": 5
}
},
"type": "if",
"locations": [
{
"start": {
"line": 9,
"column": 4
},
"end": {
"line": 13,
"column": 5
}
},
{
"start": {
"line": 9,
"column": 4
},
"end": {
"line": 13,
"column": 5
}
}
],
"line": 9
}
},
"s": {
"0": 1,
"1": 1,
"2": 0,
"3": 1,
"4": 0,
"5": 1
},
"f": {
"0": 1,
"1": 0,
"2": 1
},
"b": {
"0": [
0,
1
]
},
"_coverageSchema": "332fd63041d2c1bcb487cc26dd0d5f7d97098a6c",
"hash": "e86c0c0fa7c4fadac81e2479bfba3c0d59b657aa"
}
}
Loading