Skip to content

Commit 785fccb

Browse files
igorlimabcoe
authored andcommitted
feat: allow alternate path for .nycrc to be specified (#724)
1 parent 4ff7a38 commit 785fccb

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ node_modules
44
test/build/
55
.self_coverage
66
*.covered.js
7+
*.swp
78
needs-transpile.js
89
package-lock.json

bin/nyc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ var wrapper = require.resolve('./wrap.js')
1919
// reporting, instrumenting subprocesses, etc.
2020
var yargs = configUtil.addCommandsAndHelp(configUtil.buildYargs())
2121
var instrumenterArgs = processArgs.hideInstrumenteeArgs()
22-
var argv = yargs.parse(instrumenterArgs)
22+
var config = configUtil.loadConfig(processArgs.parseArgs())
23+
var argv = yargs.config(config).parse(instrumenterArgs)
2324

2425
if (argv._[0] === 'report') {
2526
// look in lib/commands/report.js for logic.

lib/config-util.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function guessCWD (cwd) {
1818
return cwd
1919
}
2020

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

2525
if (rcPath) {
@@ -40,7 +40,6 @@ function loadConfig (argv, cwd) {
4040
// that would cause the application to exit early.
4141
Config.buildYargs = function (cwd) {
4242
cwd = guessCWD(cwd)
43-
const config = loadConfig()
4443
return Yargs([])
4544
.usage('$0 [command] [options]')
4645
.usage('$0 [options] [bin-to-instrument]')
@@ -196,6 +195,11 @@ Config.buildYargs = function (cwd) {
196195
type: 'boolean',
197196
global: false
198197
})
198+
.option('nycrc-path', {
199+
default: '.nycrc',
200+
description: 'specify a different .nycrc path',
201+
global: false
202+
})
199203
.option('temp-directory', {
200204
describe: 'directory to output raw coverage information to',
201205
default: './.nyc_output',
@@ -208,7 +212,6 @@ Config.buildYargs = function (cwd) {
208212
.epilog('visit https://git.io/vHysA for list of available reporters')
209213
.boolean('h')
210214
.boolean('version')
211-
.config(config)
212215
.help(false)
213216
.version(false)
214217
}

lib/process-args.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ module.exports = {
1616
}
1717
return argv
1818
},
19+
parseArgs: function () {
20+
var argv = process.argv.slice(2)
21+
var yargv = parser(argv)
22+
return yargv
23+
},
1924
// don't pass arguments for the bin being
2025
// instrumented to nyc.
2126
hideInstrumenteeArgs: function () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"check-coverage": true,
3+
"per-file": true,
4+
"lines": 100,
5+
"statements": 100,
6+
"functions": 100,
7+
"branches": 100,
8+
"exclude": []
9+
}

test/nyc-bin.js

+22
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,28 @@ describe('the nyc cli', function () {
333333
})
334334
})
335335

336+
it('loads configuration from different file rather than .nycrc', function (done) {
337+
var args = [bin, '--nycrc-path', './.nycrc-config.json', process.execPath, './index.js']
338+
339+
var proc = spawn(process.execPath, args, {
340+
cwd: cwd,
341+
env: env
342+
})
343+
344+
var stdout = ''
345+
proc.stdout.on('data', function (chunk) {
346+
stdout += chunk
347+
})
348+
349+
proc.on('close', function (code) {
350+
// should be 1 due to coverage check
351+
code.should.equal(1)
352+
stdout.should.match(/SF:.*index\.js/)
353+
stdout.should.match(/SF:.*ignore\.js/)
354+
done()
355+
})
356+
})
357+
336358
it('allows .nycrc configuration to be overridden with command line args', function (done) {
337359
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']
338360

test/process-args.js

+21
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,25 @@ describe('process-args', function () {
7676
munged.should.eql(['--version'])
7777
})
7878
})
79+
80+
describe('parseArgs', function () {
81+
it('parses arguments such as --nycrc-path --reporter --arg', function () {
82+
process.argv = ['/Users/benjamincoe/bin/iojs',
83+
'/Users/benjamincoe/bin/nyc.js',
84+
'--nycrc-path',
85+
'./.nycrc-config.json',
86+
'--reporter',
87+
'lcov',
88+
'node',
89+
'test/nyc-tap.js',
90+
'--arg',
91+
'--'
92+
]
93+
94+
var munged = processArgs.parseArgs()
95+
munged.nycrcPath.should.eql('./.nycrc-config.json')
96+
munged.reporter.should.eql('lcov')
97+
munged.arg.should.eql(true)
98+
})
99+
})
79100
})

0 commit comments

Comments
 (0)