Skip to content

Commit

Permalink
fix: npm ls <pkg> with depth cli config
Browse files Browse the repository at this point in the history
Using the cli option --depth is currently not resulting in the expected
behavior of filtering depth level when running npm ls <pkg> - 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

PR-URL: #1862
Credit: @ruyadorno
Close: #1862
Reviewed-by: @nlf
  • Loading branch information
ruyadorno authored and nlf committed Sep 29, 2020
1 parent 3cc88d6 commit 2e16ab9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const defaults = {

color: process.env.NO_COLOR == null,
call: '',
depth: 0,
depth: null,
description: true,
dev: false,
'dry-run': false,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tap-snapshots/test-lib-ls.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,26 @@ [email protected] {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`] = `
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
\`-- [email protected]
`

exports[`test/lib/ls.js TAP ls filter pkg arg using depth option > should print empty results msg 1`] = `
[email protected] {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`] = `
[email protected] {CWD}/ls-ls-filter-pkg-arg-using-depth-option
\`-- [email protected]
\`-- [email protected]
\`-- [email protected]
`

exports[`test/lib/ls.js TAP ls filtering by child of missing dep > should print tree and not duplicate child of missing items 1`] = `
[email protected] {CWD}/ls-ls-filtering-by-child-of-missing-dep
+-- [email protected] extraneous
Expand Down
21 changes: 15 additions & 6 deletions tap-snapshots/test-lib-utils-config.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object {
"cidr": null,
"color": true,
"commit-hooks": true,
"depth": 0,
"depth": null,
"description": true,
"dev": false,
"dry-run": false,
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -541,7 +544,7 @@ Object {
"cidr": null,
"color": true,
"commit-hooks": true,
"depth": 0,
"depth": null,
"description": true,
"dev": false,
"dry-run": false,
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -1050,7 +1056,7 @@ Object {
"cidr": null,
"color": true,
"commit-hooks": true,
"depth": 0,
"depth": null,
"description": true,
"dev": false,
"dry-run": false,
Expand Down Expand Up @@ -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}",
Expand Down
71 changes: 71 additions & 0 deletions test/lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down

0 comments on commit 2e16ab9

Please sign in to comment.