From 3a10b457c8a8426ee5b5e3adcf17692390a48b82 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Mon, 18 Jan 2016 17:10:30 -0800 Subject: [PATCH] allow the directory that the coverage report outputs in to be configured --- bin/nyc.js | 11 ++++++++++- index.js | 3 ++- package.json | 1 + test/src/nyc-test.js | 24 ++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/bin/nyc.js b/bin/nyc.js index 5536c2741..2e7ef69b8 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -21,6 +21,10 @@ var yargs = require('yargs') describe: 'coverage reporter(s) to use', default: 'text' }) + .option('report-dir', { + describe: 'default directory to output coverage reports in', + default: 'coverage' + }) .help('h') .alias('h', 'help') .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage') @@ -57,6 +61,10 @@ var yargs = require('yargs') describe: 'coverage reporter(s) to use', default: 'text' }) + .option('report-dir', { + describe: 'default directory to output coverage reports in', + default: 'coverage' + }) .option('s', { alias: 'silent', default: false, @@ -154,7 +162,8 @@ function report (argv) { process.env.NYC_CWD = process.cwd() ;(new NYC({ - reporter: argv.reporter + reporter: argv.reporter, + reportDir: argv.reportDir })).report() } diff --git a/index.js b/index.js index 37cb209f6..57b3c647a 100755 --- a/index.js +++ b/index.js @@ -29,6 +29,7 @@ function NYC (opts) { this._istanbul = opts.istanbul this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js') this._tempDirectory = opts.tempDirectory || './.nyc_output' + this._reportDir = opts.reportDir var config = this._loadConfig(opts) this.cwd = config.cwd @@ -298,7 +299,7 @@ NYC.prototype.report = function (cb, _collector, _reporter) { var istanbul = this.istanbul() var collector = _collector || new istanbul.Collector() - var reporter = _reporter || new istanbul.Reporter() + var reporter = _reporter || new istanbul.Reporter(null, this._reportDir) this._loadReports().forEach(function (report) { collector.add(report) diff --git a/package.json b/package.json index 08cac0837..c3a563375 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "any-path": "^1.3.0", "chai": "^3.0.0", "coveralls": "^2.11.4", + "exists-sync": "0.0.3", "forking-tap": "^0.1.1", "is-windows": "^0.1.0", "lodash": "^3.10.0", diff --git a/test/src/nyc-test.js b/test/src/nyc-test.js index d4bc1a1b3..be63f3631 100644 --- a/test/src/nyc-test.js +++ b/test/src/nyc-test.js @@ -22,6 +22,7 @@ function NYC (opts) { } var path = require('path') +var existsSync = require('exists-sync') var rimraf = require('rimraf') var sinon = require('sinon') var isWindows = require('is-windows')() @@ -350,6 +351,29 @@ describe('nyc', function () { ) }) }) + + it('allows coverage report to be output in an alternative directory', function (done) { + var reporters = ['lcov'] + var nyc = new NYC({ + cwd: process.cwd(), + reporter: reporters, + reportDir: './alternative-report' + }) + nyc.reset() + + var proc = spawn(process.execPath, ['./test/fixtures/child-1.js'], { + cwd: process.cwd(), + env: process.env, + stdio: 'inherit' + }) + + proc.on('close', function () { + nyc.report() + existsSync('./alternative-report/lcov.info').should.equal(true) + rimraf.sync('./alternative-report') + return done() + }) + }) }) describe('.istanbul.yml configuration', function () {