Debian Codegraph @

testsuite/completion.00-init/031-zsh.exp

raw
1 ##############################################################################
2 # Modules Revision 3.0
3 # Providing a flexible user environment
4 #
5 # File: completion.00-init/%M%
6 # Revision: %I%
7 # First Edition: 2026/08/10
8 # Last Mod.: %U%, %G%
9 #
10 # Authors: Xavier Delaruelle, [email protected]
11 #
12 # Description: Testuite testsequence
13 # Command: avail, load, unload, use, unuse, restore
14 # Sub-Command:
15 #
16 # Comment: %C{
17 # Check zsh Tab completion: mirrors 021-bash.exp (see
18 # that file for the full rationale of each check), with
19 # every completion_bash_* call replaced by its
20 # completion_zsh_* counterpart -- and two kinds of
21 # deviation from bash's expectations, both driven by
22 # genuine differences in the two shells' completion
23 # systems (readline vs zle), not by test bugs:
24 # - zsh does not ring the bell when a Tab silently
25 # extends the typed word to a longer, still-ambiguous
26 # common prefix (only a Tab that cannot insert
27 # anything at all rings the bell), so every such case
28 # drops the leading "\a" bash expects
29 # - init/zsh-functions/_module.in has no 'ml' support
30 # at all (only '#compdef module'), so every section
31 # mirroring 'ml' behavior in 021-bash.exp is dropped;
32 # and its avail/load completion offers modulefile
33 # candidates and option flags as two mutually
34 # exclusive sets gated on whether the word being
35 # completed starts with '-', same as bash's own
36 # _module_avail() internally does for the modules
37 # half -- but unlike bash, which always mixes its
38 # @comp_*_opts@ flags into the candidate set
39 # regardless of prefix, so a bare trailing-space
40 # listing here only ever shows one set at a time
41 #
42 # Also checks, like 021-bash.exp, that a candidate word
43 # built from untrusted text (a module name,
44 # LOADEDMODULES, MODULEPATH) cannot reach a shell
45 # expansion step: init/zsh-functions/_module.in only ever
46 # hands such text to 'compadd -a <array>', where each
47 # array element is used as a literal candidate string, so
48 # this is expected to already be safe -- these cases guard
49 # against a regression, not a known vulnerability
50 # }C%
51 #
52 ##############################################################################
53
54 # _module.in declares its mandatory-argument options zsh '_arguments' style
55 # ('--tag=[...]', '--color=[...]', ..., meaning "takes a value attached
56 # with an '='"), and zsh's own listing shows the bare option name it will
57 # insert rather than that spec syntax -- so it offers eg '--tag', not the
58 # module-help-documented '--tag=' the ground truth in $help_subcmd_opts /
59 # $help_switches otherwise matches verbatim
60 proc completion_zsh_expected_opts {opts} {
61 set out {}
62 foreach tok $opts {
63 if {[string index $tok end] eq {=}} {
64 set tok [string range $tok 0 end-1]
65 }
66 lappend out $tok
67 }
68 return $out
69 }
70
71
72 #
73 # module names and option flags each get listed on Tab completion, but
74 # (unlike bash) not mixed together in the same listing: which set shows up
75 # depends on whether the word being completed starts with '-'
76 #
77
78 setenv_path_var MODULEPATH $modpath
79
80 completion_zsh_start
81
82 set got [completion_zsh_list {module avail }]
83 completion_assert_contains $got {advvers/ bar/ baz/ foo/}
84
85 set got [completion_zsh_list {module avail -}]
86 completion_assert_contains $got {--all --icase --terse}
87
88 set got [completion_zsh_list {module load }]
89 completion_assert_contains $got {advvers/ bar/ baz/ foo/}
90
91 set got [completion_zsh_list {module load -}]
92 completion_assert_contains $got {--force -f --icase}
93
94 ## "ba" is the common prefix of bar/ and baz/: a single Tab from "b" must
95 ## auto-insert the "a" inline -- with no bell, unlike bash, since zle only
96 ## beeps when nothing at all can be inserted (see this file's header)
97 set got [completion_zsh_inline {module load b}]
98 completion_assert_eq $got {a}
99
100 ## typed prefix is "ba", the full common prefix of bar/ and baz/: zle
101 ## auto-inserts any *further* common prefix on the first Tab, which would
102 ## eat one of the two Tabs the double-Tab below relies on to list matches
103 ## instead of just ringing the bell
104 set got [completion_zsh_list {module load ba}]
105 completion_assert_contains $got {bar/ baz/}
106 completion_assert_not_contains $got {foo/ advvers/}
107
108 ## "bar" uniquely matches the "bar/" module category: a single Tab must
109 ## complete it inline with a trailing '/' and no trailing space (so typing
110 ## can continue straight into a version), per the '_files -/'-alike nospace
111 ## logic in _module_avail_mods
112 set got [completion_zsh_inline {module load bar}]
113 completion_assert_eq $got {/}
114
115 ## from "bar/", double-Tab must list the versions available under it
116 set got [completion_zsh_list {module load bar/}]
117 completion_assert_contains $got {bar/1.0 bar/2.0}
118
119 ## "flatlcompat" is a single, versionless module (no further completion
120 ## possible beneath it, so unlike "bar" above it does not end in '/'): a
121 ## single Tab must complete it inline, with a trailing space since it is
122 ## not a directory-style entry
123 set got [completion_zsh_inline {module load fl}]
124 completion_assert_eq $got {atlcompat }
125
126 completion_zsh_close
127
128
129 #
130 # 'unload' lists the currently loaded modules alongside the option flags
131 # (mutually exclusive by leading '-', same as avail/load above), and
132 # lists/completes them on the typed prefix like any other candidate list
133 #
134
135 setenv_loaded_module [list bar/1.0 baz/1.0 foo/1.0]\
136 [list $modpath/bar/1.0 $modpath/baz/1.0 $modpath/foo/1.0]
137
138 completion_zsh_start
139
140 set got [completion_zsh_list {module unload }]
141 completion_assert_contains $got {bar/1.0 baz/1.0 foo/1.0}
142
143 set got [completion_zsh_list {module unload -}]
144 completion_assert_contains $got {--force -f --icase}
145
146 ## "ba" is the common prefix of bar/1.0 and baz/1.0: a single Tab from "b"
147 ## must auto-insert the "a" inline, with no bell (see this file's header)
148 set got [completion_zsh_inline {module unload b}]
149 completion_assert_eq $got {a}
150
151 ## from "ba", nothing more can be auto-inserted (bar/1.0 and baz/1.0
152 ## diverge right there), so a double-Tab must list just the two matching
153 ## loaded modules, not foo/1.0
154 set got [completion_zsh_list {module unload ba}]
155 completion_assert_contains $got {bar/1.0 baz/1.0}
156 completion_assert_not_contains $got {foo/1.0}
157
158 ## "foo/1.0" is the only loaded module starting with "f": a single Tab must
159 ## complete the rest of it inline, with a trailing space since a
160 ## loaded-module candidate is not a directory-style entry
161 set got [completion_zsh_inline {module unload f}]
162 completion_assert_eq $got {oo/1.0 }
163
164 completion_zsh_close
165 unsetenv_loaded_module
166
167
168 #
169 # 'use' completes a filesystem directory path, through _module.in's own
170 # '_files -/' action rather than a fallback to some outer default completer
171 # (there is no zsh equivalent of bash's 'complete -o default' registration
172 # -- the completion script itself must handle every sub-command, 'use'
173 # included)
174 #
175
176 set usedir "$env(TESTSUITEDIR)/completion-use-fixture"
177 file delete -force $usedir
178 file mkdir $usedir
179
180 completion_zsh_start
181
182 ## drop the last 3 characters of the fixture dir name ("ure"): a single Tab
183 ## must complete them back plus a trailing '/', with no trailing space,
184 ## exactly like the "bar" -> "bar/" case above
185 set got [completion_zsh_inline "module use [string range $usedir 0 end-3]"]
186 completion_assert_eq $got {ure/}
187
188 completion_zsh_close
189 file delete -force $usedir
190
191
192 #
193 # 'unuse' lists the currently enabled modulepaths, and filters them on the
194 # typed prefix like any other candidate list
195 #
196
197 setenv_path_var MODULEPATH $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp
198
199 completion_zsh_start
200
201 ## all three modulepaths are absolute, so they all share a leading '/':
202 ## type it explicitly, otherwise zle auto-inserts that one-character common
203 ## prefix on the first Tab and the double-Tab below would only ring the
204 ## bell instead of listing (same trap as the "bar"/"ba" cases above)
205 set got [completion_zsh_list {module unuse /}]
206 completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp]
207
208 ## $modpath and modulefiles.4 share the ".../modulefiles." prefix, diverging
209 ## only on the trailing version digit: typing that full common prefix (so
210 ## the first Tab has nothing left to auto-insert) and double-Tab must list
211 ## only the two matching modulepaths, not the unrelated /tmp entry
212 set got [completion_zsh_list "module unuse $env(TESTSUITEDIR)/modulefiles."]
213 completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4"]
214 completion_assert_not_contains $got {/tmp}
215
216 completion_zsh_close
217
218
219 #
220 # with only one modulepath left matching a typed prefix, a single Tab must
221 # complete the whole rest of it inline -- unlike the module-name case, a
222 # modulepath candidate is not a directory-style entry, so (still unlike
223 # "bar" -> "bar/" above) a trailing space is expected after it
224 #
225
226 setenv_path_var MODULEPATH "$env(TESTSUITEDIR)/modulefiles.4" /tmp
227
228 completion_zsh_start
229
230 set got [completion_zsh_inline "module unuse $env(TESTSUITEDIR)/module"]
231 completion_assert_eq $got {files.4 }
232
233 completion_zsh_close
234
235
236 #
237 # 'restore' lists the saved collections, and filters them on the typed
238 # prefix like any other candidate list
239 #
240
241 setenv_path_var MODULEPATH $modpath
242
243 completion_zsh_start
244
245 completion_zsh_raw {module save foo}
246 completion_zsh_raw {module save fox}
247 completion_zsh_raw {module save bar1}
248
249 set got [completion_zsh_list {module restore }]
250 completion_assert_contains $got {foo fox bar1}
251
252 ## "fo" is the common prefix of foo/fox: a single Tab from "f" must
253 ## auto-insert the "o" inline, with no bell (see this file's header)
254 set got [completion_zsh_inline {module restore f}]
255 completion_assert_eq $got {o}
256
257 ## from "fo", nothing more can be auto-inserted (foo/fox diverge right
258 ## there), so a double-Tab must list just the two matching collections
259 set got [completion_zsh_list {module restore fo}]
260 completion_assert_contains $got {foo fox}
261 completion_assert_not_contains $got {bar1}
262
263 ## "bar1" is the only collection starting with "ba": a single Tab must
264 ## complete the rest of it inline, with a trailing space since a
265 ## collection candidate is not a directory-style entry
266 set got [completion_zsh_inline {module restore ba}]
267 completion_assert_eq $got {r1 }
268
269 completion_zsh_close
270
271
272 #
273 # ground-truth check against 'module help' (see completion.00-init/
274 # 007-module_help.exp): a bare 'module <Tab><Tab>' must propose exactly the
275 # documented sub-commands, and 'module -<Tab><Tab>' must propose exactly the
276 # documented switches
277 #
278
279 setenv_path_var MODULEPATH $modpath
280
281 completion_zsh_start
282
283 set got [completion_zsh_list {module }]
284 set got_subcmds [lsearch -all -inline -not -regexp $got {^-}]
285 completion_assert_contains $got_subcmds $help_subcmds
286 completion_assert_only_known $got_subcmds {*} $help_subcmds
287
288 set got [completion_zsh_list {module -}]
289 set zsh_help_switches [completion_zsh_expected_opts $help_switches]
290 completion_assert_contains $got $zsh_help_switches
291 completion_assert_only_known $got {-*} $zsh_help_switches
292
293 completion_zsh_close
294
295
296 #
297 # ground-truth check, per sub-command: 'module <sub-command> -<Tab><Tab>'
298 # must propose exactly the option flags 'module help' documents for that
299 # sub-command -- including none at all, for a sub-command that takes no
300 # option
301 #
302
303 setenv_path_var MODULEPATH $modpath
304
305 completion_zsh_start
306
307 foreach subcmd $help_subcmds {
308 set expected {}
309 if {[info exists help_subcmd_opts($subcmd)]} {
310 set expected $help_subcmd_opts($subcmd)
311 }
312 set expected [completion_zsh_expected_opts $expected]
313 if {[llength $expected] == 0} {
314 ## nothing is expected to complete at all: a single Tab must insert
315 ## nothing but a bell -- unlike the ambiguous-but-something-to-insert
316 ## cases above, zle does still beep here, since it has genuinely
317 ## nothing to insert
318 set got [completion_zsh_inline "module $subcmd -"]
319 completion_assert_eq $got "\a"
320 } else {
321 ## 'config' is the only sub-command whose every option is long-form
322 ## only ('--dump-state'/'--reset', no '-x' short forms): from a
323 ## single '-' they still share one more '-' as a common prefix, which
324 ## the first Tab would silently auto-insert (the same auto-extension
325 ## trap as "bar"/"ba" earlier in this file), leaving only one Tab for
326 ## the double-Tab below -- type that second '-' already, like every
327 ## other sub-command's full common prefix here
328 set dash [expr {$subcmd eq {config} ? {--} : {-}}]
329 set got [completion_zsh_list "module $subcmd $dash"]
330 completion_assert_contains $got $expected
331 completion_assert_only_known $got {-*} $expected
332 }
333 }
334
335 completion_zsh_close
336
337
338 #
339 # ground-truth check, per sub-command: 'module <sub-command> <Tab><Tab>'
340 # must propose a candidate of the kind 'module help' documents as that
341 # sub-command's positional argument, for the two kinds with fixture data
342 # predictable enough to check for by name -- an available/loaded module
343 # ('modulefile'/'modfile'/'mod1'/'mod2'/'mod' in the usage synopsis) or a
344 # saved collection ('collection'/'coll'). The remaining kinds (a filesystem
345 # directory, an arbitrary file, or nothing completable at all -- a search
346 # string, a shell name, a stash collection name generated with an
347 # unpredictable timestamp, ...) are not asserted on here: 'use' and 'unuse'
348 # already get a dedicated, fixture-appropriate check of their own earlier
349 # in this file, and the rest fall back to _module.in's own directory/file
350 # completion, which is not fixture data this testsuite controls
351 #
352
353 setenv_path_var MODULEPATH $modpath
354 setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0]
355
356 completion_zsh_start
357
358 completion_zsh_raw {module save foo}
359 completion_zsh_raw {module save fox}
360 completion_zsh_raw {module save bar1}
361
362 foreach subcmd $help_subcmds {
363 ## 'list'/'savelist' take a search string filtering already-known state
364 ## (loaded modules / saved collections), not a name completion target:
365 ## their own completion only ever proposes their option flags, despite
366 ## their usage synopsis documenting a module/collection-shaped argument
367 if {$subcmd in {list savelist}} {
368 continue
369 }
370 if {$subcmd eq {config}} {
371 ## 'config' takes a configuration option name, not a module or
372 ## collection: check it against the ground truth fetched from
373 ## 'module config' (see 008-module_config.exp), plus the two real
374 ## flags mixed into the same candidate list -- neither less nor more
375 set got [completion_zsh_list "module $subcmd "]
376 completion_assert_contains $got $config_opts
377 completion_assert_only_known $got {*} [concat $config_opts\
378 $help_subcmd_opts(config)]
379 continue
380 }
381 if {![info exists help_subcmd_argtype($subcmd)]} {
382 continue
383 }
384 set argtype $help_subcmd_argtype($subcmd)
385 if {$argtype ni {module collection}} {
386 continue
387 }
388 set got [completion_zsh_list "module $subcmd "]
389 if {$argtype eq {module}} {
390 ## covers every module-completion mechanism this fixture set can
391 ## produce: available modules not yet loaded (directory-style,
392 ## trailing '/'), available modules regardless of load state
393 ## (whatis/display/... style), and the one loaded module (flat
394 ## 'name/version', switch/unload style)
395 completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0}
396 } else {
397 completion_assert_any $got {foo fox bar1}
398 }
399 }
400
401 completion_zsh_close
402 unsetenv_loaded_module
403
404
405 #
406 # candidate words built from untrusted text cannot reach a shell expansion
407 # step (see this file's header, and the security fix for the bash-completion
408 # command injection this guards against a regression of)
409 #
410
411 set marker "$env(TESTSUITEDIR)/completion-injection-marker"
412 file delete -force $marker
413
414 # module avail: malicious module name read off disk
415
416 # the marker path is passed through as an env var rather than embedded
417 # directly in the filename, since a filename cannot itself contain '/'
418 setenv_var INJMARKER $marker
419 set evilname {evil$(touch>$INJMARKER)}
420 file mkdir $injectdir
421 set fd [open "$injectdir/$evilname" w]
422 puts $fd {#%Module1.0}
423 close $fd
424
425 setenv_path_var MODULEPATH $injectdir
426
427 completion_zsh_start
428
429 ## _module_avail_mods hands this candidate to 'compadd', which backslash-
430 ## escapes every shell metacharacter in it before display/insertion (the
431 ## same protection this file's header notes _module.in relies on instead of
432 ## bash's compgen -W): the listing shows 'evil\$\(touch\>\$INJMARKER\)', not
433 ## the raw string
434 set got [completion_zsh_list {module load }]
435 completion_assert_no_exec $marker
436 completion_assert_contains $got [list [regsub -all {[$()>]} $evilname {\\&}]]
437
438 completion_zsh_close
439 unsetenv_path_var MODULEPATH
440 unsetenv_var INJMARKER
441 file delete -force $injectdir
442
443 # LOADEDMODULES: malicious already-loaded module name
444
445 set evilmod [string map [list MARKERPATH $marker] {python$(touch>MARKERPATH)}]
446 setenv_loaded_module [list gcc $evilmod] [list /fake/gcc /fake/evilmod]
447
448 completion_zsh_start
449
450 set got [completion_zsh_list {module unload }]
451 completion_assert_no_exec $marker
452 completion_assert_contains $got [list $evilmod]
453
454 completion_zsh_close
455 unsetenv_loaded_module
456
457 # MODULEPATH: malicious path entry (module unuse)
458
459 set evilpath [string map [list MARKERPATH $marker] {/opt$(touch>MARKERPATH)}]
460 setenv_path_var MODULEPATH /tmp $evilpath
461
462 completion_zsh_start
463
464 ## both entries share a leading '/', so type it explicitly: otherwise zle
465 ## auto-inserts that common prefix on the first Tab and the double-Tab below
466 ## would only ring the bell instead of listing (same trap as the "bar"/"ba"
467 ## cases earlier in this file)
468 set got [completion_zsh_list {module unuse /}]
469 completion_assert_no_exec $marker
470 completion_assert_contains $got [list $evilpath]
471
472 completion_zsh_close
473 unsetenv_path_var MODULEPATH
474
475 file delete -force $marker
476
477
478 #
479 # Cleanup
480 #
481
482 reset_test_env
483
484 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: