Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: npm ls <pkg> with depth cli config #1862

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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