blob: 45a364666020dd61ff0ee590c6260bc25b0c859a [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()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020017 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020018 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020019
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020020 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020021 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020022 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020023 call assert_match('%aR[^\n]*running]', execute('ls R'))
24 call assert_notmatch('%[^\n]*running]', execute('ls F'))
25 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar004a6782020-04-11 17:09:31 +020026 call assert_fails('set modifiable', 'E946:')
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020027
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020028 call StopShellInTerminal(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020029 call assert_equal('n', mode())
30 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020031 call assert_match('%aF[^\n]*finished]', execute('ls F'))
32 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
33 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034
Bram Moolenaar94053a52017-08-01 21:44:33 +020035 " closing window wipes out the terminal buffer a with finished job
36 close
37 call assert_equal("", bufname(buf))
38
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020039 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020040 unlet g:job
41endfunc
42
Bram Moolenaar00806bc2020-11-05 19:36:38 +010043func Test_terminal_no_name()
44 let buf = Run_shell_in_terminal({})
45 call assert_match('^!', bufname(buf))
46 0file
47 call assert_equal("", bufname(buf))
48 call assert_match('\[No Name\]', execute('file'))
49 call StopShellInTerminal(buf)
Bram Moolenaar00806bc2020-11-05 19:36:38 +010050endfunc
51
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020052func Test_terminal_TerminalWinOpen()
53 au TerminalWinOpen * let b:done = 'yes'
54 let buf = Run_shell_in_terminal({})
55 call assert_equal('yes', b:done)
56 call StopShellInTerminal(buf)
57 " closing window wipes out the terminal buffer with the finished job
58 close
59
60 if has("unix")
61 terminal ++hidden ++open sleep 1
62 sleep 1
63 call assert_fails("echo b:done", 'E121:')
64 endif
65
66 au! TerminalWinOpen
67endfunc
68
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020069func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020070 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020071 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020072
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010075 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020076 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar5b868a82019-02-25 06:11:53 +010082func Test_terminal_paste_register()
83 let @" = "text to paste"
84
85 let buf = Run_shell_in_terminal({})
86 " Wait for the shell to display a prompt
87 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
88
89 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
90 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020091 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010092
93 exe buf . 'bwipe!'
94 unlet g:job
95endfunc
96
Bram Moolenaar94053a52017-08-01 21:44:33 +020097func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020098 let buf = Run_shell_in_terminal({})
Bram Moolenaar28ee8922020-10-28 20:20:00 +010099 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200100 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200101 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200102 call assert_equal("", bufname(buf))
103
104 unlet g:job
105endfunc
106
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200107func Test_terminal_split_quit()
108 let buf = Run_shell_in_terminal({})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200109 split
110 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200111 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200112 sleep 50m
113 call assert_equal('run', job_status(g:job))
114
115 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200116 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200117
118 exe buf . 'bwipe'
119 unlet g:job
120endfunc
121
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100122func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200123 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200124 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200125 quit
126 for nr in range(1, winnr('$'))
127 call assert_notequal(winbufnr(nr), buf)
128 endfor
129 call assert_true(bufloaded(buf))
130 call assert_true(buflisted(buf))
131
132 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200133 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200134 exe buf . 'bwipe'
135
136 unlet g:job
137endfunc
138
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100139func Test_terminal_hide_buffer_job_finished()
140 term echo hello
141 let buf = bufnr()
142 setlocal bufhidden=hide
143 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
144 call assert_true(bufloaded(buf))
145 call assert_true(buflisted(buf))
146 edit Xasdfasdf
147 call assert_true(bufloaded(buf))
148 call assert_true(buflisted(buf))
149 exe buf .. 'buf'
150 call assert_equal(buf, bufnr())
151 setlocal bufhidden=
152 edit Xasdfasdf
153 call assert_false(bufloaded(buf))
154 call assert_false(buflisted(buf))
155 bwipe Xasdfasdf
156endfunc
157
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000158func Test_terminal_rename_buffer()
159 let cmd = Get_cat_123_cmd()
160 let buf = term_start(cmd, {'term_name': 'foo'})
161 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
162 call assert_equal('foo', bufname())
163 call assert_match('foo.*finished', execute('ls'))
164 file bar
165 call assert_equal('bar', bufname())
166 call assert_match('bar.*finished', execute('ls'))
167 exe 'bwipe! ' .. buf
168endfunc
169
Bram Moolenaar1e115362019-01-09 23:01:02 +0100170func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 exe g:buf . 'bwipe!'
172 let g:buf = 0
173endfunc
174
Bram Moolenaar9d189612017-09-09 18:11:00 +0200175func Get_cat_123_cmd()
176 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100177 if !has('conpty')
178 return 'cmd /c "cls && color 2 && echo 123"'
179 else
180 " When clearing twice, extra sequence is not output.
181 return 'cmd /c "cls && cls && color 2 && echo 123"'
182 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200183 else
184 call writefile(["\<Esc>[32m123"], 'Xtext')
185 return "cat Xtext"
186 endif
187endfunc
188
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200189func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200190 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200191 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
192 let g:job = term_getjob(g:buf)
193
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200194 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
195 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200196 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100197 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200198 call delete('Xtext')
199endfunc
200
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200201func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200202 let l = term_scrape(a:buf, 0)
203 call assert_true(len(l) == 0)
204 let l = term_scrape(a:buf, 999)
205 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200206 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200207 call assert_true(len(l) > 0)
208 call assert_equal('1', l[0].chars)
209 call assert_equal('2', l[1].chars)
210 call assert_equal('3', l[2].chars)
211 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200212 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
213 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100214 if has('win32')
215 " On Windows 'background' always defaults to dark, even though the terminal
216 " may use a light background. Therefore accept both white and black.
217 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200218 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100219 if &background == 'light'
220 call assert_equal('#ffffff', l[0].bg)
221 else
222 call assert_equal('#000000', l[0].bg)
223 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 endif
225
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200226 let l = term_getline(a:buf, -1)
227 call assert_equal('', l)
228 let l = term_getline(a:buf, 0)
229 call assert_equal('', l)
230 let l = term_getline(a:buf, 999)
231 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200232 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200233 call assert_equal('123', l)
234endfunc
235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_123()
237 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200238 let buf = term_start(cmd)
239
240 let termlist = term_list()
241 call assert_equal(1, len(termlist))
242 call assert_equal(buf, termlist[0])
243
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200244 " Nothing happens with invalid buffer number
245 call term_wait(1234)
246
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200247 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200248 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200249 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200250 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200251 call Check_123(buf)
252
253 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100254 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200255 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200256 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200257 call Check_123(buf)
258
259 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200260 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200261endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200262
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200263func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200264 call writefile(["léttまrs"], 'Xtext')
265 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200266 " Run cmd with UTF-8 codepage to make the type command print the expected
267 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100268 let buf = term_start("cmd /K chcp 65001")
269 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200270 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100271 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200272 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100273 let buf = term_start("cat Xtext")
274 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200275 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200276
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100277 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
278 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200279 call assert_true(len(l) >= 7)
280 call assert_equal('l', l[0].chars)
281 call assert_equal('é', l[1].chars)
282 call assert_equal(1, l[1].width)
283 call assert_equal('t', l[2].chars)
284 call assert_equal('t', l[3].chars)
285 call assert_equal('ま', l[4].chars)
286 call assert_equal(2, l[4].width)
287 call assert_equal('r', l[5].chars)
288 call assert_equal('s', l[6].chars)
289
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100290 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200291 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200292 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200293
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100294 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200295 call delete('Xtext')
296endfunc
297
Bram Moolenaar8b896142020-08-02 15:05:05 +0200298func Test_terminal_one_column()
299 " This creates a terminal, displays a double-wide character and makes the
300 " window one column wide. This used to cause a crash.
301 let width = &columns
302 botright vert term
303 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200304 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200305 exe "set columns=" .. (width / 2)
306 redraw
307 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200308 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200309 exe "set columns=" .. width
310 exe buf . 'bwipe!'
311endfunc
312
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200313func Test_terminal_scroll()
314 call writefile(range(1, 200), 'Xtext')
315 if has('win32')
316 let cmd = 'cmd /c "type Xtext"'
317 else
318 let cmd = "cat Xtext"
319 endif
320 let buf = term_start(cmd)
321
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100322 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200323 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200324 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200325
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100326 " wait until the scrolling stops
327 while 1
328 let scrolled = buf->term_getscrolled()
329 sleep 20m
330 if scrolled == buf->term_getscrolled()
331 break
332 endif
333 endwhile
334
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200335 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200336 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200337 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200338 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200339 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200340 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200341
342 exe buf . 'bwipe'
343 call delete('Xtext')
344endfunc
345
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200346func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200347 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200348 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200349 call writefile(range(150), 'Xtext')
350 if has('win32')
351 call term_sendkeys(buf, "type Xtext\<CR>")
352 else
353 call term_sendkeys(buf, "cat Xtext\<CR>")
354 endif
355 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200356 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200357 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200358 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200359 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200360
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200361 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200362 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200363 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100364 call delete('Xtext')
365endfunc
366
367func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200368 " tail -f only works on Unix
369 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100370
371 call writefile(range(50), 'Xtext')
372 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100373 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100374 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100375 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100376 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
377 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100378 \ 'call feedkeys("\<C-W>N", "xt")',
379 \ ], 'XTest_postponed')
380 let buf = RunVimInTerminal('-S XTest_postponed', {})
381 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100382 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100383
384 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200385 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100386 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100387 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100388
389 " Back to Terminal-Job mode, text will scroll and show the extra line.
390 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100391 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100392
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100393 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100394 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200395 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100396 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100397 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200398 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100399 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100400 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
401
402 " check type of term_sendkeys() return value
403 echo type(tsk_ret)
404
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100405 call StopVimInTerminal(buf)
406 call delete('XTest_postponed')
407 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200408endfunc
409
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100410" Run diff on two dumps with different size.
411func Test_terminal_dumpdiff_size()
412 call assert_equal(1, winnr('$'))
413 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
414 call assert_equal(2, winnr('$'))
415 call assert_match('Test_incsearch_search_01.dump', getline(10))
416 call assert_match(' +++++$', getline(11))
417 call assert_match('Test_popup_command_01.dump', getline(31))
418 call assert_equal(repeat('+', 75), getline(30))
419 quit
420endfunc
421
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200422func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200423 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200424
Bram Moolenaarb2412082017-08-20 18:09:14 +0200425 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200426 let size = term_getsize('')
427 bwipe!
428 call assert_equal(5, size[0])
429
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200430 call term_start(cmd, {'term_rows': 6})
431 let size = term_getsize('')
432 bwipe!
433 call assert_equal(6, size[0])
434
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200435 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200436 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200437 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200438
439 call term_setsize('', 6, 0)
440 call assert_equal([6, 33], term_getsize(''))
441
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200442 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200443 call assert_equal([6, 35], term_getsize(''))
444
445 call term_setsize('', 7, 30)
446 call assert_equal([7, 30], term_getsize(''))
447
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200448 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200449 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200450
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200451 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
452 let size = term_getsize('')
453 bwipe!
454 call assert_equal([6, 36], size)
455
Bram Moolenaarb2412082017-08-20 18:09:14 +0200456 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200457 let size = term_getsize('')
458 bwipe!
459 call assert_equal(20, size[1])
460
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200461 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200462 let size = term_getsize('')
463 bwipe!
464 call assert_equal(26, size[1])
465
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200466 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200467 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200468 let size = term_getsize('')
469 bwipe!
470 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200471
472 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
473 let size = term_getsize('')
474 bwipe!
475 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200476
Bram Moolenaar5300be62021-11-13 10:27:40 +0000477 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
478 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000479 if has('float')
480 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
481 endif
482
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200483 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200484endfunc
485
Bram Moolenaareba13e42021-02-23 17:47:23 +0100486func Test_terminal_zero_height()
487 split
488 wincmd j
489 anoremenu 1.1 WinBar.test :
490 terminal ++curwin
491 wincmd k
492 wincmd _
493 redraw
494
495 call term_sendkeys(bufnr(), "exit\r")
496 bwipe!
497endfunc
498
Bram Moolenaarda43b612017-08-11 22:27:50 +0200499func Test_terminal_curwin()
500 let cmd = Get_cat_123_cmd()
501 call assert_equal(1, winnr('$'))
502
Bram Moolenaarb1009092020-05-31 16:04:42 +0200503 split Xdummy
504 call setline(1, 'dummy')
505 write
506 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200507 exe 'terminal ++curwin ' . cmd
508 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200509 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200510 bwipe!
511
Bram Moolenaarb1009092020-05-31 16:04:42 +0200512 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200513 call term_start(cmd, {'curwin': 1})
514 call assert_equal(2, winnr('$'))
515 bwipe!
516
Bram Moolenaarb1009092020-05-31 16:04:42 +0200517 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200518 call setline(1, 'change')
519 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
520 call assert_equal(2, winnr('$'))
521 exe 'terminal! ++curwin ' . cmd
522 call assert_equal(2, winnr('$'))
523 bwipe!
524
Bram Moolenaarb1009092020-05-31 16:04:42 +0200525 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200526 call setline(1, 'change')
527 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
528 call assert_equal(2, winnr('$'))
529 bwipe!
530
Bram Moolenaarb1009092020-05-31 16:04:42 +0200531 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200532 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200533 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200534 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200535endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536
Bram Moolenaarff546792017-11-21 14:47:57 +0100537func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200538 if s:python != ''
539 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200540 " 500 was not enough for Travis
541 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200542 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200543 echo 'This will take five seconds...'
544 let waittime = 2000
545 if has('win32')
546 let cmd = $windir . '\system32\timeout.exe 1'
547 else
548 let cmd = 'sleep 1'
549 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200550 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100551 return [cmd, waittime]
552endfunc
553
554func Test_terminal_finish_open_close()
555 call assert_equal(1, winnr('$'))
556
557 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200558
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100559 " shell terminal closes automatically
560 terminal
561 let buf = bufnr('%')
562 call assert_equal(2, winnr('$'))
563 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200564 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200565 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200566 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100567
568 " shell terminal that does not close automatically
569 terminal ++noclose
570 let buf = bufnr('%')
571 call assert_equal(2, winnr('$'))
572 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200573 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200574 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100575 call assert_equal(2, winnr('$'))
576 quit
577 call assert_equal(1, winnr('$'))
578
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200579 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200580 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200581 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200582 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200583
584 call term_start(cmd, {'term_finish': 'close'})
585 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200586 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200587 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200588 call assert_equal(1, winnr('$'))
589
590 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200591 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200592 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200593 bwipe
594
595 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200596 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200597 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200598 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200599
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200600 exe 'terminal ++hidden ++open ' . cmd
601 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200602 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200603 bwipe
604
605 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
606 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200607 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200608 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200609
610 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
611 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
612 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
613 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
614
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100615 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 +0200616 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200617 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200618 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100619 call assert_equal('opened the buffer in a window', g:result)
620 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200621 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200622endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200623
624func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100625 if has('win32')
626 let cmd = 'cmd /c cd'
627 else
628 CheckExecutable pwd
629 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200630 endif
631 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100632 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200633 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200634
635 exe buf . 'bwipe'
636 call delete('Xdir', 'rf')
637endfunc
638
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200639func Test_terminal_cwd_failure()
640 " Case 1: Provided directory is not actually a directory. Attempt to make
641 " the file executable as well.
642 call writefile([], 'Xfile')
643 call setfperm('Xfile', 'rwx------')
644 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
645 call delete('Xfile')
646
647 " Case 2: Directory does not exist.
648 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
649
650 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100651 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200652 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100653 call mkdir('XdirNoAccess', '', '0600')
654 " return early if the directory permissions could not be set properly
655 if getfperm('XdirNoAccess')[2] == 'x'
656 call delete('XdirNoAccess', 'rf')
657 return
658 endif
659 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
660 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200661 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200662endfunc
663
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100664func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200665 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200666 call s:test_environment("VIM_SERVERNAME", v:servername)
667endfunc
668
669func Test_terminal_version()
670 call s:test_environment("VIM_TERMINAL", string(v:version))
671endfunc
672
673func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100674 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100675 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200676 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100677 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200678 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100679 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200680 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100681 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200682 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200683 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200684 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100685
Bram Moolenaar012eb662018-03-13 17:55:27 +0100686 exe buf . 'bwipe'
687 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100688endfunc
689
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200690func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100691 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200692 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200693 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100694 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100695 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100696 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100697 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100698 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200699 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200700 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200701 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200702
Bram Moolenaar012eb662018-03-13 17:55:27 +0100703 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200704endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200705
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200706func Test_terminal_list_args()
707 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100708 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200709 exe buf . 'bwipe!'
710 call assert_equal("", bufname(buf))
711endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200712
713func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100714 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100715 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100716 let wait_time = 5000
717 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100718 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200719 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100720 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200721 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100722 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100723 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100724 " NPFS is used in Windows, nonblocking mode does not work properly.
725 elseif has('win32')
726 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200727 else
728 let len = 5000
729 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200730
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100731 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100732 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100733 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200734 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100735 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200736
737 " On MS-Windows there is an extra empty line below "done". Find "done" in
738 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100739 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100740 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100741 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200742 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100743 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200744 endif
745 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200746
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100747 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200748 call StopShellInTerminal(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200749 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200750 bwipe
751endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200752
753func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100754 " TODO: enable once writing to stdin works on MS-Windows
755 CheckNotMSWindows
756 CheckExecutable wc
757
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200758 call setline(1, ['one', 'two', 'three'])
759 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200760 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200761 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200762 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100763 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200764
765 call setline(1, ['one', 'two', 'three', 'four'])
766 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200767 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200768 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200769 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100770 %bwipe!
771endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200772
Bram Moolenaar21109272020-01-30 16:27:20 +0100773func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200774 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200775
Bram Moolenaar21109272020-01-30 16:27:20 +0100776 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200777 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100778 " MS-Windows echoes the input, Unix doesn't.
779 if has('win32')
780 call WaitFor({-> getline('$') =~ 'exit(123)'})
781 call assert_equal('hello', getline(line('$') - 1))
782 else
783 call WaitFor({-> getline('$') =~ 'hello'})
784 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200785 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100786 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
787 %bwipe!
788endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200789
Bram Moolenaar21109272020-01-30 16:27:20 +0100790func Test_terminal_eof_arg_win32_ctrl_z()
791 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200792 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100793
794 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200795 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100796 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
797 call assert_match('\^Z', getline(line('$') - 1))
798 %bwipe!
799endfunc
800
801func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200802 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100803
804 " Check the last specified ++eof arg is used and should not memory leak.
805 new
806 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200807 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100808 " MS-Windows echoes the input, Unix doesn't.
809 if has('win32')
810 call WaitFor({-> getline('$') =~ 'exit(123)'})
811 call assert_equal('hello', getline(line('$') - 1))
812 else
813 call WaitFor({-> getline('$') =~ 'hello'})
814 call assert_equal('hello', getline('$'))
815 endif
816 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
817 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200818endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200819
820func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100821 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200822 let buf = term_start('NONE', {})
823 call assert_notequal(0, buf)
824
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200825 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200826 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100827 if has('gui_running') && !has('win32')
828 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200829 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100830 else
831 " Otherwise using a job works on all systems.
832 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200833 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200834 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200835
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200836 bwipe!
837endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200838
839func Test_terminal_special_chars()
840 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200841 CheckUnix
842
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200843 call mkdir('Xdir with spaces')
844 call writefile(['x'], 'Xdir with spaces/quoted"file')
845 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200846 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100847 " make sure the job has finished
848 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200849
850 call delete('Xdir with spaces', 'rf')
851 bwipe
852endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200853
854func Test_terminal_wrong_options()
855 call assert_fails('call term_start(&shell, {
856 \ "in_io": "file",
857 \ "in_name": "xxx",
858 \ "out_io": "file",
859 \ "out_name": "xxx",
860 \ "err_io": "file",
861 \ "err_name": "xxx"
862 \ })', 'E474:')
863 call assert_fails('call term_start(&shell, {
864 \ "out_buf": bufnr("%")
865 \ })', 'E474:')
866 call assert_fails('call term_start(&shell, {
867 \ "err_buf": bufnr("%")
868 \ })', 'E474:')
869endfunc
870
871func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100872 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200873 let cmd = Get_cat_123_cmd()
874 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200875 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100876 " ConPTY may precede escape sequence. There are things that are not so.
877 if !has('conpty')
878 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
879 call assert_match('123', readfile('Xfile')[0])
880 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200881 let g:job = term_getjob(buf)
882 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200883
884 if has('win32')
885 " On Windows we cannot delete a file being used by a process. When
886 " job_status() returns "dead", the process remains for a short time.
887 " Just wait for a moment.
888 sleep 50m
889 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200890 call delete('Xfile')
891 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200892
893 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200894 call writefile(['one line'], 'Xfile')
895 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200896 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200897 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200898 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200899 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200900 bwipe
901 call delete('Xfile')
902 endif
903endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200904
905func TerminalTmap(remap)
906 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +0100907 " Wait for the shell to display a prompt
908 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200909 call assert_equal('t', mode())
910
911 if a:remap
912 tmap 123 456
913 else
914 tnoremap 123 456
915 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100916 " don't use abcde, it's an existing command
917 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200918 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100919 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200920 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200921 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200922 let lnum = term_getcursor(buf)[0]
923 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100924 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200925 else
926 call assert_match('456', term_getline(buf, lnum))
927 endif
928
929 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200930 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200931
932 tunmap 123
933 tunmap 456
934 call assert_equal('', maparg('123', 't'))
935 close
936 unlet g:job
937endfunc
938
939func Test_terminal_tmap()
940 call TerminalTmap(1)
941 call TerminalTmap(0)
942endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200943
944func Test_terminal_wall()
945 let buf = Run_shell_in_terminal({})
946 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200947 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200948 exe buf . 'bwipe'
949 unlet g:job
950endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200951
Bram Moolenaar7a760922018-02-19 23:10:02 +0100952func Test_terminal_wqall()
953 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200954 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200955 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100956 exe buf . 'bwipe'
957 unlet g:job
958endfunc
959
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200960func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100961 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200962 let save_enc = &encoding
963 set encoding=utf-8
964
965 if has('win32')
966 let cmd = "cmd /K chcp 65001"
967 let lnum = [3, 6, 9]
968 else
969 let cmd = &shell
970 let lnum = [1, 3, 5]
971 endif
972
973 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200974 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100975 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200976 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200977
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200978 if has('win32')
979 call assert_equal('cmd', job_info(g:job).cmd[0])
980 else
981 call assert_equal(&shell, job_info(g:job).cmd[0])
982 endif
983
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200984 " ascii + composing
985 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200986 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200987 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200988 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200989 call term_sendkeys(buf, "\<cr>")
990 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200991 let l = term_scrape(buf, lnum[0] + 1)
992 call assert_equal("a\u0308", l[0].chars)
993 call assert_equal("b", l[1].chars)
994 call assert_equal("c", l[2].chars)
995
Bram Moolenaar4549dad2021-02-08 21:29:48 +0100996 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200997 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200998 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200999 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001000 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001001 call term_sendkeys(buf, "\<cr>")
1002 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001003 let l = term_scrape(buf, lnum[1] + 1)
1004 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001005 call assert_equal(2, l[0].width)
1006 call assert_equal("\u304e", l[1].chars)
1007 call assert_equal(2, l[1].width)
1008 call assert_equal("\u304f\u3099", l[2].chars)
1009 call assert_equal(2, l[2].width)
1010 call assert_equal("\u3052", l[3].chars)
1011 call assert_equal(2, l[3].width)
1012 call assert_equal("\u3053\u3099", l[4].chars)
1013 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001014
1015 " \u00a0 + composing
1016 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001017 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001018 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001019 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001020 call term_sendkeys(buf, "\<cr>")
1021 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001022 let l = term_scrape(buf, lnum[2] + 1)
1023 call assert_equal("\u00a0\u0308", l[3].chars)
1024
1025 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001026 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001027 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001028 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001029 let &encoding = save_enc
1030endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001031
1032func Test_terminal_aucmd_on_close()
1033 fun Nop()
1034 let s:called = 1
1035 endfun
1036
1037 aug repro
1038 au!
1039 au BufWinLeave * call Nop()
1040 aug END
1041
1042 let [cmd, waittime] = s:get_sleep_cmd()
1043
1044 call assert_equal(1, winnr('$'))
1045 new
1046 call setline(1, ['one', 'two'])
1047 exe 'term ++close ' . cmd
1048 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001049 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001050 call assert_equal(1, s:called)
1051 bwipe!
1052
1053 unlet s:called
1054 au! repro
1055 delfunc Nop
1056endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001057
1058func Test_terminal_term_start_empty_command()
1059 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001060 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001061 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001062 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001063 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001064 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001065 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001066 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001067 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001068 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001069 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001070 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001071 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001072 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001073 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001074 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001075 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001076 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001077 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1078 call assert_fails(cmd, 'E475:')
1079 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001080 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001081 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001082 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1083 call assert_fails(cmd, 'E475:')
1084 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1085 call assert_fails(cmd, 'E730:')
1086 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001087 if has('gui_running') || has('termguicolors')
1088 call assert_fails(cmd, 'E475:')
1089 else
1090 call assert_fails(cmd, 'E254:')
1091 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001092 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001093endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001094
1095func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001096 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001097
1098 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001099 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001100
Bram Moolenaar086eb872018-03-25 21:24:12 +02001101 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001102 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001103
Bram Moolenaar086eb872018-03-25 21:24:12 +02001104 " Request the cursor position.
1105 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001106
1107 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001108 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001109
Bram Moolenaar086eb872018-03-25 21:24:12 +02001110 " End "cat" gently.
1111 call term_sendkeys(buf, "\<CR>\<C-D>")
1112
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001113 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001114 exe buf . 'bwipe'
1115 unlet g:job
1116endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001117
Bram Moolenaarc2958582021-12-14 11:16:31 +00001118" Run this first, it fails when run after other tests.
1119func Test_aa_terminal_focus_events()
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001120 CheckNotGui
1121 CheckUnix
1122 CheckRunVimInTerminal
1123
1124 let save_term = &term
1125 let save_ttymouse = &ttymouse
1126 set term=xterm ttymouse=xterm2
1127
1128 let lines =<< trim END
1129 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001130 au FocusLost * call setline(1, 'I am lost') | set nomod
1131 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001132 END
1133 call writefile(lines, 'XtermFocus')
1134 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1135
1136 " Send a focus event to ourselves, it should be forwarded to the terminal
1137 call feedkeys("\<Esc>[O", "Lx!")
1138 call TermWait(buf)
1139 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1140
1141 call feedkeys("\<Esc>[I", "Lx!")
1142 call TermWait(buf)
1143 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1144
Bram Moolenaare5050712021-12-09 10:51:05 +00001145 " check that a command line being edited is redrawn in place
1146 call term_sendkeys(buf, ":" .. repeat('x', 80))
1147 call TermWait(buf)
1148 call feedkeys("\<Esc>[O", "Lx!")
1149 call TermWait(buf)
1150 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 Moolenaar435acdb2018-03-10 20:51:25 +01001221 let buf = RunVimInTerminal('', {})
1222
1223 " Open a terminal window and wait for the prompt to appear
1224 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001225 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1226 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001227
1228 " make Vim exit, it will prompt to kill the shell
1229 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001230 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001231 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001232 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001233
1234 " close the terminal window where Vim was running
1235 quit
1236endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001237
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001238" Run Vim in a terminal, then start a terminal window with a shell and check
1239" that Vim exits if it is closed.
1240func Test_terminal_exit()
1241 CheckRunVimInTerminal
1242
1243 let lines =<< trim END
1244 let winid = win_getid()
1245 help
1246 term
1247 let termid = win_getid()
1248 call win_gotoid(winid)
1249 close
1250 call win_gotoid(termid)
1251 END
1252 call writefile(lines, 'XtermExit')
1253 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1254 let job = term_getjob(buf)
1255 call WaitForAssert({-> assert_equal("run", job_status(job))})
1256
1257 " quit the shell, it will make Vim exit
1258 call term_sendkeys(buf, "exit\<CR>")
1259 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1260
1261 call delete('XtermExit')
1262endfunc
1263
Bram Moolenaar012eb662018-03-13 17:55:27 +01001264func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001265 augroup repro
1266 au!
1267 au TerminalOpen * let s:called += 1
1268 augroup END
1269
1270 let s:called = 0
1271
1272 " Open a terminal window with :terminal
1273 terminal
1274 call assert_equal(1, s:called)
1275 bwipe!
1276
1277 " Open a terminal window with term_start()
1278 call term_start(&shell)
1279 call assert_equal(2, s:called)
1280 bwipe!
1281
1282 " Open a hidden terminal buffer with :terminal
1283 terminal ++hidden
1284 call assert_equal(3, s:called)
1285 for buf in term_list()
1286 exe buf . "bwipe!"
1287 endfor
1288
1289 " Open a hidden terminal buffer with term_start()
1290 let buf = term_start(&shell, {'hidden': 1})
1291 call assert_equal(4, s:called)
1292 exe buf . "bwipe!"
1293
1294 unlet s:called
1295 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001296endfunc
1297
1298func Test_open_term_from_cmd()
1299 CheckUnix
1300 CheckRunVimInTerminal
1301
1302 let lines =<< trim END
1303 call setline(1, ['a', 'b', 'c'])
1304 3
1305 set incsearch
1306 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1307 END
1308 call writefile(lines, 'Xopenterm')
1309 let buf = RunVimInTerminal('-S Xopenterm', {})
1310
1311 " this opens a window, incsearch should not use the old cursor position
1312 call term_sendkeys(buf, "/\<F3>")
1313 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1314 call term_sendkeys(buf, "\<Esc>")
1315 call term_sendkeys(buf, ":q\<CR>")
1316
1317 call StopVimInTerminal(buf)
1318 call delete('Xopenterm')
1319endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001320
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001321func Test_combining_double_width()
1322 CheckUnix
1323 CheckRunVimInTerminal
1324
1325 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1326 let lines =<< trim END
1327 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1328 END
1329 call writefile(lines, 'Xcombining')
1330 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1331
1332 " this opens a window, incsearch should not use the old cursor position
1333 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1334 call term_sendkeys(buf, ":q\<CR>")
1335
1336 call StopVimInTerminal(buf)
1337 call delete('Xonedouble')
1338 call delete('Xcombining')
1339endfunc
1340
Bram Moolenaar02764712020-11-14 20:21:55 +01001341func Test_terminal_popup_with_cmd()
1342 " this was crashing
1343 let buf = term_start(&shell, #{hidden: v:true})
1344 let s:winid = popup_create(buf, {})
1345 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1346 call feedkeys("\<F3>", 'xt')
1347
1348 tunmap <F3>
1349 exe 'bwipe! ' .. buf
1350 unlet s:winid
1351endfunc
1352
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001353func Test_terminal_popup_bufload()
1354 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1355 let winid = popup_create(termbuf, {})
1356 sleep 50m
1357
1358 let newbuf = bufadd('')
1359 call bufload(newbuf)
1360 call setbufline(newbuf, 1, 'foobar')
1361
1362 " must not have switched to another window
1363 call assert_equal(winid, win_getid())
1364
Bram Moolenaare6329e42020-11-16 21:10:34 +01001365 call StopShellInTerminal(termbuf)
1366 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001367 exe 'bwipe! ' .. newbuf
1368endfunc
1369
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001370func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001371 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001372 CheckUnix
1373
1374 " use "sh" instead of "&shell" in the hope it will use a short prompt
1375 let lines =<< trim END
1376 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1377 exe 'buffer ' .. termbuf
1378
1379 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1380 sleep 50m
1381
1382 call term_sendkeys(termbuf, "echo 'test'")
1383 END
1384 call writefile(lines, 'XpopupScript')
1385 let buf = RunVimInTerminal('-S XpopupScript', {})
1386
1387 " typed text appears both in normal window and in popup
1388 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1389 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1390
Bram Moolenaar0407d272021-12-13 22:17:44 +00001391 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1392 call TermWait(buf)
1393 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001394 call StopVimInTerminal(buf)
1395 call delete('XpopupScript')
1396endfunc
1397
Bram Moolenaare41decc2020-11-14 21:34:59 +01001398func Test_terminal_popup_insert_cmd()
1399 CheckUnix
1400
1401 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1402 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001403 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001404 endfunc
1405 call feedkeys("i\<F3>")
1406 sleep 10m
1407 call assert_equal('n', mode())
1408
1409 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001410 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001411 delfunc StartTermInPopup
1412 iunmap <F3>
1413endfunc
1414
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001415func Check_dump01(off)
1416 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1417 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001418 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001419endfunc
1420
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001421func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001422 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001423
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001424 let save_enc = &encoding
1425 set encoding=utf-8
1426 call assert_equal(1, winnr('$'))
1427
1428 let text = " a\u0300 e\u0302 o\u0308"
1429 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001430 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001431 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001432 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001433 let dumpline = readfile('Xdump')[0]
1434 call assert_match('|à| |ê| |ö', dumpline)
1435
1436 call StopVimInTerminal(buf)
1437 call delete('Xcomposing')
1438 call delete('Xdump')
1439 let &encoding = save_enc
1440endfunc
1441
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001442" Tests for failures in the term_dumpwrite() function
1443func Test_terminal_dumpwrite_errors()
1444 CheckRunVimInTerminal
1445 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1446 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001447 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001448 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1449 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1450 call writefile([], 'Xtest.dump')
1451 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1452 call delete('Xtest.dump')
1453 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1454 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001455 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001456 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001457 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001458 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1459 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1460 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1461endfunc
1462
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001463" just testing basic functionality.
1464func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001465 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001466 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001467 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001468 call assert_equal(2, winnr('$'))
1469 call assert_equal(20, line('$'))
1470 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001471
1472 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001473 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001474 call assert_equal(buf, buf2)
1475 call assert_notequal('one two three four five', trim(getline(1)))
1476
1477 " Load the first dump again in the same window
1478 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1479 call assert_equal(buf, buf2)
1480 call Check_dump01(0)
1481
1482 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1483 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1484 new
1485 let closedbuf = winbufnr('')
1486 quit
1487 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001488 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001489 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001490
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001491 quit
1492endfunc
1493
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001494func Test_terminal_dumpload_dump()
1495 CheckRunVimInTerminal
1496
1497 let lines =<< trim END
1498 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1499 END
1500 call writefile(lines, 'XtermDumpload')
1501 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1502 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1503
1504 call StopVimInTerminal(buf)
1505 call delete('XtermDumpload')
1506endfunc
1507
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001508func Test_terminal_dumpdiff()
1509 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001510 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001511 call assert_equal(2, winnr('$'))
1512 call assert_equal(62, line('$'))
1513 call Check_dump01(0)
1514 call Check_dump01(42)
1515 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1516 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001517
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001518 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001519 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1520 call writefile([], 'X1.dump')
1521 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1522 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001523endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001524
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001525func Test_terminal_dumpdiff_swap()
1526 call assert_equal(1, winnr('$'))
1527 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1528 call assert_equal(2, winnr('$'))
1529 call assert_equal(62, line('$'))
1530 call assert_match('Test_popup_command_01.dump', getline(21))
1531 call assert_match('Test_popup_command_03.dump', getline(42))
1532 call assert_match('Undo', getline(3))
1533 call assert_match('three four five', getline(45))
1534
1535 normal s
1536 call assert_match('Test_popup_command_03.dump', getline(21))
1537 call assert_match('Test_popup_command_01.dump', getline(42))
1538 call assert_match('three four five', getline(3))
1539 call assert_match('Undo', getline(45))
1540 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001541
1542 " Diff two terminal dump files with different number of rows
1543 " Swap the diffs
1544 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1545 call assert_match('Test_popup_command_01.dump', getline(21))
1546 call assert_match('Test_winline_rnu.dump', getline(42))
1547 normal s
1548 call assert_match('Test_winline_rnu.dump', getline(6))
1549 call assert_match('Test_popup_command_01.dump', getline(27))
1550 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001551endfunc
1552
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001553func Test_terminal_dumpdiff_options()
1554 set laststatus=0
1555 call assert_equal(1, winnr('$'))
1556 let height = winheight(0)
1557 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1558 call assert_equal(2, winnr('$'))
1559 call assert_equal(height, winheight(winnr()))
1560 call assert_equal(33, winwidth(winnr()))
1561 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1562 quit
1563
1564 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001565 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1566 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001567 call assert_equal(&columns, winwidth(0))
1568 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001569 call assert_equal('something else', bufname('%'))
1570 quit
1571
1572 call assert_equal(1, winnr('$'))
1573 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1574 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001575 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 +01001576 bwipe
1577
1578 set laststatus&
1579endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001580
Bram Moolenaar10772302019-01-20 18:25:54 +01001581" When drawing the statusline the cursor position may not have been updated
1582" yet.
1583" 1. create a terminal, make it show 2 lines
1584" 2. 0.5 sec later: leave terminal window, execute "i"
1585" 3. 0.5 sec later: clear terminal window, now it's 1 line
1586" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1587" 4. 0.5 sec later: should be done, clean up
1588func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001589 CheckUnix
Bram Moolenaar81035272021-12-16 18:02:07 +00001590 CheckFeature timers
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001591
Bram Moolenaar10772302019-01-20 18:25:54 +01001592 set statusline=x
1593 terminal
1594 let tbuf = bufnr('')
1595 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1596 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1597 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1598 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1599
1600 sleep 2
1601 exe tbuf . 'bwipe!'
1602 au! BufLeave
1603 set statusline=
1604endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001605
Bram Moolenaar81035272021-12-16 18:02:07 +00001606func CheckTerminalWindowWorks(buf)
1607 call WaitForAssert({-> assert_match('!sh \[running\]', term_getline(a:buf, 10))})
1608 call term_sendkeys(a:buf, "exit\<CR>")
1609 call WaitForAssert({-> assert_match('!sh \[finished\]', term_getline(a:buf, 10))})
1610 call term_sendkeys(a:buf, ":q\<CR>")
1611 call WaitForAssert({-> assert_match('^\~', term_getline(a:buf, 10))})
1612endfunc
1613
1614func Test_start_terminal_from_timer()
1615 CheckUnix
1616 CheckFeature timers
1617
1618 " Open a terminal window from a timer, typed text goes to the terminal
1619 call writefile(["call timer_start(100, { -> term_start('sh') })"], 'XtimerTerm')
1620 let buf = RunVimInTerminal('-S XtimerTerm', {})
1621 call CheckTerminalWindowWorks(buf)
1622
1623 " do the same in Insert mode
1624 call term_sendkeys(buf, ":call timer_start(200, { -> term_start('sh') })\<CR>a")
1625 call CheckTerminalWindowWorks(buf)
1626
1627 call StopVimInTerminal(buf)
1628 call delete('XtimerTerm')
1629endfunc
1630
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001631func Test_terminal_window_focus()
1632 let winid1 = win_getid()
1633 terminal
1634 let winid2 = win_getid()
1635 call feedkeys("\<C-W>j", 'xt')
1636 call assert_equal(winid1, win_getid())
1637 call feedkeys("\<C-W>k", 'xt')
1638 call assert_equal(winid2, win_getid())
1639 " can use a cursor key here
1640 call feedkeys("\<C-W>\<Down>", 'xt')
1641 call assert_equal(winid1, win_getid())
1642 call feedkeys("\<C-W>\<Up>", 'xt')
1643 call assert_equal(winid2, win_getid())
1644
1645 bwipe!
1646endfunc
1647
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001648func Api_drop_common(options)
1649 call assert_equal(1, winnr('$'))
1650
1651 " Use the title termcap entries to output the escape sequence.
1652 call writefile([
1653 \ 'set title',
1654 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1655 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1656 \ 'redraw',
1657 \ "set t_ts=",
1658 \ ], 'Xscript')
1659 let buf = RunVimInTerminal('-S Xscript', {})
1660 call WaitFor({-> bufnr('Xtextfile') > 0})
1661 call assert_equal('Xtextfile', expand('%:t'))
1662 call assert_true(winnr('$') >= 3)
1663 return buf
1664endfunc
1665
1666func Test_terminal_api_drop_newwin()
1667 CheckRunVimInTerminal
1668 let buf = Api_drop_common('')
1669 call assert_equal(0, &bin)
1670 call assert_equal('', &fenc)
1671
1672 call StopVimInTerminal(buf)
1673 call delete('Xscript')
1674 bwipe Xtextfile
1675endfunc
1676
1677func Test_terminal_api_drop_newwin_bin()
1678 CheckRunVimInTerminal
1679 let buf = Api_drop_common(',{"bin":1}')
1680 call assert_equal(1, &bin)
1681
1682 call StopVimInTerminal(buf)
1683 call delete('Xscript')
1684 bwipe Xtextfile
1685endfunc
1686
1687func Test_terminal_api_drop_newwin_binary()
1688 CheckRunVimInTerminal
1689 let buf = Api_drop_common(',{"binary":1}')
1690 call assert_equal(1, &bin)
1691
1692 call StopVimInTerminal(buf)
1693 call delete('Xscript')
1694 bwipe Xtextfile
1695endfunc
1696
1697func Test_terminal_api_drop_newwin_nobin()
1698 CheckRunVimInTerminal
1699 set binary
1700 let buf = Api_drop_common(',{"nobin":1}')
1701 call assert_equal(0, &bin)
1702
1703 call StopVimInTerminal(buf)
1704 call delete('Xscript')
1705 bwipe Xtextfile
1706 set nobinary
1707endfunc
1708
1709func Test_terminal_api_drop_newwin_nobinary()
1710 CheckRunVimInTerminal
1711 set binary
1712 let buf = Api_drop_common(',{"nobinary":1}')
1713 call assert_equal(0, &bin)
1714
1715 call StopVimInTerminal(buf)
1716 call delete('Xscript')
1717 bwipe Xtextfile
1718 set nobinary
1719endfunc
1720
1721func Test_terminal_api_drop_newwin_ff()
1722 CheckRunVimInTerminal
1723 let buf = Api_drop_common(',{"ff":"dos"}')
1724 call assert_equal("dos", &ff)
1725
1726 call StopVimInTerminal(buf)
1727 call delete('Xscript')
1728 bwipe Xtextfile
1729endfunc
1730
1731func Test_terminal_api_drop_newwin_fileformat()
1732 CheckRunVimInTerminal
1733 let buf = Api_drop_common(',{"fileformat":"dos"}')
1734 call assert_equal("dos", &ff)
1735
1736 call StopVimInTerminal(buf)
1737 call delete('Xscript')
1738 bwipe Xtextfile
1739endfunc
1740
1741func Test_terminal_api_drop_newwin_enc()
1742 CheckRunVimInTerminal
1743 let buf = Api_drop_common(',{"enc":"utf-16"}')
1744 call assert_equal("utf-16", &fenc)
1745
1746 call StopVimInTerminal(buf)
1747 call delete('Xscript')
1748 bwipe Xtextfile
1749endfunc
1750
1751func Test_terminal_api_drop_newwin_encoding()
1752 CheckRunVimInTerminal
1753 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1754 call assert_equal("utf-16", &fenc)
1755
1756 call StopVimInTerminal(buf)
1757 call delete('Xscript')
1758 bwipe Xtextfile
1759endfunc
1760
1761func Test_terminal_api_drop_oldwin()
1762 CheckRunVimInTerminal
1763 let firstwinid = win_getid()
1764 split Xtextfile
1765 let textfile_winid = win_getid()
1766 call assert_equal(2, winnr('$'))
1767 call win_gotoid(firstwinid)
1768
1769 " Use the title termcap entries to output the escape sequence.
1770 call writefile([
1771 \ 'set title',
1772 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1773 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1774 \ 'redraw',
1775 \ "set t_ts=",
1776 \ ], 'Xscript')
1777 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1778 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1779 call assert_equal(textfile_winid, win_getid())
1780
1781 call StopVimInTerminal(buf)
1782 call delete('Xscript')
1783 bwipe Xtextfile
1784endfunc
1785
1786func Tapi_TryThis(bufnum, arg)
1787 let g:called_bufnum = a:bufnum
1788 let g:called_arg = a:arg
1789endfunc
1790
1791func WriteApiCall(funcname)
1792 " Use the title termcap entries to output the escape sequence.
1793 call writefile([
1794 \ 'set title',
1795 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1796 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1797 \ 'redraw',
1798 \ "set t_ts=",
1799 \ ], 'Xscript')
1800endfunc
1801
1802func Test_terminal_api_call()
1803 CheckRunVimInTerminal
1804
1805 unlet! g:called_bufnum
1806 unlet! g:called_arg
1807
1808 call WriteApiCall('Tapi_TryThis')
1809
1810 " Default
1811 let buf = RunVimInTerminal('-S Xscript', {})
1812 call WaitFor({-> exists('g:called_bufnum')})
1813 call assert_equal(buf, g:called_bufnum)
1814 call assert_equal(['hello', 123], g:called_arg)
1815 call StopVimInTerminal(buf)
1816
1817 unlet! g:called_bufnum
1818 unlet! g:called_arg
1819
1820 " Enable explicitly
1821 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1822 call WaitFor({-> exists('g:called_bufnum')})
1823 call assert_equal(buf, g:called_bufnum)
1824 call assert_equal(['hello', 123], g:called_arg)
1825 call StopVimInTerminal(buf)
1826
1827 unlet! g:called_bufnum
1828 unlet! g:called_arg
1829
1830 func! ApiCall_TryThis(bufnum, arg)
1831 let g:called_bufnum2 = a:bufnum
1832 let g:called_arg2 = a:arg
1833 endfunc
1834
1835 call WriteApiCall('ApiCall_TryThis')
1836
1837 " Use prefix match
1838 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1839 call WaitFor({-> exists('g:called_bufnum2')})
1840 call assert_equal(buf, g:called_bufnum2)
1841 call assert_equal(['hello', 123], g:called_arg2)
1842 call StopVimInTerminal(buf)
1843
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001844 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001845
1846 unlet! g:called_bufnum2
1847 unlet! g:called_arg2
1848
1849 call delete('Xscript')
1850 delfunction! ApiCall_TryThis
1851 unlet! g:called_bufnum2
1852 unlet! g:called_arg2
1853endfunc
1854
1855func Test_terminal_api_call_fails()
1856 CheckRunVimInTerminal
1857
1858 func! TryThis(bufnum, arg)
1859 let g:called_bufnum3 = a:bufnum
1860 let g:called_arg3 = a:arg
1861 endfunc
1862
1863 call WriteApiCall('TryThis')
1864
1865 unlet! g:called_bufnum3
1866 unlet! g:called_arg3
1867
1868 " Not permitted
1869 call ch_logfile('Xlog', 'w')
1870 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1871 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1872 call assert_false(exists('g:called_bufnum3'))
1873 call assert_false(exists('g:called_arg3'))
1874 call StopVimInTerminal(buf)
1875
1876 " No match
1877 call ch_logfile('Xlog', 'w')
1878 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1879 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1880 call assert_false(exists('g:called_bufnum3'))
1881 call assert_false(exists('g:called_arg3'))
1882 call StopVimInTerminal(buf)
1883
1884 call delete('Xscript')
1885 call ch_logfile('')
1886 call delete('Xlog')
1887 delfunction! TryThis
1888 unlet! g:called_bufnum3
1889 unlet! g:called_arg3
1890endfunc
1891
1892let s:caught_e937 = 0
1893
1894func Tapi_Delete(bufnum, arg)
1895 try
1896 execute 'bdelete!' a:bufnum
1897 catch /E937:/
1898 let s:caught_e937 = 1
1899 endtry
1900endfunc
1901
1902func Test_terminal_api_call_fail_delete()
1903 CheckRunVimInTerminal
1904
1905 call WriteApiCall('Tapi_Delete')
1906 let buf = RunVimInTerminal('-S Xscript', {})
1907 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1908
1909 call StopVimInTerminal(buf)
1910 call delete('Xscript')
1911 call ch_logfile('', '')
1912endfunc
1913
1914func Test_terminal_setapi_and_call()
1915 CheckRunVimInTerminal
1916
1917 call WriteApiCall('Tapi_TryThis')
1918 call ch_logfile('Xlog', 'w')
1919
1920 unlet! g:called_bufnum
1921 unlet! g:called_arg
1922
1923 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1924 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1925 call assert_false(exists('g:called_bufnum'))
1926 call assert_false(exists('g:called_arg'))
1927
1928 eval buf->term_setapi('Tapi_')
1929 call term_sendkeys(buf, ":set notitle\<CR>")
1930 call term_sendkeys(buf, ":source Xscript\<CR>")
1931 call WaitFor({-> exists('g:called_bufnum')})
1932 call assert_equal(buf, g:called_bufnum)
1933 call assert_equal(['hello', 123], g:called_arg)
1934
1935 call StopVimInTerminal(buf)
1936
1937 call delete('Xscript')
1938 call ch_logfile('')
1939 call delete('Xlog')
1940 unlet! g:called_bufnum
1941 unlet! g:called_arg
1942endfunc
1943
1944func Test_terminal_api_arg()
1945 CheckRunVimInTerminal
1946
1947 call WriteApiCall('Tapi_TryThis')
1948 call ch_logfile('Xlog', 'w')
1949
1950 unlet! g:called_bufnum
1951 unlet! g:called_arg
1952
1953 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1954 let buf = bufnr('%')
1955 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1956 call assert_false(exists('g:called_bufnum'))
1957 call assert_false(exists('g:called_arg'))
1958
1959 call StopVimInTerminal(buf)
1960
1961 call ch_logfile('Xlog', 'w')
1962
1963 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1964 let buf = bufnr('%')
1965 call WaitFor({-> exists('g:called_bufnum')})
1966 call assert_equal(buf, g:called_bufnum)
1967 call assert_equal(['hello', 123], g:called_arg)
1968
1969 call StopVimInTerminal(buf)
1970
1971 call delete('Xscript')
1972 call ch_logfile('')
1973 call delete('Xlog')
1974 unlet! g:called_bufnum
1975 unlet! g:called_arg
1976endfunc
1977
1978func Test_terminal_ansicolors_default()
1979 CheckFunction term_getansicolors
1980
1981 let colors = [
1982 \ '#000000', '#e00000',
1983 \ '#00e000', '#e0e000',
1984 \ '#0000e0', '#e000e0',
1985 \ '#00e0e0', '#e0e0e0',
1986 \ '#808080', '#ff4040',
1987 \ '#40ff40', '#ffff40',
1988 \ '#4040ff', '#ff40ff',
1989 \ '#40ffff', '#ffffff',
1990 \]
1991
1992 let buf = Run_shell_in_terminal({})
1993 call assert_equal(colors, term_getansicolors(buf))
1994 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001995 call assert_equal([], term_getansicolors(buf))
1996
1997 exe buf . 'bwipe'
1998endfunc
1999
2000let s:test_colors = [
2001 \ '#616e64', '#0d0a79',
2002 \ '#6d610d', '#0a7373',
2003 \ '#690d0a', '#6d696e',
2004 \ '#0d0a6f', '#616e0d',
2005 \ '#0a6479', '#6d0d0a',
2006 \ '#617373', '#0d0a69',
2007 \ '#6d690d', '#0a6e6f',
2008 \ '#610d0a', '#6e6479',
2009 \]
2010
2011func Test_terminal_ansicolors_global()
2012 CheckFeature termguicolors
2013 CheckFunction term_getansicolors
2014
2015 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2016 let buf = Run_shell_in_terminal({})
2017 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2018 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002019
2020 exe buf . 'bwipe'
2021 unlet g:terminal_ansi_colors
2022endfunc
2023
2024func Test_terminal_ansicolors_func()
2025 CheckFeature termguicolors
2026 CheckFunction term_getansicolors
2027
2028 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2029 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2030 call assert_equal(s:test_colors, term_getansicolors(buf))
2031
2032 call term_setansicolors(buf, g:terminal_ansi_colors)
2033 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2034
2035 let colors = [
2036 \ 'ivory', 'AliceBlue',
2037 \ 'grey67', 'dark goldenrod',
2038 \ 'SteelBlue3', 'PaleVioletRed4',
2039 \ 'MediumPurple2', 'yellow2',
2040 \ 'RosyBrown3', 'OrangeRed2',
2041 \ 'white smoke', 'navy blue',
2042 \ 'grey47', 'gray97',
2043 \ 'MistyRose2', 'DodgerBlue4',
2044 \]
2045 eval buf->term_setansicolors(colors)
2046
2047 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002048 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002049 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
2050
2051 call StopShellInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002052 call assert_equal(0, term_setansicolors(buf, []))
2053 exe buf . 'bwipe'
2054endfunc
2055
2056func Test_terminal_all_ansi_colors()
2057 CheckRunVimInTerminal
2058
2059 " Use all the ANSI colors.
2060 call writefile([
2061 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2062 \ 'hi Tblack ctermfg=0 ctermbg=8',
2063 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2064 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2065 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2066 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2067 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2068 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2069 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2070 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2071 \ 'hi Tred ctermfg=9 ctermbg=1',
2072 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2073 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2074 \ 'hi Tblue ctermfg=12 ctermbg=4',
2075 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2076 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2077 \ 'hi Twhite ctermfg=15 ctermbg=7',
2078 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2079 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2080 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2081 \ '',
2082 \ 'call matchadd("Tblack", "A")',
2083 \ 'call matchadd("Tdarkred", "B")',
2084 \ 'call matchadd("Tdarkgreen", "C")',
2085 \ 'call matchadd("Tbrown", "D")',
2086 \ 'call matchadd("Tdarkblue", "E")',
2087 \ 'call matchadd("Tdarkmagenta", "F")',
2088 \ 'call matchadd("Tdarkcyan", "G")',
2089 \ 'call matchadd("Tlightgrey", "H")',
2090 \ 'call matchadd("Tdarkgrey", "I")',
2091 \ 'call matchadd("Tred", "J")',
2092 \ 'call matchadd("Tgreen", "K")',
2093 \ 'call matchadd("Tyellow", "L")',
2094 \ 'call matchadd("Tblue", "M")',
2095 \ 'call matchadd("Tmagenta", "N")',
2096 \ 'call matchadd("Tcyan", "O")',
2097 \ 'call matchadd("Twhite", "P")',
2098 \ 'call matchadd("TdarkredBold", "X")',
2099 \ 'call matchadd("TgreenBold", "Y")',
2100 \ 'call matchadd("TmagentaBold", "Z")',
2101 \ 'redraw',
2102 \ ], 'Xcolorscript')
2103 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2104 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2105
2106 call term_sendkeys(buf, ":q\<CR>")
2107 call StopVimInTerminal(buf)
2108 call delete('Xcolorscript')
2109endfunc
2110
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002111function On_BufFilePost()
2112 doautocmd <nomodeline> User UserEvent
2113endfunction
2114
2115func Test_terminal_nested_autocmd()
2116 new
2117 call setline(1, range(500))
2118 $
2119 let lastline = line('.')
2120
2121 augroup TermTest
2122 autocmd BufFilePost * call On_BufFilePost()
2123 autocmd User UserEvent silent
2124 augroup END
2125
2126 let cmd = Get_cat_123_cmd()
2127 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2128 call assert_equal(lastline, line('.'))
2129
Bram Moolenaar64374752021-04-03 17:22:29 +02002130 let job = term_getjob(buf)
2131 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002132 call delete('Xtext')
2133 augroup TermTest
2134 au!
2135 augroup END
2136endfunc
2137
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002138func Test_terminal_adds_jump()
2139 clearjumps
2140 call term_start("ls", #{curwin: 1})
2141 call assert_equal(1, getjumplist()[0]->len())
2142 bwipe!
2143endfunc
2144
Bram Moolenaareea32af2021-11-21 14:51:13 +00002145func Close_cb(ch, ctx)
2146 call term_wait(a:ctx.bufnr)
2147 let g:close_done = 'done'
2148endfunc
2149
2150func Test_term_wait_in_close_cb()
2151 let g:close_done = ''
2152 let ctx = {}
2153 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2154 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2155
2156 call WaitForAssert({-> assert_equal("done", g:close_done)})
2157
2158 unlet g:close_done
2159 bwipe!
2160endfunc
2161
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002162
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002163" vim: shiftwidth=2 sts=2 expandtab