-
Notifications
You must be signed in to change notification settings - Fork 7
feat(completion): add shell autocompletion #141
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
Open
nicknisi
wants to merge
5
commits into
main
Choose a base branch
from
feat/shell-completion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0a7daf
feat(completion): add shell autocompletion for bash, zsh, fish, and p…
nicknisi d945348
docs: add shell completion instructions to README
nicknisi af85f34
chore: formatting
nicknisi 3dd7a85
refactor(completion): address review findings
nicknisi 291817a
test: add registry parity check and additional completion edge cases
nicknisi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { generateCompletions, generateShellScript, SUPPORTED_SHELLS } from './completion.js'; | ||
|
|
||
| describe('generateCompletions', () => { | ||
| it('returns top-level commands for empty input', () => { | ||
| const result = generateCompletions(['']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('auth'); | ||
| expect(names).toContain('env'); | ||
| expect(names).toContain('organization'); | ||
| expect(names).toContain('completion'); | ||
| expect(names).toContain('doctor'); | ||
| }); | ||
|
|
||
| it('filters commands by partial prefix', () => { | ||
| const result = generateCompletions(['or']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('organization'); | ||
| expect(names).toContain('org-domain'); | ||
| expect(names).not.toContain('auth'); | ||
| }); | ||
|
|
||
| it('returns subcommands when parent is given', () => { | ||
| const result = generateCompletions(['env', '']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('add'); | ||
| expect(names).toContain('remove'); | ||
| expect(names).toContain('switch'); | ||
| expect(names).toContain('list'); | ||
| expect(names).toContain('claim'); | ||
| }); | ||
|
|
||
| it('returns options when partial starts with -', () => { | ||
| const result = generateCompletions(['doctor', '--']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('--verbose'); | ||
| expect(names).toContain('--fix'); | ||
| expect(names).toContain('--json'); | ||
| }); | ||
|
|
||
| it('excludes used options', () => { | ||
| const result = generateCompletions(['doctor', '--verbose', '--']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).not.toContain('--verbose'); | ||
| expect(names).toContain('--fix'); | ||
| }); | ||
|
|
||
| it('sets NO_FILE_COMP directive', () => { | ||
| const result = generateCompletions(['']); | ||
| expect(result.directive).toBe(4); | ||
| }); | ||
|
|
||
| it('normalizes flat compound names into virtual parent (auth)', () => { | ||
| const result = generateCompletions(['auth', '']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('login'); | ||
| expect(names).toContain('logout'); | ||
| expect(names).toContain('status'); | ||
| }); | ||
|
|
||
| it('completes nested subcommands (config redirect)', () => { | ||
| const result = generateCompletions(['config', '']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('redirect'); | ||
| expect(names).toContain('cors'); | ||
| expect(names).toContain('homepage-url'); | ||
| }); | ||
|
|
||
| it('handles two-level deep subcommands (config redirect add)', () => { | ||
| const result = generateCompletions(['config', 'redirect', '']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('add'); | ||
| }); | ||
|
|
||
| it('skips option values for non-boolean options', () => { | ||
| const result = generateCompletions(['doctor', '--install-dir', '/tmp/foo', '--']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('--verbose'); | ||
| expect(names).not.toContain('--install-dir'); | ||
| }); | ||
|
|
||
| it('does not skip next word after boolean options', () => { | ||
| const result = generateCompletions(['doctor', '--verbose', 'unknownword', '--']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).not.toContain('--verbose'); | ||
| }); | ||
|
|
||
| it('returns top-level commands for completely empty args', () => { | ||
| const result = generateCompletions([]); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('auth'); | ||
| expect(names.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('returns options and subcommands when unknown word precedes partial', () => { | ||
| const result = generateCompletions(['env', 'nonexistent', '']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('--json'); | ||
| }); | ||
|
|
||
| it('includes descriptions in completions', () => { | ||
| const result = generateCompletions(['']); | ||
| const doctor = result.completions.find((c) => c.name === 'doctor'); | ||
| expect(doctor).toBeDefined(); | ||
| expect(doctor!.description).toBeTruthy(); | ||
| expect(doctor!.description.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('filters options by partial prefix', () => { | ||
| const result = generateCompletions(['doctor', '--ver']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).toContain('--verbose'); | ||
| expect(names).toContain('--version'); | ||
| expect(names).not.toContain('--fix'); | ||
| }); | ||
|
|
||
| it('does not complete hidden commands absent from registry', () => { | ||
| const result = generateCompletions(['']); | ||
| const names = result.completions.map((c) => c.name); | ||
| expect(names).not.toContain('emulate'); | ||
| expect(names).not.toContain('dashboard'); | ||
| expect(names).not.toContain('debug'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateShellScript', () => { | ||
| for (const shell of SUPPORTED_SHELLS) { | ||
| it(`generates script for ${shell}`, () => { | ||
| const script = generateShellScript(shell, 'workos'); | ||
| expect(script).toContain('workos'); | ||
| expect(script).toContain('--get-yargs-completions'); | ||
| }); | ||
| } | ||
|
|
||
| it('throws for unsupported shell', () => { | ||
| expect(() => generateShellScript('cmd', 'workos')).toThrow('Unsupported shell'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡
completioncommand not excluded from unclaimed-env middlewareThe
completioncommand is missing from the middleware exclusion list atsrc/bin.ts:200. The middleware comment explicitly states that utility commands likeskills,doctor,env, anddebugare excluded because the warning is unnecessary —completionis also a utility command but was not added. Whencompletionruns through the middleware,maybeWarnUnclaimed()may make an API call (adding latency) and emit a stderr warning. While it won't corrupt the stdout script output, runningeval "$(workos completion bash)"could flash a confusing "Unclaimed environment" warning.(Refers to lines 200-202)
Was this helpful? React with 👍 or 👎 to provide feedback.