diff options
author | isaacs <i@izs.me> | 2021-02-20 07:39:12 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-02-22 23:30:12 +0300 |
commit | 4e58274ed0fd2dd29d3c8d6c7c47f37a37dc0f0f (patch) | |
tree | a3e88314ceb246257252307985846a926e839872 /test/lib/utils | |
parent | be9f1525587d70c909da88b996220b374583d42a (diff) |
Do not print error banner for shell proxy commands
There are a few commands (exec, run-script, and the run-script proxies)
where essentially npm is acting like a very fancy shell. It is peculiar
and noisy for npm to print a verbose error banner at the end of these
commands, since presumably the command itself already did whatever it
had to do to report the error appropriately.
For example, `npm test` runs a test script, usually outputting test
results. Having npm then tell me that my tests failed with exit status
1 and print a debug log, is unnecessary and unwanted.
When the error encountered for these commands does not have a non-zero
numeric 'code', then we still print the standard npm error reporting
messages, because presumably something went wrong OTHER than a process
exiting with a non-zero status code.
PR-URL: https://github.com/npm/cli/pull/2742
Credit: @isaacs
Close: #2742
Reviewed-by: @nlf
Diffstat (limited to 'test/lib/utils')
-rw-r--r-- | test/lib/utils/error-handler.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/lib/utils/error-handler.js b/test/lib/utils/error-handler.js index 0b896fee4..b1d3e2ca7 100644 --- a/test/lib/utils/error-handler.js +++ b/test/lib/utils/error-handler.js @@ -43,6 +43,7 @@ const config = { const npm = { version: '1.0.0', config, + shelloutCommands: ['exec', 'run-script'], } const npmlog = { @@ -525,3 +526,64 @@ t.test('use exitCode when emitting exit event', (t) => { process.emit('exit') }) + +t.test('do no fancy handling for shellouts', t => { + const { exit } = process + const { command } = npm + const { log } = npmlog + const LOG_RECORD = [] + t.teardown(() => { + npmlog.log = log + process.exit = exit + npm.command = command + }) + + npmlog.log = function (level, ...args) { + log.call(this, level, ...args) + LOG_RECORD.push(npmlog.record[npmlog.record.length - 1]) + } + + npm.command = 'exec' + + let EXPECT_EXIT = 0 + process.exit = code => { + t.equal(code, EXPECT_EXIT, 'got expected exit code') + EXPECT_EXIT = 0 + } + t.beforeEach((cb) => { + LOG_RECORD.length = 0 + cb() + }) + + const loudNoises = () => LOG_RECORD + .filter(({ level }) => ['warn', 'error'].includes(level)) + + t.test('shellout with a numeric error code', t => { + EXPECT_EXIT = 5 + errorHandler(Object.assign(new Error(), { code: 5 })) + t.equal(EXPECT_EXIT, 0, 'called process.exit') + // should log no warnings or errors, verbose/silly is fine. + t.strictSame(loudNoises(), [], 'no noisy warnings') + t.end() + }) + + t.test('shellout without a numeric error code (something in npm)', t => { + EXPECT_EXIT = 1 + errorHandler(Object.assign(new Error(), { code: 'banana stand' })) + t.equal(EXPECT_EXIT, 0, 'called process.exit') + // should log some warnings and errors, because something weird happened + t.strictNotSame(loudNoises(), [], 'bring the noise') + t.end() + }) + + t.test('shellout with code=0 (extra weird?)', t => { + EXPECT_EXIT = 1 + errorHandler(Object.assign(new Error(), { code: 0 })) + t.equal(EXPECT_EXIT, 0, 'called process.exit') + // should log some warnings and errors, because something weird happened + t.strictNotSame(loudNoises(), [], 'bring the noise') + t.end() + }) + + t.end() +}) |