Skip to content

Commit 21fb2c8

Browse files
authored
fix: Exit with code 1 when nyc doesn't know what to do. (#1070)
When nyc is not told to do anything this is an error. We already printed the help message to stderr, now we exit with code 1 to indicate that nyc exited without doing anything. Add a test to cover this edge case and verify that both `nyc` and `nyc --help` produce output as expected to stdout and stderr, exit with the proper code.
1 parent 0f745ca commit 21fb2c8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

bin/nyc.js

+1
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ if ([
8585
})
8686
} else {
8787
// I don't have a clue what you're doing.
88+
process.exitCode = 1
8889
yargs.showHelp()
8990
}

test/nyc-integration.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const glob = require('glob')
1212
const isWindows = require('is-windows')()
1313
const rimraf = require('rimraf')
1414
const makeDir = require('make-dir')
15-
const spawn = require('child_process').spawn
15+
const { spawn, spawnSync } = require('child_process')
1616
const si = require('strip-indent')
1717

1818
require('chai').should()
@@ -1079,6 +1079,23 @@ describe('the nyc cli', function () {
10791079
})
10801080
})
10811081

1082+
it('help shows to stderr when main command doesn\'t know what to do', () => {
1083+
const opts = {
1084+
cwd: fixturesCLI,
1085+
env,
1086+
encoding: 'utf8'
1087+
}
1088+
1089+
const help = spawnSync(process.execPath, [bin, '--help'], opts)
1090+
const unknown = spawnSync(process.execPath, [bin], opts)
1091+
help.status.should.equal(0)
1092+
unknown.status.should.equal(1)
1093+
help.stderr.should.equal('')
1094+
unknown.stdout.should.equal('')
1095+
help.stdout.should.not.equal('')
1096+
help.stdout.should.equal(unknown.stderr)
1097+
})
1098+
10821099
describe('args', function () {
10831100
it('does not interpret args intended for instrumented bin', function (done) {
10841101
var args = [bin, '--silent', process.execPath, 'args.js', '--help', '--version']

0 commit comments

Comments
 (0)