| 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: Test procedures |
| 13 |
# Command: |
| 14 |
# Sub-Command: |
| 15 |
# |
| 16 |
# Comment: %C{ |
| 17 |
# Defines the test procedures driving a real bash pty to |
| 18 |
# exercise the module/ml Tab completion script exactly |
| 19 |
# as a user pressing Tab would, then collect the |
| 20 |
# resulting candidate word list. Other shells get their |
| 21 |
# own completion_<shell>_{start,raw,list,close} set, |
| 22 |
# since how a shell is scripted into pty interactive |
| 23 |
# completion differs from one to the next |
| 24 |
# }C% |
| 25 |
# |
| 26 |
############################################################################## |
| 27 |
|
| 28 |
# start a fresh bash session with the completion script under test sourced |
| 29 |
# and 'module'/'ml' completion registered, ready to accept commands |
| 30 |
proc completion_bash_start {} { |
| 31 |
global bashbin bashcompletion completion_prompt completion_bash_spawn_id |
| 32 |
global TCLSH MODULECMD verbose |
| 33 |
|
| 34 |
if {$verbose > 1} { |
| 35 |
send_user "\tStarting bash for completion test\n" |
| 36 |
} |
| 37 |
|
| 38 |
spawn $bashbin --norc --noprofile |
| 39 |
set completion_bash_spawn_id $spawn_id |
| 40 |
|
| 41 |
# a narrow (default 80-column) pty can make a long completed line wrap, |
| 42 |
# which makes readline redraw it with extra escape sequences that can be |
| 43 |
# mistaken for stray inserted characters (e.g. a trailing space) by |
| 44 |
# anything matching on raw output; go wide enough that no cmdline used |
| 45 |
# in these tests is ever at risk of wrapping |
| 46 |
stty rows 40 columns 500 < $spawn_out(slave,name) |
| 47 |
|
| 48 |
set spawn_id $completion_bash_spawn_id |
| 49 |
expect { |
| 50 |
-re {[$#] $} {} |
| 51 |
timeout { unresolved {completion bash did not start}; return } |
| 52 |
} |
| 53 |
|
| 54 |
# switch to a known, distinctive prompt first (matched with the default |
| 55 |
# shell prompt pattern, since completion_bash_raw's own matching only |
| 56 |
# works once this prompt is in effect); every setup command after this |
| 57 |
# point goes through completion_bash_raw |
| 58 |
send -- "bind 'set enable-bracketed-paste off' 2>/dev/null\r" |
| 59 |
expect -re {[$#] $} |
| 60 |
# readline asks "Display all N possibilities?" instead of listing outright |
| 61 |
# once matches exceed this threshold (100 by default); some completions |
| 62 |
# here (e.g. bare 'ml') list every sub-command and go well past it |
| 63 |
send -- "bind 'set completion-query-items 0'\r" |
| 64 |
expect -re {[$#] $} |
| 65 |
send -- "unset PROMPT_COMMAND\r" |
| 66 |
expect -re {[$#] $} |
| 67 |
send -- "PS1='$completion_prompt'\r" |
| 68 |
expect { |
| 69 |
-re "$completion_prompt\$" {} |
| 70 |
timeout { unresolved {completion bash prompt setup timed out}; return } |
| 71 |
} |
| 72 |
|
| 73 |
# a real 'module' command is needed since the completion script shells |
| 74 |
# out to it (module avail/spider) to list candidate module names |
| 75 |
completion_bash_raw "eval \"\$('$TCLSH' '$MODULECMD' bash autoinit)\"" |
| 76 |
completion_bash_raw "source '$bashcompletion'" |
| 77 |
completion_bash_raw {complete -o default -F _module module} |
| 78 |
completion_bash_raw {type -t _ml >/dev/null 2>&1 && complete -o default -F _ml ml} |
| 79 |
} |
| 80 |
|
| 81 |
# send a line to the completion bash session and wait for the next prompt; |
| 82 |
# used for setup commands whose output is not asserted on |
| 83 |
proc completion_bash_raw {line} { |
| 84 |
global completion_bash_spawn_id completion_prompt verbose |
| 85 |
|
| 86 |
set spawn_id $completion_bash_spawn_id |
| 87 |
send -- "$line\r" |
| 88 |
expect { |
| 89 |
-re "\r\n$completion_prompt\$" {} |
| 90 |
timeout { unresolved "completion setup command timed out: $line" } |
| 91 |
} |
| 92 |
if {$verbose > 1} { |
| 93 |
send_user "\tcompletion> $line\n" |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
# send a partial command line followed by a double-Tab (forcing readline to |
| 98 |
# list every ambiguous match), and return the sorted, de-duplicated list of |
| 99 |
# candidate words readline displayed. cmdline must be a prefix that has no |
| 100 |
# unique completion (e.g. ends with a space) so the listing always fires |
| 101 |
proc completion_bash_list {cmdline} { |
| 102 |
global completion_bash_spawn_id completion_prompt completion_last_cmdline |
| 103 |
global verbose |
| 104 |
|
| 105 |
set completion_last_cmdline $cmdline |
| 106 |
set spawn_id $completion_bash_spawn_id |
| 107 |
set recmdline [completion_re_escape $cmdline] |
| 108 |
set reprompt [completion_re_escape $completion_prompt] |
| 109 |
|
| 110 |
send -- "$cmdline\t\t" |
| 111 |
set candidates {} |
| 112 |
expect { |
| 113 |
-re "\r\n(.*)\r\n$reprompt$recmdline\$" { |
| 114 |
set candidates $expect_out(1,string) |
| 115 |
} |
| 116 |
timeout { |
| 117 |
unresolved "completion listing timed out for '$cmdline'" |
| 118 |
} |
| 119 |
} |
| 120 |
if {$verbose > 1} { |
| 121 |
send_user "\tcompletion raw listing for '$cmdline':\n$candidates\n" |
| 122 |
} |
| 123 |
|
| 124 |
# clear the (uncompleted) input line so the session is ready to reuse. |
| 125 |
# readline redraws a cleared line via cursor-movement escapes rather than |
| 126 |
# reprinting the prompt text, so submit the now-empty line to force a |
| 127 |
# real newline + fresh prompt we can reliably match on |
| 128 |
send -- "\025\r" |
| 129 |
expect -re "\r\n$reprompt\$" |
| 130 |
|
| 131 |
# collapse the multi-column listing into a flat, sorted word list |
| 132 |
set words {} |
| 133 |
foreach word [split $candidates] { |
| 134 |
if {$word ne {}} { |
| 135 |
lappend words $word |
| 136 |
} |
| 137 |
} |
| 138 |
return [lsort -unique $words] |
| 139 |
} |
| 140 |
|
| 141 |
# send a partial command line followed by a single Tab, and return the text |
| 142 |
# readline inserted inline (no listing involved). Used to check unique, |
| 143 |
# unambiguous completions -- e.g. that a directory-style entry gets |
| 144 |
# completed with a trailing '/' and no trailing space (see |
| 145 |
# 'compopt -o nospace' in init/bash_completion). cmdline must be a prefix |
| 146 |
# with exactly one match, otherwise the single Tab may ring the bell instead |
| 147 |
# of inserting text, or extend by less than a full match |
| 148 |
proc completion_bash_inline {cmdline} { |
| 149 |
global completion_bash_spawn_id completion_prompt completion_last_cmdline |
| 150 |
global verbose |
| 151 |
|
| 152 |
set completion_last_cmdline $cmdline |
| 153 |
set spawn_id $completion_bash_spawn_id |
| 154 |
set recmdline [completion_re_escape $cmdline] |
| 155 |
set reprompt [completion_re_escape $completion_prompt] |
| 156 |
|
| 157 |
send -- "$cmdline\t" |
| 158 |
# let every byte readline is going to insert land in the pty buffer |
| 159 |
# before reading it: an 'expect' pattern ending in a lazy '(.*)$' would |
| 160 |
# otherwise match the instant the plain cmdline echo arrives, racing |
| 161 |
# ahead of e.g. a trailing space that is still in flight |
| 162 |
after 300 |
| 163 |
set suffix {} |
| 164 |
expect { |
| 165 |
-re "${recmdline}(.*)\$" { |
| 166 |
set suffix $expect_out(1,string) |
| 167 |
} |
| 168 |
timeout { |
| 169 |
unresolved "completion inline insert timed out for '$cmdline'" |
| 170 |
} |
| 171 |
} |
| 172 |
if {$verbose > 1} { |
| 173 |
send_user "\tcompletion inline insert for '$cmdline': \[$suffix\]\n" |
| 174 |
} |
| 175 |
|
| 176 |
# clear the (possibly completed) input line so the session is ready to |
| 177 |
# reuse, same technique as completion_bash_list |
| 178 |
send -- "\025\r" |
| 179 |
expect -re "\r\n$reprompt\$" |
| 180 |
|
| 181 |
return $suffix |
| 182 |
} |
| 183 |
|
| 184 |
proc completion_bash_close {} { |
| 185 |
global completion_bash_spawn_id |
| 186 |
|
| 187 |
if {[info exists completion_bash_spawn_id]} { |
| 188 |
set spawn_id $completion_bash_spawn_id |
| 189 |
catch {send -- "exit\r"} |
| 190 |
catch {expect eof} |
| 191 |
catch {close -i $completion_bash_spawn_id} |
| 192 |
catch {wait -i $completion_bash_spawn_id} |
| 193 |
unset completion_bash_spawn_id |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |