blob: b64d5cf414f646916cb7dcdb944b624b44bd3ab4 [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
Bram Moolenaar94053a52017-08-01 21:44:33 +020099func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200100 let buf = Run_shell_in_terminal({})
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100101 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200102 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200103 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200104 call assert_equal("", bufname(buf))
105
106 unlet g:job
107endfunc
108
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200109func Test_terminal_split_quit()
110 let buf = Run_shell_in_terminal({})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200111 split
112 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200113 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200114 sleep 50m
115 call assert_equal('run', job_status(g:job))
116
117 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200118 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200119
120 exe buf . 'bwipe'
121 unlet g:job
122endfunc
123
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100124func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200125 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200126 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200127 quit
128 for nr in range(1, winnr('$'))
129 call assert_notequal(winbufnr(nr), buf)
130 endfor
131 call assert_true(bufloaded(buf))
132 call assert_true(buflisted(buf))
133
134 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200135 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200136 exe buf . 'bwipe'
137
138 unlet g:job
139endfunc
140
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100141func Test_terminal_hide_buffer_job_finished()
142 term echo hello
143 let buf = bufnr()
144 setlocal bufhidden=hide
145 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
146 call assert_true(bufloaded(buf))
147 call assert_true(buflisted(buf))
148 edit Xasdfasdf
149 call assert_true(bufloaded(buf))
150 call assert_true(buflisted(buf))
151 exe buf .. 'buf'
152 call assert_equal(buf, bufnr())
153 setlocal bufhidden=
154 edit Xasdfasdf
155 call assert_false(bufloaded(buf))
156 call assert_false(buflisted(buf))
157 bwipe Xasdfasdf
158endfunc
159
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000160func Test_terminal_rename_buffer()
161 let cmd = Get_cat_123_cmd()
162 let buf = term_start(cmd, {'term_name': 'foo'})
163 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
164 call assert_equal('foo', bufname())
165 call assert_match('foo.*finished', execute('ls'))
166 file bar
167 call assert_equal('bar', bufname())
168 call assert_match('bar.*finished', execute('ls'))
169 exe 'bwipe! ' .. buf
170endfunc
171
Bram Moolenaar1e115362019-01-09 23:01:02 +0100172func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200173 exe g:buf . 'bwipe!'
174 let g:buf = 0
175endfunc
176
Bram Moolenaar9d189612017-09-09 18:11:00 +0200177func Get_cat_123_cmd()
178 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100179 if !has('conpty')
180 return 'cmd /c "cls && color 2 && echo 123"'
181 else
182 " When clearing twice, extra sequence is not output.
183 return 'cmd /c "cls && cls && color 2 && echo 123"'
184 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200185 else
186 call writefile(["\<Esc>[32m123"], 'Xtext')
187 return "cat Xtext"
188 endif
189endfunc
190
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200191func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200192 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200193 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
194 let g:job = term_getjob(g:buf)
195
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200196 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
197 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200198 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100199 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200200 call delete('Xtext')
201endfunc
202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200203func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200204 let l = term_scrape(a:buf, 0)
205 call assert_true(len(l) == 0)
206 let l = term_scrape(a:buf, 999)
207 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200208 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200209 call assert_true(len(l) > 0)
210 call assert_equal('1', l[0].chars)
211 call assert_equal('2', l[1].chars)
212 call assert_equal('3', l[2].chars)
213 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200214 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
215 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100216 if has('win32')
217 " On Windows 'background' always defaults to dark, even though the terminal
218 " may use a light background. Therefore accept both white and black.
219 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100221 if &background == 'light'
222 call assert_equal('#ffffff', l[0].bg)
223 else
224 call assert_equal('#000000', l[0].bg)
225 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200226 endif
227
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200228 let l = term_getline(a:buf, -1)
229 call assert_equal('', l)
230 let l = term_getline(a:buf, 0)
231 call assert_equal('', l)
232 let l = term_getline(a:buf, 999)
233 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200234 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200235 call assert_equal('123', l)
236endfunc
237
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200238func Test_terminal_scrape_123()
239 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200240 let buf = term_start(cmd)
241
242 let termlist = term_list()
243 call assert_equal(1, len(termlist))
244 call assert_equal(buf, termlist[0])
245
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200246 " Nothing happens with invalid buffer number
247 call term_wait(1234)
248
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200249 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200250 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200251 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200252 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200253 call Check_123(buf)
254
255 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100256 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200257 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200258 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200259 call Check_123(buf)
260
261 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200262 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200263endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200264
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200265func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266 call writefile(["léttまrs"], 'Xtext')
267 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200268 " Run cmd with UTF-8 codepage to make the type command print the expected
269 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100270 let buf = term_start("cmd /K chcp 65001")
271 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200272 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100273 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200274 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100275 let buf = term_start("cat Xtext")
276 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200277 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200278
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100279 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
280 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200281 call assert_true(len(l) >= 7)
282 call assert_equal('l', l[0].chars)
283 call assert_equal('é', l[1].chars)
284 call assert_equal(1, l[1].width)
285 call assert_equal('t', l[2].chars)
286 call assert_equal('t', l[3].chars)
287 call assert_equal('ま', l[4].chars)
288 call assert_equal(2, l[4].width)
289 call assert_equal('r', l[5].chars)
290 call assert_equal('s', l[6].chars)
291
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100292 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200293 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200294 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200295
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100296 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200297 call delete('Xtext')
298endfunc
299
Bram Moolenaar8b896142020-08-02 15:05:05 +0200300func Test_terminal_one_column()
301 " This creates a terminal, displays a double-wide character and makes the
302 " window one column wide. This used to cause a crash.
303 let width = &columns
304 botright vert term
305 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200306 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200307 exe "set columns=" .. (width / 2)
308 redraw
309 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200310 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200311 exe "set columns=" .. width
312 exe buf . 'bwipe!'
313endfunc
314
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200315func Test_terminal_scroll()
316 call writefile(range(1, 200), 'Xtext')
317 if has('win32')
318 let cmd = 'cmd /c "type Xtext"'
319 else
320 let cmd = "cat Xtext"
321 endif
322 let buf = term_start(cmd)
323
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100324 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200325 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200326 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200327
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100328 " wait until the scrolling stops
329 while 1
330 let scrolled = buf->term_getscrolled()
331 sleep 20m
332 if scrolled == buf->term_getscrolled()
333 break
334 endif
335 endwhile
336
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200337 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200338 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200339 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200340 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200341 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200342 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200343
344 exe buf . 'bwipe'
345 call delete('Xtext')
346endfunc
347
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200348func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200349 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200350 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200351 call writefile(range(150), 'Xtext')
352 if has('win32')
353 call term_sendkeys(buf, "type Xtext\<CR>")
354 else
355 call term_sendkeys(buf, "cat Xtext\<CR>")
356 endif
357 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200358 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200359 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200360 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200361 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200362
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200363 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200364 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200365 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100366 call delete('Xtext')
367endfunc
368
369func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200370 " tail -f only works on Unix
371 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100372
373 call writefile(range(50), 'Xtext')
374 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100375 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100376 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100377 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100378 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
379 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100380 \ 'call feedkeys("\<C-W>N", "xt")',
381 \ ], 'XTest_postponed')
382 let buf = RunVimInTerminal('-S XTest_postponed', {})
383 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100384 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100385
386 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200387 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100388 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100389 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100390
391 " Back to Terminal-Job mode, text will scroll and show the extra line.
392 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100393 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100394
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100395 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100396 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200397 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100398 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100399 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200400 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100401 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100402 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
403
404 " check type of term_sendkeys() return value
405 echo type(tsk_ret)
406
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100407 call StopVimInTerminal(buf)
408 call delete('XTest_postponed')
409 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200410endfunc
411
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100412" Run diff on two dumps with different size.
413func Test_terminal_dumpdiff_size()
414 call assert_equal(1, winnr('$'))
415 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
416 call assert_equal(2, winnr('$'))
417 call assert_match('Test_incsearch_search_01.dump', getline(10))
418 call assert_match(' +++++$', getline(11))
419 call assert_match('Test_popup_command_01.dump', getline(31))
420 call assert_equal(repeat('+', 75), getline(30))
421 quit
422endfunc
423
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200424func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200425 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200426
Bram Moolenaarb2412082017-08-20 18:09:14 +0200427 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200428 let size = term_getsize('')
429 bwipe!
430 call assert_equal(5, size[0])
431
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200432 call term_start(cmd, {'term_rows': 6})
433 let size = term_getsize('')
434 bwipe!
435 call assert_equal(6, size[0])
436
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200437 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200438 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200439 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200440
441 call term_setsize('', 6, 0)
442 call assert_equal([6, 33], term_getsize(''))
443
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200444 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200445 call assert_equal([6, 35], term_getsize(''))
446
447 call term_setsize('', 7, 30)
448 call assert_equal([7, 30], term_getsize(''))
449
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200450 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200451 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200452
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200453 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
454 let size = term_getsize('')
455 bwipe!
456 call assert_equal([6, 36], size)
457
Bram Moolenaarb2412082017-08-20 18:09:14 +0200458 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200459 let size = term_getsize('')
460 bwipe!
461 call assert_equal(20, size[1])
462
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200463 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200464 let size = term_getsize('')
465 bwipe!
466 call assert_equal(26, size[1])
467
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200468 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200469 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200470 let size = term_getsize('')
471 bwipe!
472 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200473
474 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
475 let size = term_getsize('')
476 bwipe!
477 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200478
Bram Moolenaar5300be62021-11-13 10:27:40 +0000479 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
480 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000481 if has('float')
482 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
483 endif
484
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200485 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200486endfunc
487
Bram Moolenaareba13e42021-02-23 17:47:23 +0100488func Test_terminal_zero_height()
489 split
490 wincmd j
491 anoremenu 1.1 WinBar.test :
492 terminal ++curwin
493 wincmd k
494 wincmd _
495 redraw
496
497 call term_sendkeys(bufnr(), "exit\r")
498 bwipe!
499endfunc
500
Bram Moolenaarda43b612017-08-11 22:27:50 +0200501func Test_terminal_curwin()
502 let cmd = Get_cat_123_cmd()
503 call assert_equal(1, winnr('$'))
504
Bram Moolenaarb1009092020-05-31 16:04:42 +0200505 split Xdummy
506 call setline(1, 'dummy')
507 write
508 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200509 exe 'terminal ++curwin ' . cmd
510 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200511 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200512 bwipe!
513
Bram Moolenaarb1009092020-05-31 16:04:42 +0200514 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200515 call term_start(cmd, {'curwin': 1})
516 call assert_equal(2, winnr('$'))
517 bwipe!
518
Bram Moolenaarb1009092020-05-31 16:04:42 +0200519 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200520 call setline(1, 'change')
521 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
522 call assert_equal(2, winnr('$'))
523 exe 'terminal! ++curwin ' . cmd
524 call assert_equal(2, winnr('$'))
525 bwipe!
526
Bram Moolenaarb1009092020-05-31 16:04:42 +0200527 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200528 call setline(1, 'change')
529 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
530 call assert_equal(2, winnr('$'))
531 bwipe!
532
Bram Moolenaarb1009092020-05-31 16:04:42 +0200533 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200534 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200535 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200536 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200537endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200538
Bram Moolenaarff546792017-11-21 14:47:57 +0100539func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200540 if s:python != ''
541 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200542 " 500 was not enough for Travis
543 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200544 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200545 echo 'This will take five seconds...'
546 let waittime = 2000
547 if has('win32')
548 let cmd = $windir . '\system32\timeout.exe 1'
549 else
550 let cmd = 'sleep 1'
551 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200552 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100553 return [cmd, waittime]
554endfunc
555
556func Test_terminal_finish_open_close()
557 call assert_equal(1, winnr('$'))
558
559 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200560
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100561 " shell terminal closes automatically
562 terminal
563 let buf = bufnr('%')
564 call assert_equal(2, winnr('$'))
565 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200566 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200567 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200568 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100569
570 " shell terminal that does not close automatically
571 terminal ++noclose
572 let buf = bufnr('%')
573 call assert_equal(2, winnr('$'))
574 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200575 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200576 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100577 call assert_equal(2, winnr('$'))
578 quit
579 call assert_equal(1, winnr('$'))
580
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200581 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200582 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200583 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200584 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200585
586 call term_start(cmd, {'term_finish': 'close'})
587 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200588 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200589 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200590 call assert_equal(1, winnr('$'))
591
592 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200593 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200594 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200595 bwipe
596
597 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200598 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200600 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200601
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200602 exe 'terminal ++hidden ++open ' . cmd
603 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200604 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200605 bwipe
606
607 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
608 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200609 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200610 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200611
612 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
613 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
614 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
615 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
616
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100617 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 +0200618 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200619 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200620 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100621 call assert_equal('opened the buffer in a window', g:result)
622 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200623 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200624endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200625
626func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100627 if has('win32')
628 let cmd = 'cmd /c cd'
629 else
630 CheckExecutable pwd
631 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200632 endif
633 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100634 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200635 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200636
637 exe buf . 'bwipe'
638 call delete('Xdir', 'rf')
639endfunc
640
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200641func Test_terminal_cwd_failure()
642 " Case 1: Provided directory is not actually a directory. Attempt to make
643 " the file executable as well.
644 call writefile([], 'Xfile')
645 call setfperm('Xfile', 'rwx------')
646 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
647 call delete('Xfile')
648
649 " Case 2: Directory does not exist.
650 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
651
652 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100653 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200654 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100655 call mkdir('XdirNoAccess', '', '0600')
656 " return early if the directory permissions could not be set properly
657 if getfperm('XdirNoAccess')[2] == 'x'
658 call delete('XdirNoAccess', 'rf')
659 return
660 endif
661 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
662 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200663 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200664endfunc
665
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100666func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200667 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200668 call s:test_environment("VIM_SERVERNAME", v:servername)
669endfunc
670
671func Test_terminal_version()
672 call s:test_environment("VIM_TERMINAL", string(v:version))
673endfunc
674
675func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100676 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100677 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200678 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100679 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200680 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100681 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200682 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100683 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200684 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200685 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200686 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100687
Bram Moolenaar012eb662018-03-13 17:55:27 +0100688 exe buf . 'bwipe'
689 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100690endfunc
691
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200692func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100693 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200694 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200695 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100696 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100697 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100698 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100699 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100700 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200701 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200702 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200703 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200704
Bram Moolenaar012eb662018-03-13 17:55:27 +0100705 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200706endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200707
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200708func Test_terminal_list_args()
709 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100710 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200711 exe buf . 'bwipe!'
712 call assert_equal("", bufname(buf))
713endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200714
715func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100716 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100717 let buf = term_start(&shell)
Zdenek Dohnaldd2dfb32022-02-23 14:25:17 +0000718 " Starting a terminal can be slow, esp. on busy CI machines.
719 let wait_time = 7500
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100720 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100721 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200722 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100723 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200724 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100725 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100726 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100727 " NPFS is used in Windows, nonblocking mode does not work properly.
728 elseif has('win32')
729 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200730 else
731 let len = 5000
732 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200733
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100734 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100735 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100736 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200737 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100738 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200739
740 " On MS-Windows there is an extra empty line below "done". Find "done" in
741 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100742 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100743 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100744 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200745 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100746 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200747 endif
748 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200749
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100750 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200751 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200752 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200753 bwipe
754endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200755
756func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100757 " TODO: enable once writing to stdin works on MS-Windows
758 CheckNotMSWindows
759 CheckExecutable wc
760
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200761 call setline(1, ['one', 'two', 'three'])
762 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200763 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200764 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200765 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100766 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200767
768 call setline(1, ['one', 'two', 'three', 'four'])
769 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200770 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200771 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200772 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100773 %bwipe!
774endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200775
Bram Moolenaar21109272020-01-30 16:27:20 +0100776func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200777 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200778
Bram Moolenaar21109272020-01-30 16:27:20 +0100779 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200780 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100781 " MS-Windows echoes the input, Unix doesn't.
782 if has('win32')
783 call WaitFor({-> getline('$') =~ 'exit(123)'})
784 call assert_equal('hello', getline(line('$') - 1))
785 else
786 call WaitFor({-> getline('$') =~ 'hello'})
787 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200788 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100789 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
790 %bwipe!
791endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200792
Bram Moolenaar21109272020-01-30 16:27:20 +0100793func Test_terminal_eof_arg_win32_ctrl_z()
794 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200795 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100796
797 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200798 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100799 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
800 call assert_match('\^Z', getline(line('$') - 1))
801 %bwipe!
802endfunc
803
804func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200805 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100806
807 " Check the last specified ++eof arg is used and should not memory leak.
808 new
809 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200810 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100811 " MS-Windows echoes the input, Unix doesn't.
812 if has('win32')
813 call WaitFor({-> getline('$') =~ 'exit(123)'})
814 call assert_equal('hello', getline(line('$') - 1))
815 else
816 call WaitFor({-> getline('$') =~ 'hello'})
817 call assert_equal('hello', getline('$'))
818 endif
819 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
820 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200821endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200822
823func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100824 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200825 let buf = term_start('NONE', {})
826 call assert_notequal(0, buf)
827
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200828 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200829 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100830 if has('gui_running') && !has('win32')
831 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200832 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100833 else
834 " Otherwise using a job works on all systems.
835 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200836 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200837 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200838
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200839 bwipe!
840endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200841
842func Test_terminal_special_chars()
843 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200844 CheckUnix
845
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200846 call mkdir('Xdir with spaces')
847 call writefile(['x'], 'Xdir with spaces/quoted"file')
848 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200849 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100850 " make sure the job has finished
851 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200852
853 call delete('Xdir with spaces', 'rf')
854 bwipe
855endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200856
857func Test_terminal_wrong_options()
858 call assert_fails('call term_start(&shell, {
859 \ "in_io": "file",
860 \ "in_name": "xxx",
861 \ "out_io": "file",
862 \ "out_name": "xxx",
863 \ "err_io": "file",
864 \ "err_name": "xxx"
865 \ })', 'E474:')
866 call assert_fails('call term_start(&shell, {
867 \ "out_buf": bufnr("%")
868 \ })', 'E474:')
869 call assert_fails('call term_start(&shell, {
870 \ "err_buf": bufnr("%")
871 \ })', 'E474:')
872endfunc
873
874func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100875 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200876 let cmd = Get_cat_123_cmd()
877 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200878 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100879 " ConPTY may precede escape sequence. There are things that are not so.
880 if !has('conpty')
881 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
882 call assert_match('123', readfile('Xfile')[0])
883 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200884 let g:job = term_getjob(buf)
885 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200886
887 if has('win32')
888 " On Windows we cannot delete a file being used by a process. When
889 " job_status() returns "dead", the process remains for a short time.
890 " Just wait for a moment.
891 sleep 50m
892 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200893 call delete('Xfile')
894 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200895
896 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200897 call writefile(['one line'], 'Xfile')
898 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200899 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200900 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200901 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200902 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200903 bwipe
904 call delete('Xfile')
905 endif
906endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200907
908func TerminalTmap(remap)
909 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +0100910 " Wait for the shell to display a prompt
911 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200912 call assert_equal('t', mode())
913
914 if a:remap
915 tmap 123 456
916 else
917 tnoremap 123 456
918 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100919 " don't use abcde, it's an existing command
920 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200921 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100922 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200923 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200924 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200925 let lnum = term_getcursor(buf)[0]
926 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100927 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200928 else
929 call assert_match('456', term_getline(buf, lnum))
930 endif
931
932 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200933 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200934
935 tunmap 123
936 tunmap 456
937 call assert_equal('', maparg('123', 't'))
Yegappan Lakshmanan1104a6d2022-03-31 12:34:15 +0100938 exe buf . 'bwipe'
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200939 unlet g:job
940endfunc
941
942func Test_terminal_tmap()
943 call TerminalTmap(1)
944 call TerminalTmap(0)
945endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200946
947func Test_terminal_wall()
948 let buf = Run_shell_in_terminal({})
949 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200950 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200951 exe buf . 'bwipe'
952 unlet g:job
953endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200954
Bram Moolenaar7a760922018-02-19 23:10:02 +0100955func Test_terminal_wqall()
956 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200957 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200958 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100959 exe buf . 'bwipe'
960 unlet g:job
961endfunc
962
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200963func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100964 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200965 let save_enc = &encoding
966 set encoding=utf-8
967
968 if has('win32')
969 let cmd = "cmd /K chcp 65001"
970 let lnum = [3, 6, 9]
971 else
972 let cmd = &shell
973 let lnum = [1, 3, 5]
974 endif
975
976 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200977 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100978 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200979 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200980
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200981 if has('win32')
982 call assert_equal('cmd', job_info(g:job).cmd[0])
983 else
984 call assert_equal(&shell, job_info(g:job).cmd[0])
985 endif
986
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200987 " ascii + composing
988 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200989 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200990 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200991 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200992 call term_sendkeys(buf, "\<cr>")
993 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200994 let l = term_scrape(buf, lnum[0] + 1)
995 call assert_equal("a\u0308", l[0].chars)
996 call assert_equal("b", l[1].chars)
997 call assert_equal("c", l[2].chars)
998
Bram Moolenaar4549dad2021-02-08 21:29:48 +0100999 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001000 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001001 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001002 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001003 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001004 call term_sendkeys(buf, "\<cr>")
1005 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001006 let l = term_scrape(buf, lnum[1] + 1)
1007 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001008 call assert_equal(2, l[0].width)
1009 call assert_equal("\u304e", l[1].chars)
1010 call assert_equal(2, l[1].width)
1011 call assert_equal("\u304f\u3099", l[2].chars)
1012 call assert_equal(2, l[2].width)
1013 call assert_equal("\u3052", l[3].chars)
1014 call assert_equal(2, l[3].width)
1015 call assert_equal("\u3053\u3099", l[4].chars)
1016 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001017
1018 " \u00a0 + composing
1019 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001020 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001021 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001022 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001023 call term_sendkeys(buf, "\<cr>")
1024 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001025 let l = term_scrape(buf, lnum[2] + 1)
1026 call assert_equal("\u00a0\u0308", l[3].chars)
1027
1028 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001029 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001030 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001031 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001032 let &encoding = save_enc
1033endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001034
1035func Test_terminal_aucmd_on_close()
1036 fun Nop()
1037 let s:called = 1
1038 endfun
1039
1040 aug repro
1041 au!
1042 au BufWinLeave * call Nop()
1043 aug END
1044
1045 let [cmd, waittime] = s:get_sleep_cmd()
1046
1047 call assert_equal(1, winnr('$'))
1048 new
1049 call setline(1, ['one', 'two'])
1050 exe 'term ++close ' . cmd
1051 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001052 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001053 call assert_equal(1, s:called)
1054 bwipe!
1055
1056 unlet s:called
1057 au! repro
1058 delfunc Nop
1059endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001060
1061func Test_terminal_term_start_empty_command()
1062 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001063 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001064 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001065 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001066 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001067 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001068 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001069 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001070 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001071 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001072 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001073 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001074 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001075 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001076 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001077 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001078 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001079 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001080 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1081 call assert_fails(cmd, 'E475:')
1082 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001083 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001084 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001085 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1086 call assert_fails(cmd, 'E475:')
1087 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1088 call assert_fails(cmd, 'E730:')
1089 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001090 if has('gui_running') || has('termguicolors')
1091 call assert_fails(cmd, 'E475:')
1092 else
1093 call assert_fails(cmd, 'E254:')
1094 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001095 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001096endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001097
1098func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001099 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001100
1101 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001102 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001103
Bram Moolenaar086eb872018-03-25 21:24:12 +02001104 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001105 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001106
Bram Moolenaar086eb872018-03-25 21:24:12 +02001107 " Request the cursor position.
1108 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001109
1110 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001111 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001112
Bram Moolenaar086eb872018-03-25 21:24:12 +02001113 " End "cat" gently.
1114 call term_sendkeys(buf, "\<CR>\<C-D>")
1115
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001116 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001117 exe buf . 'bwipe'
1118 unlet g:job
1119endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001120
Bram Moolenaarc2958582021-12-14 11:16:31 +00001121" Run this first, it fails when run after other tests.
1122func Test_aa_terminal_focus_events()
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001123 CheckNotGui
1124 CheckUnix
1125 CheckRunVimInTerminal
1126
1127 let save_term = &term
1128 let save_ttymouse = &ttymouse
1129 set term=xterm ttymouse=xterm2
1130
1131 let lines =<< trim END
1132 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001133 au FocusLost * call setline(1, 'I am lost') | set nomod
1134 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001135 END
1136 call writefile(lines, 'XtermFocus')
1137 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1138
1139 " Send a focus event to ourselves, it should be forwarded to the terminal
1140 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001141 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1142
1143 call feedkeys("\<Esc>[I", "Lx!")
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001144 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1145
Bram Moolenaare5050712021-12-09 10:51:05 +00001146 " check that a command line being edited is redrawn in place
1147 call term_sendkeys(buf, ":" .. repeat('x', 80))
1148 call TermWait(buf)
1149 call feedkeys("\<Esc>[O", "Lx!")
Bram Moolenaare5050712021-12-09 10:51:05 +00001150 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1151 call term_sendkeys(buf, "\<Esc>")
1152
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001153 call StopVimInTerminal(buf)
1154 call delete('XtermFocus')
1155 let &term = save_term
1156 let &ttymouse = save_ttymouse
1157endfunc
1158
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001159" Run Vim, start a terminal in that Vim with the kill argument,
1160" :qall works.
1161func Run_terminal_qall_kill(line1, line2)
1162 " 1. Open a terminal window and wait for the prompt to appear
1163 " 2. set kill using term_setkill()
1164 " 3. make Vim exit, it will kill the shell
1165 let after = [
1166 \ a:line1,
1167 \ 'let buf = bufnr("%")',
1168 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1169 \ ' sleep 10m',
1170 \ 'endwhile',
1171 \ a:line2,
1172 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1173 \ 'qall',
1174 \ ]
1175 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001176 return
1177 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001178 call assert_equal("done", readfile("Xdone")[0])
1179 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001180endfunc
1181
1182" Run Vim in a terminal, then start a terminal in that Vim with a kill
1183" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001184func Test_terminal_qall_kill_arg()
1185 call Run_terminal_qall_kill('term ++kill=kill', '')
1186endfunc
1187
1188" Run Vim, start a terminal in that Vim, set the kill argument with
1189" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001190func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001191 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001192endfunc
1193
1194" Run Vim, start a terminal in that Vim without the kill argument,
1195" check that :qall does not exit, :qall! does.
1196func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001197 let after =<< trim [CODE]
1198 term
1199 let buf = bufnr("%")
1200 while term_getline(buf, 1) =~ "^\\s*$"
1201 sleep 10m
1202 endwhile
1203 set nomore
1204 au VimLeavePre * call writefile(["too early"], "Xdone")
1205 qall
1206 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1207 cquit
1208 [CODE]
1209
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001210 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001211 return
1212 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001213 call assert_equal("done", readfile("Xdone")[0])
1214 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001215endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001216
1217" Run Vim in a terminal, then start a terminal in that Vim without a kill
1218" argument, check that :confirm qall works.
1219func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001220 CheckRunVimInTerminal
Bram Moolenaare564c702022-06-14 15:00:28 +01001221
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001222 let buf = RunVimInTerminal('', {})
1223
Bram Moolenaar99f4b6e2022-06-14 19:52:16 +01001224 " the shell may set the window title, we don't want that here
1225 call term_sendkeys(buf, ":test_override('vterm_title', 1)\<CR>")
1226
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001227 " Open a terminal window and wait for the prompt to appear
1228 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001229 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1230 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001231
1232 " make Vim exit, it will prompt to kill the shell
1233 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001234 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001235 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001236 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001237
1238 " close the terminal window where Vim was running
1239 quit
1240endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001241
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001242" Run Vim in a terminal, then start a terminal window with a shell and check
1243" that Vim exits if it is closed.
1244func Test_terminal_exit()
1245 CheckRunVimInTerminal
1246
1247 let lines =<< trim END
1248 let winid = win_getid()
1249 help
1250 term
1251 let termid = win_getid()
1252 call win_gotoid(winid)
1253 close
1254 call win_gotoid(termid)
1255 END
1256 call writefile(lines, 'XtermExit')
1257 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1258 let job = term_getjob(buf)
1259 call WaitForAssert({-> assert_equal("run", job_status(job))})
1260
1261 " quit the shell, it will make Vim exit
1262 call term_sendkeys(buf, "exit\<CR>")
1263 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1264
1265 call delete('XtermExit')
1266endfunc
1267
Bram Moolenaar012eb662018-03-13 17:55:27 +01001268func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001269 augroup repro
1270 au!
1271 au TerminalOpen * let s:called += 1
1272 augroup END
1273
1274 let s:called = 0
1275
1276 " Open a terminal window with :terminal
1277 terminal
1278 call assert_equal(1, s:called)
1279 bwipe!
1280
1281 " Open a terminal window with term_start()
1282 call term_start(&shell)
1283 call assert_equal(2, s:called)
1284 bwipe!
1285
1286 " Open a hidden terminal buffer with :terminal
1287 terminal ++hidden
1288 call assert_equal(3, s:called)
1289 for buf in term_list()
1290 exe buf . "bwipe!"
1291 endfor
1292
1293 " Open a hidden terminal buffer with term_start()
1294 let buf = term_start(&shell, {'hidden': 1})
1295 call assert_equal(4, s:called)
1296 exe buf . "bwipe!"
1297
1298 unlet s:called
1299 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001300endfunc
1301
1302func Test_open_term_from_cmd()
1303 CheckUnix
1304 CheckRunVimInTerminal
1305
1306 let lines =<< trim END
1307 call setline(1, ['a', 'b', 'c'])
1308 3
1309 set incsearch
1310 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1311 END
1312 call writefile(lines, 'Xopenterm')
1313 let buf = RunVimInTerminal('-S Xopenterm', {})
1314
1315 " this opens a window, incsearch should not use the old cursor position
1316 call term_sendkeys(buf, "/\<F3>")
1317 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1318 call term_sendkeys(buf, "\<Esc>")
1319 call term_sendkeys(buf, ":q\<CR>")
1320
1321 call StopVimInTerminal(buf)
1322 call delete('Xopenterm')
1323endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001324
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001325func Test_combining_double_width()
1326 CheckUnix
1327 CheckRunVimInTerminal
1328
1329 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1330 let lines =<< trim END
1331 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1332 END
1333 call writefile(lines, 'Xcombining')
1334 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1335
1336 " this opens a window, incsearch should not use the old cursor position
1337 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1338 call term_sendkeys(buf, ":q\<CR>")
1339
1340 call StopVimInTerminal(buf)
1341 call delete('Xonedouble')
1342 call delete('Xcombining')
1343endfunc
1344
Bram Moolenaar02764712020-11-14 20:21:55 +01001345func Test_terminal_popup_with_cmd()
1346 " this was crashing
1347 let buf = term_start(&shell, #{hidden: v:true})
1348 let s:winid = popup_create(buf, {})
1349 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1350 call feedkeys("\<F3>", 'xt')
1351
1352 tunmap <F3>
1353 exe 'bwipe! ' .. buf
1354 unlet s:winid
1355endfunc
1356
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001357func Test_terminal_popup_bufload()
1358 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1359 let winid = popup_create(termbuf, {})
1360 sleep 50m
1361
1362 let newbuf = bufadd('')
1363 call bufload(newbuf)
1364 call setbufline(newbuf, 1, 'foobar')
1365
1366 " must not have switched to another window
1367 call assert_equal(winid, win_getid())
1368
Bram Moolenaare6329e42020-11-16 21:10:34 +01001369 call StopShellInTerminal(termbuf)
1370 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001371 exe 'bwipe! ' .. newbuf
1372endfunc
1373
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001374func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001375 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001376 CheckUnix
1377
1378 " use "sh" instead of "&shell" in the hope it will use a short prompt
1379 let lines =<< trim END
1380 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1381 exe 'buffer ' .. termbuf
1382
1383 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1384 sleep 50m
1385
1386 call term_sendkeys(termbuf, "echo 'test'")
1387 END
1388 call writefile(lines, 'XpopupScript')
1389 let buf = RunVimInTerminal('-S XpopupScript', {})
1390
1391 " typed text appears both in normal window and in popup
1392 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1393 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1394
Bram Moolenaar0407d272021-12-13 22:17:44 +00001395 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1396 call TermWait(buf)
1397 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001398 call StopVimInTerminal(buf)
1399 call delete('XpopupScript')
1400endfunc
1401
Bram Moolenaare41decc2020-11-14 21:34:59 +01001402func Test_terminal_popup_insert_cmd()
1403 CheckUnix
1404
1405 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1406 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001407 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001408 endfunc
1409 call feedkeys("i\<F3>")
1410 sleep 10m
1411 call assert_equal('n', mode())
1412
1413 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001414 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001415 delfunc StartTermInPopup
1416 iunmap <F3>
1417endfunc
1418
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001419func Check_dump01(off)
1420 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1421 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001422 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001423endfunc
1424
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001425func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001426 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001427
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001428 let save_enc = &encoding
1429 set encoding=utf-8
1430 call assert_equal(1, winnr('$'))
1431
1432 let text = " a\u0300 e\u0302 o\u0308"
1433 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001434 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001435 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001436 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001437 let dumpline = readfile('Xdump')[0]
1438 call assert_match('|à| |ê| |ö', dumpline)
1439
1440 call StopVimInTerminal(buf)
1441 call delete('Xcomposing')
1442 call delete('Xdump')
1443 let &encoding = save_enc
1444endfunc
1445
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001446" Tests for failures in the term_dumpwrite() function
1447func Test_terminal_dumpwrite_errors()
1448 CheckRunVimInTerminal
1449 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1450 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001451 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001452 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1453 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1454 call writefile([], 'Xtest.dump')
1455 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1456 call delete('Xtest.dump')
1457 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1458 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001459 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001460 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001461 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001462 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1463 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1464 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1465endfunc
1466
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001467" just testing basic functionality.
1468func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001469 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001470 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001471 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001472 call assert_equal(2, winnr('$'))
1473 call assert_equal(20, line('$'))
1474 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001475
1476 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001477 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001478 call assert_equal(buf, buf2)
1479 call assert_notequal('one two three four five', trim(getline(1)))
1480
1481 " Load the first dump again in the same window
1482 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1483 call assert_equal(buf, buf2)
1484 call Check_dump01(0)
1485
1486 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1487 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1488 new
1489 let closedbuf = winbufnr('')
1490 quit
1491 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001492 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001493 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001494
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001495 quit
1496endfunc
1497
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001498func Test_terminal_dumpload_dump()
1499 CheckRunVimInTerminal
1500
1501 let lines =<< trim END
1502 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1503 END
1504 call writefile(lines, 'XtermDumpload')
1505 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1506 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1507
1508 call StopVimInTerminal(buf)
1509 call delete('XtermDumpload')
1510endfunc
1511
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001512func Test_terminal_dumpdiff()
1513 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001514 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001515 call assert_equal(2, winnr('$'))
1516 call assert_equal(62, line('$'))
1517 call Check_dump01(0)
1518 call Check_dump01(42)
1519 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1520 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001521
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001522 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001523 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1524 call writefile([], 'X1.dump')
1525 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1526 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001527endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001528
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001529func Test_terminal_dumpdiff_swap()
1530 call assert_equal(1, winnr('$'))
1531 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1532 call assert_equal(2, winnr('$'))
1533 call assert_equal(62, line('$'))
1534 call assert_match('Test_popup_command_01.dump', getline(21))
1535 call assert_match('Test_popup_command_03.dump', getline(42))
1536 call assert_match('Undo', getline(3))
1537 call assert_match('three four five', getline(45))
1538
1539 normal s
1540 call assert_match('Test_popup_command_03.dump', getline(21))
1541 call assert_match('Test_popup_command_01.dump', getline(42))
1542 call assert_match('three four five', getline(3))
1543 call assert_match('Undo', getline(45))
1544 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001545
1546 " Diff two terminal dump files with different number of rows
1547 " Swap the diffs
1548 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1549 call assert_match('Test_popup_command_01.dump', getline(21))
1550 call assert_match('Test_winline_rnu.dump', getline(42))
1551 normal s
1552 call assert_match('Test_winline_rnu.dump', getline(6))
1553 call assert_match('Test_popup_command_01.dump', getline(27))
1554 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001555endfunc
1556
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001557func Test_terminal_dumpdiff_options()
1558 set laststatus=0
1559 call assert_equal(1, winnr('$'))
1560 let height = winheight(0)
1561 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1562 call assert_equal(2, winnr('$'))
1563 call assert_equal(height, winheight(winnr()))
1564 call assert_equal(33, winwidth(winnr()))
1565 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1566 quit
1567
1568 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001569 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1570 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001571 call assert_equal(&columns, winwidth(0))
1572 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001573 call assert_equal('something else', bufname('%'))
1574 quit
1575
1576 call assert_equal(1, winnr('$'))
1577 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1578 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001579 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 +01001580 bwipe
1581
1582 set laststatus&
1583endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001584
Bram Moolenaar10772302019-01-20 18:25:54 +01001585" When drawing the statusline the cursor position may not have been updated
1586" yet.
1587" 1. create a terminal, make it show 2 lines
1588" 2. 0.5 sec later: leave terminal window, execute "i"
1589" 3. 0.5 sec later: clear terminal window, now it's 1 line
1590" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1591" 4. 0.5 sec later: should be done, clean up
1592func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001593 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001594 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001595
Bram Moolenaar10772302019-01-20 18:25:54 +01001596 set statusline=x
1597 terminal
1598 let tbuf = bufnr('')
1599 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1600 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1601 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1602 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1603
1604 sleep 2
1605 exe tbuf . 'bwipe!'
1606 au! BufLeave
1607 set statusline=
1608endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001609
Bram Moolenaar81035272021-12-16 18:02:07 +00001610func CheckTerminalWindowWorks(buf)
1611 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1612 call term_sendkeys(a:buf, "exit\<CR>")
1613 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1614 call term_sendkeys(a:buf, ":q\<CR>")
1615 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1616endfunc
1617
1618func Test_start_terminal_from_timer()
1619 CheckUnix
1620 CheckFeature timers
1621
1622 " Open a terminal window from a timer, typed text goes to the terminal
1623 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm')
1624 let buf = RunVimInTerminal('-S XtimerTerm', {})
1625 call CheckTerminalWindowWorks(buf)
1626
1627 " do the same in Insert mode
1628 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1629 call CheckTerminalWindowWorks(buf)
1630
1631 call StopVimInTerminal(buf)
1632 call delete('XtimerTerm')
1633endfunc
1634
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001635func Test_terminal_window_focus()
1636 let winid1 = win_getid()
1637 terminal
1638 let winid2 = win_getid()
1639 call feedkeys("\<C-W>j", 'xt')
1640 call assert_equal(winid1, win_getid())
1641 call feedkeys("\<C-W>k", 'xt')
1642 call assert_equal(winid2, win_getid())
1643 " can use a cursor key here
1644 call feedkeys("\<C-W>\<Down>", 'xt')
1645 call assert_equal(winid1, win_getid())
1646 call feedkeys("\<C-W>\<Up>", 'xt')
1647 call assert_equal(winid2, win_getid())
1648
1649 bwipe!
1650endfunc
1651
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001652func Api_drop_common(options)
1653 call assert_equal(1, winnr('$'))
1654
1655 " Use the title termcap entries to output the escape sequence.
1656 call writefile([
1657 \ 'set title',
1658 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1659 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1660 \ 'redraw',
1661 \ "set t_ts=",
1662 \ ], 'Xscript')
1663 let buf = RunVimInTerminal('-S Xscript', {})
1664 call WaitFor({-> bufnr('Xtextfile') > 0})
1665 call assert_equal('Xtextfile', expand('%:t'))
1666 call assert_true(winnr('$') >= 3)
1667 return buf
1668endfunc
1669
1670func Test_terminal_api_drop_newwin()
1671 CheckRunVimInTerminal
1672 let buf = Api_drop_common('')
1673 call assert_equal(0, &bin)
1674 call assert_equal('', &fenc)
1675
1676 call StopVimInTerminal(buf)
1677 call delete('Xscript')
1678 bwipe Xtextfile
1679endfunc
1680
1681func Test_terminal_api_drop_newwin_bin()
1682 CheckRunVimInTerminal
1683 let buf = Api_drop_common(',{"bin":1}')
1684 call assert_equal(1, &bin)
1685
1686 call StopVimInTerminal(buf)
1687 call delete('Xscript')
1688 bwipe Xtextfile
1689endfunc
1690
1691func Test_terminal_api_drop_newwin_binary()
1692 CheckRunVimInTerminal
1693 let buf = Api_drop_common(',{"binary":1}')
1694 call assert_equal(1, &bin)
1695
1696 call StopVimInTerminal(buf)
1697 call delete('Xscript')
1698 bwipe Xtextfile
1699endfunc
1700
1701func Test_terminal_api_drop_newwin_nobin()
1702 CheckRunVimInTerminal
1703 set binary
1704 let buf = Api_drop_common(',{"nobin":1}')
1705 call assert_equal(0, &bin)
1706
1707 call StopVimInTerminal(buf)
1708 call delete('Xscript')
1709 bwipe Xtextfile
1710 set nobinary
1711endfunc
1712
1713func Test_terminal_api_drop_newwin_nobinary()
1714 CheckRunVimInTerminal
1715 set binary
1716 let buf = Api_drop_common(',{"nobinary":1}')
1717 call assert_equal(0, &bin)
1718
1719 call StopVimInTerminal(buf)
1720 call delete('Xscript')
1721 bwipe Xtextfile
1722 set nobinary
1723endfunc
1724
1725func Test_terminal_api_drop_newwin_ff()
1726 CheckRunVimInTerminal
1727 let buf = Api_drop_common(',{"ff":"dos"}')
1728 call assert_equal("dos", &ff)
1729
1730 call StopVimInTerminal(buf)
1731 call delete('Xscript')
1732 bwipe Xtextfile
1733endfunc
1734
1735func Test_terminal_api_drop_newwin_fileformat()
1736 CheckRunVimInTerminal
1737 let buf = Api_drop_common(',{"fileformat":"dos"}')
1738 call assert_equal("dos", &ff)
1739
1740 call StopVimInTerminal(buf)
1741 call delete('Xscript')
1742 bwipe Xtextfile
1743endfunc
1744
1745func Test_terminal_api_drop_newwin_enc()
1746 CheckRunVimInTerminal
1747 let buf = Api_drop_common(',{"enc":"utf-16"}')
1748 call assert_equal("utf-16", &fenc)
1749
1750 call StopVimInTerminal(buf)
1751 call delete('Xscript')
1752 bwipe Xtextfile
1753endfunc
1754
1755func Test_terminal_api_drop_newwin_encoding()
1756 CheckRunVimInTerminal
1757 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1758 call assert_equal("utf-16", &fenc)
1759
1760 call StopVimInTerminal(buf)
1761 call delete('Xscript')
1762 bwipe Xtextfile
1763endfunc
1764
1765func Test_terminal_api_drop_oldwin()
1766 CheckRunVimInTerminal
1767 let firstwinid = win_getid()
1768 split Xtextfile
1769 let textfile_winid = win_getid()
1770 call assert_equal(2, winnr('$'))
1771 call win_gotoid(firstwinid)
1772
1773 " Use the title termcap entries to output the escape sequence.
1774 call writefile([
1775 \ 'set title',
1776 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1777 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1778 \ 'redraw',
1779 \ "set t_ts=",
1780 \ ], 'Xscript')
1781 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1782 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1783 call assert_equal(textfile_winid, win_getid())
1784
1785 call StopVimInTerminal(buf)
1786 call delete('Xscript')
1787 bwipe Xtextfile
1788endfunc
1789
1790func Tapi_TryThis(bufnum, arg)
1791 let g:called_bufnum = a:bufnum
1792 let g:called_arg = a:arg
1793endfunc
1794
1795func WriteApiCall(funcname)
1796 " Use the title termcap entries to output the escape sequence.
1797 call writefile([
1798 \ 'set title',
1799 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1800 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1801 \ 'redraw',
1802 \ "set t_ts=",
1803 \ ], 'Xscript')
1804endfunc
1805
1806func Test_terminal_api_call()
1807 CheckRunVimInTerminal
1808
1809 unlet! g:called_bufnum
1810 unlet! g:called_arg
1811
1812 call WriteApiCall('Tapi_TryThis')
1813
1814 " Default
1815 let buf = RunVimInTerminal('-S Xscript', {})
1816 call WaitFor({-> exists('g:called_bufnum')})
1817 call assert_equal(buf, g:called_bufnum)
1818 call assert_equal(['hello', 123], g:called_arg)
1819 call StopVimInTerminal(buf)
1820
1821 unlet! g:called_bufnum
1822 unlet! g:called_arg
1823
1824 " Enable explicitly
1825 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1826 call WaitFor({-> exists('g:called_bufnum')})
1827 call assert_equal(buf, g:called_bufnum)
1828 call assert_equal(['hello', 123], g:called_arg)
1829 call StopVimInTerminal(buf)
1830
1831 unlet! g:called_bufnum
1832 unlet! g:called_arg
1833
1834 func! ApiCall_TryThis(bufnum, arg)
1835 let g:called_bufnum2 = a:bufnum
1836 let g:called_arg2 = a:arg
1837 endfunc
1838
1839 call WriteApiCall('ApiCall_TryThis')
1840
1841 " Use prefix match
1842 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1843 call WaitFor({-> exists('g:called_bufnum2')})
1844 call assert_equal(buf, g:called_bufnum2)
1845 call assert_equal(['hello', 123], g:called_arg2)
1846 call StopVimInTerminal(buf)
1847
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001848 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001849
1850 unlet! g:called_bufnum2
1851 unlet! g:called_arg2
1852
1853 call delete('Xscript')
1854 delfunction! ApiCall_TryThis
1855 unlet! g:called_bufnum2
1856 unlet! g:called_arg2
1857endfunc
1858
1859func Test_terminal_api_call_fails()
1860 CheckRunVimInTerminal
1861
1862 func! TryThis(bufnum, arg)
1863 let g:called_bufnum3 = a:bufnum
1864 let g:called_arg3 = a:arg
1865 endfunc
1866
1867 call WriteApiCall('TryThis')
1868
1869 unlet! g:called_bufnum3
1870 unlet! g:called_arg3
1871
1872 " Not permitted
1873 call ch_logfile('Xlog', 'w')
1874 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1875 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1876 call assert_false(exists('g:called_bufnum3'))
1877 call assert_false(exists('g:called_arg3'))
1878 call StopVimInTerminal(buf)
1879
1880 " No match
1881 call ch_logfile('Xlog', 'w')
1882 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1883 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1884 call assert_false(exists('g:called_bufnum3'))
1885 call assert_false(exists('g:called_arg3'))
1886 call StopVimInTerminal(buf)
1887
1888 call delete('Xscript')
1889 call ch_logfile('')
1890 call delete('Xlog')
1891 delfunction! TryThis
1892 unlet! g:called_bufnum3
1893 unlet! g:called_arg3
1894endfunc
1895
1896let s:caught_e937 = 0
1897
1898func Tapi_Delete(bufnum, arg)
1899 try
1900 execute 'bdelete!' a:bufnum
1901 catch /E937:/
1902 let s:caught_e937 = 1
1903 endtry
1904endfunc
1905
1906func Test_terminal_api_call_fail_delete()
1907 CheckRunVimInTerminal
1908
1909 call WriteApiCall('Tapi_Delete')
1910 let buf = RunVimInTerminal('-S Xscript', {})
1911 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1912
1913 call StopVimInTerminal(buf)
1914 call delete('Xscript')
1915 call ch_logfile('', '')
1916endfunc
1917
1918func Test_terminal_setapi_and_call()
1919 CheckRunVimInTerminal
1920
1921 call WriteApiCall('Tapi_TryThis')
1922 call ch_logfile('Xlog', 'w')
1923
1924 unlet! g:called_bufnum
1925 unlet! g:called_arg
1926
1927 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1928 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1929 call assert_false(exists('g:called_bufnum'))
1930 call assert_false(exists('g:called_arg'))
1931
1932 eval buf->term_setapi('Tapi_')
1933 call term_sendkeys(buf, ":set notitle\<CR>")
1934 call term_sendkeys(buf, ":source Xscript\<CR>")
1935 call WaitFor({-> exists('g:called_bufnum')})
1936 call assert_equal(buf, g:called_bufnum)
1937 call assert_equal(['hello', 123], g:called_arg)
1938
1939 call StopVimInTerminal(buf)
1940
1941 call delete('Xscript')
1942 call ch_logfile('')
1943 call delete('Xlog')
1944 unlet! g:called_bufnum
1945 unlet! g:called_arg
1946endfunc
1947
1948func Test_terminal_api_arg()
1949 CheckRunVimInTerminal
1950
1951 call WriteApiCall('Tapi_TryThis')
1952 call ch_logfile('Xlog', 'w')
1953
1954 unlet! g:called_bufnum
1955 unlet! g:called_arg
1956
1957 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1958 let buf = bufnr('%')
1959 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1960 call assert_false(exists('g:called_bufnum'))
1961 call assert_false(exists('g:called_arg'))
1962
1963 call StopVimInTerminal(buf)
1964
1965 call ch_logfile('Xlog', 'w')
1966
1967 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1968 let buf = bufnr('%')
1969 call WaitFor({-> exists('g:called_bufnum')})
1970 call assert_equal(buf, g:called_bufnum)
1971 call assert_equal(['hello', 123], g:called_arg)
1972
1973 call StopVimInTerminal(buf)
1974
1975 call delete('Xscript')
1976 call ch_logfile('')
1977 call delete('Xlog')
1978 unlet! g:called_bufnum
1979 unlet! g:called_arg
1980endfunc
1981
1982func Test_terminal_ansicolors_default()
1983 CheckFunction term_getansicolors
1984
1985 let colors = [
1986 \ '#000000', '#e00000',
1987 \ '#00e000', '#e0e000',
1988 \ '#0000e0', '#e000e0',
1989 \ '#00e0e0', '#e0e0e0',
1990 \ '#808080', '#ff4040',
1991 \ '#40ff40', '#ffff40',
1992 \ '#4040ff', '#ff40ff',
1993 \ '#40ffff', '#ffffff',
1994 \]
1995
1996 let buf = Run_shell_in_terminal({})
1997 call assert_equal(colors, term_getansicolors(buf))
1998 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001999 call assert_equal([], term_getansicolors(buf))
2000
2001 exe buf . 'bwipe'
2002endfunc
2003
2004let s:test_colors = [
2005 \ '#616e64', '#0d0a79',
2006 \ '#6d610d', '#0a7373',
2007 \ '#690d0a', '#6d696e',
2008 \ '#0d0a6f', '#616e0d',
2009 \ '#0a6479', '#6d0d0a',
2010 \ '#617373', '#0d0a69',
2011 \ '#6d690d', '#0a6e6f',
2012 \ '#610d0a', '#6e6479',
2013 \]
2014
2015func Test_terminal_ansicolors_global()
2016 CheckFeature termguicolors
2017 CheckFunction term_getansicolors
2018
LemonBoyb2b3acb2022-05-20 10:10:34 +01002019 if has('vtp') && !has('vcon') && !has('gui_running')
2020 throw 'Skipped: does not support termguicolors'
2021 endif
2022
2023 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002024 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2025 let buf = Run_shell_in_terminal({})
2026 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2027 call StopShellInTerminal(buf)
LemonBoyb2b3acb2022-05-20 10:10:34 +01002028 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002029
2030 exe buf . 'bwipe'
2031 unlet g:terminal_ansi_colors
2032endfunc
2033
2034func Test_terminal_ansicolors_func()
2035 CheckFeature termguicolors
2036 CheckFunction term_getansicolors
2037
LemonBoyb2b3acb2022-05-20 10:10:34 +01002038 if has('vtp') && !has('vcon') && !has('gui_running')
2039 throw 'Skipped: does not support termguicolors'
2040 endif
2041
2042 set tgc
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002043 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2044 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2045 call assert_equal(s:test_colors, term_getansicolors(buf))
2046
2047 call term_setansicolors(buf, g:terminal_ansi_colors)
2048 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2049
2050 let colors = [
2051 \ 'ivory', 'AliceBlue',
2052 \ 'grey67', 'dark goldenrod',
2053 \ 'SteelBlue3', 'PaleVioletRed4',
2054 \ 'MediumPurple2', 'yellow2',
2055 \ 'RosyBrown3', 'OrangeRed2',
2056 \ 'white smoke', 'navy blue',
2057 \ 'grey47', 'gray97',
2058 \ 'MistyRose2', 'DodgerBlue4',
2059 \]
2060 eval buf->term_setansicolors(colors)
2061
2062 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002063 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002064 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
LemonBoyb2b3acb2022-05-20 10:10:34 +01002065 set tgc&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002066
2067 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002068 call assert_equal(0, term_setansicolors(buf, []))
2069 exe buf . 'bwipe'
2070endfunc
2071
2072func Test_terminal_all_ansi_colors()
2073 CheckRunVimInTerminal
2074
2075 " Use all the ANSI colors.
2076 call writefile([
2077 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2078 \ 'hi Tblack ctermfg=0 ctermbg=8',
2079 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2080 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2081 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2082 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2083 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2084 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2085 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2086 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2087 \ 'hi Tred ctermfg=9 ctermbg=1',
2088 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2089 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2090 \ 'hi Tblue ctermfg=12 ctermbg=4',
2091 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2092 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2093 \ 'hi Twhite ctermfg=15 ctermbg=7',
2094 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2095 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2096 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2097 \ '',
2098 \ 'call matchadd("Tblack", "A")',
2099 \ 'call matchadd("Tdarkred", "B")',
2100 \ 'call matchadd("Tdarkgreen", "C")',
2101 \ 'call matchadd("Tbrown", "D")',
2102 \ 'call matchadd("Tdarkblue", "E")',
2103 \ 'call matchadd("Tdarkmagenta", "F")',
2104 \ 'call matchadd("Tdarkcyan", "G")',
2105 \ 'call matchadd("Tlightgrey", "H")',
2106 \ 'call matchadd("Tdarkgrey", "I")',
2107 \ 'call matchadd("Tred", "J")',
2108 \ 'call matchadd("Tgreen", "K")',
2109 \ 'call matchadd("Tyellow", "L")',
2110 \ 'call matchadd("Tblue", "M")',
2111 \ 'call matchadd("Tmagenta", "N")',
2112 \ 'call matchadd("Tcyan", "O")',
2113 \ 'call matchadd("Twhite", "P")',
2114 \ 'call matchadd("TdarkredBold", "X")',
2115 \ 'call matchadd("TgreenBold", "Y")',
2116 \ 'call matchadd("TmagentaBold", "Z")',
2117 \ 'redraw',
2118 \ ], 'Xcolorscript')
2119 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2120 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2121
2122 call term_sendkeys(buf, ":q\<CR>")
2123 call StopVimInTerminal(buf)
2124 call delete('Xcolorscript')
2125endfunc
2126
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002127function On_BufFilePost()
2128 doautocmd <nomodeline> User UserEvent
2129endfunction
2130
2131func Test_terminal_nested_autocmd()
2132 new
2133 call setline(1, range(500))
2134 $
2135 let lastline = line('.')
2136
2137 augroup TermTest
2138 autocmd BufFilePost * call On_BufFilePost()
2139 autocmd User UserEvent silent
2140 augroup END
2141
2142 let cmd = Get_cat_123_cmd()
2143 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2144 call assert_equal(lastline, line('.'))
2145
Bram Moolenaar64374752021-04-03 17:22:29 +02002146 let job = term_getjob(buf)
2147 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002148 call delete('Xtext')
2149 augroup TermTest
2150 au!
2151 augroup END
2152endfunc
2153
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002154func Test_terminal_adds_jump()
2155 clearjumps
2156 call term_start("ls", #{curwin: 1})
2157 call assert_equal(1, getjumplist()[0]->len())
2158 bwipe!
2159endfunc
2160
Bram Moolenaareea32af2021-11-21 14:51:13 +00002161func Close_cb(ch, ctx)
2162 call term_wait(a:ctx.bufnr)
2163 let g:close_done = 'done'
2164endfunc
2165
2166func Test_term_wait_in_close_cb()
2167 let g:close_done = ''
2168 let ctx = {}
2169 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2170 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2171
2172 call WaitForAssert({-> assert_equal("done", g:close_done)})
2173
2174 unlet g:close_done
2175 bwipe!
2176endfunc
2177
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002178
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002179" vim: shiftwidth=2 sts=2 expandtab