blob: 8cdb42ae8877cfb80c70bb8714882412ec8d208c [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 Moolenaar6a2c5a72020-04-08 21:50:25 +020029 call TermWait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020030 call assert_equal('n', mode())
31 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020032 call assert_match('%aF[^\n]*finished]', execute('ls F'))
33 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
34 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035
Bram Moolenaar94053a52017-08-01 21:44:33 +020036 " closing window wipes out the terminal buffer a with finished job
37 close
38 call assert_equal("", bufname(buf))
39
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020040 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020041 unlet g:job
42endfunc
43
Bram Moolenaar00806bc2020-11-05 19:36:38 +010044func Test_terminal_no_name()
45 let buf = Run_shell_in_terminal({})
46 call assert_match('^!', bufname(buf))
47 0file
48 call assert_equal("", bufname(buf))
49 call assert_match('\[No Name\]', execute('file'))
50 call StopShellInTerminal(buf)
51 call TermWait(buf)
52endfunc
53
Bram Moolenaar28ed4df2019-10-26 16:21:40 +020054func Test_terminal_TerminalWinOpen()
55 au TerminalWinOpen * let b:done = 'yes'
56 let buf = Run_shell_in_terminal({})
57 call assert_equal('yes', b:done)
58 call StopShellInTerminal(buf)
59 " closing window wipes out the terminal buffer with the finished job
60 close
61
62 if has("unix")
63 terminal ++hidden ++open sleep 1
64 sleep 1
65 call assert_fails("echo b:done", 'E121:')
66 endif
67
68 au! TerminalWinOpen
69endfunc
70
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020074 call TermWait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020075
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
Bram Moolenaar28ee8922020-10-28 20:20:00 +010078 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020079 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085func Test_terminal_paste_register()
86 let @" = "text to paste"
87
88 let buf = Run_shell_in_terminal({})
89 " Wait for the shell to display a prompt
90 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92 call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93 call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100102 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaareb44a682017-08-03 22:44:55 +0200103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200112 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200113 split
114 quit!
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200115 call TermWait(buf)
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100126func Test_terminal_hide_buffer_job_running()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 quit
130 for nr in range(1, winnr('$'))
131 call assert_notequal(winbufnr(nr), buf)
132 endfor
133 call assert_true(bufloaded(buf))
134 call assert_true(buflisted(buf))
135
136 exe 'split ' . buf . 'buf'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100143func Test_terminal_hide_buffer_job_finished()
144 term echo hello
145 let buf = bufnr()
146 setlocal bufhidden=hide
147 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
148 call assert_true(bufloaded(buf))
149 call assert_true(buflisted(buf))
150 edit Xasdfasdf
151 call assert_true(bufloaded(buf))
152 call assert_true(buflisted(buf))
153 exe buf .. 'buf'
154 call assert_equal(buf, bufnr())
155 setlocal bufhidden=
156 edit Xasdfasdf
157 call assert_false(bufloaded(buf))
158 call assert_false(buflisted(buf))
159 bwipe Xasdfasdf
160endfunc
161
Bram Moolenaar3ad69532021-11-19 17:01:08 +0000162func Test_terminal_rename_buffer()
163 let cmd = Get_cat_123_cmd()
164 let buf = term_start(cmd, {'term_name': 'foo'})
165 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
166 call assert_equal('foo', bufname())
167 call assert_match('foo.*finished', execute('ls'))
168 file bar
169 call assert_equal('bar', bufname())
170 call assert_match('bar.*finished', execute('ls'))
171 exe 'bwipe! ' .. buf
172endfunc
173
Bram Moolenaar1e115362019-01-09 23:01:02 +0100174func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200175 exe g:buf . 'bwipe!'
176 let g:buf = 0
177endfunc
178
Bram Moolenaar9d189612017-09-09 18:11:00 +0200179func Get_cat_123_cmd()
180 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100181 if !has('conpty')
182 return 'cmd /c "cls && color 2 && echo 123"'
183 else
184 " When clearing twice, extra sequence is not output.
185 return 'cmd /c "cls && cls && color 2 && echo 123"'
186 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200187 else
188 call writefile(["\<Esc>[32m123"], 'Xtext')
189 return "cat Xtext"
190 endif
191endfunc
192
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200193func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200194 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200195 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
196 let g:job = term_getjob(g:buf)
197
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200198 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
199 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200200 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100201 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200202 call delete('Xtext')
203endfunc
204
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200205func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200206 let l = term_scrape(a:buf, 0)
207 call assert_true(len(l) == 0)
208 let l = term_scrape(a:buf, 999)
209 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200210 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 call assert_true(len(l) > 0)
212 call assert_equal('1', l[0].chars)
213 call assert_equal('2', l[1].chars)
214 call assert_equal('3', l[2].chars)
215 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200216 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
217 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100218 if has('win32')
219 " On Windows 'background' always defaults to dark, even though the terminal
220 " may use a light background. Therefore accept both white and black.
221 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200222 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100223 if &background == 'light'
224 call assert_equal('#ffffff', l[0].bg)
225 else
226 call assert_equal('#000000', l[0].bg)
227 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200228 endif
229
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200230 let l = term_getline(a:buf, -1)
231 call assert_equal('', l)
232 let l = term_getline(a:buf, 0)
233 call assert_equal('', l)
234 let l = term_getline(a:buf, 999)
235 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200236 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200237 call assert_equal('123', l)
238endfunc
239
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200240func Test_terminal_scrape_123()
241 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200242 let buf = term_start(cmd)
243
244 let termlist = term_list()
245 call assert_equal(1, len(termlist))
246 call assert_equal(buf, termlist[0])
247
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200248 " Nothing happens with invalid buffer number
249 call term_wait(1234)
250
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200251 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200252 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200253 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200254 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200255 call Check_123(buf)
256
257 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100258 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200259 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200260 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200261 call Check_123(buf)
262
263 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200264 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200265endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200267func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call writefile(["léttまrs"], 'Xtext')
269 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200270 " Run cmd with UTF-8 codepage to make the type command print the expected
271 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100272 let buf = term_start("cmd /K chcp 65001")
273 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200274 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100275 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200276 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100277 let buf = term_start("cat Xtext")
278 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200279 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200280
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100281 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
282 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200283 call assert_true(len(l) >= 7)
284 call assert_equal('l', l[0].chars)
285 call assert_equal('é', l[1].chars)
286 call assert_equal(1, l[1].width)
287 call assert_equal('t', l[2].chars)
288 call assert_equal('t', l[3].chars)
289 call assert_equal('ま', l[4].chars)
290 call assert_equal(2, l[4].width)
291 call assert_equal('r', l[5].chars)
292 call assert_equal('s', l[6].chars)
293
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100294 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200295 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200296 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200297
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100298 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200299 call delete('Xtext')
300endfunc
301
Bram Moolenaar8b896142020-08-02 15:05:05 +0200302func Test_terminal_one_column()
303 " This creates a terminal, displays a double-wide character and makes the
304 " window one column wide. This used to cause a crash.
305 let width = &columns
306 botright vert term
307 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200308 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200309 exe "set columns=" .. (width / 2)
310 redraw
311 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200312 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200313 exe "set columns=" .. width
314 exe buf . 'bwipe!'
315endfunc
316
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200317func Test_terminal_scroll()
318 call writefile(range(1, 200), 'Xtext')
319 if has('win32')
320 let cmd = 'cmd /c "type Xtext"'
321 else
322 let cmd = "cat Xtext"
323 endif
324 let buf = term_start(cmd)
325
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100326 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200327 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200328 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200329
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100330 " wait until the scrolling stops
331 while 1
332 let scrolled = buf->term_getscrolled()
333 sleep 20m
334 if scrolled == buf->term_getscrolled()
335 break
336 endif
337 endwhile
338
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200339 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200340 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200341 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200342 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200343 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200344 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200345
346 exe buf . 'bwipe'
347 call delete('Xtext')
348endfunc
349
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200350func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200351 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200352 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200353 call writefile(range(150), 'Xtext')
354 if has('win32')
355 call term_sendkeys(buf, "type Xtext\<CR>")
356 else
357 call term_sendkeys(buf, "cat Xtext\<CR>")
358 endif
359 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200360 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200361 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200362 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200363 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200364
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200365 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200366 call TermWait(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200367 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200368 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100369 call delete('Xtext')
370endfunc
371
372func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200373 " tail -f only works on Unix
374 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100375
376 call writefile(range(50), 'Xtext')
377 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100378 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100379 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100380 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100381 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
382 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100383 \ 'call feedkeys("\<C-W>N", "xt")',
384 \ ], 'XTest_postponed')
385 let buf = RunVimInTerminal('-S XTest_postponed', {})
386 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100387 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100388
389 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200390 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100391 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100392 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100393
394 " Back to Terminal-Job mode, text will scroll and show the extra line.
395 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100396 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100397
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100398 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100399 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200400 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100401 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100402 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200403 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100404 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100405 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
406
407 " check type of term_sendkeys() return value
408 echo type(tsk_ret)
409
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100410 call StopVimInTerminal(buf)
411 call delete('XTest_postponed')
412 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200413endfunc
414
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100415" Run diff on two dumps with different size.
416func Test_terminal_dumpdiff_size()
417 call assert_equal(1, winnr('$'))
418 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
419 call assert_equal(2, winnr('$'))
420 call assert_match('Test_incsearch_search_01.dump', getline(10))
421 call assert_match(' +++++$', getline(11))
422 call assert_match('Test_popup_command_01.dump', getline(31))
423 call assert_equal(repeat('+', 75), getline(30))
424 quit
425endfunc
426
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200427func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200428 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200429
Bram Moolenaarb2412082017-08-20 18:09:14 +0200430 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200431 let size = term_getsize('')
432 bwipe!
433 call assert_equal(5, size[0])
434
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200435 call term_start(cmd, {'term_rows': 6})
436 let size = term_getsize('')
437 bwipe!
438 call assert_equal(6, size[0])
439
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200440 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200441 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200442 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200443
444 call term_setsize('', 6, 0)
445 call assert_equal([6, 33], term_getsize(''))
446
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200447 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200448 call assert_equal([6, 35], term_getsize(''))
449
450 call term_setsize('', 7, 30)
451 call assert_equal([7, 30], term_getsize(''))
452
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200453 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200454 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200455
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200456 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
457 let size = term_getsize('')
458 bwipe!
459 call assert_equal([6, 36], size)
460
Bram Moolenaarb2412082017-08-20 18:09:14 +0200461 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200462 let size = term_getsize('')
463 bwipe!
464 call assert_equal(20, size[1])
465
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200466 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200467 let size = term_getsize('')
468 bwipe!
469 call assert_equal(26, size[1])
470
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200471 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200472 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200473 let size = term_getsize('')
474 bwipe!
475 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200476
477 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
478 let size = term_getsize('')
479 bwipe!
480 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200481
Bram Moolenaar5300be62021-11-13 10:27:40 +0000482 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
483 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000484 if has('float')
485 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
486 endif
487
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200488 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200489endfunc
490
Bram Moolenaareba13e42021-02-23 17:47:23 +0100491func Test_terminal_zero_height()
492 split
493 wincmd j
494 anoremenu 1.1 WinBar.test :
495 terminal ++curwin
496 wincmd k
497 wincmd _
498 redraw
499
500 call term_sendkeys(bufnr(), "exit\r")
501 bwipe!
502endfunc
503
Bram Moolenaarda43b612017-08-11 22:27:50 +0200504func Test_terminal_curwin()
505 let cmd = Get_cat_123_cmd()
506 call assert_equal(1, winnr('$'))
507
Bram Moolenaarb1009092020-05-31 16:04:42 +0200508 split Xdummy
509 call setline(1, 'dummy')
510 write
511 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200512 exe 'terminal ++curwin ' . cmd
513 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200514 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200515 bwipe!
516
Bram Moolenaarb1009092020-05-31 16:04:42 +0200517 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200518 call term_start(cmd, {'curwin': 1})
519 call assert_equal(2, winnr('$'))
520 bwipe!
521
Bram Moolenaarb1009092020-05-31 16:04:42 +0200522 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200523 call setline(1, 'change')
524 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
525 call assert_equal(2, winnr('$'))
526 exe 'terminal! ++curwin ' . cmd
527 call assert_equal(2, winnr('$'))
528 bwipe!
529
Bram Moolenaarb1009092020-05-31 16:04:42 +0200530 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200531 call setline(1, 'change')
532 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
533 call assert_equal(2, winnr('$'))
534 bwipe!
535
Bram Moolenaarb1009092020-05-31 16:04:42 +0200536 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200537 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200538 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200539 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200540endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200541
Bram Moolenaarff546792017-11-21 14:47:57 +0100542func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200543 if s:python != ''
544 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200545 " 500 was not enough for Travis
546 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200547 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200548 echo 'This will take five seconds...'
549 let waittime = 2000
550 if has('win32')
551 let cmd = $windir . '\system32\timeout.exe 1'
552 else
553 let cmd = 'sleep 1'
554 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200555 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100556 return [cmd, waittime]
557endfunc
558
559func Test_terminal_finish_open_close()
560 call assert_equal(1, winnr('$'))
561
562 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200563
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100564 " shell terminal closes automatically
565 terminal
566 let buf = bufnr('%')
567 call assert_equal(2, winnr('$'))
568 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200569 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200570 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200571 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100572
573 " shell terminal that does not close automatically
574 terminal ++noclose
575 let buf = bufnr('%')
576 call assert_equal(2, winnr('$'))
577 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200578 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200579 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100580 call assert_equal(2, winnr('$'))
581 quit
582 call assert_equal(1, winnr('$'))
583
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200584 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200585 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
589 call term_start(cmd, {'term_finish': 'close'})
590 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200591 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200592 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200593 call assert_equal(1, winnr('$'))
594
595 exe 'terminal ++open ' . cmd
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 Moolenaardd693ce2017-08-10 23:15:19 +0200598 bwipe
599
600 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200601 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200602 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200603 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200604
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200605 exe 'terminal ++hidden ++open ' . cmd
606 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200607 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200608 bwipe
609
610 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
611 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200612 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200613 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200614
615 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
616 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
617 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
618 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
619
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100620 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 +0200621 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200622 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200623 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100624 call assert_equal('opened the buffer in a window', g:result)
625 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200626 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200627endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200628
629func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100630 if has('win32')
631 let cmd = 'cmd /c cd'
632 else
633 CheckExecutable pwd
634 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200635 endif
636 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100637 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200638 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200639
640 exe buf . 'bwipe'
641 call delete('Xdir', 'rf')
642endfunc
643
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200644func Test_terminal_cwd_failure()
645 " Case 1: Provided directory is not actually a directory. Attempt to make
646 " the file executable as well.
647 call writefile([], 'Xfile')
648 call setfperm('Xfile', 'rwx------')
649 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
650 call delete('Xfile')
651
652 " Case 2: Directory does not exist.
653 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
654
655 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100656 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200657 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100658 call mkdir('XdirNoAccess', '', '0600')
659 " return early if the directory permissions could not be set properly
660 if getfperm('XdirNoAccess')[2] == 'x'
661 call delete('XdirNoAccess', 'rf')
662 return
663 endif
664 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
665 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200666 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200667endfunc
668
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100669func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200670 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200671 call s:test_environment("VIM_SERVERNAME", v:servername)
672endfunc
673
674func Test_terminal_version()
675 call s:test_environment("VIM_TERMINAL", string(v:version))
676endfunc
677
678func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100679 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100680 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200681 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100682 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200683 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100684 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200685 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100686 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200687 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200688 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200689 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100690
Bram Moolenaar012eb662018-03-13 17:55:27 +0100691 exe buf . 'bwipe'
692 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100693endfunc
694
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200695func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100696 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200697 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200698 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100699 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100700 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100701 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100702 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100703 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200704 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200705 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200706 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200707
Bram Moolenaar012eb662018-03-13 17:55:27 +0100708 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200709endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200710
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200711func Test_terminal_list_args()
712 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100713 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200714 exe buf . 'bwipe!'
715 call assert_equal("", bufname(buf))
716endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200717
718func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100719 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100720 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100721 let wait_time = 5000
722 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100723 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200724 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100725 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200726 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100727 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100728 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100729 " NPFS is used in Windows, nonblocking mode does not work properly.
730 elseif has('win32')
731 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200732 else
733 let len = 5000
734 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200735
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100736 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100737 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100738 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200739 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100740 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200741
742 " On MS-Windows there is an extra empty line below "done". Find "done" in
743 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100744 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100745 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100746 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200747 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100748 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200749 endif
750 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200751
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100752 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200753 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200754 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200755 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200756 bwipe
757endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200758
759func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100760 " TODO: enable once writing to stdin works on MS-Windows
761 CheckNotMSWindows
762 CheckExecutable wc
763
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200764 call setline(1, ['one', 'two', 'three'])
765 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200766 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200767 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200768 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100769 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200770
771 call setline(1, ['one', 'two', 'three', 'four'])
772 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200773 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200774 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200775 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100776 %bwipe!
777endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200778
Bram Moolenaar21109272020-01-30 16:27:20 +0100779func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200780 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200781
Bram Moolenaar21109272020-01-30 16:27:20 +0100782 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200783 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100784 " MS-Windows echoes the input, Unix doesn't.
785 if has('win32')
786 call WaitFor({-> getline('$') =~ 'exit(123)'})
787 call assert_equal('hello', getline(line('$') - 1))
788 else
789 call WaitFor({-> getline('$') =~ 'hello'})
790 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200791 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100792 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
793 %bwipe!
794endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200795
Bram Moolenaar21109272020-01-30 16:27:20 +0100796func Test_terminal_eof_arg_win32_ctrl_z()
797 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200798 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100799
800 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200801 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100802 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
803 call assert_match('\^Z', getline(line('$') - 1))
804 %bwipe!
805endfunc
806
807func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200808 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100809
810 " Check the last specified ++eof arg is used and should not memory leak.
811 new
812 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200813 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100814 " MS-Windows echoes the input, Unix doesn't.
815 if has('win32')
816 call WaitFor({-> getline('$') =~ 'exit(123)'})
817 call assert_equal('hello', getline(line('$') - 1))
818 else
819 call WaitFor({-> getline('$') =~ 'hello'})
820 call assert_equal('hello', getline('$'))
821 endif
822 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
823 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200824endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200825
826func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100827 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200828 let buf = term_start('NONE', {})
829 call assert_notequal(0, buf)
830
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200831 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200832 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100833 if has('gui_running') && !has('win32')
834 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200835 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100836 else
837 " Otherwise using a job works on all systems.
838 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200839 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200840 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200841
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200842 bwipe!
843endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200844
845func Test_terminal_special_chars()
846 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200847 CheckUnix
848
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200849 call mkdir('Xdir with spaces')
850 call writefile(['x'], 'Xdir with spaces/quoted"file')
851 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200852 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100853 " make sure the job has finished
854 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200855
856 call delete('Xdir with spaces', 'rf')
857 bwipe
858endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200859
860func Test_terminal_wrong_options()
861 call assert_fails('call term_start(&shell, {
862 \ "in_io": "file",
863 \ "in_name": "xxx",
864 \ "out_io": "file",
865 \ "out_name": "xxx",
866 \ "err_io": "file",
867 \ "err_name": "xxx"
868 \ })', 'E474:')
869 call assert_fails('call term_start(&shell, {
870 \ "out_buf": bufnr("%")
871 \ })', 'E474:')
872 call assert_fails('call term_start(&shell, {
873 \ "err_buf": bufnr("%")
874 \ })', 'E474:')
875endfunc
876
877func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100878 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200879 let cmd = Get_cat_123_cmd()
880 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200881 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100882 " ConPTY may precede escape sequence. There are things that are not so.
883 if !has('conpty')
884 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
885 call assert_match('123', readfile('Xfile')[0])
886 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200887 let g:job = term_getjob(buf)
888 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200889
890 if has('win32')
891 " On Windows we cannot delete a file being used by a process. When
892 " job_status() returns "dead", the process remains for a short time.
893 " Just wait for a moment.
894 sleep 50m
895 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200896 call delete('Xfile')
897 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200898
899 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200900 call writefile(['one line'], 'Xfile')
901 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200902 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200903 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200904 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200905 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200906 bwipe
907 call delete('Xfile')
908 endif
909endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200910
911func TerminalTmap(remap)
912 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +0100913 " Wait for the shell to display a prompt
914 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200915 call assert_equal('t', mode())
916
917 if a:remap
918 tmap 123 456
919 else
920 tnoremap 123 456
921 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100922 " don't use abcde, it's an existing command
923 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200924 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100925 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200926 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200927 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200928 let lnum = term_getcursor(buf)[0]
929 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100930 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200931 else
932 call assert_match('456', term_getline(buf, lnum))
933 endif
934
935 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200936 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200937 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200938
939 tunmap 123
940 tunmap 456
941 call assert_equal('', maparg('123', 't'))
942 close
943 unlet g:job
944endfunc
945
946func Test_terminal_tmap()
947 call TerminalTmap(1)
948 call TerminalTmap(0)
949endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200950
951func Test_terminal_wall()
952 let buf = Run_shell_in_terminal({})
953 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200954 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200955 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200956 exe buf . 'bwipe'
957 unlet g:job
958endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200959
Bram Moolenaar7a760922018-02-19 23:10:02 +0100960func Test_terminal_wqall()
961 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200962 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200963 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200964 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100965 exe buf . 'bwipe'
966 unlet g:job
967endfunc
968
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200969func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100970 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200971 let save_enc = &encoding
972 set encoding=utf-8
973
974 if has('win32')
975 let cmd = "cmd /K chcp 65001"
976 let lnum = [3, 6, 9]
977 else
978 let cmd = &shell
979 let lnum = [1, 3, 5]
980 endif
981
982 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200983 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100984 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200985 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200986
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200987 if has('win32')
988 call assert_equal('cmd', job_info(g:job).cmd[0])
989 else
990 call assert_equal(&shell, job_info(g:job).cmd[0])
991 endif
992
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200993 " ascii + composing
994 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200995 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200996 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200997 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200998 call term_sendkeys(buf, "\<cr>")
999 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001000 let l = term_scrape(buf, lnum[0] + 1)
1001 call assert_equal("a\u0308", l[0].chars)
1002 call assert_equal("b", l[1].chars)
1003 call assert_equal("c", l[2].chars)
1004
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001005 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001006 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001007 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001008 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001009 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001010 call term_sendkeys(buf, "\<cr>")
1011 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001012 let l = term_scrape(buf, lnum[1] + 1)
1013 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001014 call assert_equal(2, l[0].width)
1015 call assert_equal("\u304e", l[1].chars)
1016 call assert_equal(2, l[1].width)
1017 call assert_equal("\u304f\u3099", l[2].chars)
1018 call assert_equal(2, l[2].width)
1019 call assert_equal("\u3052", l[3].chars)
1020 call assert_equal(2, l[3].width)
1021 call assert_equal("\u3053\u3099", l[4].chars)
1022 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001023
1024 " \u00a0 + composing
1025 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001026 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001027 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001028 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001029 call term_sendkeys(buf, "\<cr>")
1030 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001031 let l = term_scrape(buf, lnum[2] + 1)
1032 call assert_equal("\u00a0\u0308", l[3].chars)
1033
1034 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001035 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001036 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001037 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001038 let &encoding = save_enc
1039endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001040
1041func Test_terminal_aucmd_on_close()
1042 fun Nop()
1043 let s:called = 1
1044 endfun
1045
1046 aug repro
1047 au!
1048 au BufWinLeave * call Nop()
1049 aug END
1050
1051 let [cmd, waittime] = s:get_sleep_cmd()
1052
1053 call assert_equal(1, winnr('$'))
1054 new
1055 call setline(1, ['one', 'two'])
1056 exe 'term ++close ' . cmd
1057 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001058 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001059 call assert_equal(1, s:called)
1060 bwipe!
1061
1062 unlet s:called
1063 au! repro
1064 delfunc Nop
1065endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001066
1067func Test_terminal_term_start_empty_command()
1068 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001069 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001070 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001071 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001072 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001073 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001074 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001075 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001076 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001077 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001078 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001079 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001080 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001081 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001082 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001083 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001084 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001085 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001086 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1087 call assert_fails(cmd, 'E475:')
1088 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001089 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001090 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001091 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1092 call assert_fails(cmd, 'E475:')
1093 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1094 call assert_fails(cmd, 'E730:')
1095 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001096 if has('gui_running') || has('termguicolors')
1097 call assert_fails(cmd, 'E475:')
1098 else
1099 call assert_fails(cmd, 'E254:')
1100 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001101 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001102endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001103
1104func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001105 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001106
1107 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001108 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001109
Bram Moolenaar086eb872018-03-25 21:24:12 +02001110 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001111 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001112
Bram Moolenaar086eb872018-03-25 21:24:12 +02001113 " Request the cursor position.
1114 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001115
1116 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001117 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001118
Bram Moolenaar086eb872018-03-25 21:24:12 +02001119 " End "cat" gently.
1120 call term_sendkeys(buf, "\<CR>\<C-D>")
1121
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001122 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001123 exe buf . 'bwipe'
1124 unlet g:job
1125endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001126
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001127" Run Vim, start a terminal in that Vim with the kill argument,
1128" :qall works.
1129func Run_terminal_qall_kill(line1, line2)
1130 " 1. Open a terminal window and wait for the prompt to appear
1131 " 2. set kill using term_setkill()
1132 " 3. make Vim exit, it will kill the shell
1133 let after = [
1134 \ a:line1,
1135 \ 'let buf = bufnr("%")',
1136 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1137 \ ' sleep 10m',
1138 \ 'endwhile',
1139 \ a:line2,
1140 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1141 \ 'qall',
1142 \ ]
1143 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001144 return
1145 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001146 call assert_equal("done", readfile("Xdone")[0])
1147 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001148endfunc
1149
1150" Run Vim in a terminal, then start a terminal in that Vim with a kill
1151" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001152func Test_terminal_qall_kill_arg()
1153 call Run_terminal_qall_kill('term ++kill=kill', '')
1154endfunc
1155
1156" Run Vim, start a terminal in that Vim, set the kill argument with
1157" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001158func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001159 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001160endfunc
1161
1162" Run Vim, start a terminal in that Vim without the kill argument,
1163" check that :qall does not exit, :qall! does.
1164func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001165 let after =<< trim [CODE]
1166 term
1167 let buf = bufnr("%")
1168 while term_getline(buf, 1) =~ "^\\s*$"
1169 sleep 10m
1170 endwhile
1171 set nomore
1172 au VimLeavePre * call writefile(["too early"], "Xdone")
1173 qall
1174 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1175 cquit
1176 [CODE]
1177
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001178 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001179 return
1180 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001181 call assert_equal("done", readfile("Xdone")[0])
1182 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001183endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001184
1185" Run Vim in a terminal, then start a terminal in that Vim without a kill
1186" argument, check that :confirm qall works.
1187func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001188 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001189 let buf = RunVimInTerminal('', {})
1190
1191 " Open a terminal window and wait for the prompt to appear
1192 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001193 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1194 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001195
1196 " make Vim exit, it will prompt to kill the shell
1197 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001198 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001199 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001200 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001201
1202 " close the terminal window where Vim was running
1203 quit
1204endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001205
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001206" Run Vim in a terminal, then start a terminal window with a shell and check
1207" that Vim exits if it is closed.
1208func Test_terminal_exit()
1209 CheckRunVimInTerminal
1210
1211 let lines =<< trim END
1212 let winid = win_getid()
1213 help
1214 term
1215 let termid = win_getid()
1216 call win_gotoid(winid)
1217 close
1218 call win_gotoid(termid)
1219 END
1220 call writefile(lines, 'XtermExit')
1221 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1222 let job = term_getjob(buf)
1223 call WaitForAssert({-> assert_equal("run", job_status(job))})
1224
1225 " quit the shell, it will make Vim exit
1226 call term_sendkeys(buf, "exit\<CR>")
1227 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1228
1229 call delete('XtermExit')
1230endfunc
1231
Bram Moolenaar012eb662018-03-13 17:55:27 +01001232func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001233 augroup repro
1234 au!
1235 au TerminalOpen * let s:called += 1
1236 augroup END
1237
1238 let s:called = 0
1239
1240 " Open a terminal window with :terminal
1241 terminal
1242 call assert_equal(1, s:called)
1243 bwipe!
1244
1245 " Open a terminal window with term_start()
1246 call term_start(&shell)
1247 call assert_equal(2, s:called)
1248 bwipe!
1249
1250 " Open a hidden terminal buffer with :terminal
1251 terminal ++hidden
1252 call assert_equal(3, s:called)
1253 for buf in term_list()
1254 exe buf . "bwipe!"
1255 endfor
1256
1257 " Open a hidden terminal buffer with term_start()
1258 let buf = term_start(&shell, {'hidden': 1})
1259 call assert_equal(4, s:called)
1260 exe buf . "bwipe!"
1261
1262 unlet s:called
1263 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001264endfunc
1265
1266func Test_open_term_from_cmd()
1267 CheckUnix
1268 CheckRunVimInTerminal
1269
1270 let lines =<< trim END
1271 call setline(1, ['a', 'b', 'c'])
1272 3
1273 set incsearch
1274 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1275 END
1276 call writefile(lines, 'Xopenterm')
1277 let buf = RunVimInTerminal('-S Xopenterm', {})
1278
1279 " this opens a window, incsearch should not use the old cursor position
1280 call term_sendkeys(buf, "/\<F3>")
1281 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1282 call term_sendkeys(buf, "\<Esc>")
1283 call term_sendkeys(buf, ":q\<CR>")
1284
1285 call StopVimInTerminal(buf)
1286 call delete('Xopenterm')
1287endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001288
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001289func Test_combining_double_width()
1290 CheckUnix
1291 CheckRunVimInTerminal
1292
1293 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1294 let lines =<< trim END
1295 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1296 END
1297 call writefile(lines, 'Xcombining')
1298 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1299
1300 " this opens a window, incsearch should not use the old cursor position
1301 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1302 call term_sendkeys(buf, ":q\<CR>")
1303
1304 call StopVimInTerminal(buf)
1305 call delete('Xonedouble')
1306 call delete('Xcombining')
1307endfunc
1308
Bram Moolenaar02764712020-11-14 20:21:55 +01001309func Test_terminal_popup_with_cmd()
1310 " this was crashing
1311 let buf = term_start(&shell, #{hidden: v:true})
1312 let s:winid = popup_create(buf, {})
1313 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1314 call feedkeys("\<F3>", 'xt')
1315
1316 tunmap <F3>
1317 exe 'bwipe! ' .. buf
1318 unlet s:winid
1319endfunc
1320
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001321func Test_terminal_popup_bufload()
1322 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1323 let winid = popup_create(termbuf, {})
1324 sleep 50m
1325
1326 let newbuf = bufadd('')
1327 call bufload(newbuf)
1328 call setbufline(newbuf, 1, 'foobar')
1329
1330 " must not have switched to another window
1331 call assert_equal(winid, win_getid())
1332
Bram Moolenaare6329e42020-11-16 21:10:34 +01001333 call StopShellInTerminal(termbuf)
1334 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001335 exe 'bwipe! ' .. newbuf
1336endfunc
1337
Bram Moolenaare41decc2020-11-14 21:34:59 +01001338func Test_terminal_popup_insert_cmd()
1339 CheckUnix
1340
1341 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1342 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001343 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001344 endfunc
1345 call feedkeys("i\<F3>")
1346 sleep 10m
1347 call assert_equal('n', mode())
1348
1349 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001350 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001351 delfunc StartTermInPopup
1352 iunmap <F3>
1353endfunc
1354
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001355func Check_dump01(off)
1356 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1357 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001358 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001359endfunc
1360
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001361func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001362 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001363 let save_enc = &encoding
1364 set encoding=utf-8
1365 call assert_equal(1, winnr('$'))
1366
1367 let text = " a\u0300 e\u0302 o\u0308"
1368 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001369 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001370 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001371 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001372 let dumpline = readfile('Xdump')[0]
1373 call assert_match('|à| |ê| |ö', dumpline)
1374
1375 call StopVimInTerminal(buf)
1376 call delete('Xcomposing')
1377 call delete('Xdump')
1378 let &encoding = save_enc
1379endfunc
1380
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001381" Tests for failures in the term_dumpwrite() function
1382func Test_terminal_dumpwrite_errors()
1383 CheckRunVimInTerminal
1384 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1385 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001386 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001387 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1388 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1389 call writefile([], 'Xtest.dump')
1390 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1391 call delete('Xtest.dump')
1392 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1393 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001394 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001395 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001396 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001397 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1398 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1399 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1400endfunc
1401
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001402" just testing basic functionality.
1403func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001404 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001405 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001406 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001407 call assert_equal(2, winnr('$'))
1408 call assert_equal(20, line('$'))
1409 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001410
1411 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001412 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001413 call assert_equal(buf, buf2)
1414 call assert_notequal('one two three four five', trim(getline(1)))
1415
1416 " Load the first dump again in the same window
1417 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1418 call assert_equal(buf, buf2)
1419 call Check_dump01(0)
1420
1421 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1422 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1423 new
1424 let closedbuf = winbufnr('')
1425 quit
1426 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001427 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001428 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001429
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001430 quit
1431endfunc
1432
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001433func Test_terminal_dumpload_dump()
1434 CheckRunVimInTerminal
1435
1436 let lines =<< trim END
1437 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1438 END
1439 call writefile(lines, 'XtermDumpload')
1440 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1441 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1442
1443 call StopVimInTerminal(buf)
1444 call delete('XtermDumpload')
1445endfunc
1446
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001447func Test_terminal_dumpdiff()
1448 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001449 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001450 call assert_equal(2, winnr('$'))
1451 call assert_equal(62, line('$'))
1452 call Check_dump01(0)
1453 call Check_dump01(42)
1454 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1455 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001456
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001457 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001458 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1459 call writefile([], 'X1.dump')
1460 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1461 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001462endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001463
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001464func Test_terminal_dumpdiff_swap()
1465 call assert_equal(1, winnr('$'))
1466 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1467 call assert_equal(2, winnr('$'))
1468 call assert_equal(62, line('$'))
1469 call assert_match('Test_popup_command_01.dump', getline(21))
1470 call assert_match('Test_popup_command_03.dump', getline(42))
1471 call assert_match('Undo', getline(3))
1472 call assert_match('three four five', getline(45))
1473
1474 normal s
1475 call assert_match('Test_popup_command_03.dump', getline(21))
1476 call assert_match('Test_popup_command_01.dump', getline(42))
1477 call assert_match('three four five', getline(3))
1478 call assert_match('Undo', getline(45))
1479 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001480
1481 " Diff two terminal dump files with different number of rows
1482 " Swap the diffs
1483 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1484 call assert_match('Test_popup_command_01.dump', getline(21))
1485 call assert_match('Test_winline_rnu.dump', getline(42))
1486 normal s
1487 call assert_match('Test_winline_rnu.dump', getline(6))
1488 call assert_match('Test_popup_command_01.dump', getline(27))
1489 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001490endfunc
1491
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001492func Test_terminal_dumpdiff_options()
1493 set laststatus=0
1494 call assert_equal(1, winnr('$'))
1495 let height = winheight(0)
1496 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1497 call assert_equal(2, winnr('$'))
1498 call assert_equal(height, winheight(winnr()))
1499 call assert_equal(33, winwidth(winnr()))
1500 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1501 quit
1502
1503 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001504 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1505 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001506 call assert_equal(&columns, winwidth(0))
1507 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001508 call assert_equal('something else', bufname('%'))
1509 quit
1510
1511 call assert_equal(1, winnr('$'))
1512 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1513 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001514 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 +01001515 bwipe
1516
1517 set laststatus&
1518endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001519
Bram Moolenaar10772302019-01-20 18:25:54 +01001520" When drawing the statusline the cursor position may not have been updated
1521" yet.
1522" 1. create a terminal, make it show 2 lines
1523" 2. 0.5 sec later: leave terminal window, execute "i"
1524" 3. 0.5 sec later: clear terminal window, now it's 1 line
1525" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1526" 4. 0.5 sec later: should be done, clean up
1527func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001528 CheckUnix
1529
Bram Moolenaar10772302019-01-20 18:25:54 +01001530 set statusline=x
1531 terminal
1532 let tbuf = bufnr('')
1533 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1534 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1535 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1536 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1537
1538 sleep 2
1539 exe tbuf . 'bwipe!'
1540 au! BufLeave
1541 set statusline=
1542endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001543
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001544func Test_terminal_window_focus()
1545 let winid1 = win_getid()
1546 terminal
1547 let winid2 = win_getid()
1548 call feedkeys("\<C-W>j", 'xt')
1549 call assert_equal(winid1, win_getid())
1550 call feedkeys("\<C-W>k", 'xt')
1551 call assert_equal(winid2, win_getid())
1552 " can use a cursor key here
1553 call feedkeys("\<C-W>\<Down>", 'xt')
1554 call assert_equal(winid1, win_getid())
1555 call feedkeys("\<C-W>\<Up>", 'xt')
1556 call assert_equal(winid2, win_getid())
1557
1558 bwipe!
1559endfunc
1560
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001561func Api_drop_common(options)
1562 call assert_equal(1, winnr('$'))
1563
1564 " Use the title termcap entries to output the escape sequence.
1565 call writefile([
1566 \ 'set title',
1567 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1568 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1569 \ 'redraw',
1570 \ "set t_ts=",
1571 \ ], 'Xscript')
1572 let buf = RunVimInTerminal('-S Xscript', {})
1573 call WaitFor({-> bufnr('Xtextfile') > 0})
1574 call assert_equal('Xtextfile', expand('%:t'))
1575 call assert_true(winnr('$') >= 3)
1576 return buf
1577endfunc
1578
1579func Test_terminal_api_drop_newwin()
1580 CheckRunVimInTerminal
1581 let buf = Api_drop_common('')
1582 call assert_equal(0, &bin)
1583 call assert_equal('', &fenc)
1584
1585 call StopVimInTerminal(buf)
1586 call delete('Xscript')
1587 bwipe Xtextfile
1588endfunc
1589
1590func Test_terminal_api_drop_newwin_bin()
1591 CheckRunVimInTerminal
1592 let buf = Api_drop_common(',{"bin":1}')
1593 call assert_equal(1, &bin)
1594
1595 call StopVimInTerminal(buf)
1596 call delete('Xscript')
1597 bwipe Xtextfile
1598endfunc
1599
1600func Test_terminal_api_drop_newwin_binary()
1601 CheckRunVimInTerminal
1602 let buf = Api_drop_common(',{"binary":1}')
1603 call assert_equal(1, &bin)
1604
1605 call StopVimInTerminal(buf)
1606 call delete('Xscript')
1607 bwipe Xtextfile
1608endfunc
1609
1610func Test_terminal_api_drop_newwin_nobin()
1611 CheckRunVimInTerminal
1612 set binary
1613 let buf = Api_drop_common(',{"nobin":1}')
1614 call assert_equal(0, &bin)
1615
1616 call StopVimInTerminal(buf)
1617 call delete('Xscript')
1618 bwipe Xtextfile
1619 set nobinary
1620endfunc
1621
1622func Test_terminal_api_drop_newwin_nobinary()
1623 CheckRunVimInTerminal
1624 set binary
1625 let buf = Api_drop_common(',{"nobinary":1}')
1626 call assert_equal(0, &bin)
1627
1628 call StopVimInTerminal(buf)
1629 call delete('Xscript')
1630 bwipe Xtextfile
1631 set nobinary
1632endfunc
1633
1634func Test_terminal_api_drop_newwin_ff()
1635 CheckRunVimInTerminal
1636 let buf = Api_drop_common(',{"ff":"dos"}')
1637 call assert_equal("dos", &ff)
1638
1639 call StopVimInTerminal(buf)
1640 call delete('Xscript')
1641 bwipe Xtextfile
1642endfunc
1643
1644func Test_terminal_api_drop_newwin_fileformat()
1645 CheckRunVimInTerminal
1646 let buf = Api_drop_common(',{"fileformat":"dos"}')
1647 call assert_equal("dos", &ff)
1648
1649 call StopVimInTerminal(buf)
1650 call delete('Xscript')
1651 bwipe Xtextfile
1652endfunc
1653
1654func Test_terminal_api_drop_newwin_enc()
1655 CheckRunVimInTerminal
1656 let buf = Api_drop_common(',{"enc":"utf-16"}')
1657 call assert_equal("utf-16", &fenc)
1658
1659 call StopVimInTerminal(buf)
1660 call delete('Xscript')
1661 bwipe Xtextfile
1662endfunc
1663
1664func Test_terminal_api_drop_newwin_encoding()
1665 CheckRunVimInTerminal
1666 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1667 call assert_equal("utf-16", &fenc)
1668
1669 call StopVimInTerminal(buf)
1670 call delete('Xscript')
1671 bwipe Xtextfile
1672endfunc
1673
1674func Test_terminal_api_drop_oldwin()
1675 CheckRunVimInTerminal
1676 let firstwinid = win_getid()
1677 split Xtextfile
1678 let textfile_winid = win_getid()
1679 call assert_equal(2, winnr('$'))
1680 call win_gotoid(firstwinid)
1681
1682 " Use the title termcap entries to output the escape sequence.
1683 call writefile([
1684 \ 'set title',
1685 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1686 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1687 \ 'redraw',
1688 \ "set t_ts=",
1689 \ ], 'Xscript')
1690 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1691 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1692 call assert_equal(textfile_winid, win_getid())
1693
1694 call StopVimInTerminal(buf)
1695 call delete('Xscript')
1696 bwipe Xtextfile
1697endfunc
1698
1699func Tapi_TryThis(bufnum, arg)
1700 let g:called_bufnum = a:bufnum
1701 let g:called_arg = a:arg
1702endfunc
1703
1704func WriteApiCall(funcname)
1705 " Use the title termcap entries to output the escape sequence.
1706 call writefile([
1707 \ 'set title',
1708 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1709 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1710 \ 'redraw',
1711 \ "set t_ts=",
1712 \ ], 'Xscript')
1713endfunc
1714
1715func Test_terminal_api_call()
1716 CheckRunVimInTerminal
1717
1718 unlet! g:called_bufnum
1719 unlet! g:called_arg
1720
1721 call WriteApiCall('Tapi_TryThis')
1722
1723 " Default
1724 let buf = RunVimInTerminal('-S Xscript', {})
1725 call WaitFor({-> exists('g:called_bufnum')})
1726 call assert_equal(buf, g:called_bufnum)
1727 call assert_equal(['hello', 123], g:called_arg)
1728 call StopVimInTerminal(buf)
1729
1730 unlet! g:called_bufnum
1731 unlet! g:called_arg
1732
1733 " Enable explicitly
1734 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1735 call WaitFor({-> exists('g:called_bufnum')})
1736 call assert_equal(buf, g:called_bufnum)
1737 call assert_equal(['hello', 123], g:called_arg)
1738 call StopVimInTerminal(buf)
1739
1740 unlet! g:called_bufnum
1741 unlet! g:called_arg
1742
1743 func! ApiCall_TryThis(bufnum, arg)
1744 let g:called_bufnum2 = a:bufnum
1745 let g:called_arg2 = a:arg
1746 endfunc
1747
1748 call WriteApiCall('ApiCall_TryThis')
1749
1750 " Use prefix match
1751 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1752 call WaitFor({-> exists('g:called_bufnum2')})
1753 call assert_equal(buf, g:called_bufnum2)
1754 call assert_equal(['hello', 123], g:called_arg2)
1755 call StopVimInTerminal(buf)
1756
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001757 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001758
1759 unlet! g:called_bufnum2
1760 unlet! g:called_arg2
1761
1762 call delete('Xscript')
1763 delfunction! ApiCall_TryThis
1764 unlet! g:called_bufnum2
1765 unlet! g:called_arg2
1766endfunc
1767
1768func Test_terminal_api_call_fails()
1769 CheckRunVimInTerminal
1770
1771 func! TryThis(bufnum, arg)
1772 let g:called_bufnum3 = a:bufnum
1773 let g:called_arg3 = a:arg
1774 endfunc
1775
1776 call WriteApiCall('TryThis')
1777
1778 unlet! g:called_bufnum3
1779 unlet! g:called_arg3
1780
1781 " Not permitted
1782 call ch_logfile('Xlog', 'w')
1783 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1784 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1785 call assert_false(exists('g:called_bufnum3'))
1786 call assert_false(exists('g:called_arg3'))
1787 call StopVimInTerminal(buf)
1788
1789 " No match
1790 call ch_logfile('Xlog', 'w')
1791 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1792 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1793 call assert_false(exists('g:called_bufnum3'))
1794 call assert_false(exists('g:called_arg3'))
1795 call StopVimInTerminal(buf)
1796
1797 call delete('Xscript')
1798 call ch_logfile('')
1799 call delete('Xlog')
1800 delfunction! TryThis
1801 unlet! g:called_bufnum3
1802 unlet! g:called_arg3
1803endfunc
1804
1805let s:caught_e937 = 0
1806
1807func Tapi_Delete(bufnum, arg)
1808 try
1809 execute 'bdelete!' a:bufnum
1810 catch /E937:/
1811 let s:caught_e937 = 1
1812 endtry
1813endfunc
1814
1815func Test_terminal_api_call_fail_delete()
1816 CheckRunVimInTerminal
1817
1818 call WriteApiCall('Tapi_Delete')
1819 let buf = RunVimInTerminal('-S Xscript', {})
1820 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1821
1822 call StopVimInTerminal(buf)
1823 call delete('Xscript')
1824 call ch_logfile('', '')
1825endfunc
1826
1827func Test_terminal_setapi_and_call()
1828 CheckRunVimInTerminal
1829
1830 call WriteApiCall('Tapi_TryThis')
1831 call ch_logfile('Xlog', 'w')
1832
1833 unlet! g:called_bufnum
1834 unlet! g:called_arg
1835
1836 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1837 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1838 call assert_false(exists('g:called_bufnum'))
1839 call assert_false(exists('g:called_arg'))
1840
1841 eval buf->term_setapi('Tapi_')
1842 call term_sendkeys(buf, ":set notitle\<CR>")
1843 call term_sendkeys(buf, ":source Xscript\<CR>")
1844 call WaitFor({-> exists('g:called_bufnum')})
1845 call assert_equal(buf, g:called_bufnum)
1846 call assert_equal(['hello', 123], g:called_arg)
1847
1848 call StopVimInTerminal(buf)
1849
1850 call delete('Xscript')
1851 call ch_logfile('')
1852 call delete('Xlog')
1853 unlet! g:called_bufnum
1854 unlet! g:called_arg
1855endfunc
1856
1857func Test_terminal_api_arg()
1858 CheckRunVimInTerminal
1859
1860 call WriteApiCall('Tapi_TryThis')
1861 call ch_logfile('Xlog', 'w')
1862
1863 unlet! g:called_bufnum
1864 unlet! g:called_arg
1865
1866 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1867 let buf = bufnr('%')
1868 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1869 call assert_false(exists('g:called_bufnum'))
1870 call assert_false(exists('g:called_arg'))
1871
1872 call StopVimInTerminal(buf)
1873
1874 call ch_logfile('Xlog', 'w')
1875
1876 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1877 let buf = bufnr('%')
1878 call WaitFor({-> exists('g:called_bufnum')})
1879 call assert_equal(buf, g:called_bufnum)
1880 call assert_equal(['hello', 123], g:called_arg)
1881
1882 call StopVimInTerminal(buf)
1883
1884 call delete('Xscript')
1885 call ch_logfile('')
1886 call delete('Xlog')
1887 unlet! g:called_bufnum
1888 unlet! g:called_arg
1889endfunc
1890
1891func Test_terminal_ansicolors_default()
1892 CheckFunction term_getansicolors
1893
1894 let colors = [
1895 \ '#000000', '#e00000',
1896 \ '#00e000', '#e0e000',
1897 \ '#0000e0', '#e000e0',
1898 \ '#00e0e0', '#e0e0e0',
1899 \ '#808080', '#ff4040',
1900 \ '#40ff40', '#ffff40',
1901 \ '#4040ff', '#ff40ff',
1902 \ '#40ffff', '#ffffff',
1903 \]
1904
1905 let buf = Run_shell_in_terminal({})
1906 call assert_equal(colors, term_getansicolors(buf))
1907 call StopShellInTerminal(buf)
1908 call TermWait(buf)
1909 call assert_equal([], term_getansicolors(buf))
1910
1911 exe buf . 'bwipe'
1912endfunc
1913
1914let s:test_colors = [
1915 \ '#616e64', '#0d0a79',
1916 \ '#6d610d', '#0a7373',
1917 \ '#690d0a', '#6d696e',
1918 \ '#0d0a6f', '#616e0d',
1919 \ '#0a6479', '#6d0d0a',
1920 \ '#617373', '#0d0a69',
1921 \ '#6d690d', '#0a6e6f',
1922 \ '#610d0a', '#6e6479',
1923 \]
1924
1925func Test_terminal_ansicolors_global()
1926 CheckFeature termguicolors
1927 CheckFunction term_getansicolors
1928
1929 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1930 let buf = Run_shell_in_terminal({})
1931 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1932 call StopShellInTerminal(buf)
1933 call TermWait(buf)
1934
1935 exe buf . 'bwipe'
1936 unlet g:terminal_ansi_colors
1937endfunc
1938
1939func Test_terminal_ansicolors_func()
1940 CheckFeature termguicolors
1941 CheckFunction term_getansicolors
1942
1943 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1944 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1945 call assert_equal(s:test_colors, term_getansicolors(buf))
1946
1947 call term_setansicolors(buf, g:terminal_ansi_colors)
1948 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1949
1950 let colors = [
1951 \ 'ivory', 'AliceBlue',
1952 \ 'grey67', 'dark goldenrod',
1953 \ 'SteelBlue3', 'PaleVioletRed4',
1954 \ 'MediumPurple2', 'yellow2',
1955 \ 'RosyBrown3', 'OrangeRed2',
1956 \ 'white smoke', 'navy blue',
1957 \ 'grey47', 'gray97',
1958 \ 'MistyRose2', 'DodgerBlue4',
1959 \]
1960 eval buf->term_setansicolors(colors)
1961
1962 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001963 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001964 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1965
1966 call StopShellInTerminal(buf)
1967 call TermWait(buf)
1968 call assert_equal(0, term_setansicolors(buf, []))
1969 exe buf . 'bwipe'
1970endfunc
1971
1972func Test_terminal_all_ansi_colors()
1973 CheckRunVimInTerminal
1974
1975 " Use all the ANSI colors.
1976 call writefile([
1977 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1978 \ 'hi Tblack ctermfg=0 ctermbg=8',
1979 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1980 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1981 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1982 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1983 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1984 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1985 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1986 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1987 \ 'hi Tred ctermfg=9 ctermbg=1',
1988 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1989 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1990 \ 'hi Tblue ctermfg=12 ctermbg=4',
1991 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1992 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1993 \ 'hi Twhite ctermfg=15 ctermbg=7',
1994 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1995 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1996 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1997 \ '',
1998 \ 'call matchadd("Tblack", "A")',
1999 \ 'call matchadd("Tdarkred", "B")',
2000 \ 'call matchadd("Tdarkgreen", "C")',
2001 \ 'call matchadd("Tbrown", "D")',
2002 \ 'call matchadd("Tdarkblue", "E")',
2003 \ 'call matchadd("Tdarkmagenta", "F")',
2004 \ 'call matchadd("Tdarkcyan", "G")',
2005 \ 'call matchadd("Tlightgrey", "H")',
2006 \ 'call matchadd("Tdarkgrey", "I")',
2007 \ 'call matchadd("Tred", "J")',
2008 \ 'call matchadd("Tgreen", "K")',
2009 \ 'call matchadd("Tyellow", "L")',
2010 \ 'call matchadd("Tblue", "M")',
2011 \ 'call matchadd("Tmagenta", "N")',
2012 \ 'call matchadd("Tcyan", "O")',
2013 \ 'call matchadd("Twhite", "P")',
2014 \ 'call matchadd("TdarkredBold", "X")',
2015 \ 'call matchadd("TgreenBold", "Y")',
2016 \ 'call matchadd("TmagentaBold", "Z")',
2017 \ 'redraw',
2018 \ ], 'Xcolorscript')
2019 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2020 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2021
2022 call term_sendkeys(buf, ":q\<CR>")
2023 call StopVimInTerminal(buf)
2024 call delete('Xcolorscript')
2025endfunc
2026
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002027function On_BufFilePost()
2028 doautocmd <nomodeline> User UserEvent
2029endfunction
2030
2031func Test_terminal_nested_autocmd()
2032 new
2033 call setline(1, range(500))
2034 $
2035 let lastline = line('.')
2036
2037 augroup TermTest
2038 autocmd BufFilePost * call On_BufFilePost()
2039 autocmd User UserEvent silent
2040 augroup END
2041
2042 let cmd = Get_cat_123_cmd()
2043 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2044 call assert_equal(lastline, line('.'))
2045
Bram Moolenaar64374752021-04-03 17:22:29 +02002046 let job = term_getjob(buf)
2047 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002048 call delete('Xtext')
2049 augroup TermTest
2050 au!
2051 augroup END
2052endfunc
2053
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002054func Test_terminal_adds_jump()
2055 clearjumps
2056 call term_start("ls", #{curwin: 1})
2057 call assert_equal(1, getjumplist()[0]->len())
2058 bwipe!
2059endfunc
2060
Bram Moolenaareea32af2021-11-21 14:51:13 +00002061func Close_cb(ch, ctx)
2062 call term_wait(a:ctx.bufnr)
2063 let g:close_done = 'done'
2064endfunc
2065
2066func Test_term_wait_in_close_cb()
2067 let g:close_done = ''
2068 let ctx = {}
2069 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2070 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2071
2072 call WaitForAssert({-> assert_equal("done", g:close_done)})
2073
2074 unlet g:close_done
2075 bwipe!
2076endfunc
2077
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002078
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002079" vim: shiftwidth=2 sts=2 expandtab