blob: d0ffe3bdbbbdccbfc29da263a445fc706740f2da [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
312endfunc
313
Bram Moolenaar1e115362019-01-09 23:01:02 +0100314func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200315 exe g:buf . 'bwipe!'
316 let g:buf = 0
317endfunc
318
Bram Moolenaar9d189612017-09-09 18:11:00 +0200319func Get_cat_123_cmd()
320 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100321 if !has('conpty')
322 return 'cmd /c "cls && color 2 && echo 123"'
323 else
324 " When clearing twice, extra sequence is not output.
325 return 'cmd /c "cls && cls && color 2 && echo 123"'
326 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200327 else
328 call writefile(["\<Esc>[32m123"], 'Xtext')
329 return "cat Xtext"
330 endif
331endfunc
332
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200333func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200334 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200335 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
336 let g:job = term_getjob(g:buf)
337
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200338 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
339 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200340 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100341 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200342 call delete('Xtext')
343endfunc
344
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200345func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200346 let l = term_scrape(a:buf, 0)
347 call assert_true(len(l) == 0)
348 let l = term_scrape(a:buf, 999)
349 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200350 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200351 call assert_true(len(l) > 0)
352 call assert_equal('1', l[0].chars)
353 call assert_equal('2', l[1].chars)
354 call assert_equal('3', l[2].chars)
355 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200356 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
357 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100358 if has('win32')
359 " On Windows 'background' always defaults to dark, even though the terminal
360 " may use a light background. Therefore accept both white and black.
361 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200362 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100363 if &background == 'light'
364 call assert_equal('#ffffff', l[0].bg)
365 else
366 call assert_equal('#000000', l[0].bg)
367 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200368 endif
369
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200370 let l = term_getline(a:buf, -1)
371 call assert_equal('', l)
372 let l = term_getline(a:buf, 0)
373 call assert_equal('', l)
374 let l = term_getline(a:buf, 999)
375 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200376 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200377 call assert_equal('123', l)
378endfunc
379
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200380func Test_terminal_scrape_123()
381 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200382 let buf = term_start(cmd)
383
384 let termlist = term_list()
385 call assert_equal(1, len(termlist))
386 call assert_equal(buf, termlist[0])
387
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200388 " Nothing happens with invalid buffer number
389 call term_wait(1234)
390
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200391 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200392 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200393 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200394 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200395 call Check_123(buf)
396
397 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100398 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200399 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200400 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200401 call Check_123(buf)
402
403 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200404 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200405endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200406
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200407func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200408 call writefile(["léttまrs"], 'Xtext')
409 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200410 " Run cmd with UTF-8 codepage to make the type command print the expected
411 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100412 let buf = term_start("cmd /K chcp 65001")
413 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200414 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100415 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200416 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100417 let buf = term_start("cat Xtext")
418 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200419 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200420
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100421 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
422 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200423 call assert_true(len(l) >= 7)
424 call assert_equal('l', l[0].chars)
425 call assert_equal('é', l[1].chars)
426 call assert_equal(1, l[1].width)
427 call assert_equal('t', l[2].chars)
428 call assert_equal('t', l[3].chars)
429 call assert_equal('ま', l[4].chars)
430 call assert_equal(2, l[4].width)
431 call assert_equal('r', l[5].chars)
432 call assert_equal('s', l[6].chars)
433
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100434 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200435 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200436 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200437
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100438 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200439 call delete('Xtext')
440endfunc
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()
458 call writefile(range(1, 200), 'Xtext')
459 if has('win32')
460 let cmd = 'cmd /c "type Xtext"'
461 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'
487 call delete('Xtext')
488endfunc
489
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200490func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200491 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200492 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200493 call writefile(range(150), 'Xtext')
494 if has('win32')
495 call term_sendkeys(buf, "type Xtext\<CR>")
496 else
497 call term_sendkeys(buf, "cat Xtext\<CR>")
498 endif
499 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200500 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200501 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200502 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200503 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200504
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200505 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200506 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200507 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100508 call delete('Xtext')
509endfunc
510
511func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200512 " tail -f only works on Unix
513 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100514
515 call writefile(range(50), 'Xtext')
516 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100517 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100518 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100519 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100520 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
521 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100522 \ 'call feedkeys("\<C-W>N", "xt")',
523 \ ], 'XTest_postponed')
524 let buf = RunVimInTerminal('-S XTest_postponed', {})
525 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100526 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100527
528 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200529 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100530 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100531 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100532
533 " Back to Terminal-Job mode, text will scroll and show the extra line.
534 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100535 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100536
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100537 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100538 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200539 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100540 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100541 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200542 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100543 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100544 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
545
546 " check type of term_sendkeys() return value
547 echo type(tsk_ret)
548
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100549 call StopVimInTerminal(buf)
550 call delete('XTest_postponed')
551 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200552endfunc
553
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100554" Run diff on two dumps with different size.
555func Test_terminal_dumpdiff_size()
556 call assert_equal(1, winnr('$'))
557 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
558 call assert_equal(2, winnr('$'))
559 call assert_match('Test_incsearch_search_01.dump', getline(10))
560 call assert_match(' +++++$', getline(11))
561 call assert_match('Test_popup_command_01.dump', getline(31))
562 call assert_equal(repeat('+', 75), getline(30))
563 quit
564endfunc
565
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200566func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200567 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200568
Bram Moolenaarb2412082017-08-20 18:09:14 +0200569 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200570 let size = term_getsize('')
571 bwipe!
572 call assert_equal(5, size[0])
573
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200574 call term_start(cmd, {'term_rows': 6})
575 let size = term_getsize('')
576 bwipe!
577 call assert_equal(6, size[0])
578
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200579 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200580 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200581 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200582
583 call term_setsize('', 6, 0)
584 call assert_equal([6, 33], term_getsize(''))
585
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200586 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200587 call assert_equal([6, 35], term_getsize(''))
588
589 call term_setsize('', 7, 30)
590 call assert_equal([7, 30], term_getsize(''))
591
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200592 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200593 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200594
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200595 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
596 let size = term_getsize('')
597 bwipe!
598 call assert_equal([6, 36], size)
599
Bram Moolenaarb2412082017-08-20 18:09:14 +0200600 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200601 let size = term_getsize('')
602 bwipe!
603 call assert_equal(20, size[1])
604
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200605 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200606 let size = term_getsize('')
607 bwipe!
608 call assert_equal(26, size[1])
609
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200610 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200611 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200612 let size = term_getsize('')
613 bwipe!
614 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200615
616 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
617 let size = term_getsize('')
618 bwipe!
619 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200620
Bram Moolenaar5300be62021-11-13 10:27:40 +0000621 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
622 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100623 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000624
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200625 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200626endfunc
627
Bram Moolenaareba13e42021-02-23 17:47:23 +0100628func Test_terminal_zero_height()
629 split
630 wincmd j
631 anoremenu 1.1 WinBar.test :
632 terminal ++curwin
633 wincmd k
634 wincmd _
635 redraw
636
637 call term_sendkeys(bufnr(), "exit\r")
638 bwipe!
639endfunc
640
Bram Moolenaarda43b612017-08-11 22:27:50 +0200641func Test_terminal_curwin()
642 let cmd = Get_cat_123_cmd()
643 call assert_equal(1, winnr('$'))
644
Bram Moolenaarb1009092020-05-31 16:04:42 +0200645 split Xdummy
646 call setline(1, 'dummy')
647 write
648 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200649 exe 'terminal ++curwin ' . cmd
650 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200651 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200652 bwipe!
653
Bram Moolenaarb1009092020-05-31 16:04:42 +0200654 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200655 call term_start(cmd, {'curwin': 1})
656 call assert_equal(2, winnr('$'))
657 bwipe!
658
Bram Moolenaarb1009092020-05-31 16:04:42 +0200659 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200660 call setline(1, 'change')
661 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
662 call assert_equal(2, winnr('$'))
663 exe 'terminal! ++curwin ' . cmd
664 call assert_equal(2, winnr('$'))
665 bwipe!
666
Bram Moolenaarb1009092020-05-31 16:04:42 +0200667 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200668 call setline(1, 'change')
669 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
670 call assert_equal(2, winnr('$'))
671 bwipe!
672
Bram Moolenaarb1009092020-05-31 16:04:42 +0200673 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200674 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200675 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200676 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200677endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200678
Bram Moolenaarff546792017-11-21 14:47:57 +0100679func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200680 if s:python != ''
681 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200682 " 500 was not enough for Travis
683 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200684 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200685 echo 'This will take five seconds...'
686 let waittime = 2000
687 if has('win32')
688 let cmd = $windir . '\system32\timeout.exe 1'
689 else
690 let cmd = 'sleep 1'
691 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200692 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100693 return [cmd, waittime]
694endfunc
695
696func Test_terminal_finish_open_close()
697 call assert_equal(1, winnr('$'))
698
699 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200700
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100701 " shell terminal closes automatically
702 terminal
703 let buf = bufnr('%')
704 call assert_equal(2, winnr('$'))
705 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200706 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200707 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200708 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100709
710 " shell terminal that does not close automatically
711 terminal ++noclose
712 let buf = bufnr('%')
713 call assert_equal(2, winnr('$'))
714 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200715 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200716 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100717 call assert_equal(2, winnr('$'))
718 quit
719 call assert_equal(1, winnr('$'))
720
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200721 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200722 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200723 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200724 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200725
726 call term_start(cmd, {'term_finish': 'close'})
727 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200728 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200729 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200730 call assert_equal(1, winnr('$'))
731
732 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200733 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200734 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200735 bwipe
736
737 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200738 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200739 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200740 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200741
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200742 exe 'terminal ++hidden ++open ' . cmd
743 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200744 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200745 bwipe
746
747 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
748 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200749 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200750 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200751
752 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
753 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
754 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
755 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
756
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100757 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 +0200758 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200759 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200760 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100761 call assert_equal('opened the buffer in a window', g:result)
762 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200763 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200764endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200765
766func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100767 if has('win32')
768 let cmd = 'cmd /c cd'
769 else
770 CheckExecutable pwd
771 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200772 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100773 call mkdir('Xtermdir')
774 let buf = term_start(cmd, {'cwd': 'Xtermdir'})
Bram Moolenaaree7c8d92022-09-22 12:57:06 +0100775 " if the path is very long it may be split over two lines, join them
776 " together
777 call WaitForAssert({-> assert_equal('Xtermdir', fnamemodify(getline(1) .. getline(2), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200778
779 exe buf . 'bwipe'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100780 call delete('Xtermdir', 'rf')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200781endfunc
782
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200783func Test_terminal_cwd_failure()
784 " Case 1: Provided directory is not actually a directory. Attempt to make
785 " the file executable as well.
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100786 call writefile([], 'Xtcfile')
Dominique Pelle91a874e2022-09-03 10:59:32 +0100787 call setfperm('Xtcfile', 'rwx------')
788 call assert_fails("call term_start(&shell, {'cwd': 'Xtcfile'})", 'E475:')
789 call delete('Xtcfile')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200790
791 " Case 2: Directory does not exist.
792 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
793
794 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100795 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200796 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100797 call mkdir('XdirNoAccess', '', '0600')
798 " return early if the directory permissions could not be set properly
799 if getfperm('XdirNoAccess')[2] == 'x'
800 call delete('XdirNoAccess', 'rf')
801 return
802 endif
803 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
804 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200805 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200806endfunc
807
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100808func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200809 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200810 call s:test_environment("VIM_SERVERNAME", v:servername)
811endfunc
812
813func Test_terminal_version()
814 call s:test_environment("VIM_TERMINAL", string(v:version))
815endfunc
816
817func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100818 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100819 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200820 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100821 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200822 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100823 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200824 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100825 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200826 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200827 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200828 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100829
Bram Moolenaar012eb662018-03-13 17:55:27 +0100830 exe buf . 'bwipe'
831 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100832endfunc
833
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200834func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100835 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200836 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200837 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100838 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100839 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100840 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100841 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100842 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200843 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200844 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200845 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200846
Bram Moolenaar012eb662018-03-13 17:55:27 +0100847 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200848endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200849
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200850func Test_terminal_list_args()
851 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Yee Cheng Chin15b314f2022-10-09 18:53:32 +0100852 call assert_fails(buf . 'bwipe', 'E948:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200853 exe buf . 'bwipe!'
854 call assert_equal("", bufname(buf))
855endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200856
857func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100858 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100859 let buf = term_start(&shell)
Zdenek Dohnaldd2dfb32022-02-23 14:25:17 +0000860 " Starting a terminal can be slow, esp. on busy CI machines.
861 let wait_time = 7500
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100862 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100863 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200864 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100865 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200866 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100867 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100868 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100869 " NPFS is used in Windows, nonblocking mode does not work properly.
870 elseif has('win32')
871 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200872 else
873 let len = 5000
874 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200875
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100876 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100877 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100878 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200879 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100880 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200881
882 " On MS-Windows there is an extra empty line below "done". Find "done" in
883 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100884 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100885 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100886 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200887 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100888 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200889 endif
890 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200891
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100892 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200893 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200894 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200895 bwipe
896endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200897
898func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100899 " TODO: enable once writing to stdin works on MS-Windows
900 CheckNotMSWindows
901 CheckExecutable wc
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100902 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100903
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200904 call setline(1, ['one', 'two', 'three'])
905 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200906 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200907 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200908 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100909 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200910
911 call setline(1, ['one', 'two', 'three', 'four'])
912 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200913 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200914 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200915 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100916 %bwipe!
917endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200918
Bram Moolenaar21109272020-01-30 16:27:20 +0100919func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200920 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100921 let g:test_is_flaky = 1
Bram Moolenaardada6d22017-09-02 17:18:35 +0200922
Bram Moolenaar21109272020-01-30 16:27:20 +0100923 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200924 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100925 " MS-Windows echoes the input, Unix doesn't.
926 if has('win32')
927 call WaitFor({-> getline('$') =~ 'exit(123)'})
928 call assert_equal('hello', getline(line('$') - 1))
929 else
930 call WaitFor({-> getline('$') =~ 'hello'})
931 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200932 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100933 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
934 %bwipe!
935endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200936
Bram Moolenaar21109272020-01-30 16:27:20 +0100937func Test_terminal_eof_arg_win32_ctrl_z()
938 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200939 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100940 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100941
942 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200943 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100944 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
945 call assert_match('\^Z', getline(line('$') - 1))
946 %bwipe!
947endfunc
948
949func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200950 call CheckPython(s:python)
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100951 let g:test_is_flaky = 1
Bram Moolenaar21109272020-01-30 16:27:20 +0100952
Bram Moolenaar62eb2392022-06-15 17:52:44 +0100953 " Check the last specified ++eof arg is used and does not leak memory.
Bram Moolenaar21109272020-01-30 16:27:20 +0100954 new
955 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200956 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100957 " MS-Windows echoes the input, Unix doesn't.
958 if has('win32')
959 call WaitFor({-> getline('$') =~ 'exit(123)'})
960 call assert_equal('hello', getline(line('$') - 1))
961 else
962 call WaitFor({-> getline('$') =~ 'hello'})
963 call assert_equal('hello', getline('$'))
964 endif
965 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
966 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200967endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200968
969func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100970 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200971 let buf = term_start('NONE', {})
972 call assert_notequal(0, buf)
973
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200974 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200975 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100976 if has('gui_running') && !has('win32')
977 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200978 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100979 else
980 " Otherwise using a job works on all systems.
981 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200982 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200983 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200984
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200985 bwipe!
986endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200987
988func Test_terminal_special_chars()
989 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200990 CheckUnix
991
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200992 call mkdir('Xdir with spaces')
993 call writefile(['x'], 'Xdir with spaces/quoted"file')
994 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200995 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100996 " make sure the job has finished
997 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200998
999 call delete('Xdir with spaces', 'rf')
1000 bwipe
1001endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001002
1003func Test_terminal_wrong_options()
1004 call assert_fails('call term_start(&shell, {
1005 \ "in_io": "file",
1006 \ "in_name": "xxx",
1007 \ "out_io": "file",
1008 \ "out_name": "xxx",
1009 \ "err_io": "file",
1010 \ "err_name": "xxx"
1011 \ })', 'E474:')
1012 call assert_fails('call term_start(&shell, {
1013 \ "out_buf": bufnr("%")
1014 \ })', 'E474:')
1015 call assert_fails('call term_start(&shell, {
1016 \ "err_buf": bufnr("%")
1017 \ })', 'E474:')
1018endfunc
1019
1020func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001021 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001022 let cmd = Get_cat_123_cmd()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001023 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001024 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001025 " ConPTY may precede escape sequence. There are things that are not so.
1026 if !has('conpty')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001027 call WaitForAssert({-> assert_notequal(0, len(readfile("Xtrfile")))})
1028 call assert_match('123', readfile('Xtrfile')[0])
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001029 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001030 let g:job = term_getjob(buf)
1031 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +02001032
1033 if has('win32')
1034 " On Windows we cannot delete a file being used by a process. When
1035 " job_status() returns "dead", the process remains for a short time.
1036 " Just wait for a moment.
1037 sleep 50m
1038 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001039 call delete('Xtrfile')
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001040 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001041
1042 if has('unix')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001043 call writefile(['one line'], 'Xtrfile')
1044 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xtrfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001045 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +02001047 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001049 bwipe
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001050 call delete('Xtrfile')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +02001051 endif
1052endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001053
1054func TerminalTmap(remap)
1055 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +01001056 " Wait for the shell to display a prompt
1057 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001058 call assert_equal('t', mode())
1059
1060 if a:remap
1061 tmap 123 456
1062 else
1063 tnoremap 123 456
1064 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +01001065 " don't use abcde, it's an existing command
1066 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001067 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +01001068 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001069 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001070 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001071 let lnum = term_getcursor(buf)[0]
1072 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +01001073 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001074 else
1075 call assert_match('456', term_getline(buf, lnum))
1076 endif
1077
1078 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001079 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001080
1081 tunmap 123
1082 tunmap 456
1083 call assert_equal('', maparg('123', 't'))
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +01001084 exe buf . 'bwipe'
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001085 unlet g:job
1086endfunc
1087
1088func Test_terminal_tmap()
1089 call TerminalTmap(1)
1090 call TerminalTmap(0)
1091endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001092
1093func Test_terminal_wall()
1094 let buf = Run_shell_in_terminal({})
1095 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001096 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +02001097 exe buf . 'bwipe'
1098 unlet g:job
1099endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001100
Bram Moolenaar7a760922018-02-19 23:10:02 +01001101func Test_terminal_wqall()
1102 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001103 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001104 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +01001105 exe buf . 'bwipe'
1106 unlet g:job
1107endfunc
1108
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001109func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001110 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001111 let save_enc = &encoding
1112 set encoding=utf-8
1113
1114 if has('win32')
1115 let cmd = "cmd /K chcp 65001"
1116 let lnum = [3, 6, 9]
1117 else
1118 let cmd = &shell
1119 let lnum = [1, 3, 5]
1120 endif
1121
1122 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +02001123 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001124 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +02001125 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001126
Bram Moolenaarebe74b72018-04-21 23:34:43 +02001127 if has('win32')
1128 call assert_equal('cmd', job_info(g:job).cmd[0])
1129 else
1130 call assert_equal(&shell, job_info(g:job).cmd[0])
1131 endif
1132
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001133 " ascii + composing
1134 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001135 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001136 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001137 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001138 call term_sendkeys(buf, "\<cr>")
1139 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001140 let l = term_scrape(buf, lnum[0] + 1)
1141 call assert_equal("a\u0308", l[0].chars)
1142 call assert_equal("b", l[1].chars)
1143 call assert_equal("c", l[2].chars)
1144
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001145 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001146 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001147 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001148 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001149 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001150 call term_sendkeys(buf, "\<cr>")
1151 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001152 let l = term_scrape(buf, lnum[1] + 1)
1153 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001154 call assert_equal(2, l[0].width)
1155 call assert_equal("\u304e", l[1].chars)
1156 call assert_equal(2, l[1].width)
1157 call assert_equal("\u304f\u3099", l[2].chars)
1158 call assert_equal(2, l[2].width)
1159 call assert_equal("\u3052", l[3].chars)
1160 call assert_equal(2, l[3].width)
1161 call assert_equal("\u3053\u3099", l[4].chars)
1162 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001163
1164 " \u00a0 + composing
1165 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001166 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001167 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001168 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001169 call term_sendkeys(buf, "\<cr>")
1170 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001171 let l = term_scrape(buf, lnum[2] + 1)
1172 call assert_equal("\u00a0\u0308", l[3].chars)
1173
1174 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001175 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001176 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001177 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001178 let &encoding = save_enc
1179endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001180
1181func Test_terminal_aucmd_on_close()
1182 fun Nop()
1183 let s:called = 1
1184 endfun
1185
1186 aug repro
1187 au!
1188 au BufWinLeave * call Nop()
1189 aug END
1190
1191 let [cmd, waittime] = s:get_sleep_cmd()
1192
1193 call assert_equal(1, winnr('$'))
1194 new
1195 call setline(1, ['one', 'two'])
1196 exe 'term ++close ' . cmd
1197 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001198 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001199 call assert_equal(1, s:called)
1200 bwipe!
1201
1202 unlet s:called
1203 au! repro
1204 delfunc Nop
1205endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001206
1207func Test_terminal_term_start_empty_command()
1208 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001209 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001210 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001211 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001212 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001213 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001214 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001215 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001216 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001217 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001218 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001219 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001220 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001221 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001222 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001223 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001224 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001225 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001226 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1227 call assert_fails(cmd, 'E475:')
1228 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001229 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001230 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001231 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1232 call assert_fails(cmd, 'E475:')
1233 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1234 call assert_fails(cmd, 'E730:')
1235 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001236 if has('gui_running') || has('termguicolors')
1237 call assert_fails(cmd, 'E475:')
1238 else
1239 call assert_fails(cmd, 'E254:')
1240 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001241 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001242endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001243
1244func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001245 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001246
1247 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001248 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001249
Bram Moolenaar086eb872018-03-25 21:24:12 +02001250 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001251 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001252
Bram Moolenaar086eb872018-03-25 21:24:12 +02001253 " Request the cursor position.
1254 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001255
1256 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001257 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001258
Bram Moolenaar086eb872018-03-25 21:24:12 +02001259 " End "cat" gently.
1260 call term_sendkeys(buf, "\<CR>\<C-D>")
1261
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001262 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001263 exe buf . 'bwipe'
1264 unlet g:job
1265endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001266
Bram Moolenaarc2958582021-12-14 11:16:31 +00001267" Run this first, it fails when run after other tests.
1268func Test_aa_terminal_focus_events()
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001269 CheckNotGui
1270 CheckUnix
1271 CheckRunVimInTerminal
1272
1273 let save_term = &term
1274 let save_ttymouse = &ttymouse
1275 set term=xterm ttymouse=xterm2
1276
1277 let lines =<< trim END
1278 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001279 au FocusLost * call setline(1, 'I am lost') | set nomod
1280 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001281 END
1282 call writefile(lines, 'XtermFocus')
1283 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1284
1285 " Send a focus event to ourselves, it should be forwarded to the terminal
1286 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001287 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1288
1289 call feedkeys("\<Esc>[I", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001290 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1291
Bram Moolenaare5050712021-12-09 10:51:05 +00001292 " check that a command line being edited is redrawn in place
1293 call term_sendkeys(buf, ":" .. repeat('x', 80))
1294 call TermWait(buf)
1295 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaare5050712021-12-09 10:51:05 +00001296 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1297 call term_sendkeys(buf, "\<Esc>")
1298
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001299 call StopVimInTerminal(buf)
1300 call delete('XtermFocus')
1301 let &term = save_term
1302 let &ttymouse = save_ttymouse
1303endfunc
1304
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001305" Run Vim, start a terminal in that Vim with the kill argument,
1306" :qall works.
1307func Run_terminal_qall_kill(line1, line2)
1308 " 1. Open a terminal window and wait for the prompt to appear
1309 " 2. set kill using term_setkill()
1310 " 3. make Vim exit, it will kill the shell
1311 let after = [
1312 \ a:line1,
1313 \ 'let buf = bufnr("%")',
1314 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1315 \ ' sleep 10m',
1316 \ 'endwhile',
1317 \ a:line2,
1318 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1319 \ 'qall',
1320 \ ]
1321 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001322 return
1323 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001324 call assert_equal("done", readfile("Xdone")[0])
1325 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001326endfunc
1327
1328" Run Vim in a terminal, then start a terminal in that Vim with a kill
1329" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001330func Test_terminal_qall_kill_arg()
1331 call Run_terminal_qall_kill('term ++kill=kill', '')
1332endfunc
1333
1334" Run Vim, start a terminal in that Vim, set the kill argument with
1335" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001336func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001337 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001338endfunc
1339
1340" Run Vim, start a terminal in that Vim without the kill argument,
1341" check that :qall does not exit, :qall! does.
1342func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001343 let after =<< trim [CODE]
1344 term
1345 let buf = bufnr("%")
1346 while term_getline(buf, 1) =~ "^\\s*$"
1347 sleep 10m
1348 endwhile
1349 set nomore
1350 au VimLeavePre * call writefile(["too early"], "Xdone")
1351 qall
1352 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1353 cquit
1354 [CODE]
1355
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001356 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001357 return
1358 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001359 call assert_equal("done", readfile("Xdone")[0])
1360 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001361endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001362
1363" Run Vim in a terminal, then start a terminal in that Vim without a kill
1364" argument, check that :confirm qall works.
1365func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001366 CheckRunVimInTerminal
Bram Moolenaare564c702022-06-14 15:00:28 +01001367
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001368 let buf = RunVimInTerminal('', {})
1369
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001370 " the shell may set the window title, we don't want that here
Bram Moolenaar377d92a2022-06-14 21:22:12 +01001371 call term_sendkeys(buf, ":call test_override('vterm_title', 1)\<CR>")
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001372
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001373 " Open a terminal window and wait for the prompt to appear
1374 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001375 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1376 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001377
1378 " make Vim exit, it will prompt to kill the shell
1379 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001380 call WaitForAssert({-> assert_match('\[Y\]es, (N)o:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001381 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001382 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001383
1384 " close the terminal window where Vim was running
1385 quit
1386endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001387
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001388" Run Vim in a terminal, then start a terminal window with a shell and check
1389" that Vim exits if it is closed.
1390func Test_terminal_exit()
1391 CheckRunVimInTerminal
1392
1393 let lines =<< trim END
1394 let winid = win_getid()
1395 help
1396 term
1397 let termid = win_getid()
1398 call win_gotoid(winid)
1399 close
1400 call win_gotoid(termid)
1401 END
1402 call writefile(lines, 'XtermExit')
1403 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1404 let job = term_getjob(buf)
1405 call WaitForAssert({-> assert_equal("run", job_status(job))})
1406
1407 " quit the shell, it will make Vim exit
1408 call term_sendkeys(buf, "exit\<CR>")
1409 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1410
1411 call delete('XtermExit')
1412endfunc
1413
Bram Moolenaar012eb662018-03-13 17:55:27 +01001414func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001415 augroup repro
1416 au!
1417 au TerminalOpen * let s:called += 1
1418 augroup END
1419
1420 let s:called = 0
1421
1422 " Open a terminal window with :terminal
1423 terminal
1424 call assert_equal(1, s:called)
1425 bwipe!
1426
1427 " Open a terminal window with term_start()
1428 call term_start(&shell)
1429 call assert_equal(2, s:called)
1430 bwipe!
1431
1432 " Open a hidden terminal buffer with :terminal
1433 terminal ++hidden
1434 call assert_equal(3, s:called)
1435 for buf in term_list()
1436 exe buf . "bwipe!"
1437 endfor
1438
1439 " Open a hidden terminal buffer with term_start()
1440 let buf = term_start(&shell, {'hidden': 1})
1441 call assert_equal(4, s:called)
1442 exe buf . "bwipe!"
1443
1444 unlet s:called
1445 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001446endfunc
1447
1448func Test_open_term_from_cmd()
1449 CheckUnix
1450 CheckRunVimInTerminal
1451
1452 let lines =<< trim END
1453 call setline(1, ['a', 'b', 'c'])
1454 3
1455 set incsearch
1456 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1457 END
1458 call writefile(lines, 'Xopenterm')
1459 let buf = RunVimInTerminal('-S Xopenterm', {})
1460
1461 " this opens a window, incsearch should not use the old cursor position
1462 call term_sendkeys(buf, "/\<F3>")
1463 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1464 call term_sendkeys(buf, "\<Esc>")
1465 call term_sendkeys(buf, ":q\<CR>")
1466
1467 call StopVimInTerminal(buf)
1468 call delete('Xopenterm')
1469endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001470
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001471func Test_combining_double_width()
1472 CheckUnix
1473 CheckRunVimInTerminal
1474
1475 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1476 let lines =<< trim END
1477 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1478 END
1479 call writefile(lines, 'Xcombining')
1480 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1481
1482 " this opens a window, incsearch should not use the old cursor position
1483 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1484 call term_sendkeys(buf, ":q\<CR>")
1485
1486 call StopVimInTerminal(buf)
1487 call delete('Xonedouble')
1488 call delete('Xcombining')
1489endfunc
1490
Bram Moolenaar02764712020-11-14 20:21:55 +01001491func Test_terminal_popup_with_cmd()
1492 " this was crashing
1493 let buf = term_start(&shell, #{hidden: v:true})
1494 let s:winid = popup_create(buf, {})
1495 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1496 call feedkeys("\<F3>", 'xt')
1497
1498 tunmap <F3>
1499 exe 'bwipe! ' .. buf
1500 unlet s:winid
1501endfunc
1502
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001503func Test_terminal_popup_bufload()
1504 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1505 let winid = popup_create(termbuf, {})
1506 sleep 50m
1507
1508 let newbuf = bufadd('')
1509 call bufload(newbuf)
1510 call setbufline(newbuf, 1, 'foobar')
1511
1512 " must not have switched to another window
1513 call assert_equal(winid, win_getid())
1514
Bram Moolenaare6329e42020-11-16 21:10:34 +01001515 call StopShellInTerminal(termbuf)
1516 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001517 exe 'bwipe! ' .. newbuf
1518endfunc
1519
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001520func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001521 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001522 CheckUnix
1523
1524 " use "sh" instead of "&shell" in the hope it will use a short prompt
1525 let lines =<< trim END
1526 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1527 exe 'buffer ' .. termbuf
1528
1529 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1530 sleep 50m
1531
1532 call term_sendkeys(termbuf, "echo 'test'")
1533 END
1534 call writefile(lines, 'XpopupScript')
1535 let buf = RunVimInTerminal('-S XpopupScript', {})
1536
1537 " typed text appears both in normal window and in popup
1538 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1539 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1540
Bram Moolenaar0407d272021-12-13 22:17:44 +00001541 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1542 call TermWait(buf)
1543 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001544 call StopVimInTerminal(buf)
1545 call delete('XpopupScript')
1546endfunc
1547
Bram Moolenaare41decc2020-11-14 21:34:59 +01001548func Test_terminal_popup_insert_cmd()
1549 CheckUnix
1550
1551 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1552 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001553 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001554 endfunc
1555 call feedkeys("i\<F3>")
1556 sleep 10m
1557 call assert_equal('n', mode())
1558
1559 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001560 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001561 delfunc StartTermInPopup
1562 iunmap <F3>
1563endfunc
1564
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001565func Check_dump01(off)
1566 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1567 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001568 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001569endfunc
1570
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001571func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001572 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001573
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001574 let save_enc = &encoding
1575 set encoding=utf-8
1576 call assert_equal(1, winnr('$'))
1577
1578 let text = " a\u0300 e\u0302 o\u0308"
1579 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001580 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001581 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001582 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001583 let dumpline = readfile('Xdump')[0]
1584 call assert_match('|à| |ê| |ö', dumpline)
1585
1586 call StopVimInTerminal(buf)
1587 call delete('Xcomposing')
1588 call delete('Xdump')
1589 let &encoding = save_enc
1590endfunc
1591
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001592" Tests for failures in the term_dumpwrite() function
1593func Test_terminal_dumpwrite_errors()
1594 CheckRunVimInTerminal
1595 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1596 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001597 call TermWait(buf)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001598 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E1206:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001599 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1600 call writefile([], 'Xtest.dump')
1601 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1602 call delete('Xtest.dump')
1603 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1604 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001605 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001606 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001607 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001608 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1609 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1610 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1611endfunc
1612
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001613" just testing basic functionality.
1614func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001615 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001616 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001617 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001618 call assert_equal(2, winnr('$'))
1619 call assert_equal(20, line('$'))
1620 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001621
1622 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001623 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001624 call assert_equal(buf, buf2)
1625 call assert_notequal('one two three four five', trim(getline(1)))
1626
1627 " Load the first dump again in the same window
1628 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1629 call assert_equal(buf, buf2)
1630 call Check_dump01(0)
1631
1632 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1633 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1634 new
1635 let closedbuf = winbufnr('')
1636 quit
1637 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001638 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001639 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001640
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001641 quit
1642endfunc
1643
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001644func Test_terminal_dumpload_dump()
1645 CheckRunVimInTerminal
1646
1647 let lines =<< trim END
1648 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1649 END
1650 call writefile(lines, 'XtermDumpload')
1651 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1652 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1653
1654 call StopVimInTerminal(buf)
1655 call delete('XtermDumpload')
1656endfunc
1657
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001658func Test_terminal_dumpdiff()
1659 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001660 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001661 call assert_equal(2, winnr('$'))
1662 call assert_equal(62, line('$'))
1663 call Check_dump01(0)
1664 call Check_dump01(42)
1665 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1666 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001667
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001668 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001669 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1670 call writefile([], 'X1.dump')
1671 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1672 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001673endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001674
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001675func Test_terminal_dumpdiff_swap()
1676 call assert_equal(1, winnr('$'))
1677 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1678 call assert_equal(2, winnr('$'))
1679 call assert_equal(62, line('$'))
1680 call assert_match('Test_popup_command_01.dump', getline(21))
1681 call assert_match('Test_popup_command_03.dump', getline(42))
1682 call assert_match('Undo', getline(3))
1683 call assert_match('three four five', getline(45))
1684
1685 normal s
1686 call assert_match('Test_popup_command_03.dump', getline(21))
1687 call assert_match('Test_popup_command_01.dump', getline(42))
1688 call assert_match('three four five', getline(3))
1689 call assert_match('Undo', getline(45))
1690 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001691
1692 " Diff two terminal dump files with different number of rows
1693 " Swap the diffs
1694 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1695 call assert_match('Test_popup_command_01.dump', getline(21))
1696 call assert_match('Test_winline_rnu.dump', getline(42))
1697 normal s
1698 call assert_match('Test_winline_rnu.dump', getline(6))
1699 call assert_match('Test_popup_command_01.dump', getline(27))
1700 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001701endfunc
1702
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001703func Test_terminal_dumpdiff_options()
1704 set laststatus=0
1705 call assert_equal(1, winnr('$'))
1706 let height = winheight(0)
1707 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1708 call assert_equal(2, winnr('$'))
1709 call assert_equal(height, winheight(winnr()))
1710 call assert_equal(33, winwidth(winnr()))
1711 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1712 quit
1713
1714 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001715 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1716 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001717 call assert_equal(&columns, winwidth(0))
1718 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001719 call assert_equal('something else', bufname('%'))
1720 quit
1721
1722 call assert_equal(1, winnr('$'))
1723 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1724 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001725 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 +01001726 bwipe
1727
1728 set laststatus&
1729endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001730
Bram Moolenaar10772302019-01-20 18:25:54 +01001731" When drawing the statusline the cursor position may not have been updated
1732" yet.
1733" 1. create a terminal, make it show 2 lines
1734" 2. 0.5 sec later: leave terminal window, execute "i"
1735" 3. 0.5 sec later: clear terminal window, now it's 1 line
1736" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1737" 4. 0.5 sec later: should be done, clean up
1738func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001739 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001740 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001741
Bram Moolenaar10772302019-01-20 18:25:54 +01001742 set statusline=x
1743 terminal
1744 let tbuf = bufnr('')
1745 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1746 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1747 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1748 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1749
1750 sleep 2
1751 exe tbuf . 'bwipe!'
1752 au! BufLeave
1753 set statusline=
1754endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001755
Bram Moolenaar81035272021-12-16 18:02:07 +00001756func CheckTerminalWindowWorks(buf)
1757 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1758 call term_sendkeys(a:buf, "exit\<CR>")
1759 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1760 call term_sendkeys(a:buf, ":q\<CR>")
1761 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1762endfunc
1763
1764func Test_start_terminal_from_timer()
1765 CheckUnix
1766 CheckFeature timers
1767
1768 " Open a terminal window from a timer, typed text goes to the terminal
1769 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm')
1770 let buf = RunVimInTerminal('-S XtimerTerm', {})
1771 call CheckTerminalWindowWorks(buf)
1772
1773 " do the same in Insert mode
1774 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1775 call CheckTerminalWindowWorks(buf)
1776
1777 call StopVimInTerminal(buf)
1778 call delete('XtimerTerm')
1779endfunc
1780
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001781func Test_terminal_window_focus()
1782 let winid1 = win_getid()
1783 terminal
1784 let winid2 = win_getid()
1785 call feedkeys("\<C-W>j", 'xt')
1786 call assert_equal(winid1, win_getid())
1787 call feedkeys("\<C-W>k", 'xt')
1788 call assert_equal(winid2, win_getid())
1789 " can use a cursor key here
1790 call feedkeys("\<C-W>\<Down>", 'xt')
1791 call assert_equal(winid1, win_getid())
1792 call feedkeys("\<C-W>\<Up>", 'xt')
1793 call assert_equal(winid2, win_getid())
1794
1795 bwipe!
1796endfunc
1797
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001798func Api_drop_common(options)
1799 call assert_equal(1, winnr('$'))
1800
1801 " Use the title termcap entries to output the escape sequence.
1802 call writefile([
1803 \ 'set title',
1804 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1805 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1806 \ 'redraw',
1807 \ "set t_ts=",
1808 \ ], 'Xscript')
1809 let buf = RunVimInTerminal('-S Xscript', {})
1810 call WaitFor({-> bufnr('Xtextfile') > 0})
1811 call assert_equal('Xtextfile', expand('%:t'))
1812 call assert_true(winnr('$') >= 3)
1813 return buf
1814endfunc
1815
1816func Test_terminal_api_drop_newwin()
1817 CheckRunVimInTerminal
1818 let buf = Api_drop_common('')
1819 call assert_equal(0, &bin)
1820 call assert_equal('', &fenc)
1821
1822 call StopVimInTerminal(buf)
1823 call delete('Xscript')
1824 bwipe Xtextfile
1825endfunc
1826
1827func Test_terminal_api_drop_newwin_bin()
1828 CheckRunVimInTerminal
1829 let buf = Api_drop_common(',{"bin":1}')
1830 call assert_equal(1, &bin)
1831
1832 call StopVimInTerminal(buf)
1833 call delete('Xscript')
1834 bwipe Xtextfile
1835endfunc
1836
1837func Test_terminal_api_drop_newwin_binary()
1838 CheckRunVimInTerminal
1839 let buf = Api_drop_common(',{"binary":1}')
1840 call assert_equal(1, &bin)
1841
1842 call StopVimInTerminal(buf)
1843 call delete('Xscript')
1844 bwipe Xtextfile
1845endfunc
1846
1847func Test_terminal_api_drop_newwin_nobin()
1848 CheckRunVimInTerminal
1849 set binary
1850 let buf = Api_drop_common(',{"nobin":1}')
1851 call assert_equal(0, &bin)
1852
1853 call StopVimInTerminal(buf)
1854 call delete('Xscript')
1855 bwipe Xtextfile
1856 set nobinary
1857endfunc
1858
1859func Test_terminal_api_drop_newwin_nobinary()
1860 CheckRunVimInTerminal
1861 set binary
1862 let buf = Api_drop_common(',{"nobinary":1}')
1863 call assert_equal(0, &bin)
1864
1865 call StopVimInTerminal(buf)
1866 call delete('Xscript')
1867 bwipe Xtextfile
1868 set nobinary
1869endfunc
1870
1871func Test_terminal_api_drop_newwin_ff()
1872 CheckRunVimInTerminal
1873 let buf = Api_drop_common(',{"ff":"dos"}')
1874 call assert_equal("dos", &ff)
1875
1876 call StopVimInTerminal(buf)
1877 call delete('Xscript')
1878 bwipe Xtextfile
1879endfunc
1880
1881func Test_terminal_api_drop_newwin_fileformat()
1882 CheckRunVimInTerminal
1883 let buf = Api_drop_common(',{"fileformat":"dos"}')
1884 call assert_equal("dos", &ff)
1885
1886 call StopVimInTerminal(buf)
1887 call delete('Xscript')
1888 bwipe Xtextfile
1889endfunc
1890
1891func Test_terminal_api_drop_newwin_enc()
1892 CheckRunVimInTerminal
1893 let buf = Api_drop_common(',{"enc":"utf-16"}')
1894 call assert_equal("utf-16", &fenc)
1895
1896 call StopVimInTerminal(buf)
1897 call delete('Xscript')
1898 bwipe Xtextfile
1899endfunc
1900
1901func Test_terminal_api_drop_newwin_encoding()
1902 CheckRunVimInTerminal
1903 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1904 call assert_equal("utf-16", &fenc)
1905
1906 call StopVimInTerminal(buf)
1907 call delete('Xscript')
1908 bwipe Xtextfile
1909endfunc
1910
1911func Test_terminal_api_drop_oldwin()
1912 CheckRunVimInTerminal
1913 let firstwinid = win_getid()
1914 split Xtextfile
1915 let textfile_winid = win_getid()
1916 call assert_equal(2, winnr('$'))
1917 call win_gotoid(firstwinid)
1918
1919 " Use the title termcap entries to output the escape sequence.
1920 call writefile([
1921 \ 'set title',
1922 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1923 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1924 \ 'redraw',
1925 \ "set t_ts=",
1926 \ ], 'Xscript')
1927 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1928 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1929 call assert_equal(textfile_winid, win_getid())
1930
1931 call StopVimInTerminal(buf)
1932 call delete('Xscript')
1933 bwipe Xtextfile
1934endfunc
1935
1936func Tapi_TryThis(bufnum, arg)
1937 let g:called_bufnum = a:bufnum
1938 let g:called_arg = a:arg
1939endfunc
1940
1941func WriteApiCall(funcname)
1942 " Use the title termcap entries to output the escape sequence.
1943 call writefile([
1944 \ 'set title',
1945 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1946 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1947 \ 'redraw',
1948 \ "set t_ts=",
1949 \ ], 'Xscript')
1950endfunc
1951
1952func Test_terminal_api_call()
1953 CheckRunVimInTerminal
1954
1955 unlet! g:called_bufnum
1956 unlet! g:called_arg
1957
1958 call WriteApiCall('Tapi_TryThis')
1959
1960 " Default
1961 let buf = RunVimInTerminal('-S Xscript', {})
1962 call WaitFor({-> exists('g:called_bufnum')})
1963 call assert_equal(buf, g:called_bufnum)
1964 call assert_equal(['hello', 123], g:called_arg)
1965 call StopVimInTerminal(buf)
1966
1967 unlet! g:called_bufnum
1968 unlet! g:called_arg
1969
1970 " Enable explicitly
1971 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1972 call WaitFor({-> exists('g:called_bufnum')})
1973 call assert_equal(buf, g:called_bufnum)
1974 call assert_equal(['hello', 123], g:called_arg)
1975 call StopVimInTerminal(buf)
1976
1977 unlet! g:called_bufnum
1978 unlet! g:called_arg
1979
1980 func! ApiCall_TryThis(bufnum, arg)
1981 let g:called_bufnum2 = a:bufnum
1982 let g:called_arg2 = a:arg
1983 endfunc
1984
1985 call WriteApiCall('ApiCall_TryThis')
1986
1987 " Use prefix match
1988 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1989 call WaitFor({-> exists('g:called_bufnum2')})
1990 call assert_equal(buf, g:called_bufnum2)
1991 call assert_equal(['hello', 123], g:called_arg2)
1992 call StopVimInTerminal(buf)
1993
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001994 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001995
1996 unlet! g:called_bufnum2
1997 unlet! g:called_arg2
1998
1999 call delete('Xscript')
2000 delfunction! ApiCall_TryThis
2001 unlet! g:called_bufnum2
2002 unlet! g:called_arg2
2003endfunc
2004
2005func Test_terminal_api_call_fails()
2006 CheckRunVimInTerminal
2007
2008 func! TryThis(bufnum, arg)
2009 let g:called_bufnum3 = a:bufnum
2010 let g:called_arg3 = a:arg
2011 endfunc
2012
2013 call WriteApiCall('TryThis')
2014
2015 unlet! g:called_bufnum3
2016 unlet! g:called_arg3
2017
2018 " Not permitted
2019 call ch_logfile('Xlog', 'w')
2020 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2021 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
2022 call assert_false(exists('g:called_bufnum3'))
2023 call assert_false(exists('g:called_arg3'))
2024 call StopVimInTerminal(buf)
2025
2026 " No match
2027 call ch_logfile('Xlog', 'w')
2028 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
2029 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
2030 call assert_false(exists('g:called_bufnum3'))
2031 call assert_false(exists('g:called_arg3'))
2032 call StopVimInTerminal(buf)
2033
2034 call delete('Xscript')
2035 call ch_logfile('')
2036 call delete('Xlog')
2037 delfunction! TryThis
2038 unlet! g:called_bufnum3
2039 unlet! g:called_arg3
2040endfunc
2041
2042let s:caught_e937 = 0
2043
2044func Tapi_Delete(bufnum, arg)
2045 try
2046 execute 'bdelete!' a:bufnum
2047 catch /E937:/
2048 let s:caught_e937 = 1
2049 endtry
2050endfunc
2051
2052func Test_terminal_api_call_fail_delete()
2053 CheckRunVimInTerminal
2054
2055 call WriteApiCall('Tapi_Delete')
2056 let buf = RunVimInTerminal('-S Xscript', {})
2057 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
2058
2059 call StopVimInTerminal(buf)
2060 call delete('Xscript')
2061 call ch_logfile('', '')
2062endfunc
2063
2064func Test_terminal_setapi_and_call()
2065 CheckRunVimInTerminal
2066
2067 call WriteApiCall('Tapi_TryThis')
2068 call ch_logfile('Xlog', 'w')
2069
2070 unlet! g:called_bufnum
2071 unlet! g:called_arg
2072
2073 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
2074 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2075 call assert_false(exists('g:called_bufnum'))
2076 call assert_false(exists('g:called_arg'))
2077
2078 eval buf->term_setapi('Tapi_')
2079 call term_sendkeys(buf, ":set notitle\<CR>")
2080 call term_sendkeys(buf, ":source Xscript\<CR>")
2081 call WaitFor({-> exists('g:called_bufnum')})
2082 call assert_equal(buf, g:called_bufnum)
2083 call assert_equal(['hello', 123], g:called_arg)
2084
2085 call StopVimInTerminal(buf)
2086
2087 call delete('Xscript')
2088 call ch_logfile('')
2089 call delete('Xlog')
2090 unlet! g:called_bufnum
2091 unlet! g:called_arg
2092endfunc
2093
2094func Test_terminal_api_arg()
2095 CheckRunVimInTerminal
2096
2097 call WriteApiCall('Tapi_TryThis')
2098 call ch_logfile('Xlog', 'w')
2099
2100 unlet! g:called_bufnum
2101 unlet! g:called_arg
2102
2103 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2104 let buf = bufnr('%')
2105 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2106 call assert_false(exists('g:called_bufnum'))
2107 call assert_false(exists('g:called_arg'))
2108
2109 call StopVimInTerminal(buf)
2110
2111 call ch_logfile('Xlog', 'w')
2112
2113 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
2114 let buf = bufnr('%')
2115 call WaitFor({-> exists('g:called_bufnum')})
2116 call assert_equal(buf, g:called_bufnum)
2117 call assert_equal(['hello', 123], g:called_arg)
2118
2119 call StopVimInTerminal(buf)
2120
2121 call delete('Xscript')
2122 call ch_logfile('')
2123 call delete('Xlog')
2124 unlet! g:called_bufnum
2125 unlet! g:called_arg
2126endfunc
2127
2128func Test_terminal_ansicolors_default()
2129 CheckFunction term_getansicolors
2130
2131 let colors = [
2132 \ '#000000', '#e00000',
2133 \ '#00e000', '#e0e000',
2134 \ '#0000e0', '#e000e0',
2135 \ '#00e0e0', '#e0e0e0',
2136 \ '#808080', '#ff4040',
2137 \ '#40ff40', '#ffff40',
2138 \ '#4040ff', '#ff40ff',
2139 \ '#40ffff', '#ffffff',
2140 \]
2141
2142 let buf = Run_shell_in_terminal({})
2143 call assert_equal(colors, term_getansicolors(buf))
2144 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002145 call assert_equal([], term_getansicolors(buf))
2146
2147 exe buf . 'bwipe'
2148endfunc
2149
2150let s:test_colors = [
2151 \ '#616e64', '#0d0a79',
2152 \ '#6d610d', '#0a7373',
2153 \ '#690d0a', '#6d696e',
2154 \ '#0d0a6f', '#616e0d',
2155 \ '#0a6479', '#6d0d0a',
2156 \ '#617373', '#0d0a69',
2157 \ '#6d690d', '#0a6e6f',
2158 \ '#610d0a', '#6e6479',
2159 \]
2160
2161func Test_terminal_ansicolors_global()
2162 CheckFeature termguicolors
2163 CheckFunction term_getansicolors
2164
LemonBoyb2b3acb2022-05-20 10:10:34 +01002165 if has('vtp') && !has('vcon') && !has('gui_running')
2166 throw 'Skipped: does not support termguicolors'
2167 endif
2168
2169 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002170 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2171 let buf = Run_shell_in_terminal({})
2172 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2173 call StopShellInTerminal(buf)
LemonBoyb2b3acb2022-05-20 10:10:34 +01002174 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002175
2176 exe buf . 'bwipe'
2177 unlet g:terminal_ansi_colors
2178endfunc
2179
2180func Test_terminal_ansicolors_func()
2181 CheckFeature termguicolors
2182 CheckFunction term_getansicolors
2183
LemonBoyb2b3acb2022-05-20 10:10:34 +01002184 if has('vtp') && !has('vcon') && !has('gui_running')
2185 throw 'Skipped: does not support termguicolors'
2186 endif
2187
2188 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002189 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2190 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2191 call assert_equal(s:test_colors, term_getansicolors(buf))
2192
2193 call term_setansicolors(buf, g:terminal_ansi_colors)
2194 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2195
2196 let colors = [
2197 \ 'ivory', 'AliceBlue',
2198 \ 'grey67', 'dark goldenrod',
2199 \ 'SteelBlue3', 'PaleVioletRed4',
2200 \ 'MediumPurple2', 'yellow2',
2201 \ 'RosyBrown3', 'OrangeRed2',
2202 \ 'white smoke', 'navy blue',
2203 \ 'grey47', 'gray97',
2204 \ 'MistyRose2', 'DodgerBlue4',
2205 \]
2206 eval buf->term_setansicolors(colors)
2207
2208 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002209 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002210 call assert_fails('call term_setansicolors(buf, {})', 'E1211:')
LemonBoyb2b3acb2022-05-20 10:10:34 +01002211 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002212
2213 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002214 call assert_equal(0, term_setansicolors(buf, []))
2215 exe buf . 'bwipe'
2216endfunc
2217
2218func Test_terminal_all_ansi_colors()
2219 CheckRunVimInTerminal
2220
2221 " Use all the ANSI colors.
2222 call writefile([
2223 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2224 \ 'hi Tblack ctermfg=0 ctermbg=8',
2225 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2226 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2227 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2228 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2229 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2230 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2231 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2232 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2233 \ 'hi Tred ctermfg=9 ctermbg=1',
2234 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2235 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2236 \ 'hi Tblue ctermfg=12 ctermbg=4',
2237 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2238 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2239 \ 'hi Twhite ctermfg=15 ctermbg=7',
2240 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2241 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2242 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2243 \ '',
2244 \ 'call matchadd("Tblack", "A")',
2245 \ 'call matchadd("Tdarkred", "B")',
2246 \ 'call matchadd("Tdarkgreen", "C")',
2247 \ 'call matchadd("Tbrown", "D")',
2248 \ 'call matchadd("Tdarkblue", "E")',
2249 \ 'call matchadd("Tdarkmagenta", "F")',
2250 \ 'call matchadd("Tdarkcyan", "G")',
2251 \ 'call matchadd("Tlightgrey", "H")',
2252 \ 'call matchadd("Tdarkgrey", "I")',
2253 \ 'call matchadd("Tred", "J")',
2254 \ 'call matchadd("Tgreen", "K")',
2255 \ 'call matchadd("Tyellow", "L")',
2256 \ 'call matchadd("Tblue", "M")',
2257 \ 'call matchadd("Tmagenta", "N")',
2258 \ 'call matchadd("Tcyan", "O")',
2259 \ 'call matchadd("Twhite", "P")',
2260 \ 'call matchadd("TdarkredBold", "X")',
2261 \ 'call matchadd("TgreenBold", "Y")',
2262 \ 'call matchadd("TmagentaBold", "Z")',
2263 \ 'redraw',
2264 \ ], 'Xcolorscript')
2265 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2266 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2267
2268 call term_sendkeys(buf, ":q\<CR>")
2269 call StopVimInTerminal(buf)
2270 call delete('Xcolorscript')
2271endfunc
2272
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002273function On_BufFilePost()
2274 doautocmd <nomodeline> User UserEvent
2275endfunction
2276
2277func Test_terminal_nested_autocmd()
2278 new
2279 call setline(1, range(500))
2280 $
2281 let lastline = line('.')
2282
2283 augroup TermTest
2284 autocmd BufFilePost * call On_BufFilePost()
2285 autocmd User UserEvent silent
2286 augroup END
2287
2288 let cmd = Get_cat_123_cmd()
2289 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2290 call assert_equal(lastline, line('.'))
2291
Bram Moolenaar64374752021-04-03 17:22:29 +02002292 let job = term_getjob(buf)
2293 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002294 call delete('Xtext')
2295 augroup TermTest
2296 au!
2297 augroup END
2298endfunc
2299
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002300func Test_terminal_adds_jump()
2301 clearjumps
2302 call term_start("ls", #{curwin: 1})
2303 call assert_equal(1, getjumplist()[0]->len())
2304 bwipe!
2305endfunc
2306
Bram Moolenaareea32af2021-11-21 14:51:13 +00002307func Close_cb(ch, ctx)
2308 call term_wait(a:ctx.bufnr)
2309 let g:close_done = 'done'
2310endfunc
2311
2312func Test_term_wait_in_close_cb()
2313 let g:close_done = ''
2314 let ctx = {}
2315 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2316 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2317
2318 call WaitForAssert({-> assert_equal("done", g:close_done)})
2319
2320 unlet g:close_done
2321 bwipe!
2322endfunc
2323
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01002324func Test_term_TextChangedT()
2325 augroup TermTest
2326 autocmd TextChangedT * ++once
2327 \ execute expand('<abuf>') . 'buffer' |
2328 \ let b:called = 1 |
2329 \ split |
2330 \ enew
2331 augroup END
2332
2333 terminal
2334
2335 let term_buf = bufnr()
2336
2337 let b:called = 0
2338
2339 call term_sendkeys(term_buf, "aaabbc\r")
2340 call TermWait(term_buf)
2341
2342 call assert_equal(1, getbufvar(term_buf, 'called'))
2343
2344 " Current buffer will be restored
2345 call assert_equal(bufnr(), term_buf)
2346
2347 bwipe!
2348 augroup TermTest
2349 au!
2350 augroup END
2351endfunc
2352
2353func Test_term_TextChangedT_close()
2354 augroup TermTest
2355 autocmd TextChangedT * ++once split | enew | 1close!
2356 augroup END
2357
2358 terminal
2359
2360 let term_buf = bufnr()
2361
2362 call term_sendkeys(term_buf, "aaabbc\r")
2363 call TermWait(term_buf)
2364
2365 " Current buffer will be restored
2366 call assert_equal(bufnr(), term_buf)
2367
2368 bwipe!
2369 augroup TermTest
2370 au!
2371 augroup END
2372endfunc
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002373
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002374" vim: shiftwidth=2 sts=2 expandtab