From 4f7d691606b59ac9a63d82affeb1af3bda8cadd6 Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Fri, 25 Sep 2020 11:19:38 -0400 Subject: [PATCH] fix: npm ls with depth cli config Using the cli option --depth is currently not resulting in the expected behavior of filtering depth level when running npm ls - that's due the special behavior of printing all results when using a filter arg. This commit fixes it by adding support to limiting depth if a config value is set by the user. Fixes #1861 --- lib/ls.js | 7 +- lib/utils/config.js | 2 +- tap-snapshots/test-lib-ls.js-TAP.test.js | 20 ++++++ .../test-lib-utils-config.js-TAP.test.js | 6 +- test/lib/ls.js | 71 +++++++++++++++++++ 5 files changed, 101 insertions(+), 5 deletions(-) diff --git a/lib/ls.js b/lib/ls.js index 09e70c9be8ef4..5ea6e94badb75 100644 --- a/lib/ls.js +++ b/lib/ls.js @@ -406,7 +406,12 @@ const ls = async (args) => { const seenItems = new Set() const seenNodes = new Map() const problems = new Set() - const depthToPrint = (all || args.length) ? Infinity : (depth || 0) + + // defines special handling of printed depth when filtering with args + const filterDefaultDepth = depth === null ? Infinity : depth + const depthToPrint = (all || args.length) + ? filterDefaultDepth + : (depth || 0) // add root node of tree to list of seenNodes seenNodes.set(tree.path, tree) diff --git a/lib/utils/config.js b/lib/utils/config.js index d273ae8a5c459..5a49d2583dc9d 100644 --- a/lib/utils/config.js +++ b/lib/utils/config.js @@ -73,7 +73,7 @@ const defaults = { color: process.env.NO_COLOR == null, call: '', - depth: 0, + depth: null, description: true, dev: false, 'dry-run': false, diff --git a/tap-snapshots/test-lib-ls.js-TAP.test.js b/tap-snapshots/test-lib-ls.js-TAP.test.js index b5659a62e2868..6dc4d9a7137dc 100644 --- a/tap-snapshots/test-lib-ls.js-TAP.test.js +++ b/tap-snapshots/test-lib-ls.js-TAP.test.js @@ -373,6 +373,26 @@ test-npm-ls@1.0.0 {CWD}/ls-ls-extraneous-deps ` +exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should list a in top-level only 1`] = ` +test-pkg-arg-filter-with-depth-opt@1.0.0 {CWD}/ls-ls-filter-pkg-arg-using-depth-option +\`-- a@1.0.0 + +` + +exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print empty results msg 1`] = ` +test-pkg-arg-filter-with-depth-opt@1.0.0 {CWD}/ls-ls-filter-pkg-arg-using-depth-option +\`-- (empty) + +` + +exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print expected result 1`] = ` +test-pkg-arg-filter-with-depth-opt@1.0.0 {CWD}/ls-ls-filter-pkg-arg-using-depth-option +\`-- b@1.0.0 + \`-- c@1.0.0 + \`-- d@1.0.0 + +` + exports[`test/lib/ls.js TAP ls filtering by child of missing dep > should print tree and not duplicate child of missing items 1`] = ` filter-by-child-of-missing-dep@1.0.0 {CWD}/ls-ls-filtering-by-child-of-missing-dep +-- b@1.0.0 extraneous diff --git a/tap-snapshots/test-lib-utils-config.js-TAP.test.js b/tap-snapshots/test-lib-utils-config.js-TAP.test.js index 771837959f20b..3056a0fa92a80 100644 --- a/tap-snapshots/test-lib-utils-config.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-config.js-TAP.test.js @@ -32,7 +32,7 @@ Object { "cidr": null, "color": true, "commit-hooks": true, - "depth": 0, + "depth": null, "description": true, "dev": false, "dry-run": false, @@ -541,7 +541,7 @@ Object { "cidr": null, "color": true, "commit-hooks": true, - "depth": 0, + "depth": null, "description": true, "dev": false, "dry-run": false, @@ -1050,7 +1050,7 @@ Object { "cidr": null, "color": true, "commit-hooks": true, - "depth": 0, + "depth": null, "description": true, "dev": false, "dry-run": false, diff --git a/test/lib/ls.js b/test/lib/ls.js index cdf5cc9f6ed29..f968f406fe7be 100644 --- a/test/lib/ls.js +++ b/test/lib/ls.js @@ -1426,6 +1426,77 @@ t.test('ls', (t) => { }) }) + t.test('filter pkg arg using depth option', (t) => { + _flatOptions.depth = 0 + prefix = t.testdir({ + 'package.json': JSON.stringify({ + name: 'test-pkg-arg-filter-with-depth-opt', + version: '1.0.0', + dependencies: { + a: '^1.0.0', + b: '^1.0.0' + } + }), + node_modules: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0' + }) + }, + b: { + 'package.json': JSON.stringify({ + name: 'b', + version: '1.0.0', + dependencies: { + c: '^1.0.0' + } + }) + }, + c: { + 'package.json': JSON.stringify({ + name: 'c', + version: '1.0.0', + dependencies: { + d: '^1.0.0' + } + }) + }, + d: { + 'package.json': JSON.stringify({ + name: 'd', + version: '1.0.0', + dependencies: { + a: '^1.0.0' + } + }) + } + } + }) + + t.plan(6) + ls(['a'], (err) => { + t.ifError(err, 'should NOT have ELSPROBLEMS error code') + t.matchSnapshot(redactCwd(result), 'should list a in top-level only') + + ls(['d'], (err) => { + t.ifError(err, 'should NOT have ELSPROBLEMS error code when filter') + t.matchSnapshot(redactCwd(result), 'should print empty results msg') + + // if no --depth config is defined, should print path to dep + _flatOptions.depth = null // default config value + ls(['d'], (err) => { + t.ifError(err, 'should NOT have ELSPROBLEMS error code when filter') + t.matchSnapshot(redactCwd(result), 'should print expected result') + }) + }) + }) + }) + + t.teardown(() => { + _flatOptions.depth = Infinity + }) + t.end() })