Debian Codegraph @

testsuite/completion.00-init/021-bash.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/07
8 # Last Mod.: %U%, %G%
9 #
10 # Authors: Xavier Delaruelle, [email protected]
11 #
12 # Description: Testuite testsequence
13 # Command: avail, load, unload, ml, use, unuse, restore, switch
14 # Sub-Command:
15 #
16 # Comment: %C{
17 # Check bash Tab completion: module names and option
18 # flags get listed, 'unload' lists/completes the
19 # currently loaded modules, 'ml' mirrors 'load' and also
20 # lists loaded modules among the option flags, 'use'
21 # completes a filesystem directory path, 'unuse'
22 # lists/filters the enabled modulepaths, 'restore'
23 # lists/filters the saved collections, 'config' lists
24 # every configuration option name, and every
25 # sub-command/switch/option/argument-kind 'module' and
26 # 'ml' Tab-completion proposes is checked against the
27 # ground truth fetched from 'module help' (see
28 # 007-module_help.exp) and 'module config' (see
29 # 008-module_config.exp) -- neither less nor more, and
30 # completion candidate words built from untrusted text
31 # (module names read off disk, LOADEDMODULES,
32 # MODULEPATH) cannot reach a shell expansion step -- a
33 # candidate embedding shell code must be listed as an
34 # inert string, never executed
35 # }C%
36 #
37 ##############################################################################
38
39
40 #
41 # module names and option flags both get listed on Tab completion
42 #
43
44 setenv_path_var MODULEPATH $modpath
45
46 completion_bash_start
47
48 set got [completion_bash_list {module avail }]
49 completion_assert_contains $got {advvers/ bar/ baz/ foo/}
50 completion_assert_contains $got {--all --icase --terse}
51
52 set got [completion_bash_list {module load }]
53 completion_assert_contains $got {advvers/ bar/ baz/ foo/}
54 completion_assert_contains $got {--force -f --icase}
55
56 ## "ba" is the common prefix of bar/ and baz/: a single Tab from "b" must
57 ## auto-insert the "a" inline, with a bell right before it since bar/baz
58 ## still leave the completion ambiguous beyond it
59 set got [completion_bash_inline {module load b}]
60 completion_assert_eq $got "\aa"
61
62 ## typed prefix is "ba", the full common prefix of bar/ and baz/: readline
63 ## auto-inserts any *further* common prefix on the first Tab, which would
64 ## eat one of the two Tabs the double-Tab below relies on to list matches
65 ## instead of just ringing the bell
66 set got [completion_bash_list {module load ba}]
67 completion_assert_contains $got {bar/ baz/}
68 completion_assert_not_contains $got {foo/ advvers/}
69
70 ## "bar" uniquely matches the "bar/" module category: a single Tab must
71 ## complete it inline with a trailing '/' and no trailing space (so typing
72 ## can continue straight into a version), per the 'compopt -o nospace'
73 ## logic in _module_comgen_words_and_files
74 set got [completion_bash_inline {module load bar}]
75 completion_assert_eq $got {/}
76
77 ## from "bar/", double-Tab must list the versions available under it
78 set got [completion_bash_list {module load bar/}]
79 completion_assert_contains $got {bar/1.0 bar/2.0}
80
81 ## "flatlcompat" is a single, versionless module (no further completion
82 ## possible beneath it, so unlike "bar" above it does not end in '/'): a
83 ## single Tab must complete it inline, with a trailing space since it is
84 ## not a directory-style entry
85 set got [completion_bash_inline {module load fl}]
86 completion_assert_eq $got {atlcompat }
87
88 completion_bash_close
89
90
91 #
92 # 'unload' lists the currently loaded modules alongside the option flags,
93 # and lists/completes them on the typed prefix like any other candidate list
94 #
95
96 setenv_loaded_module [list bar/1.0 baz/1.0 foo/1.0]\
97 [list $modpath/bar/1.0 $modpath/baz/1.0 $modpath/foo/1.0]
98
99 completion_bash_start
100
101 set got [completion_bash_list {module unload }]
102 completion_assert_contains $got {bar/1.0 baz/1.0 foo/1.0 --force -f --icase}
103
104 ## "ba" is the common prefix of bar/1.0 and baz/1.0: a single Tab from "b"
105 ## must auto-insert the "a" inline, with a bell right before it since
106 ## bar/1.0 and baz/1.0 still leave the completion ambiguous beyond it
107 set got [completion_bash_inline {module unload b}]
108 completion_assert_eq $got "\aa"
109
110 ## from "ba", nothing more can be auto-inserted (bar/1.0 and baz/1.0
111 ## diverge right there), so a double-Tab must list just the two matching
112 ## loaded modules, not foo/1.0
113 set got [completion_bash_list {module unload ba}]
114 completion_assert_contains $got {bar/1.0 baz/1.0}
115 completion_assert_not_contains $got {foo/1.0}
116
117 ## "foo/1.0" is the only loaded module starting with "f", and no option
118 ## flag does either: a single Tab must complete the rest of it inline,
119 ## with a trailing space since a loaded-module candidate is not a
120 ## directory-style entry
121 set got [completion_bash_inline {module unload f}]
122 completion_assert_eq $got {oo/1.0 }
123
124 completion_bash_close
125 unsetenv_loaded_module
126
127
128 #
129 # 'ml' mirrors 'module load' completion for the bare sub-command case, and
130 # additionally appends a "-<name>" candidate for every currently loaded
131 # module once an option flag is being typed, letting it be typed straight
132 # after "ml -" to unload it
133 #
134
135 setenv_path_var MODULEPATH $modpath
136
137 completion_bash_start
138
139 ## unlike plain 'module load', a bare 'ml' also lists the top-level '-*'
140 ## command-line options (e.g. '-h'/'--help'), since with no sub-command
141 ## typed yet it cannot tell a bare 'module <sub-command>' apart from a
142 ## direct 'ml <module-to-load>' invocation
143 set got [completion_bash_list {ml }]
144 completion_assert_contains $got {advvers/ bar/ baz/ foo/ -h --help -D}
145
146 completion_bash_raw {module load bar}
147
148 ## with bar/2.0 loaded (the default version, no .version file pins it), a
149 ## "-bar/2.0" candidate must appear among the option flags
150 set got [completion_bash_list {ml -}]
151 completion_assert_contains $got {-bar/2.0 --force -f --icase}
152
153 ## "-bar/2.0" is the only candidate starting with "-ba": a single Tab must
154 ## complete the rest of it inline, with a trailing space since it is not a
155 ## directory-style entry
156 set got [completion_bash_inline {ml -ba}]
157 completion_assert_eq $got {r/2.0 }
158
159 completion_bash_close
160
161
162 #
163 # 'use' completes a filesystem directory path: the completion script
164 # intentionally leaves COMPREPLY untouched for this sub-command, so it must
165 # fall through to readline's own default filename completion (registered
166 # via 'complete -o default')
167 #
168
169 set usedir "$env(TESTSUITEDIR)/completion-use-fixture"
170 file delete -force $usedir
171 file mkdir $usedir
172
173 completion_bash_start
174
175 ## drop the last 3 characters of the fixture dir name ("ure"): a single Tab
176 ## must complete them back plus a trailing '/', with no trailing space,
177 ## exactly like the "bar" -> "bar/" case above -- this time from plain
178 ## readline directory completion rather than anything in the completion
179 ## script itself
180 set got [completion_bash_inline "module use [string range $usedir 0 end-3]"]
181 completion_assert_eq $got {ure/}
182
183 completion_bash_close
184 file delete -force $usedir
185
186
187 #
188 # 'unuse' lists the currently enabled modulepaths, and filters them on the
189 # typed prefix like any other candidate list
190 #
191
192 setenv_path_var MODULEPATH $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp
193
194 completion_bash_start
195
196 ## all three modulepaths are absolute, so they all share a leading '/':
197 ## type it explicitly, otherwise readline auto-inserts that one-character
198 ## common prefix on the first Tab and the double-Tab below would only ring
199 ## the bell instead of listing (same trap as the "bar"/"ba" cases above)
200 set got [completion_bash_list {module unuse /}]
201 completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp]
202
203 ## $modpath and modulefiles.4 share the ".../modulefiles." prefix, diverging
204 ## only on the trailing version digit: typing that full common prefix (so
205 ## the first Tab has nothing left to auto-insert) and double-Tab must list
206 ## only the two matching modulepaths, not the unrelated /tmp entry
207 set got [completion_bash_list "module unuse $env(TESTSUITEDIR)/modulefiles."]
208 completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4"]
209 completion_assert_not_contains $got {/tmp}
210
211 completion_bash_close
212
213
214 #
215 # with only one modulepath left matching a typed prefix, a single Tab must
216 # complete the whole rest of it inline -- unlike the module-name case, a
217 # modulepath candidate is not a directory-style entry, so (still unlike
218 # "bar" -> "bar/" above) a trailing space is expected after it
219 #
220
221 setenv_path_var MODULEPATH "$env(TESTSUITEDIR)/modulefiles.4" /tmp
222
223 completion_bash_start
224
225 set got [completion_bash_inline "module unuse $env(TESTSUITEDIR)/module"]
226 completion_assert_eq $got {files.4 }
227
228 completion_bash_close
229
230
231 #
232 # 'restore' lists the saved collections, and filters them on the typed
233 # prefix like any other candidate list
234 #
235
236 setenv_path_var MODULEPATH $modpath
237
238 completion_bash_start
239
240 completion_bash_raw {module save foo}
241 completion_bash_raw {module save fox}
242 completion_bash_raw {module save bar1}
243
244 set got [completion_bash_list {module restore }]
245 completion_assert_contains $got {foo fox bar1}
246
247 ## "fo" is the common prefix of foo/fox: a single Tab from "f" must
248 ## auto-insert the "o" inline (this is the same mechanism as the "bar"/"ba"
249 ## trap above, caught in the act instead of just avoided). Readline also
250 ## rings the bell right before that insertion, since foo/fox still leave
251 ## the completion ambiguous beyond it
252 set got [completion_bash_inline {module restore f}]
253 completion_assert_eq $got "\ao"
254
255 ## from "fo", nothing more can be auto-inserted (foo/fox diverge right
256 ## there), so a double-Tab must list just the two matching collections
257 set got [completion_bash_list {module restore fo}]
258 completion_assert_contains $got {foo fox}
259 completion_assert_not_contains $got {bar1}
260
261 ## "bar1" is the only collection starting with "ba": a single Tab must
262 ## complete the rest of it inline, with a trailing space since a
263 ## collection candidate is not a directory-style entry
264 set got [completion_bash_inline {module restore ba}]
265 completion_assert_eq $got {r1 }
266
267 completion_bash_close
268
269
270 #
271 # ground-truth check against 'module help' (see completion.00-init/
272 # 007-module_help.exp): a bare 'module <Tab><Tab>' must propose exactly the
273 # documented sub-commands plus the global switches, and 'module -<Tab><Tab>'
274 # (isolating the switches from the sub-command names) must propose exactly
275 # the documented switches
276 #
277
278 setenv_path_var MODULEPATH $modpath
279
280 completion_bash_start
281
282 set got [completion_bash_list {module }]
283 set got_subcmds [lsearch -all -inline -not -regexp $got {^-}]
284 completion_assert_contains $got_subcmds $help_subcmds
285 completion_assert_only_known $got_subcmds {*} $help_subcmds
286
287 set got [completion_bash_list {module -}]
288 completion_assert_contains $got $help_switches
289 completion_assert_only_known $got {-*} $help_switches
290
291 completion_bash_close
292
293
294 #
295 # ground-truth check, per sub-command: 'module <sub-command> -<Tab><Tab>'
296 # must propose exactly the option flags 'module help' documents for that
297 # sub-command -- including none at all, for a sub-command that takes no
298 # option
299 #
300
301 ## typing '-' first, rather than double-Tab from a bare trailing space,
302 ## matters for two reasons: every option-bearing sub-command's own
303 ## completion helper (_module_avail, _module_not_yet_loaded, ...) skips
304 ## looking up real module/collection/modulepath candidates once the word
305 ## being completed starts with '-', isolating the option flags on their
306 ## own; and it sidesteps the common-prefix auto-extension trap (as with
307 ## "bar"/"ba" earlier in this file) that a bare trailing space hits for any
308 ## sub-command whose *entire* candidate set is option flags (which, unlike
309 ## module names, always share a leading '-') or modulepath-only entries
310 ## (which always share a leading '/') -- eg "module aliases <Tab><Tab>"
311 ## silently completes the shared '-' inline on the first Tab, leaving only
312 ## one Tab (a bell, no listing) for the second
313 setenv_path_var MODULEPATH $modpath
314
315 completion_bash_start
316
317 foreach subcmd $help_subcmds {
318 set expected {}
319 if {[info exists help_subcmd_opts($subcmd)]} {
320 set expected $help_subcmd_opts($subcmd)
321 }
322 if {[llength $expected] == 0} {
323 ## nothing is expected to complete at all: a single Tab must insert
324 ## nothing but a bell (not even a directory-style default-completion
325 ## fallback match, since no file in the test run's working directory
326 ## starts with '-') -- readline rings the bell on any Tab that finds
327 ## zero completions, not just an ambiguous one
328 set got [completion_bash_inline "module $subcmd -"]
329 completion_assert_eq $got "\a"
330 } else {
331 ## 'config' is the only sub-command whose every option is long-form
332 ## only ('--dump-state'/'--reset', no '-x' short forms): from a
333 ## single '-' they still share one more '-' as a common prefix,
334 ## which the first Tab would silently auto-insert (the same
335 ## auto-extension trap as "bar"/"ba" earlier in this file), leaving
336 ## only one Tab for the double-Tab below -- type that second '-'
337 ## already, like every other sub-command's full common prefix here
338 set dash [expr {$subcmd eq {config} ? {--} : {-}}]
339 set got [completion_bash_list "module $subcmd $dash"]
340 completion_assert_contains $got $expected
341 completion_assert_only_known $got {-*} $expected
342 }
343 }
344
345 completion_bash_close
346
347
348 #
349 # same ground truth, this time against 'ml': its per-sub-command dispatch
350 # (_ml in init/bash_completion.in) mirrors 'module's case-for-case, so the
351 # exact same fetched option sets apply to 'ml <sub-command> -<Tab><Tab>'
352 #
353
354 setenv_path_var MODULEPATH $modpath
355
356 completion_bash_start
357
358 foreach subcmd $help_subcmds {
359 set expected {}
360 if {[info exists help_subcmd_opts($subcmd)]} {
361 set expected $help_subcmd_opts($subcmd)
362 }
363 if {[llength $expected] == 0} {
364 set got [completion_bash_inline "ml $subcmd -"]
365 completion_assert_eq $got "\a"
366 } else {
367 set dash [expr {$subcmd eq {config} ? {--} : {-}}]
368 set got [completion_bash_list "ml $subcmd $dash"]
369 completion_assert_contains $got $expected
370 completion_assert_only_known $got {-*} $expected
371 }
372 }
373
374 completion_bash_close
375
376
377 #
378 # ground-truth check, per sub-command: 'module <sub-command> <Tab><Tab>'
379 # must propose a candidate of the kind 'module help' documents as that
380 # sub-command's positional argument, for the two kinds with fixture data
381 # predictable enough to check for by name -- an available/loaded module
382 # ('modulefile'/'modfile'/'mod1'/'mod2'/'mod' in the usage synopsis) or a
383 # saved collection ('collection'/'coll'). The remaining kinds
384 # (a filesystem directory, an arbitrary file, or nothing completable at
385 # all -- a search string, a shell name, a stash collection name generated
386 # with an unpredictable timestamp, ...) are not asserted on here: 'use' and
387 # 'unuse' already get a dedicated, fixture-appropriate check of their own
388 # earlier in this file, and the rest fall back to plain filename completion
389 # of the working directory, which is not fixture data this testsuite
390 # controls
391 #
392
393 setenv_path_var MODULEPATH $modpath
394 setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0]
395
396 completion_bash_start
397
398 completion_bash_raw {module save foo}
399 completion_bash_raw {module save fox}
400 completion_bash_raw {module save bar1}
401
402 foreach subcmd $help_subcmds {
403 ## 'list'/'savelist' take a search string filtering already-known state
404 ## (loaded modules / saved collections), not a name completion target:
405 ## their own completion only ever proposes their option flags, despite
406 ## their usage synopsis documenting a module/collection-shaped argument
407 if {$subcmd in {list savelist}} {
408 continue
409 }
410 if {$subcmd eq {config}} {
411 ## 'config' takes a configuration option name, not a module or
412 ## collection: check it against the ground truth fetched from
413 ## 'module config' (see 008-module_config.exp), plus the two real
414 ## flags mixed into the same candidate list -- neither less nor more
415 set got [completion_bash_list "module $subcmd "]
416 completion_assert_contains $got $config_opts
417 completion_assert_only_known $got {*} [concat $config_opts\
418 $help_subcmd_opts(config)]
419 continue
420 }
421 if {![info exists help_subcmd_argtype($subcmd)]} {
422 continue
423 }
424 set argtype $help_subcmd_argtype($subcmd)
425 if {$argtype ni {module collection}} {
426 continue
427 }
428 set got [completion_bash_list "module $subcmd "]
429 if {$argtype eq {module}} {
430 ## covers every module-completion mechanism this fixture set can
431 ## produce: available modules not yet loaded (directory-style,
432 ## trailing '/'), available modules regardless of load state
433 ## (whatis/display/... style), and the one loaded module (flat
434 ## 'name/version', switch/unload style)
435 completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0}
436 } else {
437 completion_assert_any $got {foo fox bar1}
438 }
439 }
440
441 completion_bash_close
442 unsetenv_loaded_module
443
444
445 #
446 # same per-sub-command argument-kind ground truth, this time against 'ml'
447 #
448
449 setenv_path_var MODULEPATH $modpath
450 setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0]
451
452 completion_bash_start
453
454 completion_bash_raw {module save foo}
455 completion_bash_raw {module save fox}
456 completion_bash_raw {module save bar1}
457
458 foreach subcmd $help_subcmds {
459 if {$subcmd in {list savelist}} {
460 continue
461 }
462 if {$subcmd eq {config}} {
463 set got [completion_bash_list "ml $subcmd "]
464 completion_assert_contains $got $config_opts
465 completion_assert_only_known $got {*} [concat $config_opts\
466 $help_subcmd_opts(config)]
467 continue
468 }
469 if {![info exists help_subcmd_argtype($subcmd)]} {
470 continue
471 }
472 set argtype $help_subcmd_argtype($subcmd)
473 if {$argtype ni {module collection}} {
474 continue
475 }
476 set got [completion_bash_list "ml $subcmd "]
477 if {$argtype eq {module}} {
478 completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0}
479 } else {
480 completion_assert_any $got {foo fox bar1}
481 }
482 }
483
484 completion_bash_close
485 unsetenv_loaded_module
486
487
488 #
489 # candidate words built from untrusted text cannot reach a shell expansion
490 # step (see security fix for the bash-completion command injection)
491 #
492
493 set marker "$env(TESTSUITEDIR)/completion-injection-marker"
494 file delete -force $marker
495
496 # module/spider avail: malicious module name read off disk
497
498 # the marker path is passed through as an env var rather than embedded
499 # directly in the filename, since a filename cannot itself contain '/'
500 setenv_var INJMARKER $marker
501 set evilname {evil$(touch>$INJMARKER)}
502 file mkdir $injectdir
503 set fd [open "$injectdir/$evilname" w]
504 puts $fd {#%Module1.0}
505 close $fd
506
507 setenv_path_var MODULEPATH $injectdir
508
509 completion_bash_start
510
511 set got [completion_bash_list {module load }]
512 completion_assert_no_exec $marker
513 completion_assert_contains $got [list $evilname]
514
515 completion_bash_close
516 unsetenv_path_var MODULEPATH
517 unsetenv_var INJMARKER
518 file delete -force $injectdir
519
520 # LOADEDMODULES: malicious already-loaded module name
521
522 set evilmod [string map [list MARKERPATH $marker] {python$(touch>MARKERPATH)}]
523 # _LMFILES_ is set consistently alongside LOADEDMODULES (matching entry
524 # count) purely to avoid an unrelated "inconsistent state" warning; its
525 # content is not used by the completion script
526 setenv_loaded_module [list gcc $evilmod] [list /fake/gcc /fake/evilmod]
527
528 completion_bash_start
529
530 set got [completion_bash_list {module unload }]
531 completion_assert_no_exec $marker
532 completion_assert_contains $got [list $evilmod]
533
534 completion_bash_close
535 unsetenv_loaded_module
536
537 # MODULEPATH: malicious path entry (module unuse)
538
539 set evilpath [string map [list MARKERPATH $marker] {/opt$(touch>MARKERPATH)}]
540 setenv_path_var MODULEPATH /tmp $evilpath
541
542 completion_bash_start
543
544 ## both entries share a leading '/', so type it explicitly: otherwise
545 ## readline auto-inserts that common prefix on the first Tab and the
546 ## double-Tab below would only ring the bell instead of listing
547 set got [completion_bash_list {module unuse /}]
548 completion_assert_no_exec $marker
549 completion_assert_contains $got [list $evilpath]
550
551 completion_bash_close
552 unsetenv_path_var MODULEPATH
553
554 file delete -force $marker
555
556
557 #
558 # Cleanup
559 #
560
561 reset_test_env
562
563 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: