From 53fef4820e7b502d00561fb5d16f5bfb4b641192 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Sat, 23 May 2015 13:29:36 -0700 Subject: [PATCH] put tests around @shackpank's work on .istanbul.yml --- README.md | 11 +++++++ index.js | 34 +++++++++++++-------- package.json | 1 + test/fixtures/.istanbul.yml | 2 ++ test/nyc-test.js | 60 +++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/.istanbul.yml diff --git a/README.md b/README.md index 6f52c70dc..6fb5efa93 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,17 @@ adding the following configuration: }} ``` +## Configuring Istanbul + +Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You +can place a `.istanbul.yml` file in your project's root directory to pass config +setings to istanbul's code instrumenter: + +```yml +instrumentation: + preserve-comments: true +``` + ## Integrating With Coveralls [coveralls.io](https://coveralls.io) is a great tool for adding diff --git a/index.js b/index.js index 5b877b8c9..9598f6f9b 100755 --- a/index.js +++ b/index.js @@ -2,14 +2,6 @@ var _ = require('lodash'), fs = require('fs'), - istanbul = require('istanbul'), - instrumenterConfig = istanbul.config.loadFile().instrumentation.config, - instrumenter = new istanbul.Instrumenter({ - coverageVariable: instrumenterConfig.variable, - embedSource: instrumenterConfig['embed-source'], - noCompact: !instrumenterConfig.compact, - preserveComments: instrumenterConfig['preserve-comments'] - }), mkdirp = require('mkdirp'), path = require('path'), rimraf = require('rimraf'), @@ -24,7 +16,8 @@ function NYC (opts) { ), tempDirectory: './nyc_output', cwd: process.env.NYC_CWD || process.cwd(), - reporter: 'text' + reporter: 'text', + istanbul: require('istanbul') }, opts) var config = require(path.resolve(this.cwd, './package.json')).config || {} @@ -36,9 +29,26 @@ function NYC (opts) { return new RegExp(p) }) + this.instrumenter = this._createInstrumenter() + mkdirp.sync(this.tmpDirectory()) } +NYC.prototype._createInstrumenter = function () { + var configFile = path.resolve(this.cwd, './.istanbul.yml') + + if (!fs.existsSync(configFile)) configFile = undefined + + var instrumenterConfig = this.istanbul.config.loadFile(configFile).instrumentation.config + + return new this.istanbul.Instrumenter({ + coverageVariable: '__coverage__', + embedSource: instrumenterConfig['embed-source'], + noCompact: !instrumenterConfig.compact, + preserveComments: instrumenterConfig['preserve-comments'] + }) +} + NYC.prototype.cleanup = function () { if (!process.env.NYC_CWD) rimraf.sync(this.tmpDirectory()) } @@ -58,7 +68,7 @@ NYC.prototype._wrapRequire = function () { } if (instrument) { - content = instrumenter.instrumentSync( + content = _this.instrumenter.instrumentSync( content, './' + relFile ) @@ -96,8 +106,8 @@ NYC.prototype.wrap = function (bin) { } NYC.prototype.report = function (_collector, _reporter) { - var collector = _collector || new istanbul.Collector(), - reporter = _reporter || new istanbul.Reporter() + var collector = _collector || new this.istanbul.Collector(), + reporter = _reporter || new this.istanbul.Reporter() this._loadReports().forEach(function (report) { collector.add(report) diff --git a/package.json b/package.json index 9853e624b..e714a4e97 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "devDependencies": { "chai": "^2.3.0", "coveralls": "^2.11.2", + "sinon": "^1.14.1", "standard": "^3.7.3", "tap": "1.0.4" }, diff --git a/test/fixtures/.istanbul.yml b/test/fixtures/.istanbul.yml new file mode 100644 index 000000000..0b54cadb9 --- /dev/null +++ b/test/fixtures/.istanbul.yml @@ -0,0 +1,2 @@ +instrumentation: + preserve-comments: true diff --git a/test/nyc-test.js b/test/nyc-test.js index 1408c03bd..50d33f5e7 100644 --- a/test/nyc-test.js +++ b/test/nyc-test.js @@ -5,6 +5,7 @@ var _ = require('lodash'), NYC = require('../'), path = require('path'), rimraf = require('rimraf'), + sinon = require('sinon'), spawn = require('child_process').spawn require('chai').should() @@ -127,4 +128,63 @@ describe('nyc', function () { }) }) }) + + describe('.istanbul.yml configuration', function () { + var istanbul = require('istanbul') + var configSpy = sinon.spy(istanbul.config, 'loadFile') + var instrumenterSpy = sinon.spy(istanbul, 'Instrumenter') + + function writeConfig () { + fs.writeFileSync('./.istanbul.yml', 'instrumentation:\n\tpreserve-comments: true', 'utf-8') + } + + function afterEach () { + configSpy.reset() + instrumenterSpy.reset() + rimraf.sync('./.istanbul.yml') + } + + it('it handles having no .istanbul.yml in the root directory', function (done) { + afterEach() + var nyc = new NYC() + return done() + }) + + it('uses the values in .istanbul.yml to instantiate the instrumenter', function (done) { + writeConfig() + + var nyc = new NYC({ + istanbul: istanbul + }) + + istanbul.config.loadFile.calledWithMatch('.istanbul.yml').should.equal(true) + istanbul.Instrumenter.calledWith({ + coverageVariable: '__coverage__', + embedSource: false, + noCompact: false, + preserveComments: false + }).should.equal(true) + + afterEach() + return done(); + }) + + it('loads the .istanbul.yml configuration from NYC_CWD', function (done) { + var nyc = new NYC({ + istanbul: istanbul, + cwd: './test/fixtures' + }) + + istanbul.config.loadFile.calledWithMatch('test/fixtures/.istanbul.yml').should.equal(true) + istanbul.Instrumenter.calledWith({ + coverageVariable: '__coverage__', + embedSource: false, + noCompact: false, + preserveComments: true + }).should.equal(true) + + afterEach() + return done() + }) + }) })