############################################################################## # Modules Revision 3.0 # Providing a flexible user environment # # File: completion.00-init/%M% # Revision: %I% # First Edition: 2026/08/07 # Last Mod.: %U%, %G% # # Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr # # Description: Testuite testsequence # Command: avail, load, unload, ml, use, unuse, restore, switch # Sub-Command: # # Comment: %C{ # Check bash Tab completion: module names and option # flags get listed, 'unload' lists/completes the # currently loaded modules, 'ml' mirrors 'load' and also # lists loaded modules among the option flags, 'use' # completes a filesystem directory path, 'unuse' # lists/filters the enabled modulepaths, 'restore' # lists/filters the saved collections, 'config' lists # every configuration option name, and every # sub-command/switch/option/argument-kind 'module' and # 'ml' Tab-completion proposes is checked against the # ground truth fetched from 'module help' (see # 007-module_help.exp) and 'module config' (see # 008-module_config.exp) -- neither less nor more, and # completion candidate words built from untrusted text # (module names read off disk, LOADEDMODULES, # MODULEPATH) cannot reach a shell expansion step -- a # candidate embedding shell code must be listed as an # inert string, never executed # }C% # ############################################################################## # # module names and option flags both get listed on Tab completion # setenv_path_var MODULEPATH $modpath completion_bash_start set got [completion_bash_list {module avail }] completion_assert_contains $got {advvers/ bar/ baz/ foo/} completion_assert_contains $got {--all --icase --terse} set got [completion_bash_list {module load }] completion_assert_contains $got {advvers/ bar/ baz/ foo/} completion_assert_contains $got {--force -f --icase} ## "ba" is the common prefix of bar/ and baz/: a single Tab from "b" must ## auto-insert the "a" inline, with a bell right before it since bar/baz ## still leave the completion ambiguous beyond it set got [completion_bash_inline {module load b}] completion_assert_eq $got "\aa" ## typed prefix is "ba", the full common prefix of bar/ and baz/: readline ## auto-inserts any *further* common prefix on the first Tab, which would ## eat one of the two Tabs the double-Tab below relies on to list matches ## instead of just ringing the bell set got [completion_bash_list {module load ba}] completion_assert_contains $got {bar/ baz/} completion_assert_not_contains $got {foo/ advvers/} ## "bar" uniquely matches the "bar/" module category: a single Tab must ## complete it inline with a trailing '/' and no trailing space (so typing ## can continue straight into a version), per the 'compopt -o nospace' ## logic in _module_comgen_words_and_files set got [completion_bash_inline {module load bar}] completion_assert_eq $got {/} ## from "bar/", double-Tab must list the versions available under it set got [completion_bash_list {module load bar/}] completion_assert_contains $got {bar/1.0 bar/2.0} ## "flatlcompat" is a single, versionless module (no further completion ## possible beneath it, so unlike "bar" above it does not end in '/'): a ## single Tab must complete it inline, with a trailing space since it is ## not a directory-style entry set got [completion_bash_inline {module load fl}] completion_assert_eq $got {atlcompat } completion_bash_close # # 'unload' lists the currently loaded modules alongside the option flags, # and lists/completes them on the typed prefix like any other candidate list # setenv_loaded_module [list bar/1.0 baz/1.0 foo/1.0]\ [list $modpath/bar/1.0 $modpath/baz/1.0 $modpath/foo/1.0] completion_bash_start set got [completion_bash_list {module unload }] completion_assert_contains $got {bar/1.0 baz/1.0 foo/1.0 --force -f --icase} ## "ba" is the common prefix of bar/1.0 and baz/1.0: a single Tab from "b" ## must auto-insert the "a" inline, with a bell right before it since ## bar/1.0 and baz/1.0 still leave the completion ambiguous beyond it set got [completion_bash_inline {module unload b}] completion_assert_eq $got "\aa" ## from "ba", nothing more can be auto-inserted (bar/1.0 and baz/1.0 ## diverge right there), so a double-Tab must list just the two matching ## loaded modules, not foo/1.0 set got [completion_bash_list {module unload ba}] completion_assert_contains $got {bar/1.0 baz/1.0} completion_assert_not_contains $got {foo/1.0} ## "foo/1.0" is the only loaded module starting with "f", and no option ## flag does either: a single Tab must complete the rest of it inline, ## with a trailing space since a loaded-module candidate is not a ## directory-style entry set got [completion_bash_inline {module unload f}] completion_assert_eq $got {oo/1.0 } completion_bash_close unsetenv_loaded_module # # 'ml' mirrors 'module load' completion for the bare sub-command case, and # additionally appends a "-" candidate for every currently loaded # module once an option flag is being typed, letting it be typed straight # after "ml -" to unload it # setenv_path_var MODULEPATH $modpath completion_bash_start ## unlike plain 'module load', a bare 'ml' also lists the top-level '-*' ## command-line options (e.g. '-h'/'--help'), since with no sub-command ## typed yet it cannot tell a bare 'module ' apart from a ## direct 'ml ' invocation set got [completion_bash_list {ml }] completion_assert_contains $got {advvers/ bar/ baz/ foo/ -h --help -D} completion_bash_raw {module load bar} ## with bar/2.0 loaded (the default version, no .version file pins it), a ## "-bar/2.0" candidate must appear among the option flags set got [completion_bash_list {ml -}] completion_assert_contains $got {-bar/2.0 --force -f --icase} ## "-bar/2.0" is the only candidate starting with "-ba": a single Tab must ## complete the rest of it inline, with a trailing space since it is not a ## directory-style entry set got [completion_bash_inline {ml -ba}] completion_assert_eq $got {r/2.0 } completion_bash_close # # 'use' completes a filesystem directory path: the completion script # intentionally leaves COMPREPLY untouched for this sub-command, so it must # fall through to readline's own default filename completion (registered # via 'complete -o default') # set usedir "$env(TESTSUITEDIR)/completion-use-fixture" file delete -force $usedir file mkdir $usedir completion_bash_start ## drop the last 3 characters of the fixture dir name ("ure"): a single Tab ## must complete them back plus a trailing '/', with no trailing space, ## exactly like the "bar" -> "bar/" case above -- this time from plain ## readline directory completion rather than anything in the completion ## script itself set got [completion_bash_inline "module use [string range $usedir 0 end-3]"] completion_assert_eq $got {ure/} completion_bash_close file delete -force $usedir # # 'unuse' lists the currently enabled modulepaths, and filters them on the # typed prefix like any other candidate list # setenv_path_var MODULEPATH $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp completion_bash_start ## all three modulepaths are absolute, so they all share a leading '/': ## type it explicitly, otherwise readline auto-inserts that one-character ## common prefix on the first Tab and the double-Tab below would only ring ## the bell instead of listing (same trap as the "bar"/"ba" cases above) set got [completion_bash_list {module unuse /}] completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp] ## $modpath and modulefiles.4 share the ".../modulefiles." prefix, diverging ## only on the trailing version digit: typing that full common prefix (so ## the first Tab has nothing left to auto-insert) and double-Tab must list ## only the two matching modulepaths, not the unrelated /tmp entry set got [completion_bash_list "module unuse $env(TESTSUITEDIR)/modulefiles."] completion_assert_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4"] completion_assert_not_contains $got {/tmp} completion_bash_close # # with only one modulepath left matching a typed prefix, a single Tab must # complete the whole rest of it inline -- unlike the module-name case, a # modulepath candidate is not a directory-style entry, so (still unlike # "bar" -> "bar/" above) a trailing space is expected after it # setenv_path_var MODULEPATH "$env(TESTSUITEDIR)/modulefiles.4" /tmp completion_bash_start set got [completion_bash_inline "module unuse $env(TESTSUITEDIR)/module"] completion_assert_eq $got {files.4 } completion_bash_close # # 'restore' lists the saved collections, and filters them on the typed # prefix like any other candidate list # setenv_path_var MODULEPATH $modpath completion_bash_start completion_bash_raw {module save foo} completion_bash_raw {module save fox} completion_bash_raw {module save bar1} set got [completion_bash_list {module restore }] completion_assert_contains $got {foo fox bar1} ## "fo" is the common prefix of foo/fox: a single Tab from "f" must ## auto-insert the "o" inline (this is the same mechanism as the "bar"/"ba" ## trap above, caught in the act instead of just avoided). Readline also ## rings the bell right before that insertion, since foo/fox still leave ## the completion ambiguous beyond it set got [completion_bash_inline {module restore f}] completion_assert_eq $got "\ao" ## from "fo", nothing more can be auto-inserted (foo/fox diverge right ## there), so a double-Tab must list just the two matching collections set got [completion_bash_list {module restore fo}] completion_assert_contains $got {foo fox} completion_assert_not_contains $got {bar1} ## "bar1" is the only collection starting with "ba": a single Tab must ## complete the rest of it inline, with a trailing space since a ## collection candidate is not a directory-style entry set got [completion_bash_inline {module restore ba}] completion_assert_eq $got {r1 } completion_bash_close # # ground-truth check against 'module help' (see completion.00-init/ # 007-module_help.exp): a bare 'module ' must propose exactly the # documented sub-commands plus the global switches, and 'module -' # (isolating the switches from the sub-command names) must propose exactly # the documented switches # setenv_path_var MODULEPATH $modpath completion_bash_start set got [completion_bash_list {module }] set got_subcmds [lsearch -all -inline -not -regexp $got {^-}] completion_assert_contains $got_subcmds $help_subcmds completion_assert_only_known $got_subcmds {*} $help_subcmds set got [completion_bash_list {module -}] completion_assert_contains $got $help_switches completion_assert_only_known $got {-*} $help_switches completion_bash_close # # ground-truth check, per sub-command: 'module -' # must propose exactly the option flags 'module help' documents for that # sub-command -- including none at all, for a sub-command that takes no # option # ## typing '-' first, rather than double-Tab from a bare trailing space, ## matters for two reasons: every option-bearing sub-command's own ## completion helper (_module_avail, _module_not_yet_loaded, ...) skips ## looking up real module/collection/modulepath candidates once the word ## being completed starts with '-', isolating the option flags on their ## own; and it sidesteps the common-prefix auto-extension trap (as with ## "bar"/"ba" earlier in this file) that a bare trailing space hits for any ## sub-command whose *entire* candidate set is option flags (which, unlike ## module names, always share a leading '-') or modulepath-only entries ## (which always share a leading '/') -- eg "module aliases " ## silently completes the shared '-' inline on the first Tab, leaving only ## one Tab (a bell, no listing) for the second setenv_path_var MODULEPATH $modpath completion_bash_start foreach subcmd $help_subcmds { set expected {} if {[info exists help_subcmd_opts($subcmd)]} { set expected $help_subcmd_opts($subcmd) } if {[llength $expected] == 0} { ## nothing is expected to complete at all: a single Tab must insert ## nothing but a bell (not even a directory-style default-completion ## fallback match, since no file in the test run's working directory ## starts with '-') -- readline rings the bell on any Tab that finds ## zero completions, not just an ambiguous one set got [completion_bash_inline "module $subcmd -"] completion_assert_eq $got "\a" } else { ## 'config' is the only sub-command whose every option is long-form ## only ('--dump-state'/'--reset', no '-x' short forms): from a ## single '-' they still share one more '-' as a common prefix, ## which the first Tab would silently auto-insert (the same ## auto-extension trap as "bar"/"ba" earlier in this file), leaving ## only one Tab for the double-Tab below -- type that second '-' ## already, like every other sub-command's full common prefix here set dash [expr {$subcmd eq {config} ? {--} : {-}}] set got [completion_bash_list "module $subcmd $dash"] completion_assert_contains $got $expected completion_assert_only_known $got {-*} $expected } } completion_bash_close # # same ground truth, this time against 'ml': its per-sub-command dispatch # (_ml in init/bash_completion.in) mirrors 'module's case-for-case, so the # exact same fetched option sets apply to 'ml -' # setenv_path_var MODULEPATH $modpath completion_bash_start foreach subcmd $help_subcmds { set expected {} if {[info exists help_subcmd_opts($subcmd)]} { set expected $help_subcmd_opts($subcmd) } if {[llength $expected] == 0} { set got [completion_bash_inline "ml $subcmd -"] completion_assert_eq $got "\a" } else { set dash [expr {$subcmd eq {config} ? {--} : {-}}] set got [completion_bash_list "ml $subcmd $dash"] completion_assert_contains $got $expected completion_assert_only_known $got {-*} $expected } } completion_bash_close # # ground-truth check, per sub-command: 'module ' # must propose a candidate of the kind 'module help' documents as that # sub-command's positional argument, for the two kinds with fixture data # predictable enough to check for by name -- an available/loaded module # ('modulefile'/'modfile'/'mod1'/'mod2'/'mod' in the usage synopsis) or a # saved collection ('collection'/'coll'). The remaining kinds # (a filesystem directory, an arbitrary file, or nothing completable at # all -- a search string, a shell name, a stash collection name generated # with an unpredictable timestamp, ...) are not asserted on here: 'use' and # 'unuse' already get a dedicated, fixture-appropriate check of their own # earlier in this file, and the rest fall back to plain filename completion # of the working directory, which is not fixture data this testsuite # controls # setenv_path_var MODULEPATH $modpath setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0] completion_bash_start completion_bash_raw {module save foo} completion_bash_raw {module save fox} completion_bash_raw {module save bar1} foreach subcmd $help_subcmds { ## 'list'/'savelist' take a search string filtering already-known state ## (loaded modules / saved collections), not a name completion target: ## their own completion only ever proposes their option flags, despite ## their usage synopsis documenting a module/collection-shaped argument if {$subcmd in {list savelist}} { continue } if {$subcmd eq {config}} { ## 'config' takes a configuration option name, not a module or ## collection: check it against the ground truth fetched from ## 'module config' (see 008-module_config.exp), plus the two real ## flags mixed into the same candidate list -- neither less nor more set got [completion_bash_list "module $subcmd "] completion_assert_contains $got $config_opts completion_assert_only_known $got {*} [concat $config_opts\ $help_subcmd_opts(config)] continue } if {![info exists help_subcmd_argtype($subcmd)]} { continue } set argtype $help_subcmd_argtype($subcmd) if {$argtype ni {module collection}} { continue } set got [completion_bash_list "module $subcmd "] if {$argtype eq {module}} { ## covers every module-completion mechanism this fixture set can ## produce: available modules not yet loaded (directory-style, ## trailing '/'), available modules regardless of load state ## (whatis/display/... style), and the one loaded module (flat ## 'name/version', switch/unload style) completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0} } else { completion_assert_any $got {foo fox bar1} } } completion_bash_close unsetenv_loaded_module # # same per-sub-command argument-kind ground truth, this time against 'ml' # setenv_path_var MODULEPATH $modpath setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0] completion_bash_start completion_bash_raw {module save foo} completion_bash_raw {module save fox} completion_bash_raw {module save bar1} foreach subcmd $help_subcmds { if {$subcmd in {list savelist}} { continue } if {$subcmd eq {config}} { set got [completion_bash_list "ml $subcmd "] completion_assert_contains $got $config_opts completion_assert_only_known $got {*} [concat $config_opts\ $help_subcmd_opts(config)] continue } if {![info exists help_subcmd_argtype($subcmd)]} { continue } set argtype $help_subcmd_argtype($subcmd) if {$argtype ni {module collection}} { continue } set got [completion_bash_list "ml $subcmd "] if {$argtype eq {module}} { completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0} } else { completion_assert_any $got {foo fox bar1} } } completion_bash_close unsetenv_loaded_module # # candidate words built from untrusted text cannot reach a shell expansion # step (see security fix for the bash-completion command injection) # set marker "$env(TESTSUITEDIR)/completion-injection-marker" file delete -force $marker # module/spider avail: malicious module name read off disk # the marker path is passed through as an env var rather than embedded # directly in the filename, since a filename cannot itself contain '/' setenv_var INJMARKER $marker set evilname {evil$(touch>$INJMARKER)} file mkdir $injectdir set fd [open "$injectdir/$evilname" w] puts $fd {#%Module1.0} close $fd setenv_path_var MODULEPATH $injectdir completion_bash_start set got [completion_bash_list {module load }] completion_assert_no_exec $marker completion_assert_contains $got [list $evilname] completion_bash_close unsetenv_path_var MODULEPATH unsetenv_var INJMARKER file delete -force $injectdir # LOADEDMODULES: malicious already-loaded module name set evilmod [string map [list MARKERPATH $marker] {python$(touch>MARKERPATH)}] # _LMFILES_ is set consistently alongside LOADEDMODULES (matching entry # count) purely to avoid an unrelated "inconsistent state" warning; its # content is not used by the completion script setenv_loaded_module [list gcc $evilmod] [list /fake/gcc /fake/evilmod] completion_bash_start set got [completion_bash_list {module unload }] completion_assert_no_exec $marker completion_assert_contains $got [list $evilmod] completion_bash_close unsetenv_loaded_module # MODULEPATH: malicious path entry (module unuse) set evilpath [string map [list MARKERPATH $marker] {/opt$(touch>MARKERPATH)}] setenv_path_var MODULEPATH /tmp $evilpath completion_bash_start ## both entries share a leading '/', so type it explicitly: otherwise ## readline auto-inserts that common prefix on the first Tab and the ## double-Tab below would only ring the bell instead of listing set got [completion_bash_list {module unuse /}] completion_assert_no_exec $marker completion_assert_contains $got [list $evilpath] completion_bash_close unsetenv_path_var MODULEPATH file delete -force $marker # # Cleanup # reset_test_env # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: