Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new argument to specify a different path for nycrc #724

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})