From f0700d98a8b3046c7f9d0a35ee8cb02dbcdc0dcb Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 Nov 2020 17:15:54 -0800 Subject: [PATCH 1/3] send json errors to stderr, not stdout Fixes: #2150 --- lib/utils/error-handler.js | 2 +- test/lib/utils/error-handler.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils/error-handler.js b/lib/utils/error-handler.js index dc9e2a26bcfa5..476ca9e917b36 100644 --- a/lib/utils/error-handler.js +++ b/lib/utils/error-handler.js @@ -182,7 +182,7 @@ const errorHandler = (er) => { detail: messageText(msg.detail), }, } - console.log(JSON.stringify(error, null, 2)) + console.error(JSON.stringify(error, null, 2)) } exit(typeof er.errno === 'number' ? er.errno : typeof er.code === 'number' ? er.code : 1) diff --git a/test/lib/utils/error-handler.js b/test/lib/utils/error-handler.js index 840b3a3565031..9e32dfc5ff8db 100644 --- a/test/lib/utils/error-handler.js +++ b/test/lib/utils/error-handler.js @@ -218,8 +218,8 @@ t.test('console.log output using --json', (t) => { config.values.json = true - const _log = console.log - console.log = (jsonOutput) => { + const _error = console.error + console.error = (jsonOutput) => { t.deepEqual( JSON.parse(jsonOutput), { @@ -236,7 +236,7 @@ t.test('console.log output using --json', (t) => { errorHandler(new Error('Error: EBADTHING Something happened')) t.teardown(() => { - console.log = _log + console.error = _error delete config.values.json }) }) From 5ae21e4701c377a39eff91a160b54f375c069853 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 Nov 2020 17:21:59 -0800 Subject: [PATCH 2/3] lint --- test/lib/utils/explain-dep.js | 81 +++++++++++++++++------------------ 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/test/lib/utils/explain-dep.js b/test/lib/utils/explain-dep.js index e0305348653af..ec450345f893f 100644 --- a/test/lib/utils/explain-dep.js +++ b/test/lib/utils/explain-dep.js @@ -2,7 +2,7 @@ const t = require('tap') const requireInject = require('require-inject') const npm = {} const { explainNode, printNode } = requireInject('../../../lib/utils/explain-dep.js', { - '../../../lib/npm.js': npm + '../../../lib/npm.js': npm, }) const cases = { @@ -16,10 +16,10 @@ const cases = { name: 'prod-dep', spec: '1.x', from: { - location: '/path/to/project' - } - } - ] + location: '/path/to/project', + }, + }, + ], }, deepDev: { @@ -51,16 +51,16 @@ const cases = { name: 'topdev', spec: '4.x', from: { - location: '/path/to/project' - } - } - ] - } - } - ] - } - } - ] + location: '/path/to/project', + }, + }, + ], + }, + }, + ], + }, + }, + ], }, optional: { @@ -74,10 +74,10 @@ const cases = { name: 'optdep', spec: '1.0.0', from: { - location: '/path/to/project' - } - } - ] + location: '/path/to/project', + }, + }, + ], }, peer: { @@ -91,10 +91,10 @@ const cases = { name: 'peer', spec: '1.0.0', from: { - location: '/path/to/project' - } - } - ] + location: '/path/to/project', + }, + }, + ], }, extraneous: { @@ -102,8 +102,8 @@ const cases = { version: '1337.420.69-lol', location: 'node_modules/extra-neos', dependents: [], - extraneous: true - } + extraneous: true, + }, } cases.manyDeps = { @@ -114,31 +114,31 @@ cases.manyDeps = { type: 'prod', name: 'manydep', spec: '1.0.0', - from: cases.prodDep + from: cases.prodDep, }, { type: 'optional', name: 'manydep', spec: '1.x', - from: cases.optional + from: cases.optional, }, { type: 'prod', name: 'manydep', spec: '1.0.x', - from: cases.extraneous + from: cases.extraneous, }, { type: 'dev', name: 'manydep', spec: '*', - from: cases.deepDev + from: cases.deepDev, }, { type: 'peer', name: 'manydep', spec: '>1.0.0-beta <1.0.1', - from: cases.peer + from: cases.peer, }, { type: 'prod', @@ -148,9 +148,9 @@ cases.manyDeps = { name: 'a package with a pretty long name', version: '1.2.3', dependents: { - location: '/path/to/project' - } - } + location: '/path/to/project', + }, + }, }, { type: 'prod', @@ -160,9 +160,9 @@ cases.manyDeps = { name: 'another package with a pretty long name', version: '1.2.3', dependents: { - location: '/path/to/project' - } - } + location: '/path/to/project', + }, + }, }, { type: 'prod', @@ -172,14 +172,13 @@ cases.manyDeps = { name: 'yet another a package with a pretty long name', version: '1.2.3', dependents: { - location: '/path/to/project' - } - } + location: '/path/to/project', + }, + }, }, - ] + ], } - for (const [name, expl] of Object.entries(cases)) { t.test(name, t => { npm.color = true From cb3d4895e75c51f9c0881686772a3b57141da264 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 Nov 2020 17:26:14 -0800 Subject: [PATCH 3/3] explain-dep: handle shortening edge from root project Fixes: #2134 --- lib/utils/explain-dep.js | 2 +- tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js | 7 ++++--- test/lib/utils/explain-dep.js | 8 ++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/utils/explain-dep.js b/lib/utils/explain-dep.js index 096df97edfda3..ed69a02c143c0 100644 --- a/lib/utils/explain-dep.js +++ b/lib/utils/explain-dep.js @@ -64,7 +64,7 @@ const explainDependents = ({ name, dependents }, depth, color) => { const maxLen = 50 const showNames = [] for (let i = max; i < dependents.length; i++) { - const { from: { name } } = dependents[i] + const { from: { name = 'the root project' } } = dependents[i] len += name.length if (len >= maxLen && i < dependents.length - 1) { showNames.push('...') diff --git a/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js b/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js index 48f84e5111821..54a77bc122b4d 100644 --- a/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-explain-dep.js-TAP.test.js @@ -10,7 +10,7 @@ manydep@1.0.0 manydep@"1.0.0" from prod-dep@1.2.3 node_modules/prod-dep prod-dep@"1.x" from the root project - 6 more (optdep, extra-neos, deep-dev, peer, ...) + 7 more (optdep, extra-neos, deep-dev, peer, the root project, ...) ` exports[`test/lib/utils/explain-dep.js TAP > ellipses test two 1`] = ` @@ -18,7 +18,7 @@ manydep@1.0.0 manydep@"1.0.0" from prod-dep@1.2.3 node_modules/prod-dep prod-dep@"1.x" from the root project - 5 more (optdep, extra-neos, deep-dev, peer, a package with a pretty long name) + 6 more (optdep, extra-neos, deep-dev, peer, the root project, a package with a pretty long name) ` exports[`test/lib/utils/explain-dep.js TAP deepDev > explain color deep 1`] = ` @@ -90,6 +90,7 @@ exports[`test/lib/utils/explain-dep.js TAP manyDeps > explain color deep 1`] = ` peer manydep@">1.0.0-beta <1.0.1" from peer@1.0.0 peer node_modules/peer peer peer@"1.0.0" from the root project + manydep@">1.0.0-beta <1.0.1" from the root project manydep@"1" from a package with a pretty long name@1.2.3 manydep@"1" from another package with a pretty long name@1.2.3 manydep@"1" from yet another a package with a pretty long name@1.2.3 @@ -100,7 +101,7 @@ manydep@1.0.0 manydep@"1.0.0" from prod-dep@1.2.3 node_modules/prod-dep prod-dep@"1.x" from the root project - 7 more (optdep, extra-neos, deep-dev, peer, ...) + 8 more (optdep, extra-neos, deep-dev, peer, the root project, ...) ` exports[`test/lib/utils/explain-dep.js TAP manyDeps > print color 1`] = ` diff --git a/test/lib/utils/explain-dep.js b/test/lib/utils/explain-dep.js index ec450345f893f..9a205e3c39ce2 100644 --- a/test/lib/utils/explain-dep.js +++ b/test/lib/utils/explain-dep.js @@ -140,6 +140,14 @@ cases.manyDeps = { spec: '>1.0.0-beta <1.0.1', from: cases.peer, }, + { + type: 'prod', + name: 'manydep', + spec:'>1.0.0-beta <1.0.1', + from: { + location: '/path/to/project', + }, + }, { type: 'prod', name: 'manydep',