Skip to content

Commit c55870f

Browse files
committed
fix: add serve command and use snapshot testing for shells
- Add missing serve command to demo.cac.ts to correct CLI test snapshots - Implement suggestion to use toMatchSnapshot() for fish shell tests - Update snapshots to include proper completions
1 parent f0cd37b commit c55870f

File tree

2 files changed

+311
-58
lines changed

2 files changed

+311
-58
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`shell completion generators > fish shell completion > should generate a valid fish completion script 1`] = `
4+
"# fish completion for testcli -*- shell-script -*-
5+
6+
# Define shell completion directives
7+
set -l ShellCompDirectiveError 1
8+
set -l ShellCompDirectiveNoSpace 2
9+
set -l ShellCompDirectiveNoFileComp 4
10+
set -l ShellCompDirectiveFilterFileExt 8
11+
set -l ShellCompDirectiveFilterDirs 16
12+
set -l ShellCompDirectiveKeepOrder 32
13+
14+
function __testcli_debug
15+
set -l file "$BASH_COMP_DEBUG_FILE"
16+
if test -n "$file"
17+
echo "$argv" >> $file
18+
end
19+
end
20+
21+
function __testcli_perform_completion
22+
__testcli_debug "Starting __testcli_perform_completion"
23+
24+
# Extract all args except the completion flag
25+
set -l args (string match -v -- "--completion=" (commandline -opc))
26+
27+
# Extract the current token being completed
28+
set -l current_token (commandline -ct)
29+
30+
# Check if current token starts with a dash
31+
set -l flag_prefix ""
32+
if string match -q -- "-*" $current_token
33+
set flag_prefix "--flag="
34+
end
35+
36+
__testcli_debug "Current token: $current_token"
37+
__testcli_debug "All args: $args"
38+
39+
# Call the completion program and get the results
40+
set -l requestComp "/usr/bin/node /path/to/testcli complete -- $args"
41+
__testcli_debug "Calling $requestComp"
42+
set -l results (eval $requestComp 2> /dev/null)
43+
44+
# Some programs may output extra empty lines after the directive.
45+
# Let's ignore them or else it will break completion.
46+
# Ref: https://github.com/spf13/cobra/issues/1279
47+
for line in $results[-1..1]
48+
if test (string sub -s 1 -l 1 -- $line) = ":"
49+
# The directive
50+
set -l directive (string sub -s 2 -- $line)
51+
set -l directive_num (math $directive)
52+
break
53+
end
54+
end
55+
56+
# No directive specified, use default
57+
if not set -q directive_num
58+
set directive_num 0
59+
end
60+
61+
__testcli_debug "Directive: $directive_num"
62+
63+
# Process completions based on directive
64+
if test $directive_num -eq $ShellCompDirectiveError
65+
# Error code. No completion.
66+
__testcli_debug "Received error directive: aborting."
67+
return 1
68+
end
69+
70+
# Filter out the directive (last line)
71+
if test (count $results) -gt 0 -a (string sub -s 1 -l 1 -- $results[-1]) = ":"
72+
set results $results[1..-2]
73+
end
74+
75+
# No completions, let fish handle file completions unless forbidden
76+
if test (count $results) -eq 0
77+
if test $directive_num -ne $ShellCompDirectiveNoFileComp
78+
__testcli_debug "No completions, performing file completion"
79+
return 1
80+
end
81+
__testcli_debug "No completions, but file completion forbidden"
82+
return 0
83+
end
84+
85+
# Filter file extensions
86+
if test $directive_num -eq $ShellCompDirectiveFilterFileExt
87+
__testcli_debug "File extension filtering"
88+
set -l file_extensions
89+
for item in $results
90+
if test -n "$item" -a (string sub -s 1 -l 1 -- $item) != "-"
91+
set -a file_extensions "*$item"
92+
end
93+
end
94+
__testcli_debug "File extensions: $file_extensions"
95+
96+
# Use the file extensions as completions
97+
set -l completions
98+
for ext in $file_extensions
99+
# Get all files matching the extension
100+
set -a completions (string replace -r '^.*/' '' -- $ext)
101+
end
102+
103+
for item in $completions
104+
echo -e "$item "
105+
end
106+
return 0
107+
end
108+
109+
# Filter directories
110+
if test $directive_num -eq $ShellCompDirectiveFilterDirs
111+
__testcli_debug "Directory filtering"
112+
set -l dirs
113+
for item in $results
114+
if test -d "$item"
115+
set -a dirs "$item/"
116+
end
117+
end
118+
119+
for item in $dirs
120+
echo -e "$item "
121+
end
122+
return 0
123+
end
124+
125+
# Process remaining completions
126+
for item in $results
127+
if test -n "$item"
128+
# Check if the item has a description
129+
if string match -q "* *" -- "$item"
130+
set -l completion_parts (string split -- "$item")
131+
set -l comp $completion_parts[1]
132+
set -l desc $completion_parts[2]
133+
134+
# Add the completion and description
135+
echo -e "$comp $desc"
136+
else
137+
# Add just the completion
138+
echo -e "$item "
139+
end
140+
end
141+
end
142+
143+
# If directive contains NoSpace, tell fish not to add a space after completion
144+
if test (math "$directive_num & $ShellCompDirectiveNoSpace") -ne 0
145+
return 2
146+
end
147+
148+
return 0
149+
end
150+
151+
# Set up the completion for the testcli command
152+
complete -c testcli -f -a "(eval __testcli_perform_completion)"
153+
"
154+
`;
155+
156+
exports[`shell completion generators > fish shell completion > should handle special characters in the name 1`] = `
157+
"# fish completion for test-cli:app -*- shell-script -*-
158+
159+
# Define shell completion directives
160+
set -l ShellCompDirectiveError 1
161+
set -l ShellCompDirectiveNoSpace 2
162+
set -l ShellCompDirectiveNoFileComp 4
163+
set -l ShellCompDirectiveFilterFileExt 8
164+
set -l ShellCompDirectiveFilterDirs 16
165+
set -l ShellCompDirectiveKeepOrder 32
166+
167+
function __test_cli_app_debug
168+
set -l file "$BASH_COMP_DEBUG_FILE"
169+
if test -n "$file"
170+
echo "$argv" >> $file
171+
end
172+
end
173+
174+
function __test_cli_app_perform_completion
175+
__test_cli_app_debug "Starting __test_cli_app_perform_completion"
176+
177+
# Extract all args except the completion flag
178+
set -l args (string match -v -- "--completion=" (commandline -opc))
179+
180+
# Extract the current token being completed
181+
set -l current_token (commandline -ct)
182+
183+
# Check if current token starts with a dash
184+
set -l flag_prefix ""
185+
if string match -q -- "-*" $current_token
186+
set flag_prefix "--flag="
187+
end
188+
189+
__test_cli_app_debug "Current token: $current_token"
190+
__test_cli_app_debug "All args: $args"
191+
192+
# Call the completion program and get the results
193+
set -l requestComp "/usr/bin/node /path/to/testcli complete -- $args"
194+
__test_cli_app_debug "Calling $requestComp"
195+
set -l results (eval $requestComp 2> /dev/null)
196+
197+
# Some programs may output extra empty lines after the directive.
198+
# Let's ignore them or else it will break completion.
199+
# Ref: https://github.com/spf13/cobra/issues/1279
200+
for line in $results[-1..1]
201+
if test (string sub -s 1 -l 1 -- $line) = ":"
202+
# The directive
203+
set -l directive (string sub -s 2 -- $line)
204+
set -l directive_num (math $directive)
205+
break
206+
end
207+
end
208+
209+
# No directive specified, use default
210+
if not set -q directive_num
211+
set directive_num 0
212+
end
213+
214+
__test_cli_app_debug "Directive: $directive_num"
215+
216+
# Process completions based on directive
217+
if test $directive_num -eq $ShellCompDirectiveError
218+
# Error code. No completion.
219+
__test_cli_app_debug "Received error directive: aborting."
220+
return 1
221+
end
222+
223+
# Filter out the directive (last line)
224+
if test (count $results) -gt 0 -a (string sub -s 1 -l 1 -- $results[-1]) = ":"
225+
set results $results[1..-2]
226+
end
227+
228+
# No completions, let fish handle file completions unless forbidden
229+
if test (count $results) -eq 0
230+
if test $directive_num -ne $ShellCompDirectiveNoFileComp
231+
__test_cli_app_debug "No completions, performing file completion"
232+
return 1
233+
end
234+
__test_cli_app_debug "No completions, but file completion forbidden"
235+
return 0
236+
end
237+
238+
# Filter file extensions
239+
if test $directive_num -eq $ShellCompDirectiveFilterFileExt
240+
__test_cli_app_debug "File extension filtering"
241+
set -l file_extensions
242+
for item in $results
243+
if test -n "$item" -a (string sub -s 1 -l 1 -- $item) != "-"
244+
set -a file_extensions "*$item"
245+
end
246+
end
247+
__test_cli_app_debug "File extensions: $file_extensions"
248+
249+
# Use the file extensions as completions
250+
set -l completions
251+
for ext in $file_extensions
252+
# Get all files matching the extension
253+
set -a completions (string replace -r '^.*/' '' -- $ext)
254+
end
255+
256+
for item in $completions
257+
echo -e "$item "
258+
end
259+
return 0
260+
end
261+
262+
# Filter directories
263+
if test $directive_num -eq $ShellCompDirectiveFilterDirs
264+
__test_cli_app_debug "Directory filtering"
265+
set -l dirs
266+
for item in $results
267+
if test -d "$item"
268+
set -a dirs "$item/"
269+
end
270+
end
271+
272+
for item in $dirs
273+
echo -e "$item "
274+
end
275+
return 0
276+
end
277+
278+
# Process remaining completions
279+
for item in $results
280+
if test -n "$item"
281+
# Check if the item has a description
282+
if string match -q "* *" -- "$item"
283+
set -l completion_parts (string split -- "$item")
284+
set -l comp $completion_parts[1]
285+
set -l desc $completion_parts[2]
286+
287+
# Add the completion and description
288+
echo -e "$comp $desc"
289+
else
290+
# Add just the completion
291+
echo -e "$item "
292+
end
293+
end
294+
end
295+
296+
# If directive contains NoSpace, tell fish not to add a space after completion
297+
if test (math "$directive_num & $ShellCompDirectiveNoSpace") -ne 0
298+
return 2
299+
end
300+
301+
return 0
302+
end
303+
304+
# Set up the completion for the test-cli:app command
305+
complete -c test-cli:app -f -a "(eval __test_cli_app_perform_completion)"
306+
"
307+
`;

