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..6dc0f75e44616 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, @@ -208,7 +208,7 @@ const types = { cidr: [null, String, Array], color: ['always', Boolean], call: String, - depth: Number, + depth: [null, Number], description: Boolean, dev: Boolean, 'dry-run': Boolean, 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..90030fef7fda3 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, @@ -326,7 +326,10 @@ Object { "{Boolean TYPE}", ], "commit-hooks": "{Boolean TYPE}", - "depth": "{Number TYPE}", + "depth": Array [ + null, + "{Number TYPE}", + ], "description": "{Boolean TYPE}", "dev": "{Boolean TYPE}", "dry-run": "{Boolean TYPE}", @@ -541,7 +544,7 @@ Object { "cidr": null, "color": true, "commit-hooks": true, - "depth": 0, + "depth": null, "description": true, "dev": false, "dry-run": false, @@ -835,7 +838,10 @@ Object { "{Boolean TYPE}", ], "commit-hooks": "{Boolean TYPE}", - "depth": "{Number TYPE}", + "depth": Array [ + null, + "{Number TYPE}", + ], "description": "{Boolean TYPE}", "dev": "{Boolean TYPE}", "dry-run": "{Boolean TYPE}", @@ -1050,7 +1056,7 @@ Object { "cidr": null, "color": true, "commit-hooks": true, - "depth": 0, + "depth": null, "description": true, "dev": false, "dry-run": false, @@ -1344,7 +1350,10 @@ Object { "{Boolean TYPE}", ], "commit-hooks": "{Boolean TYPE}", - "depth": "{Number TYPE}", + "depth": Array [ + null, + "{Number TYPE}", + ], "description": "{Boolean TYPE}", "dev": "{Boolean TYPE}", "dry-run": "{Boolean TYPE}", 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() })