Skip to content

Commit

Permalink
fix(cli): Report error if unwanted positional arguments are received
Browse files Browse the repository at this point in the history
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
  • Loading branch information
coreyfarrell committed May 9, 2019
1 parent b3dfae8 commit c509e64
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/commands/check-coverage.js
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/commands/instrument.js
Expand Up @@ -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: [],
Expand Down
1 change: 1 addition & 0 deletions lib/commands/merge.js
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lib/commands/report.js
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test/nyc-integration.js
Expand Up @@ -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']
}))
})

Expand Down

0 comments on commit c509e64

Please sign in to comment.