blob: 89a8abc3b2e7500f0a598b2cff6a65bfaa6e30e1 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02002" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal2.vim and test_terminal3.vim for further tests.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02005source check.vim
6CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02007
8source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01009source screendump.vim
Bram Moolenaar91689ea2020-05-11 22:04:53 +020010source mouse.vim
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020011source term_util.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020012
Bram Moolenaarb81bc772017-08-11 22:45:01 +020013let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020014let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020015
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020016func Test_terminal_basic()
ichizokae1bd872022-01-20 14:57:29 +000017 call test_override('vterm_title', 1)
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020018 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020019 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020020
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020021 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020022 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020023 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020024 call assert_match('%aR[^\n]*running]', execute('ls R'))
25 call assert_notmatch('%[^\n]*running]', execute('ls F'))
26 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020027 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020028
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020029 call StopShellInTerminal(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020030 call assert_equal('n', mode())
31 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020032 call assert_match('%aF[^\n]*finished]', execute('ls F'))
33 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
34 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035
Bram Moolenaar94053a52017-08-01 21:44:33 +020036 " closing window wipes out the terminal buffer a with finished job
37 close
38 call assert_equal("", bufname(buf))
39
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020040 au! TerminalOpen
ichizokae1bd872022-01-20 14:57:29 +000041 call test_override('ALL', 0)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020042 unlet g:job
43endfunc
44
Bram Moolenaar00806bc2020-11-05 19:36:38 +010045func Test_terminal_no_name()
46 let buf = Run_shell_in_terminal({})
47 call assert_match('^!', bufname(buf))
48 0file
49 call assert_equal("", bufname(buf))
50 call assert_match('\[No Name\]', execute('file'))
51 call StopShellInTerminal(buf)
Bram Moolenaar00806bc2020-11-05 19:36:38 +010052endfunc
53
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020054func Test_terminal_TerminalWinOpen()
55 au TerminalWinOpen * let b:done = 'yes'
56 let buf = Run_shell_in_terminal({})
57 call assert_equal('yes', b:done)
58 call StopShellInTerminal(buf)
59 " closing window wipes out the terminal buffer with the finished job
60 close
61
62 if has("unix")
63 terminal ++hidden ++open sleep 1
64 sleep 1
65 call assert_fails("echo b:done", 'E121:')
66 endif
67
68 au! TerminalWinOpen
69endfunc
70
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074
75 setlocal modifiable
76 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010077 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020078 undo
79
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020080 exe buf . 'bwipe'
81 unlet g:job
82endfunc
83
Bram Moolenaar5b868a82019-02-25 06:11:53 +010084func Test_terminal_paste_register()
85 let @" = "text to paste"
86
87 let buf = Run_shell_in_terminal({})
88 " Wait for the shell to display a prompt
89 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
90
91 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
92 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020093 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010094
95 exe buf . 'bwipe!'
96 unlet g:job
97endfunc
98
Yee Cheng Chin42826332022-10-10 11:46:16 +010099func Test_terminal_unload_buffer()
100 let buf = Run_shell_in_terminal({})
101 call assert_fails(buf . 'bunload', 'E948:')
102 exe buf . 'bunload!'
103 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
104 call assert_equal("", bufname(buf))
105
106 unlet g:job
107endfunc
108
Bram Moolenaar94053a52017-08-01 21:44:33 +0200109func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200110 let buf = Run_shell_in_terminal({})
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100111 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200112 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200113 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200114 call assert_equal("", bufname(buf))
115
116 unlet g:job
117endfunc
118
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100119" Test that using ':confirm bwipe' on terminal works
120func Test_terminal_confirm_wipe_buffer()
121 CheckUnix
122 CheckNotGui
123 CheckFeature dialog_con
124 let buf = Run_shell_in_terminal({})
125 call assert_fails(buf . 'bwipe', 'E948:')
126 call feedkeys('n', 'L')
127 call assert_fails('confirm ' .. buf .. 'bwipe', 'E517:')
128 call assert_equal(buf, bufnr())
129 call assert_equal(1, &modified)
130 call feedkeys('y', 'L')
131 exe 'confirm ' .. buf .. 'bwipe'
132 call assert_notequal(buf, bufnr())
133 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
134 call assert_equal("", bufname(buf))
135
136 unlet g:job
137endfunc
138
139" Test that using :b! will hide the terminal
140func Test_terminal_goto_buffer()
141 let buf_mod = bufnr()
142 let buf_term = Run_shell_in_terminal({})
143 call assert_equal(buf_term, bufnr())
144 call assert_fails(buf_mod . 'b', 'E948:')
145 exe buf_mod . 'b!'
146 call assert_equal(buf_mod, bufnr())
147 call assert_equal('run', job_status(g:job))
148 call assert_notequal('', bufname(buf_term))
149 exec buf_mod .. 'bwipe!'
150 exec buf_term .. 'bwipe!'
151
152 unlet g:job
153endfunc
154
155" Test that using ':confirm :b' will kill terminal
156func Test_terminal_confirm_goto_buffer()
157 CheckUnix
158 CheckNotGui
159 CheckFeature dialog_con
160 let buf_mod = bufnr()
161 let buf_term = Run_shell_in_terminal({})
162 call feedkeys('n', 'L')
163 exe 'confirm ' .. buf_mod .. 'b'
164 call assert_equal(buf_term, bufnr())
165 call feedkeys('y', 'L')
166 exec 'confirm ' .. buf_mod .. 'b'
167 call assert_equal(buf_mod, bufnr())
168 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
169 call assert_equal("", bufname(buf_term))
170 exec buf_mod .. 'bwipe!'
171
172 unlet g:job
173endfunc
174
175" Test that using :close! will hide the terminal
176func Test_terminal_close_win()
177 let buf = Run_shell_in_terminal({})
178 call assert_equal(buf, bufnr())
179 call assert_fails('close', 'E948:')
180 close!
181 call assert_notequal(buf, bufnr())
182 call assert_equal('run', job_status(g:job))
183 call assert_notequal('', bufname(buf))
184 exec buf .. 'bwipe!'
185
186 unlet g:job
187endfunc
188
189" Test that using ':confirm close' will kill terminal
190func Test_terminal_confirm_close_win()
191 CheckUnix
192 CheckNotGui
193 CheckFeature dialog_con
194 let buf = Run_shell_in_terminal({})
195 call feedkeys('n', 'L')
196 confirm close
197 call assert_equal(buf, bufnr())
198 call feedkeys('y', 'L')
199 confirm close
200 call assert_notequal(buf, bufnr())
201 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
202 call assert_equal("", bufname(buf))
203
204 unlet g:job
205endfunc
206
207" Test that using :quit! will kill the terminal
208func Test_terminal_quit()
209 let buf = Run_shell_in_terminal({})
210 call assert_equal(buf, bufnr())
211 call assert_fails('quit', 'E948:')
212 quit!
213 call assert_notequal(buf, bufnr())
214 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100215 call assert_equal("", bufname(buf))
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100216
217 unlet g:job
218endfunc
219
220" Test that using ':confirm quit' will kill terminal
221func Test_terminal_confirm_quit()
222 CheckUnix
223 CheckNotGui
224 CheckFeature dialog_con
225 let buf = Run_shell_in_terminal({})
226 call feedkeys('n', 'L')
227 confirm quit
228 call assert_equal(buf, bufnr())
229 call feedkeys('y', 'L')
230 confirm quit
231 call assert_notequal(buf, bufnr())
232 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
233
234 unlet g:job
235endfunc
236
237" Test :q or :next
238
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200239func Test_terminal_split_quit()
240 let buf = Run_shell_in_terminal({})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200241 split
242 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200243 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200244 sleep 50m
245 call assert_equal('run', job_status(g:job))
246
247 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200248 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200249
Yee Cheng Chin42826332022-10-10 11:46:16 +0100250 call assert_equal("", bufname(buf))
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200251 unlet g:job
252endfunc
253
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100254func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200255 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200256 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200257 quit
258 for nr in range(1, winnr('$'))
259 call assert_notequal(winbufnr(nr), buf)
260 endfor
261 call assert_true(bufloaded(buf))
262 call assert_true(buflisted(buf))
263
264 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200265 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200266 exe buf . 'bwipe'
267
268 unlet g:job
269endfunc
270
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100271func Test_terminal_hide_buffer_job_finished()
272 term echo hello
273 let buf = bufnr()
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100274 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Yee Cheng Chin42826332022-10-10 11:46:16 +0100275
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100276 call assert_true(bufloaded(buf))
277 call assert_true(buflisted(buf))
Yee Cheng Chin42826332022-10-10 11:46:16 +0100278
279 " Test :hide
280 hide
281 call assert_true(bufloaded(buf))
282 call assert_true(buflisted(buf))
283 split
284 exe buf .. 'buf'
285 call assert_equal(buf, bufnr())
286
287 " Test bufhidden, which exercises a different code path
288 setlocal bufhidden=hide
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100289 edit Xasdfasdf
290 call assert_true(bufloaded(buf))
291 call assert_true(buflisted(buf))
292 exe buf .. 'buf'
293 call assert_equal(buf, bufnr())
294 setlocal bufhidden=
Yee Cheng Chin42826332022-10-10 11:46:16 +0100295
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100296 edit Xasdfasdf
297 call assert_false(bufloaded(buf))
298 call assert_false(buflisted(buf))
299 bwipe Xasdfasdf
300endfunc
301
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000302func Test_terminal_rename_buffer()
303 let cmd = Get_cat_123_cmd()
304 let buf = term_start(cmd, {'term_name': 'foo'})
305 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
306 call assert_equal('foo', bufname())
307 call assert_match('foo.*finished', execute('ls'))
308 file bar
309 call assert_equal('bar', bufname())
310 call assert_match('bar.*finished', execute('ls'))
311 exe 'bwipe! ' .. buf
Christian Brabandt84bc00e2023-07-13 11:45:54 +0200312 call delete('Xtext')
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000313endfunc
314
Bram Moolenaar1e115362019-01-09 23:01:02 +0100315func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200316 exe g:buf . 'bwipe!'
317 let g:buf = 0
318endfunc
319
Bram Moolenaar9d189612017-09-09 18:11:00 +0200320func Get_cat_123_cmd()
321 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100322 if !has('conpty')
Milly4f5681d2024-10-20 11:06:00 +0200323 return 'cmd /D /c "cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100324 else
325 " When clearing twice, extra sequence is not output.
Milly4f5681d2024-10-20 11:06:00 +0200326 return 'cmd /D /c "cls && cls && color 2 && echo 123"'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100327 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200328 else
329 call writefile(["\<Esc>[32m123"], 'Xtext')
330 return "cat Xtext"
331 endif
332endfunc
333
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200334func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200335 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200336 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
337 let g:job = term_getjob(g:buf)
338
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200339 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
340 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200341 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100342 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200343 call delete('Xtext')
344endfunc
345
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200346func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200347 let l = term_scrape(a:buf, 0)
348 call assert_true(len(l) == 0)
349 let l = term_scrape(a:buf, 999)
350 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200351 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200352 call assert_true(len(l) > 0)
353 call assert_equal('1', l[0].chars)
354 call assert_equal('2', l[1].chars)
355 call assert_equal('3', l[2].chars)
356 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200357 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
358 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100359 if has('win32')
360 " On Windows 'background' always defaults to dark, even though the terminal
361 " may use a light background. Therefore accept both white and black.
362 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200363 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100364 if &background == 'light'
365 call assert_equal('#ffffff', l[0].bg)
366 else
367 call assert_equal('#000000', l[0].bg)
368 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200369 endif
370
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200371 let l = term_getline(a:buf, -1)
372 call assert_equal('', l)
373 let l = term_getline(a:buf, 0)
374 call assert_equal('', l)
375 let l = term_getline(a:buf, 999)
376 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200377 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200378 call assert_equal('123', l)
379endfunc
380
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200381func Test_terminal_scrape_123()
382 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200383 let buf = term_start(cmd)
384
385 let termlist = term_list()
386 call assert_equal(1, len(termlist))
387 call assert_equal(buf, termlist[0])
388
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200389 " Nothing happens with invalid buffer number
390 call term_wait(1234)
391
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200392 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200393 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200394 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200395 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200396 call Check_123(buf)
397
398 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100399 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200400 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200401 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200402 call Check_123(buf)
403
404 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200405 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200406endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200408func Test_terminal_scrape_multibyte()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100409 call writefile(["léttまrs"], 'Xtext', 'D')
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200410 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200411 " Run cmd with UTF-8 codepage to make the type command print the expected
412 " multibyte characters.
Milly4f5681d2024-10-20 11:06:00 +0200413 let buf = term_start("cmd /D /K chcp 65001")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100414 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200415 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100416 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200417 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100418 let buf = term_start("cat Xtext")
419 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200420 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200421
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100422 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
423 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200424 call assert_true(len(l) >= 7)
425 call assert_equal('l', l[0].chars)
426 call assert_equal('é', l[1].chars)
427 call assert_equal(1, l[1].width)
428 call assert_equal('t', l[2].chars)
429 call assert_equal('t', l[3].chars)
430 call assert_equal('ま', l[4].chars)
431 call assert_equal(2, l[4].width)
432 call assert_equal('r', l[5].chars)
433 call assert_equal('s', l[6].chars)
434
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100435 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200436 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200437 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200438
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100439 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200440endfunc
441
Bram Moolenaar8b896142020-08-02 15:05:05 +0200442func Test_terminal_one_column()
443 " This creates a terminal, displays a double-wide character and makes the
444 " window one column wide. This used to cause a crash.
445 let width = &columns
446 botright vert term
447 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200448 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200449 exe "set columns=" .. (width / 2)
450 redraw
451 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200452 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200453 exe "set columns=" .. width
454 exe buf . 'bwipe!'
455endfunc
456
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200457func Test_terminal_scroll()
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100458 call writefile(range(1, 200), 'Xtext', 'D')
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200459 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200460 let cmd = 'cmd /D /c "type Xtext"'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200461 else
462 let cmd = "cat Xtext"
463 endif
464 let buf = term_start(cmd)
465
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100466 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200467 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200468 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200469
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100470 " wait until the scrolling stops
471 while 1
472 let scrolled = buf->term_getscrolled()
473 sleep 20m
474 if scrolled == buf->term_getscrolled()
475 break
476 endif
477 endwhile
478
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200479 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200480 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200481 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200482 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200483 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200484 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200485
486 exe buf . 'bwipe'
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200487endfunc
488
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200489func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200490 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200491 set termwinscroll=100
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100492 call writefile(range(150), 'Xtext', 'D')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200493 if has('win32')
494 call term_sendkeys(buf, "type Xtext\<CR>")
495 else
496 call term_sendkeys(buf, "cat Xtext\<CR>")
497 endif
498 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200499 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200500 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200501 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200502 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200503
Milly6d5f4a02024-10-22 23:17:45 +0200504 " When 'termwinscroll' becomes small, the scrollback should become small.
505 set termwinscroll=20
506 call term_sendkeys(buf, "echo set20\<CR>")
507 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set20') >= 0)})
508 let lines = line('$')
509 call assert_inrange(19, 20, lines)
510
511 " When 'termwinscroll' under 10 which means 10% of it will be 0,
512 " the scrollback should become small.
513 set termwinscroll=1
514 call term_sendkeys(buf, "echo set1\<CR>")
515 call WaitForAssert({-> assert_true([term_getline(buf, rows - 1), term_getline(buf, rows - 2)]->index('set1') >= 0)})
516 let lines = line('$')
517 call assert_inrange(1, 2, lines)
518
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200519 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200520 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200521 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100522endfunc
523
524func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200525 " tail -f only works on Unix
526 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100527
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100528 call writefile(range(50), 'Xtext', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100529 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100530 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100531 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100532 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100533 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
534 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100535 \ 'call feedkeys("\<C-W>N", "xt")',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100536 \ ], 'XTest_postponed', 'D')
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100537 let buf = RunVimInTerminal('-S XTest_postponed', {})
538 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100539 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100540
541 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200542 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100543 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100544 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100545
546 " Back to Terminal-Job mode, text will scroll and show the extra line.
547 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100548 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100549
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100550 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100551 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200552 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100553 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100554 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200555 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100556 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100557 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
558
559 " check type of term_sendkeys() return value
560 echo type(tsk_ret)
561
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100562 call StopVimInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200563endfunc
564
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100565" Run diff on two dumps with different size.
566func Test_terminal_dumpdiff_size()
567 call assert_equal(1, winnr('$'))
568 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
569 call assert_equal(2, winnr('$'))
570 call assert_match('Test_incsearch_search_01.dump', getline(10))
571 call assert_match(' +++++$', getline(11))
572 call assert_match('Test_popup_command_01.dump', getline(31))
573 call assert_equal(repeat('+', 75), getline(30))
574 quit
575endfunc
576
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200577func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200578 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200579
Bram Moolenaarb2412082017-08-20 18:09:14 +0200580 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200581 let size = term_getsize('')
582 bwipe!
583 call assert_equal(5, size[0])
584
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200585 call term_start(cmd, {'term_rows': 6})
586 let size = term_getsize('')
587 bwipe!
588 call assert_equal(6, size[0])
589
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200590 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200591 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200592 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200593
594 call term_setsize('', 6, 0)
595 call assert_equal([6, 33], term_getsize(''))
596
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200597 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200598 call assert_equal([6, 35], term_getsize(''))
599
600 call term_setsize('', 7, 30)
601 call assert_equal([7, 30], term_getsize(''))
602
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200603 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200604 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200605
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200606 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
607 let size = term_getsize('')
608 bwipe!
609 call assert_equal([6, 36], size)
610
Bram Moolenaarb2412082017-08-20 18:09:14 +0200611 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200612 let size = term_getsize('')
613 bwipe!
614 call assert_equal(20, size[1])
615
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200616 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200617 let size = term_getsize('')
618 bwipe!
619 call assert_equal(26, size[1])
620
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200621 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200622 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200623 let size = term_getsize('')
624 bwipe!
625 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200626
627 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
628 let size = term_getsize('')
629 bwipe!
630 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200631
Bram Moolenaar5300be62021-11-13 10:27:40 +0000632 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
633 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100634 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000635
Kenta Satoc28e7a22023-05-08 18:26:03 +0100636 call assert_fails("call term_start(cmd, {'term_cols': -1})", 'E475:')
637 call assert_fails("call term_start(cmd, {'term_cols': 1001})", 'E475:')
638 call assert_fails("call term_start(cmd, {'term_cols': 10.0})", 'E805:')
639
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200640 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200641endfunc
642
Bram Moolenaareba13e42021-02-23 17:47:23 +0100643func Test_terminal_zero_height()
644 split
645 wincmd j
646 anoremenu 1.1 WinBar.test :
647 terminal ++curwin
648 wincmd k
649 wincmd _
650 redraw
651
652 call term_sendkeys(bufnr(), "exit\r")
653 bwipe!
654endfunc
655
Bram Moolenaarda43b612017-08-11 22:27:50 +0200656func Test_terminal_curwin()
657 let cmd = Get_cat_123_cmd()
658 call assert_equal(1, winnr('$'))
659
Bram Moolenaarb1009092020-05-31 16:04:42 +0200660 split Xdummy
661 call setline(1, 'dummy')
662 write
663 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200664 exe 'terminal ++curwin ' . cmd
665 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200666 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200667 bwipe!
668
Bram Moolenaarb1009092020-05-31 16:04:42 +0200669 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200670 call term_start(cmd, {'curwin': 1})
671 call assert_equal(2, winnr('$'))
672 bwipe!
673
Bram Moolenaarb1009092020-05-31 16:04:42 +0200674 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200675 call setline(1, 'change')
676 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
677 call assert_equal(2, winnr('$'))
678 exe 'terminal! ++curwin ' . cmd
679 call assert_equal(2, winnr('$'))
680 bwipe!
681
Bram Moolenaarb1009092020-05-31 16:04:42 +0200682 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200683 call setline(1, 'change')
684 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
685 call assert_equal(2, winnr('$'))
686 bwipe!
687
Bram Moolenaarb1009092020-05-31 16:04:42 +0200688 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200689 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200690 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200691 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200692endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200693
Bram Moolenaarff546792017-11-21 14:47:57 +0100694func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200695 if s:python != ''
696 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200697 " 500 was not enough for Travis
698 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200699 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200700 echo 'This will take five seconds...'
701 let waittime = 2000
702 if has('win32')
703 let cmd = $windir . '\system32\timeout.exe 1'
704 else
705 let cmd = 'sleep 1'
706 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200707 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100708 return [cmd, waittime]
709endfunc
710
711func Test_terminal_finish_open_close()
712 call assert_equal(1, winnr('$'))
713
714 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200715
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100716 " shell terminal closes automatically
717 terminal
718 let buf = bufnr('%')
719 call assert_equal(2, winnr('$'))
720 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200721 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200722 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200723 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100724
725 " shell terminal that does not close automatically
726 terminal ++noclose
727 let buf = bufnr('%')
728 call assert_equal(2, winnr('$'))
729 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200730 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200731 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100732 call assert_equal(2, winnr('$'))
733 quit
734 call assert_equal(1, winnr('$'))
735
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200736 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200737 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200738 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200739 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200740
741 call term_start(cmd, {'term_finish': 'close'})
742 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200743 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200744 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200745 call assert_equal(1, winnr('$'))
746
747 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200748 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200749 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200750 bwipe
751
752 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200753 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200754 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200755 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200756
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200757 exe 'terminal ++hidden ++open ' . cmd
758 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200759 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200760 bwipe
761
762 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
763 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200764 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200765 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200766
767 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
768 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
769 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
770 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
771
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100772 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d | let g:result = "opened the buffer in a window"'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200773 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200774 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200775 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100776 call assert_equal('opened the buffer in a window', g:result)
777 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200778 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200779endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200780
781func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100782 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +0200783 let cmd = 'cmd /D /c cd'
Bram Moolenaar077ff432019-10-28 00:42:21 +0100784 else
785 CheckExecutable pwd
786 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200787 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100788 call mkdir('Xtermdir')
789 let buf = term_start(cmd, {'cwd': 'Xtermdir'})
Bram Moolenaaree7c8d92022-09-22 12:57:06 +0100790 " if the path is very long it may be split over two lines, join them
791 " together
792 call WaitForAssert({-> assert_equal('Xtermdir', fnamemodify(getline(1) .. getline(2), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200793
794 exe buf . 'bwipe'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100795 call delete('Xtermdir', 'rf')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200796endfunc
797
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200798func Test_terminal_cwd_failure()
799 " Case 1: Provided directory is not actually a directory. Attempt to make
800 " the file executable as well.
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100801 call writefile([], 'Xtcfile', 'D')
Dominique Pelle91a874e2022-09-03 10:59:32 +0100802 call setfperm('Xtcfile', 'rwx------')
803 call assert_fails("call term_start(&shell, {'cwd': 'Xtcfile'})", 'E475:')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200804
805 " Case 2: Directory does not exist.
806 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
807
808 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100809 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200810 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100811 call mkdir('XdirNoAccess', '', '0600')
812 " return early if the directory permissions could not be set properly
813 if getfperm('XdirNoAccess')[2] == 'x'
814 call delete('XdirNoAccess', 'rf')
815 return
816 endif
817 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
818 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200819 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200820endfunc
821
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100822func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200823 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200824 call s:test_environment("VIM_SERVERNAME", v:servername)
825endfunc
826
827func Test_terminal_version()
828 call s:test_environment("VIM_TERMINAL", string(v:version))
829endfunc
830
831func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100832 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100833 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200834 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100835 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200836 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100837 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200838 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100839 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200840 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200841 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200842 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100843
Bram Moolenaar012eb662018-03-13 17:55:27 +0100844 exe buf . 'bwipe'
845 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100846endfunc
847
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200848func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100849 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200850 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200851 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100852 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100853 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100854 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100855 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100856 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200857 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200858 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200859 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200860
Bram Moolenaar012eb662018-03-13 17:55:27 +0100861 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200862endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200863
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200864func Test_terminal_list_args()
865 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100866 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200867 exe buf . 'bwipe!'
868 call assert_equal("", bufname(buf))
869endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200870
871func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100872 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100873 let buf = term_start(&shell)
Zdenek Dohnaldd2dfb32022-02-23 14:25:17 +0000874 " Starting a terminal can be slow, esp. on busy CI machines.
875 let wait_time = 7500
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100876 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100877 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200878 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100879 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200880 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100881 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100882 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100883 " NPFS is used in Windows, nonblocking mode does not work properly.
884 elseif has('win32')
885 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200886 else
887 let len = 5000
888 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200889
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100890 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100891 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100892 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200893 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100894 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200895
896 " On MS-Windows there is an extra empty line below "done". Find "done" in
897 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100898 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100899 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100900 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200901 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100902 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200903 endif
904 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200905
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100906 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200907 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200908 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200909 bwipe
910endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200911
912func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100913 " TODO: enable once writing to stdin works on MS-Windows
914 CheckNotMSWindows
915 CheckExecutable wc
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100916 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100917
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200918 call setline(1, ['one', 'two', 'three'])
919 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200920 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200921 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200922 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100923 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200924
925 call setline(1, ['one', 'two', 'three', 'four'])
926 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200927 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200928 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200929 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100930 %bwipe!
931endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200932
Bram Moolenaar21109272020-01-30 16:27:20 +0100933func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200934 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100935 let g:test_is_flaky = 1
Bram Moolenaardada6d22017-09-02 17:18:35 +0200936
Bram Moolenaar21109272020-01-30 16:27:20 +0100937 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200938 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100939 " MS-Windows echoes the input, Unix doesn't.
940 if has('win32')
941 call WaitFor({-> getline('$') =~ 'exit(123)'})
942 call assert_equal('hello', getline(line('$') - 1))
943 else
944 call WaitFor({-> getline('$') =~ 'hello'})
945 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200946 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100947 let exitval = bufnr()->term_getjob()->job_info().exitval
948 if !has('win32')
949 call assert_equal(123, exitval)
950 else
951 " python 3.13 on Windows returns exit code 1
952 " older versions returned correctly exit code 123
953 " https://github.com/python/cpython/issues/129900
954 call assert_match('1\|123', exitval)
955 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100956 %bwipe!
957endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200958
Bram Moolenaar21109272020-01-30 16:27:20 +0100959func Test_terminal_eof_arg_win32_ctrl_z()
960 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200961 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100962 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100963
964 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200965 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100966 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
967 call assert_match('\^Z', getline(line('$') - 1))
968 %bwipe!
969endfunc
970
971func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200972 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100973 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100974
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100975 " Check the last specified ++eof arg is used and does not leak memory.
Bram Moolenaar21109272020-01-30 16:27:20 +0100976 new
977 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200978 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100979 " MS-Windows echoes the input, Unix doesn't.
980 if has('win32')
981 call WaitFor({-> getline('$') =~ 'exit(123)'})
982 call assert_equal('hello', getline(line('$') - 1))
983 else
984 call WaitFor({-> getline('$') =~ 'hello'})
985 call assert_equal('hello', getline('$'))
986 endif
Christian Brabandt2e4361b2025-02-09 17:18:07 +0100987 let exitval = bufnr()->term_getjob()->job_info().exitval
988 if !has('win32')
989 call assert_equal(123, exitval)
990 else
991 " python 3.13 on Windows returns exit code 1
992 " older versions returned correctly exit code 123
993 " https://github.com/python/cpython/issues/129900
994 call assert_match('1\|123', exitval)
995 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100996 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200997endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200998
999func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001000 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001001 let buf = term_start('NONE', {})
1002 call assert_notequal(0, buf)
1003
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001004 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001005 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001006 if has('gui_running') && !has('win32')
1007 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001008 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +01001009 else
1010 " Otherwise using a job works on all systems.
1011 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001012 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001013 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001014
Bram Moolenaar13ebb032017-08-26 22:02:51 +02001015 bwipe!
1016endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001017
1018func Test_terminal_special_chars()
1019 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001020 CheckUnix
1021
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001022 call mkdir('Xdir with spaces', 'R')
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001023 call writefile(['x'], 'Xdir with spaces/quoted"file')
1024 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001025 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +01001026 " make sure the job has finished
1027 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001028
Bram Moolenaar9d654a82017-09-03 19:52:17 +02001029 bwipe
1030endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001031
1032func Test_terminal_wrong_options()
1033 call assert_fails('call term_start(&shell, {
1034 \ "in_io": "file",
1035 \ "in_name": "xxx",
1036 \ "out_io": "file",
1037 \ "out_name": "xxx",
1038 \ "err_io": "file",
1039 \ "err_name": "xxx"
1040 \ })', 'E474:')
1041 call assert_fails('call term_start(&shell, {
1042 \ "out_buf": bufnr("%")
1043 \ })', 'E474:')
1044 call assert_fails('call term_start(&shell, {
1045 \ "err_buf": bufnr("%")
1046 \ })', 'E474:')
1047endfunc
1048
1049func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001050 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001051 let cmd = Get_cat_123_cmd()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001052 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001053 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001054 " ConPTY may precede escape sequence. There are things that are not so.
1055 if !has('conpty')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001056 call WaitForAssert({-> assert_notequal(0, len(readfile("Xtrfile")))})
1057 call assert_match('123', readfile('Xtrfile')[0])
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001058 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001059 let g:job = term_getjob(buf)
1060 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +02001061
1062 if has('win32')
1063 " On Windows we cannot delete a file being used by a process. When
1064 " job_status() returns "dead", the process remains for a short time.
1065 " Just wait for a moment.
1066 sleep 50m
1067 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001068 call delete('Xtrfile')
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001069 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001070
1071 if has('unix')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001072 call writefile(['one line'], 'Xtrfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001073 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001074 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001075 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +02001076 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001077 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001078 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001079 endif
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001080
1081 call delete('Xtext')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001082endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001083
1084func TerminalTmap(remap)
1085 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +01001086 " Wait for the shell to display a prompt
1087 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001088 call assert_equal('t', mode())
1089
1090 if a:remap
1091 tmap 123 456
1092 else
1093 tnoremap 123 456
1094 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +01001095 " don't use abcde, it's an existing command
1096 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001097 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +01001098 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001099 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001100 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001101 let lnum = term_getcursor(buf)[0]
1102 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +01001103 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001104 else
1105 call assert_match('456', term_getline(buf, lnum))
1106 endif
1107
1108 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001109 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001110
1111 tunmap 123
1112 tunmap 456
1113 call assert_equal('', maparg('123', 't'))
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01001114 exe buf . 'bwipe'
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001115 unlet g:job
1116endfunc
1117
1118func Test_terminal_tmap()
1119 call TerminalTmap(1)
1120 call TerminalTmap(0)
1121endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001122
1123func Test_terminal_wall()
1124 let buf = Run_shell_in_terminal({})
1125 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001126 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001127 exe buf . 'bwipe'
1128 unlet g:job
1129endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001130
Bram Moolenaar7a760922018-02-19 23:10:02 +01001131func Test_terminal_wqall()
1132 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001133 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001134 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +01001135 exe buf . 'bwipe'
1136 unlet g:job
1137endfunc
1138
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001139func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001140 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001141 let save_enc = &encoding
1142 set encoding=utf-8
1143
1144 if has('win32')
Milly4f5681d2024-10-20 11:06:00 +02001145 let cmd = "cmd /D /K chcp 65001"
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001146 let lnum = [3, 6, 9]
1147 else
1148 let cmd = &shell
1149 let lnum = [1, 3, 5]
1150 endif
1151
1152 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +02001153 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001154 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +02001155 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001156
Bram Moolenaarebe74b72018-04-21 23:34:43 +02001157 if has('win32')
1158 call assert_equal('cmd', job_info(g:job).cmd[0])
1159 else
1160 call assert_equal(&shell, job_info(g:job).cmd[0])
1161 endif
1162
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001163 " ascii + composing
1164 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001165 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001166 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001167 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001168 call term_sendkeys(buf, "\<cr>")
1169 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001170 let l = term_scrape(buf, lnum[0] + 1)
1171 call assert_equal("a\u0308", l[0].chars)
1172 call assert_equal("b", l[1].chars)
1173 call assert_equal("c", l[2].chars)
1174
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001175 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001176 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001177 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001178 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001179 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001180 call term_sendkeys(buf, "\<cr>")
1181 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001182 let l = term_scrape(buf, lnum[1] + 1)
1183 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001184 call assert_equal(2, l[0].width)
1185 call assert_equal("\u304e", l[1].chars)
1186 call assert_equal(2, l[1].width)
1187 call assert_equal("\u304f\u3099", l[2].chars)
1188 call assert_equal(2, l[2].width)
1189 call assert_equal("\u3052", l[3].chars)
1190 call assert_equal(2, l[3].width)
1191 call assert_equal("\u3053\u3099", l[4].chars)
1192 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001193
1194 " \u00a0 + composing
1195 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001196 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001197 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001198 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001199 call term_sendkeys(buf, "\<cr>")
1200 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001201 let l = term_scrape(buf, lnum[2] + 1)
1202 call assert_equal("\u00a0\u0308", l[3].chars)
1203
1204 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001205 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001206 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001207 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001208 let &encoding = save_enc
1209endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001210
1211func Test_terminal_aucmd_on_close()
1212 fun Nop()
1213 let s:called = 1
1214 endfun
1215
1216 aug repro
1217 au!
1218 au BufWinLeave * call Nop()
1219 aug END
1220
1221 let [cmd, waittime] = s:get_sleep_cmd()
1222
1223 call assert_equal(1, winnr('$'))
1224 new
1225 call setline(1, ['one', 'two'])
1226 exe 'term ++close ' . cmd
1227 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001228 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001229 call assert_equal(1, s:called)
1230 bwipe!
1231
1232 unlet s:called
1233 au! repro
1234 delfunc Nop
1235endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001236
1237func Test_terminal_term_start_empty_command()
1238 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001239 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001240 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001241 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001242 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001243 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001244 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001245 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001246 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001247 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001248 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001249 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001250 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001251 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001252 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001253 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001254 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001255 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001256 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1257 call assert_fails(cmd, 'E475:')
1258 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001259 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001260 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001261 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1262 call assert_fails(cmd, 'E475:')
1263 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1264 call assert_fails(cmd, 'E730:')
1265 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001266 if has('gui_running') || has('termguicolors')
1267 call assert_fails(cmd, 'E475:')
1268 else
1269 call assert_fails(cmd, 'E254:')
1270 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001271 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001272endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001273
1274func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001275 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001276
1277 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001278 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001279
Bram Moolenaar086eb872018-03-25 21:24:12 +02001280 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001281 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001282
Bram Moolenaar086eb872018-03-25 21:24:12 +02001283 " Request the cursor position.
1284 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001285
1286 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001287 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001288
Bram Moolenaar086eb872018-03-25 21:24:12 +02001289 " End "cat" gently.
1290 call term_sendkeys(buf, "\<CR>\<C-D>")
1291
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001292 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001293 exe buf . 'bwipe'
1294 unlet g:job
1295endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001296
Bram Moolenaarc2958582021-12-14 11:16:31 +00001297" Run this first, it fails when run after other tests.
1298func Test_aa_terminal_focus_events()
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001299 CheckNotGui
1300 CheckUnix
1301 CheckRunVimInTerminal
1302
1303 let save_term = &term
1304 let save_ttymouse = &ttymouse
1305 set term=xterm ttymouse=xterm2
1306
1307 let lines =<< trim END
1308 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001309 au FocusLost * call setline(1, 'I am lost') | set nomod
1310 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001311 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001312 call writefile(lines, 'XtermFocus', 'D')
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001313 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1314
1315 " Send a focus event to ourselves, it should be forwarded to the terminal
1316 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001317 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1318
1319 call feedkeys("\<Esc>[I", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001320 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1321
Bram Moolenaare5050712021-12-09 10:51:05 +00001322 " check that a command line being edited is redrawn in place
1323 call term_sendkeys(buf, ":" .. repeat('x', 80))
1324 call TermWait(buf)
1325 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaare5050712021-12-09 10:51:05 +00001326 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1327 call term_sendkeys(buf, "\<Esc>")
1328
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001329 call StopVimInTerminal(buf)
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001330 let &term = save_term
1331 let &ttymouse = save_ttymouse
1332endfunc
1333
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001334" Run Vim, start a terminal in that Vim with the kill argument,
1335" :qall works.
1336func Run_terminal_qall_kill(line1, line2)
1337 " 1. Open a terminal window and wait for the prompt to appear
1338 " 2. set kill using term_setkill()
1339 " 3. make Vim exit, it will kill the shell
1340 let after = [
1341 \ a:line1,
1342 \ 'let buf = bufnr("%")',
1343 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1344 \ ' sleep 10m',
1345 \ 'endwhile',
1346 \ a:line2,
1347 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1348 \ 'qall',
1349 \ ]
1350 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001351 return
1352 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001353 call assert_equal("done", readfile("Xdone")[0])
1354 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001355endfunc
1356
1357" Run Vim in a terminal, then start a terminal in that Vim with a kill
1358" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001359func Test_terminal_qall_kill_arg()
1360 call Run_terminal_qall_kill('term ++kill=kill', '')
1361endfunc
1362
1363" Run Vim, start a terminal in that Vim, set the kill argument with
1364" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001365func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001366 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001367endfunc
1368
1369" Run Vim, start a terminal in that Vim without the kill argument,
1370" check that :qall does not exit, :qall! does.
1371func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001372 let after =<< trim [CODE]
1373 term
1374 let buf = bufnr("%")
1375 while term_getline(buf, 1) =~ "^\\s*$"
1376 sleep 10m
1377 endwhile
1378 set nomore
1379 au VimLeavePre * call writefile(["too early"], "Xdone")
1380 qall
1381 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1382 cquit
1383 [CODE]
1384
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001385 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001386 return
1387 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001388 call assert_equal("done", readfile("Xdone")[0])
1389 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001390endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001391
1392" Run Vim in a terminal, then start a terminal in that Vim without a kill
1393" argument, check that :confirm qall works.
1394func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001395 CheckRunVimInTerminal
Bram Moolenaare564c702022-06-14 15:00:28 +01001396
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001397 let buf = RunVimInTerminal('', {})
1398
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001399 " the shell may set the window title, we don't want that here
Bram Moolenaar377d92a2022-06-14 21:22:12 +01001400 call term_sendkeys(buf, ":call test_override('vterm_title', 1)\<CR>")
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001401
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001402 " Open a terminal window and wait for the prompt to appear
1403 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001404 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1405 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001406
1407 " make Vim exit, it will prompt to kill the shell
1408 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001409 call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001410 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001411 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001412
1413 " close the terminal window where Vim was running
1414 quit
1415endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001416
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001417" Run Vim in a terminal, then start a terminal window with a shell and check
1418" that Vim exits if it is closed.
1419func Test_terminal_exit()
1420 CheckRunVimInTerminal
1421
1422 let lines =<< trim END
1423 let winid = win_getid()
1424 help
1425 term
1426 let termid = win_getid()
1427 call win_gotoid(winid)
1428 close
1429 call win_gotoid(termid)
1430 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001431 call writefile(lines, 'XtermExit', 'D')
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001432 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1433 let job = term_getjob(buf)
1434 call WaitForAssert({-> assert_equal("run", job_status(job))})
1435
1436 " quit the shell, it will make Vim exit
1437 call term_sendkeys(buf, "exit\<CR>")
1438 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001439endfunc
1440
Bram Moolenaar012eb662018-03-13 17:55:27 +01001441func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001442 augroup repro
1443 au!
1444 au TerminalOpen * let s:called += 1
1445 augroup END
1446
1447 let s:called = 0
1448
1449 " Open a terminal window with :terminal
1450 terminal
1451 call assert_equal(1, s:called)
1452 bwipe!
1453
1454 " Open a terminal window with term_start()
1455 call term_start(&shell)
1456 call assert_equal(2, s:called)
1457 bwipe!
1458
1459 " Open a hidden terminal buffer with :terminal
1460 terminal ++hidden
1461 call assert_equal(3, s:called)
1462 for buf in term_list()
1463 exe buf . "bwipe!"
1464 endfor
1465
1466 " Open a hidden terminal buffer with term_start()
1467 let buf = term_start(&shell, {'hidden': 1})
1468 call assert_equal(4, s:called)
1469 exe buf . "bwipe!"
1470
1471 unlet s:called
1472 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001473endfunc
1474
1475func Test_open_term_from_cmd()
1476 CheckUnix
1477 CheckRunVimInTerminal
1478
1479 let lines =<< trim END
1480 call setline(1, ['a', 'b', 'c'])
1481 3
1482 set incsearch
1483 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1484 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001485 call writefile(lines, 'Xopenterm', 'D')
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001486 let buf = RunVimInTerminal('-S Xopenterm', {})
1487
1488 " this opens a window, incsearch should not use the old cursor position
1489 call term_sendkeys(buf, "/\<F3>")
1490 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1491 call term_sendkeys(buf, "\<Esc>")
1492 call term_sendkeys(buf, ":q\<CR>")
1493
1494 call StopVimInTerminal(buf)
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001495endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001496
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001497func Test_combining_double_width()
1498 CheckUnix
1499 CheckRunVimInTerminal
1500
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001501 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001502 let lines =<< trim END
1503 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1504 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001505 call writefile(lines, 'Xcombining', 'D')
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001506 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1507
1508 " this opens a window, incsearch should not use the old cursor position
1509 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1510 call term_sendkeys(buf, ":q\<CR>")
1511
1512 call StopVimInTerminal(buf)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001513endfunc
1514
Bram Moolenaar02764712020-11-14 20:21:55 +01001515func Test_terminal_popup_with_cmd()
1516 " this was crashing
1517 let buf = term_start(&shell, #{hidden: v:true})
1518 let s:winid = popup_create(buf, {})
1519 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1520 call feedkeys("\<F3>", 'xt')
1521
1522 tunmap <F3>
1523 exe 'bwipe! ' .. buf
1524 unlet s:winid
1525endfunc
1526
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001527func Test_terminal_popup_bufload()
1528 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1529 let winid = popup_create(termbuf, {})
1530 sleep 50m
1531
1532 let newbuf = bufadd('')
1533 call bufload(newbuf)
1534 call setbufline(newbuf, 1, 'foobar')
1535
1536 " must not have switched to another window
1537 call assert_equal(winid, win_getid())
1538
Bram Moolenaare6329e42020-11-16 21:10:34 +01001539 call StopShellInTerminal(termbuf)
1540 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001541 exe 'bwipe! ' .. newbuf
1542endfunc
1543
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001544func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001545 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001546 CheckUnix
1547
1548 " use "sh" instead of "&shell" in the hope it will use a short prompt
1549 let lines =<< trim END
1550 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1551 exe 'buffer ' .. termbuf
1552
1553 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1554 sleep 50m
1555
1556 call term_sendkeys(termbuf, "echo 'test'")
1557 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001558 call writefile(lines, 'XpopupScript', 'D')
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001559 let buf = RunVimInTerminal('-S XpopupScript', {})
1560
1561 " typed text appears both in normal window and in popup
1562 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1563 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1564
Bram Moolenaar0407d272021-12-13 22:17:44 +00001565 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1566 call TermWait(buf)
1567 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001568 call StopVimInTerminal(buf)
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001569endfunc
1570
Bram Moolenaare41decc2020-11-14 21:34:59 +01001571func Test_terminal_popup_insert_cmd()
1572 CheckUnix
1573
1574 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1575 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001576 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001577 endfunc
1578 call feedkeys("i\<F3>")
1579 sleep 10m
1580 call assert_equal('n', mode())
1581
1582 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001583 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001584 delfunc StartTermInPopup
1585 iunmap <F3>
1586endfunc
1587
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001588func Check_dump01(off)
1589 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1590 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001591 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001592endfunc
1593
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001594func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001595 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001596
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001597 let save_enc = &encoding
1598 set encoding=utf-8
1599 call assert_equal(1, winnr('$'))
1600
1601 let text = " a\u0300 e\u0302 o\u0308"
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001602 call writefile([text], 'Xcomposing', 'D')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001603 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001604 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001605 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001606 let dumpline = readfile('Xdump')[0]
1607 call assert_match('|à| |ê| |ö', dumpline)
1608
1609 call StopVimInTerminal(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001610 call delete('Xdump')
1611 let &encoding = save_enc
1612endfunc
1613
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001614" Tests for failures in the term_dumpwrite() function
1615func Test_terminal_dumpwrite_errors()
1616 CheckRunVimInTerminal
1617 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1618 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001619 call TermWait(buf)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001620 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E1206:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001621 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1622 call writefile([], 'Xtest.dump')
1623 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1624 call delete('Xtest.dump')
1625 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1626 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001627 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001628 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001629 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001630 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1631 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1632 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1633endfunc
1634
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001635" just testing basic functionality.
1636func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001637 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001638 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001639 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001640 call assert_equal(2, winnr('$'))
1641 call assert_equal(20, line('$'))
1642 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001643
1644 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001645 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001646 call assert_equal(buf, buf2)
1647 call assert_notequal('one two three four five', trim(getline(1)))
1648
1649 " Load the first dump again in the same window
1650 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1651 call assert_equal(buf, buf2)
1652 call Check_dump01(0)
1653
1654 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1655 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1656 new
1657 let closedbuf = winbufnr('')
1658 quit
1659 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001660 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001661 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001662
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001663 quit
1664endfunc
1665
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001666func Test_terminal_dumpload_dump()
1667 CheckRunVimInTerminal
1668
1669 let lines =<< trim END
1670 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1671 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001672 call writefile(lines, 'XtermDumpload', 'D')
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001673 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1674 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1675
1676 call StopVimInTerminal(buf)
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001677endfunc
1678
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001679func Test_terminal_dumpdiff()
1680 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001681 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001682 call assert_equal(2, winnr('$'))
1683 call assert_equal(62, line('$'))
1684 call Check_dump01(0)
1685 call Check_dump01(42)
1686 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1687 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001688
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001689 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001690 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001691 call writefile([], 'X1.dump', 'D')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001692 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001693endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001694
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001695func Test_terminal_dumpdiff_swap()
1696 call assert_equal(1, winnr('$'))
1697 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1698 call assert_equal(2, winnr('$'))
1699 call assert_equal(62, line('$'))
1700 call assert_match('Test_popup_command_01.dump', getline(21))
1701 call assert_match('Test_popup_command_03.dump', getline(42))
1702 call assert_match('Undo', getline(3))
1703 call assert_match('three four five', getline(45))
1704
1705 normal s
1706 call assert_match('Test_popup_command_03.dump', getline(21))
1707 call assert_match('Test_popup_command_01.dump', getline(42))
1708 call assert_match('three four five', getline(3))
1709 call assert_match('Undo', getline(45))
1710 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001711
1712 " Diff two terminal dump files with different number of rows
1713 " Swap the diffs
1714 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1715 call assert_match('Test_popup_command_01.dump', getline(21))
1716 call assert_match('Test_winline_rnu.dump', getline(42))
1717 normal s
1718 call assert_match('Test_winline_rnu.dump', getline(6))
1719 call assert_match('Test_popup_command_01.dump', getline(27))
1720 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001721endfunc
1722
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001723func Test_terminal_dumpdiff_options()
1724 set laststatus=0
1725 call assert_equal(1, winnr('$'))
1726 let height = winheight(0)
1727 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1728 call assert_equal(2, winnr('$'))
1729 call assert_equal(height, winheight(winnr()))
1730 call assert_equal(33, winwidth(winnr()))
1731 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1732 quit
1733
1734 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001735 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1736 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001737 call assert_equal(&columns, winwidth(0))
1738 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001739 call assert_equal('something else', bufname('%'))
1740 quit
1741
1742 call assert_equal(1, winnr('$'))
1743 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1744 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001745 call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001746 bwipe
1747
1748 set laststatus&
1749endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001750
Bram Moolenaar10772302019-01-20 18:25:54 +01001751" When drawing the statusline the cursor position may not have been updated
1752" yet.
1753" 1. create a terminal, make it show 2 lines
1754" 2. 0.5 sec later: leave terminal window, execute "i"
1755" 3. 0.5 sec later: clear terminal window, now it's 1 line
1756" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1757" 4. 0.5 sec later: should be done, clean up
1758func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001759 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001760 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001761
Bram Moolenaar10772302019-01-20 18:25:54 +01001762 set statusline=x
1763 terminal
1764 let tbuf = bufnr('')
1765 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1766 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1767 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1768 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1769
1770 sleep 2
1771 exe tbuf . 'bwipe!'
1772 au! BufLeave
1773 set statusline=
1774endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001775
Bram Moolenaar81035272021-12-16 18:02:07 +00001776func CheckTerminalWindowWorks(buf)
1777 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1778 call term_sendkeys(a:buf, "exit\<CR>")
1779 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1780 call term_sendkeys(a:buf, ":q\<CR>")
1781 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1782endfunc
1783
1784func Test_start_terminal_from_timer()
1785 CheckUnix
1786 CheckFeature timers
1787
1788 " Open a terminal window from a timer, typed text goes to the terminal
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001789 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm', 'D')
Bram Moolenaar81035272021-12-16 18:02:07 +00001790 let buf = RunVimInTerminal('-S XtimerTerm', {})
1791 call CheckTerminalWindowWorks(buf)
1792
1793 " do the same in Insert mode
1794 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1795 call CheckTerminalWindowWorks(buf)
1796
1797 call StopVimInTerminal(buf)
Bram Moolenaar81035272021-12-16 18:02:07 +00001798endfunc
1799
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001800func Test_terminal_window_focus()
1801 let winid1 = win_getid()
1802 terminal
1803 let winid2 = win_getid()
1804 call feedkeys("\<C-W>j", 'xt')
1805 call assert_equal(winid1, win_getid())
1806 call feedkeys("\<C-W>k", 'xt')
1807 call assert_equal(winid2, win_getid())
1808 " can use a cursor key here
1809 call feedkeys("\<C-W>\<Down>", 'xt')
1810 call assert_equal(winid1, win_getid())
1811 call feedkeys("\<C-W>\<Up>", 'xt')
1812 call assert_equal(winid2, win_getid())
1813
1814 bwipe!
1815endfunc
1816
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001817func Api_drop_common(options)
1818 call assert_equal(1, winnr('$'))
1819
1820 " Use the title termcap entries to output the escape sequence.
1821 call writefile([
1822 \ 'set title',
1823 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1824 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1825 \ 'redraw',
1826 \ "set t_ts=",
1827 \ ], 'Xscript')
1828 let buf = RunVimInTerminal('-S Xscript', {})
1829 call WaitFor({-> bufnr('Xtextfile') > 0})
1830 call assert_equal('Xtextfile', expand('%:t'))
1831 call assert_true(winnr('$') >= 3)
1832 return buf
1833endfunc
1834
1835func Test_terminal_api_drop_newwin()
1836 CheckRunVimInTerminal
1837 let buf = Api_drop_common('')
1838 call assert_equal(0, &bin)
1839 call assert_equal('', &fenc)
1840
1841 call StopVimInTerminal(buf)
1842 call delete('Xscript')
1843 bwipe Xtextfile
1844endfunc
1845
1846func Test_terminal_api_drop_newwin_bin()
1847 CheckRunVimInTerminal
1848 let buf = Api_drop_common(',{"bin":1}')
1849 call assert_equal(1, &bin)
1850
1851 call StopVimInTerminal(buf)
1852 call delete('Xscript')
1853 bwipe Xtextfile
1854endfunc
1855
1856func Test_terminal_api_drop_newwin_binary()
1857 CheckRunVimInTerminal
1858 let buf = Api_drop_common(',{"binary":1}')
1859 call assert_equal(1, &bin)
1860
1861 call StopVimInTerminal(buf)
1862 call delete('Xscript')
1863 bwipe Xtextfile
1864endfunc
1865
1866func Test_terminal_api_drop_newwin_nobin()
1867 CheckRunVimInTerminal
1868 set binary
1869 let buf = Api_drop_common(',{"nobin":1}')
1870 call assert_equal(0, &bin)
1871
1872 call StopVimInTerminal(buf)
1873 call delete('Xscript')
1874 bwipe Xtextfile
1875 set nobinary
1876endfunc
1877
1878func Test_terminal_api_drop_newwin_nobinary()
1879 CheckRunVimInTerminal
1880 set binary
1881 let buf = Api_drop_common(',{"nobinary":1}')
1882 call assert_equal(0, &bin)
1883
1884 call StopVimInTerminal(buf)
1885 call delete('Xscript')
1886 bwipe Xtextfile
1887 set nobinary
1888endfunc
1889
1890func Test_terminal_api_drop_newwin_ff()
1891 CheckRunVimInTerminal
1892 let buf = Api_drop_common(',{"ff":"dos"}')
1893 call assert_equal("dos", &ff)
1894
1895 call StopVimInTerminal(buf)
1896 call delete('Xscript')
1897 bwipe Xtextfile
1898endfunc
1899
1900func Test_terminal_api_drop_newwin_fileformat()
1901 CheckRunVimInTerminal
1902 let buf = Api_drop_common(',{"fileformat":"dos"}')
1903 call assert_equal("dos", &ff)
1904
1905 call StopVimInTerminal(buf)
1906 call delete('Xscript')
1907 bwipe Xtextfile
1908endfunc
1909
1910func Test_terminal_api_drop_newwin_enc()
1911 CheckRunVimInTerminal
1912 let buf = Api_drop_common(',{"enc":"utf-16"}')
1913 call assert_equal("utf-16", &fenc)
1914
1915 call StopVimInTerminal(buf)
1916 call delete('Xscript')
1917 bwipe Xtextfile
1918endfunc
1919
1920func Test_terminal_api_drop_newwin_encoding()
1921 CheckRunVimInTerminal
1922 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1923 call assert_equal("utf-16", &fenc)
1924
1925 call StopVimInTerminal(buf)
1926 call delete('Xscript')
1927 bwipe Xtextfile
1928endfunc
1929
1930func Test_terminal_api_drop_oldwin()
1931 CheckRunVimInTerminal
1932 let firstwinid = win_getid()
1933 split Xtextfile
1934 let textfile_winid = win_getid()
1935 call assert_equal(2, winnr('$'))
1936 call win_gotoid(firstwinid)
1937
1938 " Use the title termcap entries to output the escape sequence.
1939 call writefile([
1940 \ 'set title',
1941 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1942 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1943 \ 'redraw',
1944 \ "set t_ts=",
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01001945 \ ], 'Xscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001946 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1947 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1948 call assert_equal(textfile_winid, win_getid())
1949
1950 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001951 bwipe Xtextfile
1952endfunc
1953
1954func Tapi_TryThis(bufnum, arg)
1955 let g:called_bufnum = a:bufnum
1956 let g:called_arg = a:arg
1957endfunc
1958
1959func WriteApiCall(funcname)
1960 " Use the title termcap entries to output the escape sequence.
1961 call writefile([
1962 \ 'set title',
1963 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1964 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1965 \ 'redraw',
1966 \ "set t_ts=",
1967 \ ], 'Xscript')
1968endfunc
1969
1970func Test_terminal_api_call()
1971 CheckRunVimInTerminal
1972
1973 unlet! g:called_bufnum
1974 unlet! g:called_arg
1975
1976 call WriteApiCall('Tapi_TryThis')
1977
1978 " Default
1979 let buf = RunVimInTerminal('-S Xscript', {})
1980 call WaitFor({-> exists('g:called_bufnum')})
1981 call assert_equal(buf, g:called_bufnum)
1982 call assert_equal(['hello', 123], g:called_arg)
1983 call StopVimInTerminal(buf)
1984
1985 unlet! g:called_bufnum
1986 unlet! g:called_arg
1987
1988 " Enable explicitly
1989 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1990 call WaitFor({-> exists('g:called_bufnum')})
1991 call assert_equal(buf, g:called_bufnum)
1992 call assert_equal(['hello', 123], g:called_arg)
1993 call StopVimInTerminal(buf)
1994
1995 unlet! g:called_bufnum
1996 unlet! g:called_arg
1997
1998 func! ApiCall_TryThis(bufnum, arg)
1999 let g:called_bufnum2 = a:bufnum
2000 let g:called_arg2 = a:arg
2001 endfunc
2002
2003 call WriteApiCall('ApiCall_TryThis')
2004
2005 " Use prefix match
2006 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
2007 call WaitFor({-> exists('g:called_bufnum2')})
2008 call assert_equal(buf, g:called_bufnum2)
2009 call assert_equal(['hello', 123], g:called_arg2)
2010 call StopVimInTerminal(buf)
2011
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002012 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002013
2014 unlet! g:called_bufnum2
2015 unlet! g:called_arg2
2016
2017 call delete('Xscript')
2018 delfunction! ApiCall_TryThis
2019 unlet! g:called_bufnum2
2020 unlet! g:called_arg2
2021endfunc
2022
2023func Test_terminal_api_call_fails()
2024 CheckRunVimInTerminal
2025
2026 func! TryThis(bufnum, arg)
2027 let g:called_bufnum3 = a:bufnum
2028 let g:called_arg3 = a:arg
2029 endfunc
2030
2031 call WriteApiCall('TryThis')
2032
2033 unlet! g:called_bufnum3
2034 unlet! g:called_arg3
2035
2036 " Not permitted
2037 call ch_logfile('Xlog', 'w')
2038 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2039 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
2040 call assert_false(exists('g:called_bufnum3'))
2041 call assert_false(exists('g:called_arg3'))
2042 call StopVimInTerminal(buf)
2043
2044 " No match
2045 call ch_logfile('Xlog', 'w')
2046 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
2047 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
2048 call assert_false(exists('g:called_bufnum3'))
2049 call assert_false(exists('g:called_arg3'))
2050 call StopVimInTerminal(buf)
2051
2052 call delete('Xscript')
2053 call ch_logfile('')
2054 call delete('Xlog')
2055 delfunction! TryThis
2056 unlet! g:called_bufnum3
2057 unlet! g:called_arg3
2058endfunc
2059
2060let s:caught_e937 = 0
2061
2062func Tapi_Delete(bufnum, arg)
2063 try
2064 execute 'bdelete!' a:bufnum
2065 catch /E937:/
2066 let s:caught_e937 = 1
2067 endtry
2068endfunc
2069
2070func Test_terminal_api_call_fail_delete()
2071 CheckRunVimInTerminal
2072
2073 call WriteApiCall('Tapi_Delete')
2074 let buf = RunVimInTerminal('-S Xscript', {})
2075 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
2076
2077 call StopVimInTerminal(buf)
2078 call delete('Xscript')
2079 call ch_logfile('', '')
2080endfunc
2081
2082func Test_terminal_setapi_and_call()
2083 CheckRunVimInTerminal
2084
2085 call WriteApiCall('Tapi_TryThis')
2086 call ch_logfile('Xlog', 'w')
2087
2088 unlet! g:called_bufnum
2089 unlet! g:called_arg
2090
2091 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2092 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2093 call assert_false(exists('g:called_bufnum'))
2094 call assert_false(exists('g:called_arg'))
2095
2096 eval buf->term_setapi('Tapi_')
2097 call term_sendkeys(buf, ":set notitle\<CR>")
2098 call term_sendkeys(buf, ":source Xscript\<CR>")
2099 call WaitFor({-> exists('g:called_bufnum')})
2100 call assert_equal(buf, g:called_bufnum)
2101 call assert_equal(['hello', 123], g:called_arg)
2102
2103 call StopVimInTerminal(buf)
2104
2105 call delete('Xscript')
2106 call ch_logfile('')
2107 call delete('Xlog')
2108 unlet! g:called_bufnum
2109 unlet! g:called_arg
2110endfunc
2111
2112func Test_terminal_api_arg()
2113 CheckRunVimInTerminal
2114
2115 call WriteApiCall('Tapi_TryThis')
2116 call ch_logfile('Xlog', 'w')
2117
2118 unlet! g:called_bufnum
2119 unlet! g:called_arg
2120
2121 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2122 let buf = bufnr('%')
2123 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2124 call assert_false(exists('g:called_bufnum'))
2125 call assert_false(exists('g:called_arg'))
2126
2127 call StopVimInTerminal(buf)
2128
2129 call ch_logfile('Xlog', 'w')
2130
2131 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2132 let buf = bufnr('%')
2133 call WaitFor({-> exists('g:called_bufnum')})
2134 call assert_equal(buf, g:called_bufnum)
2135 call assert_equal(['hello', 123], g:called_arg)
2136
2137 call StopVimInTerminal(buf)
2138
2139 call delete('Xscript')
2140 call ch_logfile('')
2141 call delete('Xlog')
2142 unlet! g:called_bufnum
2143 unlet! g:called_arg
2144endfunc
2145
2146func Test_terminal_ansicolors_default()
2147 CheckFunction term_getansicolors
2148
2149 let colors = [
2150 \ '#000000', '#e00000',
2151 \ '#00e000', '#e0e000',
2152 \ '#0000e0', '#e000e0',
2153 \ '#00e0e0', '#e0e0e0',
2154 \ '#808080', '#ff4040',
2155 \ '#40ff40', '#ffff40',
2156 \ '#4040ff', '#ff40ff',
2157 \ '#40ffff', '#ffffff',
2158 \]
2159
2160 let buf = Run_shell_in_terminal({})
2161 call assert_equal(colors, term_getansicolors(buf))
2162 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002163 call assert_equal([], term_getansicolors(buf))
2164
2165 exe buf . 'bwipe'
2166endfunc
2167
Julio Bcde8ff62025-02-06 20:31:27 +01002168func Test_terminal_ansicolors_default_reset_tgc()
2169 CheckFeature termguicolors
2170 CheckRunVimInTerminal
2171
2172 let $PS1="$ "
2173 let buf = RunVimInTerminal('-c "term sh"', {'rows': 12})
2174 call TermWait(buf)
2175 " Wait for the shell to display a prompt
2176 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
2177
2178 call term_sendkeys(buf, "printf '\\033[0;30;41mhello world\\033[0m\\n'\<CR>")
2179 call WaitForAssert({-> assert_match('hello world', term_getline(buf, 2))})
2180 call term_sendkeys(buf, "\<C-W>:set notgc\<CR>")
2181 call term_sendkeys(buf, "printf '\\033[0;30;41mhello world\\033[0m\\n'\<CR>")
2182 call WaitForAssert({-> assert_match('hello world', term_getline(buf, 4))})
2183
2184 call VerifyScreenDump(buf, 'Test_terminal_ansi_reset_tgc', {})
2185
2186 call term_sendkeys(buf, "exit\<CR>")
2187 call TermWait(buf)
2188 call StopVimInTerminal(buf)
2189 unlet! $PS1
2190endfunc
2191
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002192let s:test_colors = [
2193 \ '#616e64', '#0d0a79',
2194 \ '#6d610d', '#0a7373',
2195 \ '#690d0a', '#6d696e',
2196 \ '#0d0a6f', '#616e0d',
2197 \ '#0a6479', '#6d0d0a',
2198 \ '#617373', '#0d0a69',
2199 \ '#6d690d', '#0a6e6f',
2200 \ '#610d0a', '#6e6479',
2201 \]
2202
2203func Test_terminal_ansicolors_global()
2204 CheckFeature termguicolors
2205 CheckFunction term_getansicolors
2206
LemonBoyb2b3acb2022-05-20 10:10:34 +01002207 if has('vtp') && !has('vcon') && !has('gui_running')
2208 throw 'Skipped: does not support termguicolors'
2209 endif
2210
2211 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002212 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2213 let buf = Run_shell_in_terminal({})
2214 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2215 call StopShellInTerminal(buf)
LemonBoyb2b3acb2022-05-20 10:10:34 +01002216 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002217
2218 exe buf . 'bwipe'
2219 unlet g:terminal_ansi_colors
2220endfunc
2221
2222func Test_terminal_ansicolors_func()
2223 CheckFeature termguicolors
2224 CheckFunction term_getansicolors
2225
LemonBoyb2b3acb2022-05-20 10:10:34 +01002226 if has('vtp') && !has('vcon') && !has('gui_running')
2227 throw 'Skipped: does not support termguicolors'
2228 endif
2229
2230 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002231 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2232 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2233 call assert_equal(s:test_colors, term_getansicolors(buf))
2234
2235 call term_setansicolors(buf, g:terminal_ansi_colors)
2236 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2237
2238 let colors = [
2239 \ 'ivory', 'AliceBlue',
2240 \ 'grey67', 'dark goldenrod',
2241 \ 'SteelBlue3', 'PaleVioletRed4',
2242 \ 'MediumPurple2', 'yellow2',
2243 \ 'RosyBrown3', 'OrangeRed2',
2244 \ 'white smoke', 'navy blue',
2245 \ 'grey47', 'gray97',
2246 \ 'MistyRose2', 'DodgerBlue4',
2247 \]
2248 eval buf->term_setansicolors(colors)
2249
2250 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002251 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002252 call assert_fails('call term_setansicolors(buf, {})', 'E1211:')
Bram Moolenaar23919542023-05-05 22:12:22 +01002253 call assert_fails('call term_setansicolors(buf, [])', 'E475: Invalid value for argument "colors"')
LemonBoyb2b3acb2022-05-20 10:10:34 +01002254 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002255
2256 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002257 call assert_equal(0, term_setansicolors(buf, []))
2258 exe buf . 'bwipe'
2259endfunc
2260
2261func Test_terminal_all_ansi_colors()
2262 CheckRunVimInTerminal
2263
2264 " Use all the ANSI colors.
2265 call writefile([
2266 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2267 \ 'hi Tblack ctermfg=0 ctermbg=8',
2268 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2269 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2270 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2271 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2272 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2273 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2274 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2275 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2276 \ 'hi Tred ctermfg=9 ctermbg=1',
2277 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2278 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2279 \ 'hi Tblue ctermfg=12 ctermbg=4',
2280 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2281 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2282 \ 'hi Twhite ctermfg=15 ctermbg=7',
2283 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2284 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2285 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2286 \ '',
2287 \ 'call matchadd("Tblack", "A")',
2288 \ 'call matchadd("Tdarkred", "B")',
2289 \ 'call matchadd("Tdarkgreen", "C")',
2290 \ 'call matchadd("Tbrown", "D")',
2291 \ 'call matchadd("Tdarkblue", "E")',
2292 \ 'call matchadd("Tdarkmagenta", "F")',
2293 \ 'call matchadd("Tdarkcyan", "G")',
2294 \ 'call matchadd("Tlightgrey", "H")',
2295 \ 'call matchadd("Tdarkgrey", "I")',
2296 \ 'call matchadd("Tred", "J")',
2297 \ 'call matchadd("Tgreen", "K")',
2298 \ 'call matchadd("Tyellow", "L")',
2299 \ 'call matchadd("Tblue", "M")',
2300 \ 'call matchadd("Tmagenta", "N")',
2301 \ 'call matchadd("Tcyan", "O")',
2302 \ 'call matchadd("Twhite", "P")',
2303 \ 'call matchadd("TdarkredBold", "X")',
2304 \ 'call matchadd("TgreenBold", "Y")',
2305 \ 'call matchadd("TmagentaBold", "Z")',
2306 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +01002307 \ ], 'Xcolorscript', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002308 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2309 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2310
2311 call term_sendkeys(buf, ":q\<CR>")
2312 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002313endfunc
2314
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002315function On_BufFilePost()
2316 doautocmd <nomodeline> User UserEvent
2317endfunction
2318
2319func Test_terminal_nested_autocmd()
2320 new
2321 call setline(1, range(500))
2322 $
2323 let lastline = line('.')
2324
2325 augroup TermTest
2326 autocmd BufFilePost * call On_BufFilePost()
2327 autocmd User UserEvent silent
2328 augroup END
2329
2330 let cmd = Get_cat_123_cmd()
2331 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2332 call assert_equal(lastline, line('.'))
2333
Bram Moolenaar64374752021-04-03 17:22:29 +02002334 let job = term_getjob(buf)
2335 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002336 call delete('Xtext')
2337 augroup TermTest
2338 au!
2339 augroup END
2340endfunc
2341
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002342func Test_terminal_adds_jump()
2343 clearjumps
2344 call term_start("ls", #{curwin: 1})
2345 call assert_equal(1, getjumplist()[0]->len())
2346 bwipe!
2347endfunc
2348
Bram Moolenaareea32af2021-11-21 14:51:13 +00002349func Close_cb(ch, ctx)
2350 call term_wait(a:ctx.bufnr)
2351 let g:close_done = 'done'
2352endfunc
2353
2354func Test_term_wait_in_close_cb()
2355 let g:close_done = ''
2356 let ctx = {}
2357 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2358 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2359
2360 call WaitForAssert({-> assert_equal("done", g:close_done)})
2361
2362 unlet g:close_done
2363 bwipe!
2364endfunc
2365
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01002366func Test_term_TextChangedT()
2367 augroup TermTest
2368 autocmd TextChangedT * ++once
2369 \ execute expand('<abuf>') . 'buffer' |
2370 \ let b:called = 1 |
2371 \ split |
2372 \ enew
2373 augroup END
2374
2375 terminal
2376
2377 let term_buf = bufnr()
2378
2379 let b:called = 0
2380
2381 call term_sendkeys(term_buf, "aaabbc\r")
2382 call TermWait(term_buf)
2383
2384 call assert_equal(1, getbufvar(term_buf, 'called'))
2385
2386 " Current buffer will be restored
2387 call assert_equal(bufnr(), term_buf)
2388
2389 bwipe!
2390 augroup TermTest
2391 au!
2392 augroup END
2393endfunc
2394
2395func Test_term_TextChangedT_close()
2396 augroup TermTest
2397 autocmd TextChangedT * ++once split | enew | 1close!
2398 augroup END
2399
2400 terminal
2401
2402 let term_buf = bufnr()
2403
2404 call term_sendkeys(term_buf, "aaabbc\r")
2405 call TermWait(term_buf)
2406
2407 " Current buffer will be restored
2408 call assert_equal(bufnr(), term_buf)
2409
2410 bwipe!
2411 augroup TermTest
2412 au!
2413 augroup END
2414endfunc
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002415
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002416" vim: shiftwidth=2 sts=2 expandtab