Skip to content

Commit 57debc1

Browse files
authored
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
1 parent b3dfae8 commit 57debc1

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

lib/commands/check-coverage.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports.describe = 'check whether coverage is within thresholds provided'
77

88
exports.builder = function (yargs) {
99
yargs
10+
.demandCommand(0, 0)
1011
.option('exclude', {
1112
alias: 'x',
1213
default: testExclude.defaultExclude,

lib/commands/instrument.js

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ exports.describe = 'instruments a file or a directory tree and writes the instru
99

1010
exports.builder = function (yargs) {
1111
return yargs
12+
.demandCommand(0, 0)
13+
.positional('input', {
14+
describe: 'file or directory to instrument',
15+
type: 'text'
16+
})
17+
.positional('output', {
18+
describe: 'directory to output instrumented files',
19+
type: 'text'
20+
})
1221
.option('require', {
1322
alias: 'i',
1423
default: [],

lib/commands/merge.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exports.describe = 'merge istanbul format coverage output in a given folder'
1111

1212
exports.builder = function (yargs) {
1313
return yargs
14+
.demandCommand(0, 0)
1415
.positional('input-directory', {
1516
describe: 'directory containing multiple istanbul coverage files',
1617
type: 'text',

lib/commands/report.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports.describe = 'run coverage report for .nyc_output'
77

88
exports.builder = function (yargs) {
99
return yargs
10+
.demandCommand(0, 0)
1011
.option('reporter', {
1112
alias: 'r',
1213
describe: 'coverage reporter(s) to use',

tap-snapshots/test-nyc-integration.js-TAP.test.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ All files | 33.33 | 0 | 100 | 33.33 |
2020
2121
`
2222

23-
exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stderr 1`] = `
24-
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
25-
26-
`
27-
28-
exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stdout 1`] = `
29-
-----------------|----------|----------|----------|----------|-------------------|
30-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
31-
-----------------|----------|----------|----------|----------|-------------------|
32-
All files | 50 | 50 | 100 | 50 | |
33-
half-covered.js | 50 | 50 | 100 | 50 | 6,7,8 |
34-
-----------------|----------|----------|----------|----------|-------------------|
35-
36-
`
37-
38-
exports[`test/nyc-integration.js TAP --check-coverage fails when check-coverage command is used rather than flag > stdout 2`] = `
39-
40-
`
41-
4223
exports[`test/nyc-integration.js TAP --check-coverage fails when the expected coverage is below a threshold > stderr 1`] = `
4324
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
4425
@@ -310,6 +291,25 @@ end_of_record
310291
311292
`
312293

294+
exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stderr 1`] = `
295+
ERROR: Coverage for lines (50%) does not meet global threshold (51%)
296+
297+
`
298+
299+
exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stdout 1`] = `
300+
-----------------|----------|----------|----------|----------|-------------------|
301+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
302+
-----------------|----------|----------|----------|----------|-------------------|
303+
All files | 50 | 50 | 100 | 50 | |
304+
half-covered.js | 50 | 50 | 100 | 50 | 6,7,8 |
305+
-----------------|----------|----------|----------|----------|-------------------|
306+
307+
`
308+
309+
exports[`test/nyc-integration.js TAP check-coverage command is equivalent to the flag > stdout 2`] = `
310+
311+
`
312+
313313
exports[`test/nyc-integration.js TAP does not interpret args intended for instrumented bin > undefined 1`] = `
314314
[ '--help', '--version' ]
315315
`

test/nyc-integration.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ t.test('--check-coverage fails when the expected coverage is below a threshold',
4545
}))
4646

4747
// https://github.com/istanbuljs/nyc/issues/384
48-
t.test('--check-coverage fails when check-coverage command is used rather than flag', t => {
48+
t.test('check-coverage command is equivalent to the flag', t => {
4949
return testSuccess(t, {
5050
args: [process.execPath, './half-covered.js']
5151
}).then(() => testFailure(t, {
52-
args: ['check-coverage', '--lines', '51', process.execPath, './half-covered.js']
52+
args: ['check-coverage', '--lines', '51']
5353
}))
5454
})
5555

0 commit comments

Comments
 (0)