Debian Codegraph @

testsuite/completion.00-init/050-tcsh-procs.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/12
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 tcsh pty to
18 # exercise the module Tab completion script exactly as a
19 # user pressing Tab would, then collect the resulting
20 # candidate word list. This mirrors 020-bash-procs.exp's
21 # completion_bash_{start,raw,list,close} set (see that
22 # file's header for why each shell gets its own), but
23 # tcsh's own line editor differs from readline/zle/fish's
24 # line editor in ways that shape almost every proc below:
25 # - Tab (bound to 'complete-word') only ever extends the
26 # typed word to the longest common prefix it can
27 # determine, ringing the bell whenever the result is
28 # still ambiguous -- even when it did insert further
29 # characters, unlike bash (which stays silent once it
30 # reaches a directory-style '/' boundary) but like
31 # bash otherwise (eg still ambiguous after inserting
32 # as much of a common prefix as it can)
33 # - listing every ambiguous match is not a second Tab
34 # away (pressing Tab again once nothing more can be
35 # inserted just rings the bell again): it is bound to
36 # a separate key, Ctrl-D ('delete-char-or-list-or-eof',
37 # which lists instead of deleting when the cursor is
38 # at the end of a non-empty line). Unlike Tab, Ctrl-D
39 # never edits the line -- it always lists every
40 # candidate matching the word exactly as currently
41 # typed, be there one or many -- so, unlike bash/zsh/
42 # fish's double/single-Tab convention,
43 # completion_tcsh_list below sends no Tab at all, only
44 # Ctrl-D: sending one first would risk its own
45 # common-prefix auto-extension silently eating part of
46 # the ambiguity a test cmdline is relying on, the same
47 # trap bash/zsh/fish's own header comments have to
48 # route around by choosing cmdline prefixes carefully
49 # -- Ctrl-D alone sidesteps it entirely, and also means
50 # this file needs no bash/zsh-style workaround for
51 # 'module config -<Tab>' silently auto-extending to
52 # '--' before a listing can be requested. When the
53 # typed word matches zero candidates, Ctrl-D prints
54 # nothing at all -- not even a bell -- so
55 # completion_tcsh_list matches that case separately
56 # and returns an empty list, rather than the listing
57 # block every other outcome produces
58 # - every submitted command line (a real Enter, not a
59 # Tab/Ctrl-D) echoes back followed by a doubled '\r'
60 # before the '\n', rather than a plain '\r\n' -- folded
61 # into every regexp below that waits on one
62 # - killing the (uncompleted or already-completed) input
63 # line with Ctrl-U redraws via a cursor-column-position
64 # plus clear-to-end-of-line escape sequence, rather
65 # than reprinting the prompt text the way bash's
66 # readline does -- so, like zsh, this file submits the
67 # now-empty line afterwards to force a real newline and
68 # a fresh, reliably matchable prompt
69 # - init/tcsh_completion.in does not shell out to the
70 # 'module' command itself the way bash/zsh do to fetch
71 # candidates: its own aliases (_module_avail,
72 # _module_loaded, ...) invoke modulecmd.tcl directly,
73 # through an absolute interpreter path baked in at
74 # './configure' time -- see 005-init_ts.exp's comment
75 # on 'tcshcompletion' for how that one hardcoded
76 # invocation gets rewritten to route through this
77 # testsuite's own $TCLSH/$MODULECMD before
78 # completion_tcsh_start ever sources it below
79 # }C%
80 #
81 ##############################################################################
82
83 # start a fresh tcsh session with the completion script under test sourced
84 # and 'module'/'ml' completion registered, ready to accept commands
85 proc completion_tcsh_start {} {
86 global tcshbin tcshcompletion completion_prompt completion_tcsh_spawn_id
87 global TCLSH MODULECMD verbose
88
89 if {$verbose > 1} {
90 send_user "\tStarting tcsh for completion test\n"
91 }
92
93 spawn $tcshbin -f
94 set completion_tcsh_spawn_id $spawn_id
95
96 # a listing as wide as the biggest fixture (eg bare 'module avail',
97 # hundreds of module names) easily exceeds Expect's default 'match_max'
98 # (2000 bytes), which silently drops the oldest bytes of the read buffer
99 # once it fills before a pattern matches -- raise it well past the
100 # largest listing this file can produce
101 match_max -i $completion_tcsh_spawn_id 200000
102
103 # a narrow (default 80-column) pty can make a long completed line wrap,
104 # which makes tcsh redraw it with extra escape sequences that can be
105 # mistaken for stray inserted characters (e.g. a trailing space) by
106 # anything matching on raw output; go wide enough that no cmdline used
107 # in these tests is ever at risk of wrapping
108 stty rows 40 columns 500 < $spawn_out(slave,name)
109
110 set spawn_id $completion_tcsh_spawn_id
111 expect {
112 -re {[>%#] $} {}
113 timeout { unresolved {completion tcsh did not start}; return }
114 }
115
116 # switch to a known, distinctive prompt first (matched with the default
117 # shell prompt pattern, since completion_tcsh_raw's own matching only
118 # works once this prompt is in effect); every setup command after this
119 # point goes through completion_tcsh_raw
120 #
121 # the command line setting it is itself 'set prompt='$completion_prompt''
122 # -- its own echoed-back input already contains that exact text, so a
123 # bare "$completion_prompt\$" pattern can match that echo mid-flight,
124 # before tcsh has even processed Enter, the moment a partial read
125 # happens to end right after it; require the same '\r+\n' fresh-line
126 # prefix completion_tcsh_raw's own matching below relies on, so this
127 # only matches the real prompt tcsh prints once the command is done
128 send -- "set prompt='$completion_prompt'\r"
129 expect {
130 -re "\r+\n$completion_prompt\$" {}
131 timeout { unresolved {completion tcsh prompt setup timed out}; return }
132 }
133
134 # a real 'module' command is needed since the completion script's own
135 # aliases shell out to modulecmd.tcl (see this file's header) to list
136 # candidate module names
137 completion_tcsh_raw "eval \"\`'$TCLSH' '$MODULECMD' tcsh autoinit\`\""
138 completion_tcsh_raw "source '$tcshcompletion'"
139 }
140
141 # send a line to the completion tcsh session and wait for the next prompt;
142 # used for setup commands whose output is not asserted on
143 proc completion_tcsh_raw {line} {
144 global completion_tcsh_spawn_id completion_prompt verbose
145
146 set spawn_id $completion_tcsh_spawn_id
147 send -- "$line\r"
148 expect {
149 -re "\r+\n$completion_prompt\$" {}
150 timeout { unresolved "completion setup command timed out: $line" }
151 }
152 if {$verbose > 1} {
153 send_user "\tcompletion> $line\n"
154 }
155 }
156
157 # send a partial command line followed by Ctrl-D (tcsh's 'list-choices'
158 # binding, see this file's header), and return the sorted, de-duplicated
159 # list of candidate words tcsh displayed
160 proc completion_tcsh_list {cmdline} {
161 global completion_tcsh_spawn_id completion_prompt completion_last_cmdline
162 global verbose
163
164 set completion_last_cmdline $cmdline
165 set spawn_id $completion_tcsh_spawn_id
166 set recmdline [completion_re_escape $cmdline]
167 set reprompt [completion_re_escape $completion_prompt]
168
169 send -- "$cmdline"
170 send -- "\x04"
171 # no fixed wait is needed before reading back the result, unlike zsh's
172 # own equivalent proc: tcsh cannot print anything -- not even a partial
173 # listing -- until the whole candidate set (however long that takes to
174 # generate, eg shelling out to modulecmd.tcl) is ready, so the 'expect'
175 # below already blocks for exactly as long as needed on its own
176 set candidates {}
177 expect {
178 -re "(?s)\r+\n(.*)\r+\n$reprompt$recmdline\$" {
179 set candidates $expect_out(1,string)
180 }
181 -re "\r+\n$reprompt$recmdline\$" {
182 # zero matching candidates: unlike every other case, Ctrl-D prints
183 # nothing at all here -- no listing line, not even a bell -- so
184 # there is no listing block for the pattern above to capture
185 # between the echoed cmdline and the redrawn prompt
186 }
187 timeout {
188 unresolved "completion listing timed out for '$cmdline'"
189 }
190 }
191 if {$verbose > 1} {
192 send_user "\tcompletion raw listing for '$cmdline':\n$candidates\n"
193 }
194
195 # clear the (untouched -- Ctrl-D never edits the line) input line so the
196 # session is ready to reuse; tcsh's Ctrl-U redraws via a cursor-column-
197 # position plus clear-to-end-of-line escape rather than reprinting the
198 # prompt text (see this file's header), so submit the now-empty line to
199 # force a real newline + fresh prompt we can reliably match on
200 send -- "\x15\r"
201 expect -re "\r+\n$reprompt\$"
202
203 # collapse the multi-column listing into a flat, sorted word list
204 set words {}
205 foreach word [split $candidates] {
206 if {$word ne {}} {
207 lappend words $word
208 }
209 }
210 return [lsort -unique $words]
211 }
212
213 # send a partial command line followed by a single Tab, and return the text
214 # tcsh inserted inline -- a bell ('\a') if the result is still ambiguous
215 # (whether or not further characters got inserted alongside it, see this
216 # file's header) or nothing at all could be completed, otherwise the extra
217 # characters tcsh appended. Used to check unique, unambiguous completions --
218 # eg that a directory-style entry gets completed with a trailing '/' and no
219 # trailing space. cmdline must be a prefix with exactly one match, or with a
220 # common prefix short enough that no further Tab is needed to reach it,
221 # otherwise a single Tab may extend by less than a full match
222 proc completion_tcsh_inline {cmdline} {
223 global completion_tcsh_spawn_id completion_prompt completion_last_cmdline
224 global verbose
225
226 set completion_last_cmdline $cmdline
227 set spawn_id $completion_tcsh_spawn_id
228 set recmdline [completion_re_escape $cmdline]
229 set reprompt [completion_re_escape $completion_prompt]
230
231 send -- "$cmdline\t"
232 # let every byte tcsh is going to insert land in the pty buffer before
233 # reading it: an 'expect' pattern ending in a lazy '(.*)$' would
234 # otherwise match the instant the plain cmdline echo arrives, racing
235 # ahead of e.g. a trailing space that is still in flight
236 after 300
237 set suffix {}
238 expect {
239 -re "${recmdline}(.*)\$" {
240 set suffix $expect_out(1,string)
241 }
242 timeout {
243 unresolved "completion inline insert timed out for '$cmdline'"
244 }
245 }
246 if {$verbose > 1} {
247 send_user "\tcompletion inline insert for '$cmdline': \[$suffix\]\n"
248 }
249
250 # clear the (possibly completed) input line so the session is ready to
251 # reuse, same technique as completion_tcsh_list
252 send -- "\x15\r"
253 expect -re "\r+\n$reprompt\$"
254
255 return $suffix
256 }
257
258 proc completion_tcsh_close {} {
259 global completion_tcsh_spawn_id
260
261 if {[info exists completion_tcsh_spawn_id]} {
262 set spawn_id $completion_tcsh_spawn_id
263 catch {send -- "exit\r"}
264 catch {expect eof}
265 catch {close -i $completion_tcsh_spawn_id}
266 catch {wait -i $completion_tcsh_spawn_id}
267 unset completion_tcsh_spawn_id
268 }
269 }
270
271 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: