From 785fccbc58dbbeaf3c7003c0c670279019fbd505 Mon Sep 17 00:00:00 2001 From: Igor Ribeiro Lima Date: Sun, 26 Nov 2017 22:19:48 -0200 Subject: [PATCH] feat: allow alternate path for .nycrc to be specified (#724) --- .gitignore | 1 + bin/nyc.js | 3 ++- lib/config-util.js | 11 +++++++---- lib/process-args.js | 5 +++++ test/fixtures/cli/nycrc/.nycrc-config.json | 9 +++++++++ test/nyc-bin.js | 22 ++++++++++++++++++++++ test/process-args.js | 21 +++++++++++++++++++++ 7 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/cli/nycrc/.nycrc-config.json diff --git a/.gitignore b/.gitignore index fcc4efbad..3da79de42 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ node_modules test/build/ .self_coverage *.covered.js +*.swp needs-transpile.js package-lock.json diff --git a/bin/nyc.js b/bin/nyc.js index 8918a3503..5581a308c 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -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. diff --git a/lib/config-util.js b/lib/config-util.js index 2c55a3a11..41c9fbd03 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -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) { @@ -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]') @@ -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', @@ -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) } diff --git a/lib/process-args.js b/lib/process-args.js index df6bcaac1..5cbb2b8e4 100644 --- a/lib/process-args.js +++ b/lib/process-args.js @@ -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 () { diff --git a/test/fixtures/cli/nycrc/.nycrc-config.json b/test/fixtures/cli/nycrc/.nycrc-config.json new file mode 100644 index 000000000..85b404dca --- /dev/null +++ b/test/fixtures/cli/nycrc/.nycrc-config.json @@ -0,0 +1,9 @@ +{ + "check-coverage": true, + "per-file": true, + "lines": 100, + "statements": 100, + "functions": 100, + "branches": 100, + "exclude": [] +} diff --git a/test/nyc-bin.js b/test/nyc-bin.js index c65739b26..341930773 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -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'] diff --git a/test/process-args.js b/test/process-args.js index bab9806e8..6e5da7843 100644 --- a/test/process-args.js +++ b/test/process-args.js @@ -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) + }) + }) })