Skip to content

Commit

Permalink
feat: allow alternate path for .nycrc to be specified (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlima authored and bcoe committed Nov 27, 2017
1 parent 4ff7a38 commit 785fccb
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ node_modules
test/build/
.self_coverage
*.covered.js
*.swp
needs-transpile.js
package-lock.json
3 changes: 2 additions & 1 deletion bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var wrapper = require.resolve('./wrap.js')
// reporting, instrumenting subprocesses, etc.
var yargs = configUtil.addCommandsAndHelp(configUtil.buildYargs())
var instrumenterArgs = processArgs.hideInstrumenteeArgs()
var argv = yargs.parse(instrumenterArgs)
var config = configUtil.loadConfig(processArgs.parseArgs())
var argv = yargs.config(config).parse(instrumenterArgs)

if (argv._[0] === 'report') {
// look in lib/commands/report.js for logic.
Expand Down
11 changes: 7 additions & 4 deletions lib/config-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function guessCWD (cwd) {
return cwd
}

function loadConfig (argv, cwd) {
const rcPath = findUp.sync(['.nycrc', '.nycrc.json'], {cwd: cwd})
Config.loadConfig = function (argv, cwd) {
const rcPath = findUp.sync([argv.nycrcPath || '.nycrc', '.nycrc.json'], {cwd: guessCWD(cwd)})
let config = {}

if (rcPath) {
Expand All @@ -40,7 +40,6 @@ function loadConfig (argv, cwd) {
// that would cause the application to exit early.
Config.buildYargs = function (cwd) {
cwd = guessCWD(cwd)
const config = loadConfig()
return Yargs([])
.usage('$0 [command] [options]')
.usage('$0 [options] [bin-to-instrument]')
Expand Down Expand Up @@ -196,6 +195,11 @@ Config.buildYargs = function (cwd) {
type: 'boolean',
global: false
})
.option('nycrc-path', {
default: '.nycrc',
description: 'specify a different .nycrc path',
global: false
})
.option('temp-directory', {
describe: 'directory to output raw coverage information to',
default: './.nyc_output',
Expand All @@ -208,7 +212,6 @@ Config.buildYargs = function (cwd) {
.epilog('visit https://git.io/vHysA for list of available reporters')
.boolean('h')
.boolean('version')
.config(config)
.help(false)
.version(false)
}
Expand Down
5 changes: 5 additions & 0 deletions lib/process-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module.exports = {
}
return argv
},
parseArgs: function () {
var argv = process.argv.slice(2)
var yargv = parser(argv)
return yargv
},
// don't pass arguments for the bin being
// instrumented to nyc.
hideInstrumenteeArgs: function () {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"check-coverage": true,
"per-file": true,
"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,
"exclude": []
}
22 changes: 22 additions & 0 deletions test/nyc-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ describe('the nyc cli', function () {
})
})

it('loads configuration from different file rather than .nycrc', function (done) {
var args = [bin, '--nycrc-path', './.nycrc-config.json', 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) {
// should be 1 due to coverage check
code.should.equal(1)
stdout.should.match(/SF:.*index\.js/)
stdout.should.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']

Expand Down
21 changes: 21 additions & 0 deletions test/process-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,25 @@ describe('process-args', function () {
munged.should.eql(['--version'])
})
})

describe('parseArgs', function () {
it('parses arguments such as --nycrc-path --reporter --arg', function () {
process.argv = ['/Users/benjamincoe/bin/iojs',
'/Users/benjamincoe/bin/nyc.js',
'--nycrc-path',
'./.nycrc-config.json',
'--reporter',
'lcov',
'node',
'test/nyc-tap.js',
'--arg',
'--'
]

var munged = processArgs.parseArgs()
munged.nycrcPath.should.eql('./.nycrc-config.json')
munged.reporter.should.eql('lcov')
munged.arg.should.eql(true)
})
})
})

2 comments on commit 785fccb

@hontas
Copy link

@hontas hontas commented on 785fccb Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when will this be released? I needs it ;)

@bcoe
Copy link
Member

@bcoe bcoe commented on 785fccb Nov 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hontas please give this a try;

npm i nyc@next - this will give you nyc@11.4.0 which should have these changes. Once we've throughly tested 11.4.0, I'll promote it to latest.

Please sign in to comment.