From c509e648c646e4654e6be4e56081a46c70b8c573 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Mon, 6 May 2019 10:27:36 -0400 Subject: [PATCH] fix(cli): Report error if unwanted positional arguments are received This applies to check-coverage, instrument, merge and report. Passing additional arguments will now cause the help script to be displayed and an error exit code. Unknown flags `nyc report --unknown=1` are still not reported. Reporting unknown flags would require additional work as `yargs.strict()` causes unknown items from configuration to be reported, including flags that are defined for the global command but not a sub-command. Fixes #401 --- lib/commands/check-coverage.js | 1 + lib/commands/instrument.js | 9 +++++++++ lib/commands/merge.js | 1 + lib/commands/report.js | 1 + test/nyc-integration.js | 4 ++-- 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/commands/check-coverage.js b/lib/commands/check-coverage.js index 6b12341b7..62dfb9081 100644 --- a/lib/commands/check-coverage.js +++ b/lib/commands/check-coverage.js @@ -7,6 +7,7 @@ exports.describe = 'check whether coverage is within thresholds provided' exports.builder = function (yargs) { yargs + .demandCommand(0, 0) .option('exclude', { alias: 'x', default: testExclude.defaultExclude, diff --git a/lib/commands/instrument.js b/lib/commands/instrument.js index f744faef6..701dcb6f7 100644 --- a/lib/commands/instrument.js +++ b/lib/commands/instrument.js @@ -9,6 +9,15 @@ exports.describe = 'instruments a file or a directory tree and writes the instru exports.builder = function (yargs) { return yargs + .demandCommand(0, 0) + .positional('input', { + describe: 'file or directory to instrument', + type: 'text' + }) + .positional('output', { + describe: 'directory to output instrumented files', + type: 'text' + }) .option('require', { alias: 'i', default: [], diff --git a/lib/commands/merge.js b/lib/commands/merge.js index 072cfc450..98c6ff2d0 100644 --- a/lib/commands/merge.js +++ b/lib/commands/merge.js @@ -11,6 +11,7 @@ exports.describe = 'merge istanbul format coverage output in a given folder' exports.builder = function (yargs) { return yargs + .demandCommand(0, 0) .positional('input-directory', { describe: 'directory containing multiple istanbul coverage files', type: 'text', diff --git a/lib/commands/report.js b/lib/commands/report.js index 739ff30dc..345983d23 100644 --- a/lib/commands/report.js +++ b/lib/commands/report.js @@ -7,6 +7,7 @@ exports.describe = 'run coverage report for .nyc_output' exports.builder = function (yargs) { return yargs + .demandCommand(0, 0) .option('reporter', { alias: 'r', describe: 'coverage reporter(s) to use', diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 86937e7aa..c4e0d9064 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -45,11 +45,11 @@ t.test('--check-coverage fails when the expected coverage is below a threshold', })) // https://github.com/istanbuljs/nyc/issues/384 -t.test('--check-coverage fails when check-coverage command is used rather than flag', t => { +t.test('check-coverage command is equivalent to the flag', t => { return testSuccess(t, { args: [process.execPath, './half-covered.js'] }).then(() => testFailure(t, { - args: ['check-coverage', '--lines', '51', process.execPath, './half-covered.js'] + args: ['check-coverage', '--lines', '51'] })) })