Skip to content
Permalink
Browse files

fix(cli): Report error if unwanted positional arguments are received (#…

…1100)

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 57debc1e8d57e6835045f8fbb1563b5732de1c07
@@ -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,
@@ -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: [],
@@ -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',
@@ -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',
@@ -20,25 +20,6 @@ All files | 33.33 | 0 | 100 | 33.33 |
`

exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stderr 1`] = `
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
`

exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stdout 1`] = `
-----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files | 50 | 50 | 100 | 50 | |
half-covered.js | 50 | 50 | 100 | 50 | 6,7,8 |
-----------------|----------|----------|----------|----------|-------------------|
`

exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stdout 2`] = `
`

exports[`test/nyc-integration.js TAP --check-coverage fails when the expected coverage is below a threshold > stderr 1`] = `
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
@@ -310,6 +291,25 @@ end_of_record
`

exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stderr 1`] = `
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
`

exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stdout 1`] = `
-----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files | 50 | 50 | 100 | 50 | |
half-covered.js | 50 | 50 | 100 | 50 | 6,7,8 |
-----------------|----------|----------|----------|----------|-------------------|
`

exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stdout 2`] = `
`

exports[`test/nyc-integration.js TAP does not interpret args intended for instrumented bin > undefined 1`] = `
[ '--help', '--version' ]
`
@@ -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']
}))
})

0 comments on commit 57debc1

Please sign in to comment.
You can’t perform that action at this time.