Skip to content

Commit

Permalink
feat: refactored config to fix precedence of config vs. args see #379
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Sep 13, 2016
1 parent 99dbbb3 commit d8f7d87
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 5 deletions.
194 changes: 194 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
var arrify = require('arrify')
var path = require('path')
var pkgUp = require('pkg-up')
var testExclude = require('test-exclude')
var Yargs = require('yargs/yargs')

// load config from a cascade of sources:
// * command line arguments.
// * package.json.
// * .nycrc (coming soon)
function Config (argv, cwd) {
var cwd = cwd || process.env.NYC_CWD || process.cwd()
var pkgPath = pkgUp.sync(cwd)

if (pkgPath) {
cwd = path.dirname(pkgPath)
}

var config = Config.buildYargs(cwd)
.default({
cwd: cwd
})
.parse(argv || [])

// post-hoc, we convert several of the
// configuration settings to arrays, providing
// a consistent contract to index.js.
config.require = arrify(config.require)
config.extension = arrify(config.extension)
config.exclude = arrify(config.exclude)
config.include = arrify(config.include)
config.cwd = cwd

return config
}

// build a yargs object, omitting any settings
// that would cause the application to exit early.
Config.buildYargs = function (cwd) {
return 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) {
return yargs
.usage('$0 report [options]')
.option('reporter', {
alias: 'r',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('report-dir', {
describe: 'directory to output coverage reports in',
default: 'coverage'
})
.option('temp-directory', {
describe: 'directory from which coverage JSON files are read',
default: './.nyc_output'
})
.option('show-process-tree', {
describe: 'display the tree of spawned processes',
default: false,
type: 'boolean'
})
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
})
.command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
return yargs
.usage('$0 check-coverage [options]')
.option('branches', {
default: 0,
description: 'what % of branches must be covered?'
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?'
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?'
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?'
})
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
})
.option('reporter', {
alias: 'r',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('report-dir', {
describe: 'directory to output coverage reports in',
default: 'coverage'
})
.option('silent', {
alias: 's',
default: false,
type: 'boolean',
describe: "don't output a report after tests finish running"
})
.option('all', {
alias: 'a',
default: false,
type: 'boolean',
describe: 'whether or not to instrument all files of the project (not just the ones touched by your test suite)'
})
.option('exclude', {
alias: 'x',
default: testExclude.defaultExclude,
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded'
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files that should be covered, glob patterns are supported'
})
.option('require', {
alias: 'i',
default: [],
describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., babel-register, babel-polyfill.'
})
.option('cache', {
alias: 'c',
default: false,
type: 'boolean',
describe: 'cache instrumentation results for improved performance'
})
.option('extension', {
alias: 'e',
default: [],
describe: 'a list of extensions that nyc should handle in addition to .js'
})
.option('check-coverage', {
type: 'boolean',
default: false,
describe: 'check whether coverage is within thresholds provided'
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?'
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?'
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?'
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?'
})
.option('source-map', {
default: true,
type: 'boolean',
description: 'should nyc detect and handle source maps?'
})
.option('instrument', {
default: true,
type: 'boolean',
description: 'should nyc handle instrumentation?'
})
.option('hook-run-in-context', {
default: true,
type: 'boolean',
description: 'should nyc wrap vm.runInThisContext?'
})
.option('show-process-tree', {
describe: 'display the tree of spawned processes',
default: false,
type: 'boolean'
})
.pkgConf('nyc', cwd || process.cwd())
.example('$0 npm test', 'instrument your tests with coverage')
.example('$0 --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 https://git.io/voHar for list of available reporters')
.boolean('help')
.boolean('h')
.boolean('version')
}

// decorate yargs with all the actions
// that would make it exit: help, version, command.
Config.decorateYargs = function (yargs) {
return yargs
.help('h')
.alias('h', 'help')
.version()
.command(require('../lib/commands/instrument'))
}

module.exports = Config
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test/nyc-test.js",
"index.covered.js",
"test/fixtures/_generateCoverage.js"
]
],
"reporter": ["text-lcov"]
},
"standard": {
"ignore": [
Expand Down
4 changes: 0 additions & 4 deletions test/src/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ describe('nyc', function () {
describe('config', function () {
it("loads 'exclude' patterns from package.json#nyc", function () {
var nyc = new NYC(configUtil.loadConfig([], path.resolve(__dirname, '../fixtures')))

nyc.exclude.exclude.length.should.eql(4)
})

it("loads 'extension' patterns from package.json#nyc", function () {
var nyc = new NYC(configUtil.loadConfig([], path.resolve(__dirname, '../fixtures/conf-multiple-extensions')))

nyc.extensions.length.should.eql(3)
})

Expand All @@ -93,7 +91,6 @@ describe('nyc', function () {

it("ignores 'exclude' option if it's falsy", function () {
var nyc1 = new NYC(configUtil.loadConfig([], path.resolve(__dirname, '../fixtures/conf-empty')))

nyc1.exclude.exclude.length.should.eql(7)
})

Expand Down Expand Up @@ -378,7 +375,6 @@ describe('nyc', function () {
)

var nyc = (new NYC(configUtil.loadConfig(['--require', './test/fixtures/transpile-hook'], fixtures)))

nyc.reset()
nyc.addAllFiles()

Expand Down

0 comments on commit d8f7d87

Please sign in to comment.