Files
pnpm/completion/plugin-commands-completion/src/complete.test.ts
T
Khải 004addf63e feat(completion): print completion code to stdout (#7597)
* feat(completion): generate-completion

close #3083

* feat: better error message

* test: generate-completion

* feat(completion): add powershell

* chore(deps): update @pnpm/tabtab to 0.3.0

* switch to provided type declarations
* fix typings
* update tests
* update bundle scripts

* refactor: remove unnecessary `??`

* refactor: replace a type def with provided types

* chore(deps): update @pnpm/tabtab to 0.4.0

* feat(cli): rename completion command

* chore(deps): update @pnpm/tabtab to 0.4.1

* refactor: use tabtab's new features

* fix: pass shell

* chore(deps): update @pnpm/tabtab to 0.5.0

* chore(deps): update @pnpm/tabtab to 0.5.1

* fix: remove unused import

* refactor: move completion to plugins

* feat: remove `{install,uninstall}-completion`

Just `pnpm completion` is enough

* test: fix

* refactor: direct import

* refactor: move tests to next to the lib

* refactor: merge 2 packages into 1

* fix: update changeset and remove install-completion
2024-02-06 23:18:17 +01:00

212 lines
4.3 KiB
TypeScript

import { complete } from './complete'
test('complete an option value', async () => {
const completions = await complete(
{
cliOptionsTypesByCommandName: {
install: () => ({
'resolution-strategy': ['fast', 'fewer-dependencies'],
}),
},
completionByCommandName: {},
initialCompletion: () => [],
shorthandsByCommandName: {},
universalOptionsTypes: {},
universalShorthands: {},
},
{
cmd: 'install',
currentTypedWordType: null,
lastOption: '--resolution-strategy',
options: {},
params: [],
}
)
expect(completions).toStrictEqual([
{ name: 'fast' },
{ name: 'fewer-dependencies' },
])
})
test('complete a command', async () => {
const ctx = {
cliOptionsTypesByCommandName: {
run: () => ({
'if-present': Boolean,
}),
},
completionByCommandName: {
run: async () => [{ name: 'test' }],
},
initialCompletion: () => [],
shorthandsByCommandName: {},
universalOptionsTypes: {
filter: String,
},
universalShorthands: {},
}
expect(
await complete(ctx,
{
cmd: 'run',
currentTypedWordType: 'value',
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual(
[{ name: 'test' }]
)
expect(
await complete(ctx,
{
cmd: 'run',
currentTypedWordType: null,
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual(
[
{ name: 'test' },
{ name: '--filter' },
{ name: '--if-present' },
{ name: '--no-if-present' },
]
)
expect(
await complete(ctx,
{
cmd: 'run',
currentTypedWordType: 'option',
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual(
[
{ name: '--filter' },
{ name: '--if-present' },
{ name: '--no-if-present' },
]
)
})
test('if command completion fails, return empty array', async () => {
expect(
await complete(
{
cliOptionsTypesByCommandName: {},
completionByCommandName: {
run: async () => {
throw new Error('error')
},
},
initialCompletion: () => [],
shorthandsByCommandName: {},
universalOptionsTypes: {
filter: String,
},
universalShorthands: {},
},
{
cmd: 'run',
currentTypedWordType: 'value',
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual(
[]
)
})
test('initial completion', async () => {
const ctx = {
cliOptionsTypesByCommandName: {},
completionByCommandName: {},
initialCompletion: () => [
{ name: 'add' },
{ name: 'install' },
],
shorthandsByCommandName: {},
universalOptionsTypes: {
filter: String,
},
universalShorthands: {},
}
expect(
await complete(ctx,
{
cmd: null,
currentTypedWordType: null,
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual([
{ name: 'add' },
{ name: 'install' },
{ name: '--filter' },
{ name: '--version' },
])
expect(
await complete(ctx,
{
cmd: 'ad',
currentTypedWordType: 'value',
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual([
{ name: 'add' },
{ name: 'install' },
])
expect(
await complete(ctx,
{
cmd: null,
currentTypedWordType: 'option',
lastOption: null,
options: {},
params: [],
}
)
).toStrictEqual([
{ name: '--filter' },
{ name: '--version' },
])
})
test('suggest no completions for after --version', async () => {
expect(
await complete(
{
cliOptionsTypesByCommandName: {},
completionByCommandName: {},
initialCompletion: () => [
{ name: 'add' },
{ name: 'install' },
],
shorthandsByCommandName: {},
universalOptionsTypes: {},
universalShorthands: {},
},
{
cmd: null,
currentTypedWordType: null,
lastOption: null,
options: { version: true },
params: [],
}
)
).toStrictEqual([])
})