Skip to content

Commit

Permalink
feat: add support for .nycrc (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 15, 2016
1 parent bc0a99a commit 1c2349b
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ improve runtime performance.

## Configuring `nyc`

Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json (these will not affect `nyc` subcommands):
Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a `.nycrc` file:

**package.json:**

```json
{
Expand Down Expand Up @@ -260,6 +262,20 @@ Any configuration options that can be set via the command line can also be speci
}
```

**.nycrc:**

```json
{
"reporter": [
"lcov",
"text-summary"
],
"require": [
"./test/helpers/some-helper.js"
]
}
```

## Instrumenting source files

nyc's `instrument` command can be used to instrument
Expand Down
16 changes: 13 additions & 3 deletions lib/config-util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var arrify = require('arrify')
var fs = require('fs')
var path = require('path')
var pkgUp = require('pkg-up')
var findUp = require('find-up')
var testExclude = require('test-exclude')
var Yargs = require('yargs/yargs')

Expand All @@ -12,7 +13,15 @@ var Config = {}
// * .nycrc (coming soon)
Config.loadConfig = function (argv, cwd) {
cwd = cwd || process.env.NYC_CWD || process.cwd()
var pkgPath = pkgUp.sync(cwd)
var pkgPath = findUp.sync('package.json', {cwd: cwd})
var rcPath = findUp.sync('.nycrc', {cwd: cwd})
var rcConfig = null

if (rcPath) {
rcConfig = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
)
}

if (pkgPath) {
cwd = path.dirname(pkgPath)
Expand All @@ -22,7 +31,8 @@ Config.loadConfig = function (argv, cwd) {
.default({
cwd: cwd
})
.parse(argv || [])
if (rcConfig) config.config(rcConfig)
config = config.parse(argv || [])

// post-hoc, we convert several of the
// configuration settings to arrays, providing
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
"md5-hex": "^1.2.0",
"micromatch": "^2.3.11",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
"resolve-from": "^2.0.0",
"rimraf": "^2.5.4",
"signal-exit": "^3.0.1",
Expand Down Expand Up @@ -143,7 +142,6 @@
"md5-hex",
"micromatch",
"mkdirp",
"pkg-up",
"resolve-from",
"rimraf",
"signal-exit",
Expand All @@ -152,4 +150,4 @@
"yargs",
"yargs-parser"
]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exclude": ["ignore.js"]
}
1 change: 1 addition & 0 deletions test/fixtures/cli/nycrc/ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var i = 2
11 changes: 11 additions & 0 deletions test/fixtures/cli/nycrc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('./ignore')

var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
5 changes: 5 additions & 0 deletions test/fixtures/cli/nycrc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"nyc": {
"reporter": ["text-lcov"]
}
}
46 changes: 46 additions & 0 deletions test/src/nyc-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,52 @@ describe('the nyc cli', function () {
done()
})
})

describe('.nycrc', function () {
var cwd = path.resolve(fixturesCLI, './nycrc')

it('loads configuration from package.json and .nycrc', function (done) {
var args = [bin, process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.not.match(/SF:.*ignore\.js/)
done()
})
})

it('allows .nycrc configuration to be overridden with command line args', function (done) {
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.match(/SF:.*ignore\.js/)
done()
})
})
})
})

it('setting instrument to "false" sets noop instrumenter', function (done) {
Expand Down

0 comments on commit 1c2349b

Please sign in to comment.