From 2ff6dbaf97ba74b7a03139b7f1f92c47fbdfe553 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Wed, 17 Apr 2019 09:36:40 -0400 Subject: [PATCH] fix: Exit with code 1 when nyc doesn't know what to do. 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. --- bin/nyc.js | 1 + package.json | 1 + test/nyc-integration.js | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/bin/nyc.js b/bin/nyc.js index cd225844d..7c188e40a 100755 --- a/bin/nyc.js +++ b/bin/nyc.js @@ -85,5 +85,6 @@ if ([ }) } else { // I don't have a clue what you're doing. + process.exitCode = 1 yargs.showHelp() } diff --git a/package.json b/package.json index 372e2c28d..8f842fb21 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "any-path": "^1.3.0", "chai": "^4.2.0", "coveralls": "^3.0.3", + "execa": "^1.0.0", "is-windows": "^1.0.2", "lodash": "^4.17.11", "newline-regex": "^0.2.1", diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 91bd358ca..1f2436665 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -14,6 +14,8 @@ const rimraf = require('rimraf') const makeDir = require('make-dir') const spawn = require('child_process').spawn const si = require('strip-indent') +// BUGBUG: when upgrading execa to 2.x stripEof is renamed to stripFinalNewline. +const execa = require('execa') require('chai').should() require('tap').mochaGlobals() @@ -1079,6 +1081,25 @@ describe('the nyc cli', function () { }) }) + it('help shows to stderr when main command doesn\'t know what to do', () => { + const opts = { + cwd: fixturesCLI, + env, + stripEof: false + } + return Promise.all([ + execa(process.execPath, [bin, '--help'], opts), + execa(process.execPath, [bin], opts).catch(error => error) + ]).then(([help, unknown]) => { + help.code.should.equal(0) + unknown.code.should.equal(1) + help.stderr.should.equal('') + unknown.stdout.should.equal('') + help.stdout.should.not.equal('') + help.stdout.should.equal(unknown.stderr) + }) + }) + describe('args', function () { it('does not interpret args intended for instrumented bin', function (done) { var args = [bin, '--silent', process.execPath, 'args.js', '--help', '--version']