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
  • Loading branch information
ruyadorno committed Sep 25, 2020
1 parent 9bc6966 commit 4f7d691
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 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
2 changes: 1 addition & 1 deletion 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
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
6 changes: 3 additions & 3 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 @@ -541,7 +541,7 @@ Object {
"cidr": null,
"color": true,
"commit-hooks": true,
"depth": 0,
"depth": null,
"description": true,
"dev": false,
"dry-run": false,
Expand Down Expand Up @@ -1050,7 +1050,7 @@ Object {
"cidr": null,
"color": true,
"commit-hooks": true,
"depth": 0,
"depth": null,
"description": true,
"dev": false,
"dry-run": false,
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 4f7d691

Please sign in to comment.