| 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/11 |
| 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 fish Tab completion: mirrors 021-bash.exp / |
| 18 |
# 031-zsh.exp (see 021-bash.exp for the full rationale of |
| 19 |
# each check) as closely as fish's own completion script |
| 20 |
# allows, with every completion_bash_*/completion_zsh_* |
| 21 |
# call replaced by its completion_fish_* counterpart, and |
| 22 |
# the following deviations, all driven by genuine |
| 23 |
# differences between init/fish_completion and its bash/ |
| 24 |
# zsh counterparts (see 040-fish-procs.exp's header for |
| 25 |
# the differences in fish's own Tab-key behavior instead): |
| 26 |
# - init/fish_completion has no 'ml' support at all (no |
| 27 |
# 'complete -c ml' of its own), so every section |
| 28 |
# mirroring 'ml' behavior in 021-bash.exp is dropped |
| 29 |
# - init/fish_completion does not gate its option flags |
| 30 |
# by sub-command at all: every 'complete -c module |
| 31 |
# -s/-l ...' line applies unconditionally, so |
| 32 |
# 'module <sub-command> -<Tab>' always proposes the |
| 33 |
# exact same full set of flags -- both the global ones |
| 34 |
# and every other sub-command's own documented extras |
| 35 |
# (eg 'avail's '--all'/'--terse' show up right alongside |
| 36 |
# 'load's '--force') -- rather than just the subset |
| 37 |
# 'module help' documents for that particular |
| 38 |
# sub-command; so unlike 021-bash.exp/031-zsh.exp, this |
| 39 |
# file has no per-sub-command option ground-truth |
| 40 |
# section (only 'config' still gets one, its own option |
| 41 |
# *names* rather than flags being unaffected by this). |
| 42 |
# The one ground-truth check this file keeps for flags |
| 43 |
# at all -- a bare 'module -<Tab>' -- is checked against |
| 44 |
# the union of every sub-command's documented options |
| 45 |
# ($help_switches plus every $help_subcmd_opts(*) entry) |
| 46 |
# rather than just $help_switches (the global |
| 47 |
# 'Options:' section alone), to match this lack of |
| 48 |
# gating; the 'module avail'/'module load'/ |
| 49 |
# 'module unload' checks earlier only look for a small |
| 50 |
# illustrative subset of flags, same as |
| 51 |
# 021-bash.exp/031-zsh.exp, so they are unaffected |
| 52 |
# - init/fish_completion also has no '-h'/'--help' |
| 53 |
# completion at all (no 'complete -c module -s h -l |
| 54 |
# help' line, unlike every other documented global |
| 55 |
# switch) -- filtered out of the fetched ground truth |
| 56 |
# before comparing against what fish actually proposes |
| 57 |
# - 'unuse' is not covered by any of the fish completion |
| 58 |
# script's own '__fish_module_use_*' predicates (unlike |
| 59 |
# 'use', which is): Tab-completion here falls through |
| 60 |
# entirely to fish's own default filename completion, |
| 61 |
# so it never proposes the enabled modulepaths |
| 62 |
# themselves the way 021-bash.exp/031-zsh.exp expect |
| 63 |
# - like zsh's own _module.in, module names and option |
| 64 |
# flags are two mutually exclusive candidate sets, only |
| 65 |
# one of which is shown at a time depending on whether |
| 66 |
# the word being completed starts with '-' -- so a bare |
| 67 |
# trailing-space listing only ever shows one set, same |
| 68 |
# as 031-zsh.exp |
| 69 |
# |
| 70 |
# Also checks, like 021-bash.exp/031-zsh.exp, that a |
| 71 |
# candidate word built from untrusted text (a module |
| 72 |
# name, LOADEDMODULES) cannot reach a shell expansion |
| 73 |
# step: init/fish_completion only ever hands such text to |
| 74 |
# 'complete -a "(...)"', where the parenthesized command's |
| 75 |
# output is split into literal candidate lines, so this is |
| 76 |
# expected to already be safe -- these cases guard against |
| 77 |
# a regression, not a known vulnerability. No MODULEPATH |
| 78 |
# case here: 'unuse' does not list modulepaths at all in |
| 79 |
# fish (see this file's header) |
| 80 |
# }C% |
| 81 |
# |
| 82 |
############################################################################## |
| 83 |
|
| 84 |
|
| 85 |
# |
| 86 |
# module names and option flags each get listed on Tab completion, but |
| 87 |
# (like zsh) not mixed together in the same listing: which set shows up |
| 88 |
# depends on whether the word being completed starts with '-' |
| 89 |
# |
| 90 |
|
| 91 |
setenv_path_var MODULEPATH $modpath |
| 92 |
|
| 93 |
completion_fish_start |
| 94 |
|
| 95 |
set got [completion_fish_list {module avail }] |
| 96 |
completion_assert_contains $got {advvers/ bar/ baz/ foo/} |
| 97 |
|
| 98 |
set got [completion_fish_list {module avail -}] |
| 99 |
completion_assert_contains $got {--all --icase --terse} |
| 100 |
|
| 101 |
set got [completion_fish_list {module load }] |
| 102 |
completion_assert_contains $got {advvers/ bar/ baz/ foo/} |
| 103 |
|
| 104 |
set got [completion_fish_list {module load -}] |
| 105 |
completion_assert_contains $got {--force -f --icase} |
| 106 |
|
| 107 |
## "ba" is the common prefix of bar/ and baz/: a single Tab from "b" must |
| 108 |
## auto-insert the "a" inline -- fish, like zsh, does not additionally ring |
| 109 |
## a bell just because the completion remains ambiguous beyond it (see |
| 110 |
## 040-fish-procs.exp's header: fish gives no audible/visible feedback for |
| 111 |
## this over a plain pty at all) |
| 112 |
set got [completion_fish_inline {module load b}] |
| 113 |
completion_assert_eq $got {a} |
| 114 |
|
| 115 |
## typed prefix is "ba", the full common prefix of bar/ and baz/: fish |
| 116 |
## auto-inserts any *further* common prefix on the Tab, which would eat the |
| 117 |
## one Tab the list check below relies on to show a real listing instead of |
| 118 |
## silently completing further |
| 119 |
set got [completion_fish_list {module load ba}] |
| 120 |
completion_assert_contains $got {bar/ baz/} |
| 121 |
completion_assert_not_contains $got {foo/ advvers/} |
| 122 |
|
| 123 |
## "bar" uniquely matches the "bar/" module category: a single Tab must |
| 124 |
## complete it inline with a trailing '/' and no trailing space (so typing |
| 125 |
## can continue straight into a version), per the directory-style handling |
| 126 |
## in _module_avail_mods |
| 127 |
set got [completion_fish_inline {module load bar}] |
| 128 |
completion_assert_eq $got {/} |
| 129 |
|
| 130 |
## from "bar/", a Tab must list the versions available under it |
| 131 |
set got [completion_fish_list {module load bar/}] |
| 132 |
completion_assert_contains $got {bar/1.0 bar/2.0} |
| 133 |
|
| 134 |
## "flatlcompat" is a single, versionless module (no further completion |
| 135 |
## possible beneath it, so unlike "bar" above it does not end in '/'): a |
| 136 |
## single Tab must complete it inline, with a trailing space since it is |
| 137 |
## not a directory-style entry |
| 138 |
set got [completion_fish_inline {module load fl}] |
| 139 |
completion_assert_eq $got {atlcompat } |
| 140 |
|
| 141 |
completion_fish_close |
| 142 |
|
| 143 |
|
| 144 |
# |
| 145 |
# 'unload' lists the currently loaded modules (mutually exclusive with the |
| 146 |
# option flags, same as avail/load above), and lists/completes them on the |
| 147 |
# typed prefix like any other candidate list |
| 148 |
# |
| 149 |
|
| 150 |
setenv_loaded_module [list bar/1.0 baz/1.0 foo/1.0]\ |
| 151 |
[list $modpath/bar/1.0 $modpath/baz/1.0 $modpath/foo/1.0] |
| 152 |
|
| 153 |
completion_fish_start |
| 154 |
|
| 155 |
set got [completion_fish_list {module unload }] |
| 156 |
completion_assert_contains $got {bar/1.0 baz/1.0 foo/1.0} |
| 157 |
|
| 158 |
set got [completion_fish_list {module unload -}] |
| 159 |
completion_assert_contains $got {--force -f --icase} |
| 160 |
|
| 161 |
## "ba" is the common prefix of bar/1.0 and baz/1.0: a single Tab from "b" |
| 162 |
## must auto-insert the "a" inline, with no audible/visible feedback (see |
| 163 |
## 040-fish-procs.exp's header) |
| 164 |
set got [completion_fish_inline {module unload b}] |
| 165 |
completion_assert_eq $got {a} |
| 166 |
|
| 167 |
## from "ba", nothing more can be auto-inserted (bar/1.0 and baz/1.0 |
| 168 |
## diverge right there), so a Tab must list just the two matching loaded |
| 169 |
## modules, not foo/1.0 |
| 170 |
set got [completion_fish_list {module unload ba}] |
| 171 |
completion_assert_contains $got {bar/1.0 baz/1.0} |
| 172 |
completion_assert_not_contains $got {foo/1.0} |
| 173 |
|
| 174 |
## "foo/1.0" is the only loaded module starting with "f": a single Tab must |
| 175 |
## complete the rest of it inline, with a trailing space since a |
| 176 |
## loaded-module candidate is not a directory-style entry |
| 177 |
set got [completion_fish_inline {module unload f}] |
| 178 |
completion_assert_eq $got {oo/1.0 } |
| 179 |
|
| 180 |
completion_fish_close |
| 181 |
unsetenv_loaded_module |
| 182 |
|
| 183 |
|
| 184 |
# |
| 185 |
# 'use' completes a filesystem directory path, falling through to fish's |
| 186 |
# own default filename completion -- there is no fish equivalent of bash's |
| 187 |
# 'complete -o default' registration, but 'use' is also not covered by any |
| 188 |
# of init/fish_completion's own '__fish_module_use_*' predicates, so |
| 189 |
# nothing in the completion script intercepts it either way |
| 190 |
# |
| 191 |
|
| 192 |
set usedir "$env(TESTSUITEDIR)/completion-use-fixture" |
| 193 |
file delete -force $usedir |
| 194 |
file mkdir $usedir |
| 195 |
|
| 196 |
completion_fish_start |
| 197 |
|
| 198 |
## drop the last 3 characters of the fixture dir name ("ure"): a single Tab |
| 199 |
## must complete them back plus a trailing '/', with no trailing space, |
| 200 |
## exactly like the "bar" -> "bar/" case above |
| 201 |
set got [completion_fish_inline "module use [string range $usedir 0 end-3]"] |
| 202 |
completion_assert_eq $got {ure/} |
| 203 |
|
| 204 |
completion_fish_close |
| 205 |
file delete -force $usedir |
| 206 |
|
| 207 |
|
| 208 |
# |
| 209 |
# 'unuse' is not covered by any '__fish_module_use_*' predicate (see this |
| 210 |
# file's header): Tab-completion here falls through to plain filename |
| 211 |
# completion of the working directory, the same fallback 'use' relies on |
| 212 |
# above, and never proposes the enabled modulepaths themselves |
| 213 |
# |
| 214 |
|
| 215 |
setenv_path_var MODULEPATH $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp |
| 216 |
|
| 217 |
completion_fish_start |
| 218 |
|
| 219 |
set got [completion_fish_list {module unuse }] |
| 220 |
completion_assert_not_contains $got [list $modpath "$env(TESTSUITEDIR)/modulefiles.4" /tmp] |
| 221 |
|
| 222 |
completion_fish_close |
| 223 |
|
| 224 |
|
| 225 |
# |
| 226 |
# 'restore' lists the saved collections, and filters them on the typed |
| 227 |
# prefix like any other candidate list |
| 228 |
# |
| 229 |
|
| 230 |
setenv_path_var MODULEPATH $modpath |
| 231 |
|
| 232 |
completion_fish_start |
| 233 |
|
| 234 |
completion_fish_raw {module save foo} |
| 235 |
completion_fish_raw {module save fox} |
| 236 |
completion_fish_raw {module save bar1} |
| 237 |
|
| 238 |
set got [completion_fish_list {module restore }] |
| 239 |
completion_assert_contains $got {foo fox bar1} |
| 240 |
|
| 241 |
## "fo" is the common prefix of foo/fox: a single Tab from "f" must |
| 242 |
## auto-insert the "o" inline, with no audible/visible feedback (see |
| 243 |
## 040-fish-procs.exp's header) |
| 244 |
set got [completion_fish_inline {module restore f}] |
| 245 |
completion_assert_eq $got {o} |
| 246 |
|
| 247 |
## from "fo", nothing more can be auto-inserted (foo/fox diverge right |
| 248 |
## there), so a Tab must list just the two matching collections |
| 249 |
set got [completion_fish_list {module restore fo}] |
| 250 |
completion_assert_contains $got {foo fox} |
| 251 |
completion_assert_not_contains $got {bar1} |
| 252 |
|
| 253 |
## "bar1" is the only collection starting with "ba": a single Tab must |
| 254 |
## complete the rest of it inline, with a trailing space since a |
| 255 |
## collection candidate is not a directory-style entry |
| 256 |
set got [completion_fish_inline {module restore ba}] |
| 257 |
completion_assert_eq $got {r1 } |
| 258 |
|
| 259 |
completion_fish_close |
| 260 |
|
| 261 |
|
| 262 |
# |
| 263 |
# ground-truth check against 'module help' (see completion.00-init/ |
| 264 |
# 007-module_help.exp): a bare 'module <Tab>' must propose exactly the |
| 265 |
# documented sub-commands, and 'module -<Tab>' must propose exactly the |
| 266 |
# documented switches -- like the avail/load/unload checks above, these are |
| 267 |
# two mutually exclusive candidate sets in fish, so each gets its own Tab. |
| 268 |
# Since init/fish_completion does not gate its option flags by sub-command |
| 269 |
# (see this file's header), 'module -<Tab>' is checked against every |
| 270 |
# documented option this testsuite knows about -- $help_switches (the |
| 271 |
# global 'Options:' section) *and* every sub-command's own extras |
| 272 |
# ($help_subcmd_opts(*)) -- combined into one set, minus '-h'/'--help' |
| 273 |
# (fish proposes neither, another gap noted in this file's header). Two |
| 274 |
# groups of sub-command-specific options are excluded from this union: |
| 275 |
# 'config''s own '--dump-state'/'--reset' (real flags, but only declared in |
| 276 |
# its own dedicated, gated completion entry, not the unconditional block |
| 277 |
# every other flag comes from) and the '*-path' commands' |
| 278 |
# '--delim'/'--duplicates'/'--index' (not declared anywhere in the |
| 279 |
# completion script at all -- a further, narrower gap than the 'ml'/'-h'/ |
| 280 |
# 'unuse' ones already noted in this file's header, not broad enough to |
| 281 |
# warrant its own bullet there) |
| 282 |
# |
| 283 |
|
| 284 |
setenv_path_var MODULEPATH $modpath |
| 285 |
|
| 286 |
set fish_all_opts $help_switches |
| 287 |
foreach n [array names help_subcmd_opts] { |
| 288 |
if {$n in {config append-path prepend-path remove-path}} { |
| 289 |
continue |
| 290 |
} |
| 291 |
foreach o $help_subcmd_opts($n) { lappend fish_all_opts $o } |
| 292 |
} |
| 293 |
set fish_all_opts [lsort -unique [lsearch -all -inline -not -exact\ |
| 294 |
[lsearch -all -inline -not -exact $fish_all_opts -h] --help]] |
| 295 |
|
| 296 |
completion_fish_start |
| 297 |
|
| 298 |
set got [completion_fish_list {module }] |
| 299 |
completion_assert_contains $got $help_subcmds |
| 300 |
completion_assert_only_known $got {*} $help_subcmds |
| 301 |
|
| 302 |
set got [completion_fish_list {module -}] |
| 303 |
completion_assert_contains $got $fish_all_opts |
| 304 |
completion_assert_only_known $got {-*} $fish_all_opts |
| 305 |
|
| 306 |
completion_fish_close |
| 307 |
|
| 308 |
|
| 309 |
# |
| 310 |
# unlike 021-bash.exp/031-zsh.exp, there is no per-sub-command option |
| 311 |
# ground-truth section here: init/fish_completion does not gate its option |
| 312 |
# flags by sub-command at all (see this file's header), so |
| 313 |
# 'module <sub-command> -<Tab>' always proposes the same full $fish_all_opts |
| 314 |
# set checked above, for every sub-command alike |
| 315 |
# |
| 316 |
|
| 317 |
|
| 318 |
# |
| 319 |
# ground-truth check, per sub-command: 'module <sub-command> <Tab>' must |
| 320 |
# propose a candidate of the kind 'module help' documents as that |
| 321 |
# sub-command's positional argument, for the two kinds with fixture data |
| 322 |
# predictable enough to check for by name -- an available/loaded module |
| 323 |
# ('modulefile'/'modfile'/'mod1'/'mod2'/'mod' in the usage synopsis) or a |
| 324 |
# saved collection ('collection'/'coll'). The remaining kinds (a filesystem |
| 325 |
# directory, an arbitrary file, or nothing completable at all -- a search |
| 326 |
# string, a shell name, a stash collection name generated with an |
| 327 |
# unpredictable timestamp, ...) are not asserted on here: 'use' and 'unuse' |
| 328 |
# already get a dedicated, fixture-appropriate check of their own earlier |
| 329 |
# in this file, and the rest fall back to fish's own directory/file |
| 330 |
# completion, which is not fixture data this testsuite controls |
| 331 |
# |
| 332 |
|
| 333 |
setenv_path_var MODULEPATH $modpath |
| 334 |
setenv_loaded_module [list bar/1.0] [list $modpath/bar/1.0] |
| 335 |
|
| 336 |
completion_fish_start |
| 337 |
|
| 338 |
completion_fish_raw {module save foo} |
| 339 |
completion_fish_raw {module save fox} |
| 340 |
completion_fish_raw {module save bar1} |
| 341 |
|
| 342 |
foreach subcmd $help_subcmds { |
| 343 |
## 'list'/'savelist' take a search string filtering already-known state |
| 344 |
## (loaded modules / saved collections), not a name completion target: |
| 345 |
## their own completion only ever proposes their option flags, despite |
| 346 |
## their usage synopsis documenting a module/collection-shaped argument |
| 347 |
if {$subcmd in {list savelist}} { |
| 348 |
continue |
| 349 |
} |
| 350 |
if {$subcmd eq {config}} { |
| 351 |
## 'config' takes a configuration option name, not a module or |
| 352 |
## collection: check it against the ground truth fetched from |
| 353 |
## 'module config' (see 008-module_config.exp), plus the two real |
| 354 |
## flags mixed into the same candidate list -- neither less nor more |
| 355 |
set got [completion_fish_list "module $subcmd "] |
| 356 |
completion_assert_contains $got $config_opts |
| 357 |
completion_assert_only_known $got {*} [concat $config_opts\ |
| 358 |
$help_subcmd_opts(config)] |
| 359 |
continue |
| 360 |
} |
| 361 |
if {![info exists help_subcmd_argtype($subcmd)]} { |
| 362 |
continue |
| 363 |
} |
| 364 |
set argtype $help_subcmd_argtype($subcmd) |
| 365 |
if {$argtype ni {module collection}} { |
| 366 |
continue |
| 367 |
} |
| 368 |
set got [completion_fish_list "module $subcmd "] |
| 369 |
if {$argtype eq {module}} { |
| 370 |
## covers every module-completion mechanism this fixture set can |
| 371 |
## produce: available modules not yet loaded (directory-style, |
| 372 |
## trailing '/'), available modules regardless of load state |
| 373 |
## (whatis/display/... style), and the one loaded module (flat |
| 374 |
## 'name/version', switch/unload style) |
| 375 |
completion_assert_any $got {bar/ baz/ foo/ advvers/ flatlcompat bar/1.0} |
| 376 |
} else { |
| 377 |
completion_assert_any $got {foo fox bar1} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
completion_fish_close |
| 382 |
unsetenv_loaded_module |
| 383 |
|
| 384 |
|
| 385 |
# |
| 386 |
# candidate words built from untrusted text cannot reach a shell expansion |
| 387 |
# step (see this file's header, and the security fix for the bash-completion |
| 388 |
# command injection this guards against a regression of) |
| 389 |
# |
| 390 |
|
| 391 |
set marker "$env(TESTSUITEDIR)/completion-injection-marker" |
| 392 |
file delete -force $marker |
| 393 |
|
| 394 |
# module avail: malicious module name read off disk |
| 395 |
|
| 396 |
# the marker path is passed through as an env var rather than embedded |
| 397 |
# directly in the filename, since a filename cannot itself contain '/' |
| 398 |
setenv_var INJMARKER $marker |
| 399 |
set evilname {evil$(touch>$INJMARKER)} |
| 400 |
file mkdir $injectdir |
| 401 |
set fd [open "$injectdir/$evilname" w] |
| 402 |
puts $fd {#%Module1.0} |
| 403 |
close $fd |
| 404 |
|
| 405 |
setenv_path_var MODULEPATH $injectdir |
| 406 |
|
| 407 |
completion_fish_start |
| 408 |
|
| 409 |
## this fixture has only one module, so a single Tab completes it inline |
| 410 |
## rather than showing a pager listing (see completion_fish_list's own |
| 411 |
## single-candidate fallback) -- and fish, like zsh, protects a candidate |
| 412 |
## containing shell metacharacters before inserting it: fish 4 wraps it in |
| 413 |
## single quotes whereas fish 3 backslash-escapes each metacharacter |
| 414 |
set got [completion_fish_list {module load }] |
| 415 |
completion_assert_no_exec $marker |
| 416 |
completion_assert_any $got [list "'$evilname'"\ |
| 417 |
[regsub -all {[$()>]} $evilname {\\&}]] |
| 418 |
|
| 419 |
completion_fish_close |
| 420 |
unsetenv_path_var MODULEPATH |
| 421 |
unsetenv_var INJMARKER |
| 422 |
file delete -force $injectdir |
| 423 |
|
| 424 |
# LOADEDMODULES: malicious already-loaded module name |
| 425 |
|
| 426 |
set evilmod [string map [list MARKERPATH $marker] {python$(touch>MARKERPATH)}] |
| 427 |
setenv_loaded_module [list gcc $evilmod] [list /fake/gcc /fake/evilmod] |
| 428 |
|
| 429 |
completion_fish_start |
| 430 |
|
| 431 |
## two candidates here (gcc and evilmod) means a real pager listing, which |
| 432 |
## completion_fish_list runs through completion_fish_strip_descriptions to |
| 433 |
## drop each candidate's "(description)" suffix -- the injected |
| 434 |
## '(touch>...)' substring is indistinguishable from that convention and |
| 435 |
## gets stripped the same way, so the surviving candidate is the text up to |
| 436 |
## its own first '(', not the full raw string (still proven never executed |
| 437 |
## by completion_assert_no_exec above) |
| 438 |
set got [completion_fish_list {module unload }] |
| 439 |
completion_assert_no_exec $marker |
| 440 |
completion_assert_contains $got [list [completion_fish_strip_descriptions $evilmod]] |
| 441 |
|
| 442 |
completion_fish_close |
| 443 |
unsetenv_loaded_module |
| 444 |
|
| 445 |
file delete -force $marker |
| 446 |
|
| 447 |
|
| 448 |
# |
| 449 |
# Cleanup |
| 450 |
# |
| 451 |
|
| 452 |
reset_test_env |
| 453 |
|
| 454 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |