Skip to content

Commit

Permalink
fix: command completion
Browse files Browse the repository at this point in the history
The fake npm object in the tests wasn't returning an async function

Fixes: #4020

PR-URL: #4032
Credit: @wraithgar
Close: #4032
Reviewed-by: @lukekarrys
  • Loading branch information
wraithgar committed Nov 10, 2021
1 parent ac4f9e4 commit 72ca4a4
Show file tree
Hide file tree
Showing 3 changed files with 452 additions and 539 deletions.
15 changes: 5 additions & 10 deletions lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Completion extends BaseCommand {
// at this point, if words[1] is some kind of npm command,
// then complete on it.
// otherwise, do nothing
const impl = this.npm.cmd(cmd)
const impl = await this.npm.cmd(cmd)
if (impl.completion) {
const comps = await impl.completion(opts)
return this.wrap(opts, comps)
Expand All @@ -180,12 +180,10 @@ class Completion extends BaseCommand {
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
// to: 'a', 'b c', or 'd' 'e'
wrap (opts, compls) {
if (!Array.isArray(compls)) {
compls = compls ? [compls] : []
}

compls = compls.map(c =>
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))
// TODO this was dead code, leaving it in case we find some command we
// forgot that requires this. if so *that command should fix its
// completions*
// compls = compls.map(w => !/\s+/.test(w) ? w : '\'' + w + '\'')

if (opts.partialWord) {
compls = compls.filter(c => c.startsWith(opts.partialWord))
Expand Down Expand Up @@ -246,9 +244,6 @@ const dumpScript = async () => {
const unescape = w => w.charAt(0) === '\'' ? w.replace(/^'|'$/g, '')
: w.replace(/\\ /g, ' ')

const escape = w => !/\s+/.test(w) ? w
: '\'' + w + '\''

// the current word has a dash. Return the config names,
// with the same number of dashes as the current word has.
const configCompl = opts => {
Expand Down
239 changes: 239 additions & 0 deletions tap-snapshots/test/lib/commands/completion.js.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/commands/completion.js TAP completion --no- flags > flags 1`] = `
Array [
Array [
String(
--no-version
--no-versions
),
],
]
`

exports[`test/lib/commands/completion.js TAP completion commands with no completion > no results 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion completion cannot complete options that take a value in mid-command > does not try to complete option arguments in the middle of a command 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion completion completion > both shells 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion completion completion no known shells > no responses 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion completion completion wrong word count > no responses 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion completion of invalid command name does nothing > no results 1`] = `
Array []
`

exports[`test/lib/commands/completion.js TAP completion double dashes escape from flag completion > full command list 1`] = `
Array [
Array [
String(
ci
install-ci-test
install
install-test
uninstall
cache
config
set
get
update
outdated
prune
pack
find-dupes
dedupe
hook
rebuild
link
publish
star
stars
unstar
adduser
login
logout
unpublish
owner
access
team
deprecate
shrinkwrap
token
profile
audit
fund
org
help
ls
ll
search
view
init
version
edit
explore
docs
repo
bugs
root
prefix
bin
whoami
diff
dist-tag
ping
pkg
test
stop
start
restart
run-script
set-script
completion
doctor
exec
explain
un
rb
list
ln
create
i
it
cit
up
c
s
se
tst
t
ddp
v
run
clean-install
clean-install-test
x
why
la
verison
ic
innit
in
ins
inst
insta
instal
isnt
isnta
isntal
install-clean
isntall-clean
hlep
dist-tags
upgrade
udpate
login
add-user
author
home
issues
info
show
find
add
unlink
remove
rm
r
rum
sit
urn
ogr
),
],
]
`

exports[`test/lib/commands/completion.js TAP completion filtered subcommands > filtered subcommands 1`] = `
Array [
Array [
"public",
],
]
`

exports[`test/lib/commands/completion.js TAP completion flags > flags 1`] = `
Array [
Array [
String(
--version
--versions
--viewer
--verbose
--v
),
],
]
`

exports[`test/lib/commands/completion.js TAP completion multiple command names > multiple command names 1`] = `
Array [
Array [
String(
adduser
access
audit
add-user
author
add
),
],
]
`

exports[`test/lib/commands/completion.js TAP completion single command name > single command name 1`] = `
Array [
Array [
"config",
],
]
`

exports[`test/lib/commands/completion.js TAP completion subcommand completion > subcommands 1`] = `
Array [
Array [
String(
public
restricted
grant
revoke
ls-packages
ls-collaborators
edit
2fa-required
2fa-not-required
),
],
]
`

exports[`test/lib/commands/completion.js TAP windows without bash > no output 1`] = `
Array []
`
Loading

0 comments on commit 72ca4a4

Please sign in to comment.