diff options
Diffstat (limited to 'node_modules/nyc/index.js')
-rwxr-xr-x | node_modules/nyc/index.js | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/node_modules/nyc/index.js b/node_modules/nyc/index.js index 87ada2daf..be11aa59b 100755 --- a/node_modules/nyc/index.js +++ b/node_modules/nyc/index.js @@ -1,3 +1,5 @@ +'use strict' + /* global __coverage__ */ const arrify = require('arrify') @@ -49,7 +51,7 @@ function NYC (config) { this.cwd = config.cwd || process.cwd() this.reporter = arrify(config.reporter || 'text') - this.cacheDirectory = config.cacheDir || findCacheDir({name: 'nyc', cwd: this.cwd}) + this.cacheDirectory = (config.cacheDir && path.resolve(config.cacheDir)) || findCacheDir({name: 'nyc', cwd: this.cwd}) this.cache = Boolean(this.cacheDirectory && config.cache) this.exclude = testExclude({ @@ -136,7 +138,10 @@ NYC.prototype.instrumenter = function () { NYC.prototype._createInstrumenter = function () { return this._instrumenterLib(this.cwd, { - produceSourceMap: this.config.produceSourceMap + ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a), + produceSourceMap: this.config.produceSourceMap, + compact: this.config.compact, + preserveComments: this.config.preserveComments }) } @@ -231,6 +236,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) { } catch (err) { return cb(err) } + cb() } NYC.prototype.walkAllFiles = function (dir, visitor) { @@ -264,25 +270,27 @@ NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) { } NYC.prototype._transformFactory = function (cacheDir) { - var _this = this - var instrumenter = this.instrumenter() - var instrumented + const instrumenter = this.instrumenter() + let instrumented - return function (code, metadata, hash) { - var filename = metadata.filename - var sourceMap = null + return (code, metadata, hash) => { + const filename = metadata.filename + let sourceMap = null - if (_this._sourceMap) sourceMap = _this.sourceMaps.extractAndRegister(code, filename, hash) + if (this._sourceMap) sourceMap = this.sourceMaps.extractAndRegister(code, filename, hash) try { instrumented = instrumenter.instrumentSync(code, filename, sourceMap) } catch (e) { - // don't fail external tests due to instrumentation bugs. debugLog('failed to instrument ' + filename + 'with error: ' + e.stack) - instrumented = code + if (this.config.exitOnError) { + process.exit(1) + } else { + instrumented = code + } } - if (_this.fakeRequire) { + if (this.fakeRequire) { return 'function x () {}' } else { return instrumented @@ -417,13 +425,13 @@ function coverageFinder () { return coverage } -NYC.prototype._getCoverageMapFromAllCoverageFiles = function () { +NYC.prototype.getCoverageMapFromAllCoverageFiles = function (baseDirectory) { var _this = this var map = libCoverage.createCoverageMap({}) - this.loadReports().forEach(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 @@ -439,16 +447,18 @@ NYC.prototype._getCoverageMapFromAllCoverageFiles = function () { NYC.prototype.report = function () { var tree - var map = this._getCoverageMapFromAllCoverageFiles() + var map = this.getCoverageMapFromAllCoverageFiles() var context = libReport.createContext({ - dir: this._reportDir, + dir: this.reportDirectory(), watermarks: this.config.watermarks }) tree = libReport.summarizers.pkg(map) - this.reporter.forEach(function (_reporter) { - tree.visit(reports.create(_reporter), context) + this.reporter.forEach((_reporter) => { + tree.visit(reports.create(_reporter, { + skipEmpty: this.config.skipEmpty + }), context) }) if (this._showProcessTree) { @@ -463,7 +473,7 @@ NYC.prototype.showProcessTree = function () { } NYC.prototype.checkCoverage = function (thresholds, perFile) { - var map = this._getCoverageMapFromAllCoverageFiles() + var map = this.getCoverageMapFromAllCoverageFiles() var nyc = this if (perFile) { @@ -510,30 +520,52 @@ NYC.prototype._loadProcessInfos = function () { }) } -NYC.prototype.loadReports = function (filenames) { +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) - return files.map(function (f) { + files.forEach(function (f) { var report try { report = JSON.parse(fs.readFileSync( - path.resolve(_this.tempDirectory(), f), + path.resolve(baseDirectory, f), 'utf-8' )) + + _this.sourceMaps.reloadCachedSourceMaps(report) } catch (e) { // handle corrupt JSON output. - return {} + report = {} } - _this.sourceMaps.reloadCachedSourceMaps(report) - return report + iterator(report) + }) +} + +NYC.prototype.loadReports = function (filenames) { + var reports = [] + + this.eachReport(filenames, (report) => { + reports.push(report) }) + + return reports } NYC.prototype.tempDirectory = function () { return path.resolve(this.cwd, this._tempDirectory) } +NYC.prototype.reportDirectory = function () { + return path.resolve(this.cwd, this._reportDir) +} + NYC.prototype.processInfoDirectory = function () { return path.resolve(this.tempDirectory(), 'processinfo') } |