Skip to content

Commit

Permalink
put tests around @shackpank's work on .istanbul.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed May 23, 2015
1 parent faa0afb commit 53fef48
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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 || {}
Expand All @@ -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())
}
Expand All @@ -58,7 +68,7 @@ NYC.prototype._wrapRequire = function () {
}

if (instrument) {
content = instrumenter.instrumentSync(
content = _this.instrumenter.instrumentSync(
content,
'./' + relFile
)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/.istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrumentation:
preserve-comments: true
60 changes: 60 additions & 0 deletions test/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
})
})
})

0 comments on commit 53fef48

Please sign in to comment.