Skip to content

Commit

Permalink
using some of yargs 4.x's new functionality, I've made nyc more confi…
Browse files Browse the repository at this point in the history
…gurable fixes #156
  • Loading branch information
Benjamin Coe committed Feb 14, 2016
1 parent 8089839 commit 0b45e56
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 75 deletions.
20 changes: 18 additions & 2 deletions README.md
Expand Up @@ -60,6 +60,7 @@ to inline the source map in the transpiled code. For Babel that means setting
the `sourceMaps` option to `inline`.

## Support For Custom File Extensions (.jsx, .es6)

Supporting file extensions can be configured through either the configuration arguments or with the `nyc` config section in `package.json`.

```shell
Expand Down Expand Up @@ -148,11 +149,11 @@ As an alternative to providing a list of files to `exclude`, you can provide
an `include` key to specify specific files that should be covered:

```json
{"config": {
{
"nyc": {
"include": ["**/build/umd/moment.js"]
}
}}
}
```

> Note: include defaults to `['**']`
Expand All @@ -175,6 +176,21 @@ You can run `nyc` with the optional `--cache` flag, to prevent it from
instrumenting the same files multiple times. This can signficantly
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:

```json
{
"nyc": {
"lines": 99,
"check-coverage": false,
"report-dir": "./alternative"
},
}
```

## Configuring Istanbul

Behind the scenes nyc uses [istanbul](https://www.npmjs.com/package/istanbul). You
Expand Down
59 changes: 17 additions & 42 deletions bin/nyc.js
Expand Up @@ -14,56 +14,25 @@ var wrapper = require.resolve('./wrap.js')
var yargs = require('yargs')
.usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
.command('report', 'run coverage report for .nyc_output', function (yargs) {
yargs
return yargs
.usage('$0 report [options]')
.option('r', {
alias: 'reporter',
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')
})
.command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
yargs
return yargs
.usage('$0 check-coverage [options]')
.option('b', {
alias: 'branches',
default: 0,
description: 'what % of branches must be covered?'
})
.option('f', {
alias: 'functions',
default: 0,
description: 'what % of functions must be covered?'
})
.option('l', {
alias: 'lines',
default: 90,
description: 'what % of lines must be covered?'
})
.option('s', {
alias: 'statements',
default: 0,
description: 'what % of statements must be covered?'
})
.help('h')
.alias('h', 'help')
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
})
.option('r', {
alias: 'reporter',
describe: 'coverage reporter(s) to use',
default: 'text'
default: 'text',
global: true
})
.option('report-dir', {
describe: 'default directory to output coverage reports in',
default: 'coverage'
default: 'coverage',
global: true
})
.option('s', {
alias: 'silent',
Expand Down Expand Up @@ -100,27 +69,33 @@ var yargs = require('yargs')
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?'
description: 'what % of branches must be covered?',
global: true
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?'
description: 'what % of functions must be covered?',
global: true
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?'
description: 'what % of lines must be covered?',
global: true
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?'
description: 'what % of statements must be covered?',
global: true
})
.help('h')
.alias('h', 'help')
.version(require('../package.json').version)
.version()
.pkgConf('nyc', process.cwd())
.example('$0 npm test', 'instrument your tests with coverage')
.example('$0 --require babel-core/polyfill --require babel-core/register npm test', 'instrument your tests with coverage and babel')
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
.epilog('visit http://git.io/vTJJB for list of available reporters')

var argv = yargs.argv

if (argv._[0] === 'report') {
Expand Down
36 changes: 16 additions & 20 deletions index.js
Expand Up @@ -15,27 +15,25 @@ var SourceMapCache = require('./lib/source-map-cache')
var convertSourceMap = require('convert-source-map')
var md5hex = require('md5-hex')
var findCacheDir = require('find-cache-dir')
var pkgUp = require('pkg-up')
var readPkg = require('read-pkg')
var js = require('default-require-extensions/js')
var pkgUp = require('pkg-up')
var yargs = require('yargs')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
require('./lib/self-coverage-helper')
}

function NYC (opts) {
opts = 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 || {})

var config = this._loadConfig(opts)
this._istanbul = config.istanbul
this.subprocessBin = config.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
this._tempDirectory = config.tempDirectory || './.nyc_output'
this._reportDir = config.reportDir
this.cwd = config.cwd

this.reporter = arrify(opts.reporter || 'text')
this.reporter = arrify(config.reporter || 'text')

// load exclude stanza from config.
this.include = false
Expand All @@ -49,12 +47,12 @@ function NYC (opts) {

this.cacheDirectory = findCacheDir({name: 'nyc', cwd: this.cwd})

this.enableCache = Boolean(this.cacheDirectory && (opts.enableCache === true || process.env.NYC_CACHE === 'enable'))
this.enableCache = Boolean(this.cacheDirectory && (config.enableCache === true || process.env.NYC_CACHE === 'enable'))

// require extensions can be provided as config in package.json.
this.require = arrify(config.require || opts.require)
this.require = arrify(config.require)

this.extensions = arrify(config.extension || opts.extension).concat('.js').map(function (ext) {
this.extensions = arrify(config.extension).concat('.js').map(function (ext) {
return ext.toLowerCase()
})

Expand All @@ -73,18 +71,16 @@ NYC.prototype._loadConfig = function (opts) {
var cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
var pkgPath = pkgUp.sync(cwd)

// you can specify config in the nyc stanza of package.json.
var config
if (pkgPath) {
cwd = path.dirname(pkgPath)
var pkg = readPkg.sync(pkgPath, {normalize: false})
config = pkg.nyc || (pkg.config && pkg.config.nyc)
}
config = config || {}

config.cwd = cwd
opts.cwd = cwd

return config
return yargs([])
.pkgConf('nyc', cwd)
.default(opts)
.argv
}

NYC.prototype._createTransform = function (ext) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -35,7 +35,9 @@
"index.covered.js",
"test/fixtures/_generateCoverage.js"
],
"extension": [".es6"]
"extension": [
".es6"
]
},
"standard": {
"ignore": [
Expand Down Expand Up @@ -79,18 +81,18 @@
"micromatch": "~2.1.6",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
"read-pkg": "^1.1.0",
"resolve-from": "^2.0.0",
"rimraf": "^2.5.0",
"signal-exit": "^2.1.1",
"source-map": "^0.5.3",
"spawn-wrap": "^1.1.1",
"strip-bom": "^2.0.0",
"yargs": "^3.15.0"
"yargs": "^4.0.0"
},
"devDependencies": {
"any-path": "^1.3.0",
"chai": "^3.0.0",
"clear-require": "^1.0.1",
"coveralls": "^2.11.4",
"exists-sync": "0.0.3",
"forking-tap": "^0.1.1",
Expand Down
14 changes: 6 additions & 8 deletions test/fixtures/package.json
Expand Up @@ -7,13 +7,11 @@
"nyc": "./bin/nyc.js",
"nyc-report": "./bin/nyc-report.js"
},
"config": {
"nyc": {
"exclude": [
"**/blarg",
"**/blerg"
],
"extension": [".es6", ".foo.BAR"]
}
"nyc": {
"exclude": [
"**/blarg",
"**/blerg"
],
"extension": [".es6", ".foo.BAR"]
}
}
2 changes: 2 additions & 0 deletions test/src/nyc-test.js
Expand Up @@ -22,6 +22,7 @@ function NYC (opts) {
}

var path = require('path')
var clearRequire = require('clear-require')
var existsSync = require('exists-sync')
var rimraf = require('rimraf')
var sinon = require('sinon')
Expand Down Expand Up @@ -532,6 +533,7 @@ describe('nyc', function () {
'test/nyc-test.js'
]

clearRequire('yargs')
var yargv = require('yargs').argv

var munged = (new NYC()).mungeArgs(yargv)
Expand Down

0 comments on commit 0b45e56

Please sign in to comment.