diff --git a/README.md b/README.md index 2873e44e8..00a1c870a 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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 diff --git a/lib/config-util.js b/lib/config-util.js index b137d599f..f1e67522b 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -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') @@ -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) @@ -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 diff --git a/package.json b/package.json index c14dbc3e1..519a5bd25 100644 --- a/package.json +++ b/package.json @@ -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", @@ -143,7 +142,6 @@ "md5-hex", "micromatch", "mkdirp", - "pkg-up", "resolve-from", "rimraf", "signal-exit", @@ -152,4 +150,4 @@ "yargs", "yargs-parser" ] -} +} \ No newline at end of file diff --git a/test/fixtures/cli/nycrc/.nycrc b/test/fixtures/cli/nycrc/.nycrc new file mode 100644 index 000000000..b5b5a0a32 --- /dev/null +++ b/test/fixtures/cli/nycrc/.nycrc @@ -0,0 +1,3 @@ +{ + "exclude": ["ignore.js"] +} diff --git a/test/fixtures/cli/nycrc/ignore.js b/test/fixtures/cli/nycrc/ignore.js new file mode 100644 index 000000000..2ace002b5 --- /dev/null +++ b/test/fixtures/cli/nycrc/ignore.js @@ -0,0 +1 @@ +var i = 2 diff --git a/test/fixtures/cli/nycrc/index.js b/test/fixtures/cli/nycrc/index.js new file mode 100644 index 000000000..8f4dfee22 --- /dev/null +++ b/test/fixtures/cli/nycrc/index.js @@ -0,0 +1,11 @@ +require('./ignore') + +var a = 0 + +a++ + +if (a === 0) { + a++; + a--; + a++; +} diff --git a/test/fixtures/cli/nycrc/package.json b/test/fixtures/cli/nycrc/package.json new file mode 100644 index 000000000..d73c43154 --- /dev/null +++ b/test/fixtures/cli/nycrc/package.json @@ -0,0 +1,5 @@ +{ + "nyc": { + "reporter": ["text-lcov"] + } +} diff --git a/test/src/nyc-bin.js b/test/src/nyc-bin.js index 4f998954e..f498ccbde 100644 --- a/test/src/nyc-bin.js +++ b/test/src/nyc-bin.js @@ -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) {