From a8aa38513ad5c4ad44e6bb3e1499bfc40c31e213 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 Nov 2020 17:26:14 -0800 Subject: [PATCH] explain-dep: handle shortening edge from root project Fixes: #2134 Credit: @isaacs Close: #2156 Reviewed-by: @ruyadorno --- lib/utils/explain-dep.js | 2 +- .../test-lib-utils-explain-dep.js-TAP.test.js | 7 +- test/lib/utils/explain-dep.js | 89 ++++++++++--------- 3 files changed, 53 insertions(+), 45 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 e0305348653af..9a205e3c39ce2 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,39 @@ 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', + name: 'manydep', + spec:'>1.0.0-beta <1.0.1', + from: { + location: '/path/to/project', + }, }, { type: 'prod', @@ -148,9 +156,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 +168,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 +180,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