tests/shell.test.ts

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,15 @@ describe('shell completion generators', () => {
1515
it('should generate a valid fish completion script', () => {
1616
const script = fish.generate(name, exec);
1717

18-
// Check that the script contains the shell name
19-
expect(script).toContain(`# fish completion for ${name}`);
20-
21-
// Check that the script defines the directives
22-
expect(script).toContain(
23-
`set -l ShellCompDirectiveError ${ShellCompDirective.ShellCompDirectiveError}`
24-
);
25-
expect(script).toContain(
26-
`set -l ShellCompDirectiveNoSpace ${ShellCompDirective.ShellCompDirectiveNoSpace}`
27-
);
28-
expect(script).toContain(
29-
`set -l ShellCompDirectiveNoFileComp ${ShellCompDirective.ShellCompDirectiveNoFileComp}`
30-
);
31-
expect(script).toContain(
32-
`set -l ShellCompDirectiveFilterFileExt ${ShellCompDirective.ShellCompDirectiveFilterFileExt}`
33-
);
34-
expect(script).toContain(
35-
`set -l ShellCompDirectiveFilterDirs ${ShellCompDirective.ShellCompDirectiveFilterDirs}`
36-
);
37-
expect(script).toContain(
38-
`set -l ShellCompDirectiveKeepOrder ${ShellCompDirective.ShellCompDirectiveKeepOrder}`
39-
);
40-
41-
// Check that the script contains the debug function
42-
expect(script).toContain(`function __${name}_debug`);
43-
44-
// Check that the script contains the completion function
45-
expect(script).toContain(`function __${name}_perform_completion`);
46-
47-
// Check that the script contains the completion registration
48-
expect(script).toContain(
49-
`complete -c ${name} -f -a "(eval __${name}_perform_completion)"`
50-
);
51-
52-
// Check that the script uses the provided exec path
53-
expect(script).toContain(
54-
`set -l requestComp "${exec} complete -- $args"`
55-
);
56-
57-
// Check that the script handles directives correctly
58-
expect(script).toContain(
59-
`if test $directive_num -eq $ShellCompDirectiveError`
60-
);
61-
expect(script).toContain(
62-
`if test $directive_num -eq $ShellCompDirectiveFilterFileExt`
63-
);
64-
expect(script).toContain(
65-
`if test $directive_num -eq $ShellCompDirectiveFilterDirs`
66-
);
67-
expect(script).toContain(
68-
`if test (math "$directive_num & $ShellCompDirectiveNoSpace") -ne 0`
69-
);
18+
// Use snapshot testing instead of individual assertions
19+
expect(script).toMatchSnapshot();
7020
});
7121

7222
it('should handle special characters in the name', () => {
7323
const script = fish.generate(specialName, exec);
7424

75-
// Check that the script properly escapes the name
76-
expect(script).toContain(`function __${escapedName}_debug`);
77-
expect(script).toContain(`function __${escapedName}_perform_completion`);
78-
expect(script).toContain(
79-
`complete -c ${specialName} -f -a "(eval __${escapedName}_perform_completion)"`
80-
);
25+
// Use snapshot testing instead of individual assertions
26+
expect(script).toMatchSnapshot();
8127
});
8228
});
8329

0 commit comments

Comments
 (0)