blob: 3e2a69106c83f61e8f85c5ceddc4d85e240a50fa [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 Moolenaara48d4e42021-12-08 22:13:38 +00001127func Test_terminal_focus_events()
1128 CheckNotGui
1129 CheckUnix
1130 CheckRunVimInTerminal
1131
1132 let save_term = &term
1133 let save_ttymouse = &ttymouse
1134 set term=xterm ttymouse=xterm2
1135
1136 let lines =<< trim END
1137 set term=xterm ttymouse=xterm2
Bram Moolenaare5050712021-12-09 10:51:05 +00001138 au FocusLost * call setline(1, 'I am lost') | set nomod
1139 au FocusGained * call setline(1, 'I am back') | set nomod
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001140 " FIXME: sometimes this job hangs, exit after a couple of seconds
1141 call timer_start(2000, {id -> execute('qall')})
1142 END
1143 call writefile(lines, 'XtermFocus')
1144 let buf = RunVimInTerminal('-S XtermFocus', #{rows: 6})
1145
1146 " Send a focus event to ourselves, it should be forwarded to the terminal
1147 call feedkeys("\<Esc>[O", "Lx!")
1148 call TermWait(buf)
1149 call VerifyScreenDump(buf, 'Test_terminal_focus_1', {})
1150
1151 call feedkeys("\<Esc>[I", "Lx!")
1152 call TermWait(buf)
1153 call VerifyScreenDump(buf, 'Test_terminal_focus_2', {})
1154
Bram Moolenaare5050712021-12-09 10:51:05 +00001155 " check that a command line being edited is redrawn in place
1156 call term_sendkeys(buf, ":" .. repeat('x', 80))
1157 call TermWait(buf)
1158 call feedkeys("\<Esc>[O", "Lx!")
1159 call TermWait(buf)
1160 call VerifyScreenDump(buf, 'Test_terminal_focus_3', {})
1161 call term_sendkeys(buf, "\<Esc>")
1162
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001163 call StopVimInTerminal(buf)
1164 call delete('XtermFocus')
1165 let &term = save_term
1166 let &ttymouse = save_ttymouse
1167endfunc
1168
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001169" Run Vim, start a terminal in that Vim with the kill argument,
1170" :qall works.
1171func Run_terminal_qall_kill(line1, line2)
1172 " 1. Open a terminal window and wait for the prompt to appear
1173 " 2. set kill using term_setkill()
1174 " 3. make Vim exit, it will kill the shell
1175 let after = [
1176 \ a:line1,
1177 \ 'let buf = bufnr("%")',
1178 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1179 \ ' sleep 10m',
1180 \ 'endwhile',
1181 \ a:line2,
1182 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1183 \ 'qall',
1184 \ ]
1185 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001186 return
1187 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001188 call assert_equal("done", readfile("Xdone")[0])
1189 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001190endfunc
1191
1192" Run Vim in a terminal, then start a terminal in that Vim with a kill
1193" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001194func Test_terminal_qall_kill_arg()
1195 call Run_terminal_qall_kill('term ++kill=kill', '')
1196endfunc
1197
1198" Run Vim, start a terminal in that Vim, set the kill argument with
1199" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001200func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001201 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001202endfunc
1203
1204" Run Vim, start a terminal in that Vim without the kill argument,
1205" check that :qall does not exit, :qall! does.
1206func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001207 let after =<< trim [CODE]
1208 term
1209 let buf = bufnr("%")
1210 while term_getline(buf, 1) =~ "^\\s*$"
1211 sleep 10m
1212 endwhile
1213 set nomore
1214 au VimLeavePre * call writefile(["too early"], "Xdone")
1215 qall
1216 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1217 cquit
1218 [CODE]
1219
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001220 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001221 return
1222 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001223 call assert_equal("done", readfile("Xdone")[0])
1224 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001225endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001226
1227" Run Vim in a terminal, then start a terminal in that Vim without a kill
1228" argument, check that :confirm qall works.
1229func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001230 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001231 let buf = RunVimInTerminal('', {})
1232
1233 " Open a terminal window and wait for the prompt to appear
1234 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001235 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1236 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001237
1238 " make Vim exit, it will prompt to kill the shell
1239 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001240 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001241 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001242 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001243
1244 " close the terminal window where Vim was running
1245 quit
1246endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001247
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001248" Run Vim in a terminal, then start a terminal window with a shell and check
1249" that Vim exits if it is closed.
1250func Test_terminal_exit()
1251 CheckRunVimInTerminal
1252
1253 let lines =<< trim END
1254 let winid = win_getid()
1255 help
1256 term
1257 let termid = win_getid()
1258 call win_gotoid(winid)
1259 close
1260 call win_gotoid(termid)
1261 END
1262 call writefile(lines, 'XtermExit')
1263 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1264 let job = term_getjob(buf)
1265 call WaitForAssert({-> assert_equal("run", job_status(job))})
1266
1267 " quit the shell, it will make Vim exit
1268 call term_sendkeys(buf, "exit\<CR>")
1269 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1270
1271 call delete('XtermExit')
1272endfunc
1273
Bram Moolenaar012eb662018-03-13 17:55:27 +01001274func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001275 augroup repro
1276 au!
1277 au TerminalOpen * let s:called += 1
1278 augroup END
1279
1280 let s:called = 0
1281
1282 " Open a terminal window with :terminal
1283 terminal
1284 call assert_equal(1, s:called)
1285 bwipe!
1286
1287 " Open a terminal window with term_start()
1288 call term_start(&shell)
1289 call assert_equal(2, s:called)
1290 bwipe!
1291
1292 " Open a hidden terminal buffer with :terminal
1293 terminal ++hidden
1294 call assert_equal(3, s:called)
1295 for buf in term_list()
1296 exe buf . "bwipe!"
1297 endfor
1298
1299 " Open a hidden terminal buffer with term_start()
1300 let buf = term_start(&shell, {'hidden': 1})
1301 call assert_equal(4, s:called)
1302 exe buf . "bwipe!"
1303
1304 unlet s:called
1305 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001306endfunc
1307
1308func Test_open_term_from_cmd()
1309 CheckUnix
1310 CheckRunVimInTerminal
1311
1312 let lines =<< trim END
1313 call setline(1, ['a', 'b', 'c'])
1314 3
1315 set incsearch
1316 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1317 END
1318 call writefile(lines, 'Xopenterm')
1319 let buf = RunVimInTerminal('-S Xopenterm', {})
1320
1321 " this opens a window, incsearch should not use the old cursor position
1322 call term_sendkeys(buf, "/\<F3>")
1323 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1324 call term_sendkeys(buf, "\<Esc>")
1325 call term_sendkeys(buf, ":q\<CR>")
1326
1327 call StopVimInTerminal(buf)
1328 call delete('Xopenterm')
1329endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001330
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001331func Test_combining_double_width()
1332 CheckUnix
1333 CheckRunVimInTerminal
1334
1335 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1336 let lines =<< trim END
1337 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1338 END
1339 call writefile(lines, 'Xcombining')
1340 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1341
1342 " this opens a window, incsearch should not use the old cursor position
1343 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1344 call term_sendkeys(buf, ":q\<CR>")
1345
1346 call StopVimInTerminal(buf)
1347 call delete('Xonedouble')
1348 call delete('Xcombining')
1349endfunc
1350
Bram Moolenaar02764712020-11-14 20:21:55 +01001351func Test_terminal_popup_with_cmd()
1352 " this was crashing
1353 let buf = term_start(&shell, #{hidden: v:true})
1354 let s:winid = popup_create(buf, {})
1355 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1356 call feedkeys("\<F3>", 'xt')
1357
1358 tunmap <F3>
1359 exe 'bwipe! ' .. buf
1360 unlet s:winid
1361endfunc
1362
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001363func Test_terminal_popup_bufload()
1364 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1365 let winid = popup_create(termbuf, {})
1366 sleep 50m
1367
1368 let newbuf = bufadd('')
1369 call bufload(newbuf)
1370 call setbufline(newbuf, 1, 'foobar')
1371
1372 " must not have switched to another window
1373 call assert_equal(winid, win_getid())
1374
Bram Moolenaare6329e42020-11-16 21:10:34 +01001375 call StopShellInTerminal(termbuf)
1376 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001377 exe 'bwipe! ' .. newbuf
1378endfunc
1379
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001380func Test_terminal_popup_two_windows()
Bram Moolenaar0407d272021-12-13 22:17:44 +00001381 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001382 CheckUnix
1383
1384 " use "sh" instead of "&shell" in the hope it will use a short prompt
1385 let lines =<< trim END
1386 let termbuf = term_start('sh', #{hidden: v:true, term_finish: 'close'})
1387 exe 'buffer ' .. termbuf
1388
1389 let winid = popup_create(termbuf, #{line: 2, minwidth: 30, border: []})
1390 sleep 50m
1391
1392 call term_sendkeys(termbuf, "echo 'test'")
1393 END
1394 call writefile(lines, 'XpopupScript')
1395 let buf = RunVimInTerminal('-S XpopupScript', {})
1396
1397 " typed text appears both in normal window and in popup
1398 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 1))})
1399 call WaitForAssert({-> assert_match("echo 'test'", term_getline(buf, 3))})
1400
Bram Moolenaar0407d272021-12-13 22:17:44 +00001401 call term_sendkeys(buf, "\<CR>\<CR>exit\<CR>")
1402 call TermWait(buf)
1403 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001404 call StopVimInTerminal(buf)
1405 call delete('XpopupScript')
1406endfunc
1407
Bram Moolenaare41decc2020-11-14 21:34:59 +01001408func Test_terminal_popup_insert_cmd()
1409 CheckUnix
1410
1411 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1412 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001413 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001414 endfunc
1415 call feedkeys("i\<F3>")
1416 sleep 10m
1417 call assert_equal('n', mode())
1418
1419 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001420 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001421 delfunc StartTermInPopup
1422 iunmap <F3>
1423endfunc
1424
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001425func Check_dump01(off)
1426 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1427 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001428 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001429endfunc
1430
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001431func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001432 CheckRunVimInTerminal
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00001433
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001434 let save_enc = &encoding
1435 set encoding=utf-8
1436 call assert_equal(1, winnr('$'))
1437
1438 let text = " a\u0300 e\u0302 o\u0308"
1439 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001440 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001441 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001442 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001443 let dumpline = readfile('Xdump')[0]
1444 call assert_match('|à| |ê| |ö', dumpline)
1445
1446 call StopVimInTerminal(buf)
1447 call delete('Xcomposing')
1448 call delete('Xdump')
1449 let &encoding = save_enc
1450endfunc
1451
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001452" Tests for failures in the term_dumpwrite() function
1453func Test_terminal_dumpwrite_errors()
1454 CheckRunVimInTerminal
1455 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1456 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001457 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001458 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1459 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1460 call writefile([], 'Xtest.dump')
1461 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1462 call delete('Xtest.dump')
1463 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1464 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001465 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001466 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001467 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001468 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1469 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1470 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1471endfunc
1472
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001473" just testing basic functionality.
1474func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001475 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001476 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001477 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001478 call assert_equal(2, winnr('$'))
1479 call assert_equal(20, line('$'))
1480 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001481
1482 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001483 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001484 call assert_equal(buf, buf2)
1485 call assert_notequal('one two three four five', trim(getline(1)))
1486
1487 " Load the first dump again in the same window
1488 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1489 call assert_equal(buf, buf2)
1490 call Check_dump01(0)
1491
1492 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1493 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1494 new
1495 let closedbuf = winbufnr('')
1496 quit
1497 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001498 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001499 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001500
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001501 quit
1502endfunc
1503
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001504func Test_terminal_dumpload_dump()
1505 CheckRunVimInTerminal
1506
1507 let lines =<< trim END
1508 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1509 END
1510 call writefile(lines, 'XtermDumpload')
1511 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1512 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1513
1514 call StopVimInTerminal(buf)
1515 call delete('XtermDumpload')
1516endfunc
1517
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001518func Test_terminal_dumpdiff()
1519 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001520 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001521 call assert_equal(2, winnr('$'))
1522 call assert_equal(62, line('$'))
1523 call Check_dump01(0)
1524 call Check_dump01(42)
1525 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1526 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001527
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001528 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001529 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1530 call writefile([], 'X1.dump')
1531 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1532 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001533endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001534
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001535func Test_terminal_dumpdiff_swap()
1536 call assert_equal(1, winnr('$'))
1537 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1538 call assert_equal(2, winnr('$'))
1539 call assert_equal(62, line('$'))
1540 call assert_match('Test_popup_command_01.dump', getline(21))
1541 call assert_match('Test_popup_command_03.dump', getline(42))
1542 call assert_match('Undo', getline(3))
1543 call assert_match('three four five', getline(45))
1544
1545 normal s
1546 call assert_match('Test_popup_command_03.dump', getline(21))
1547 call assert_match('Test_popup_command_01.dump', getline(42))
1548 call assert_match('three four five', getline(3))
1549 call assert_match('Undo', getline(45))
1550 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001551
1552 " Diff two terminal dump files with different number of rows
1553 " Swap the diffs
1554 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1555 call assert_match('Test_popup_command_01.dump', getline(21))
1556 call assert_match('Test_winline_rnu.dump', getline(42))
1557 normal s
1558 call assert_match('Test_winline_rnu.dump', getline(6))
1559 call assert_match('Test_popup_command_01.dump', getline(27))
1560 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001561endfunc
1562
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001563func Test_terminal_dumpdiff_options()
1564 set laststatus=0
1565 call assert_equal(1, winnr('$'))
1566 let height = winheight(0)
1567 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1568 call assert_equal(2, winnr('$'))
1569 call assert_equal(height, winheight(winnr()))
1570 call assert_equal(33, winwidth(winnr()))
1571 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1572 quit
1573
1574 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001575 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1576 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001577 call assert_equal(&columns, winwidth(0))
1578 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001579 call assert_equal('something else', bufname('%'))
1580 quit
1581
1582 call assert_equal(1, winnr('$'))
1583 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1584 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001585 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 +01001586 bwipe
1587
1588 set laststatus&
1589endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001590
Bram Moolenaar10772302019-01-20 18:25:54 +01001591" When drawing the statusline the cursor position may not have been updated
1592" yet.
1593" 1. create a terminal, make it show 2 lines
1594" 2. 0.5 sec later: leave terminal window, execute "i"
1595" 3. 0.5 sec later: clear terminal window, now it's 1 line
1596" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1597" 4. 0.5 sec later: should be done, clean up
1598func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001599 CheckUnix
1600
Bram Moolenaar10772302019-01-20 18:25:54 +01001601 set statusline=x
1602 terminal
1603 let tbuf = bufnr('')
1604 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1605 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1606 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1607 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1608
1609 sleep 2
1610 exe tbuf . 'bwipe!'
1611 au! BufLeave
1612 set statusline=
1613endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001614
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001615func Test_terminal_window_focus()
1616 let winid1 = win_getid()
1617 terminal
1618 let winid2 = win_getid()
1619 call feedkeys("\<C-W>j", 'xt')
1620 call assert_equal(winid1, win_getid())
1621 call feedkeys("\<C-W>k", 'xt')
1622 call assert_equal(winid2, win_getid())
1623 " can use a cursor key here
1624 call feedkeys("\<C-W>\<Down>", 'xt')
1625 call assert_equal(winid1, win_getid())
1626 call feedkeys("\<C-W>\<Up>", 'xt')
1627 call assert_equal(winid2, win_getid())
1628
1629 bwipe!
1630endfunc
1631
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001632func Api_drop_common(options)
1633 call assert_equal(1, winnr('$'))
1634
1635 " Use the title termcap entries to output the escape sequence.
1636 call writefile([
1637 \ 'set title',
1638 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1639 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1640 \ 'redraw',
1641 \ "set t_ts=",
1642 \ ], 'Xscript')
1643 let buf = RunVimInTerminal('-S Xscript', {})
1644 call WaitFor({-> bufnr('Xtextfile') > 0})
1645 call assert_equal('Xtextfile', expand('%:t'))
1646 call assert_true(winnr('$') >= 3)
1647 return buf
1648endfunc
1649
1650func Test_terminal_api_drop_newwin()
1651 CheckRunVimInTerminal
1652 let buf = Api_drop_common('')
1653 call assert_equal(0, &bin)
1654 call assert_equal('', &fenc)
1655
1656 call StopVimInTerminal(buf)
1657 call delete('Xscript')
1658 bwipe Xtextfile
1659endfunc
1660
1661func Test_terminal_api_drop_newwin_bin()
1662 CheckRunVimInTerminal
1663 let buf = Api_drop_common(',{"bin":1}')
1664 call assert_equal(1, &bin)
1665
1666 call StopVimInTerminal(buf)
1667 call delete('Xscript')
1668 bwipe Xtextfile
1669endfunc
1670
1671func Test_terminal_api_drop_newwin_binary()
1672 CheckRunVimInTerminal
1673 let buf = Api_drop_common(',{"binary":1}')
1674 call assert_equal(1, &bin)
1675
1676 call StopVimInTerminal(buf)
1677 call delete('Xscript')
1678 bwipe Xtextfile
1679endfunc
1680
1681func Test_terminal_api_drop_newwin_nobin()
1682 CheckRunVimInTerminal
1683 set binary
1684 let buf = Api_drop_common(',{"nobin":1}')
1685 call assert_equal(0, &bin)
1686
1687 call StopVimInTerminal(buf)
1688 call delete('Xscript')
1689 bwipe Xtextfile
1690 set nobinary
1691endfunc
1692
1693func Test_terminal_api_drop_newwin_nobinary()
1694 CheckRunVimInTerminal
1695 set binary
1696 let buf = Api_drop_common(',{"nobinary":1}')
1697 call assert_equal(0, &bin)
1698
1699 call StopVimInTerminal(buf)
1700 call delete('Xscript')
1701 bwipe Xtextfile
1702 set nobinary
1703endfunc
1704
1705func Test_terminal_api_drop_newwin_ff()
1706 CheckRunVimInTerminal
1707 let buf = Api_drop_common(',{"ff":"dos"}')
1708 call assert_equal("dos", &ff)
1709
1710 call StopVimInTerminal(buf)
1711 call delete('Xscript')
1712 bwipe Xtextfile
1713endfunc
1714
1715func Test_terminal_api_drop_newwin_fileformat()
1716 CheckRunVimInTerminal
1717 let buf = Api_drop_common(',{"fileformat":"dos"}')
1718 call assert_equal("dos", &ff)
1719
1720 call StopVimInTerminal(buf)
1721 call delete('Xscript')
1722 bwipe Xtextfile
1723endfunc
1724
1725func Test_terminal_api_drop_newwin_enc()
1726 CheckRunVimInTerminal
1727 let buf = Api_drop_common(',{"enc":"utf-16"}')
1728 call assert_equal("utf-16", &fenc)
1729
1730 call StopVimInTerminal(buf)
1731 call delete('Xscript')
1732 bwipe Xtextfile
1733endfunc
1734
1735func Test_terminal_api_drop_newwin_encoding()
1736 CheckRunVimInTerminal
1737 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1738 call assert_equal("utf-16", &fenc)
1739
1740 call StopVimInTerminal(buf)
1741 call delete('Xscript')
1742 bwipe Xtextfile
1743endfunc
1744
1745func Test_terminal_api_drop_oldwin()
1746 CheckRunVimInTerminal
1747 let firstwinid = win_getid()
1748 split Xtextfile
1749 let textfile_winid = win_getid()
1750 call assert_equal(2, winnr('$'))
1751 call win_gotoid(firstwinid)
1752
1753 " Use the title termcap entries to output the escape sequence.
1754 call writefile([
1755 \ 'set title',
1756 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1757 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1758 \ 'redraw',
1759 \ "set t_ts=",
1760 \ ], 'Xscript')
1761 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1762 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1763 call assert_equal(textfile_winid, win_getid())
1764
1765 call StopVimInTerminal(buf)
1766 call delete('Xscript')
1767 bwipe Xtextfile
1768endfunc
1769
1770func Tapi_TryThis(bufnum, arg)
1771 let g:called_bufnum = a:bufnum
1772 let g:called_arg = a:arg
1773endfunc
1774
1775func WriteApiCall(funcname)
1776 " Use the title termcap entries to output the escape sequence.
1777 call writefile([
1778 \ 'set title',
1779 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1780 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1781 \ 'redraw',
1782 \ "set t_ts=",
1783 \ ], 'Xscript')
1784endfunc
1785
1786func Test_terminal_api_call()
1787 CheckRunVimInTerminal
1788
1789 unlet! g:called_bufnum
1790 unlet! g:called_arg
1791
1792 call WriteApiCall('Tapi_TryThis')
1793
1794 " Default
1795 let buf = RunVimInTerminal('-S Xscript', {})
1796 call WaitFor({-> exists('g:called_bufnum')})
1797 call assert_equal(buf, g:called_bufnum)
1798 call assert_equal(['hello', 123], g:called_arg)
1799 call StopVimInTerminal(buf)
1800
1801 unlet! g:called_bufnum
1802 unlet! g:called_arg
1803
1804 " Enable explicitly
1805 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1806 call WaitFor({-> exists('g:called_bufnum')})
1807 call assert_equal(buf, g:called_bufnum)
1808 call assert_equal(['hello', 123], g:called_arg)
1809 call StopVimInTerminal(buf)
1810
1811 unlet! g:called_bufnum
1812 unlet! g:called_arg
1813
1814 func! ApiCall_TryThis(bufnum, arg)
1815 let g:called_bufnum2 = a:bufnum
1816 let g:called_arg2 = a:arg
1817 endfunc
1818
1819 call WriteApiCall('ApiCall_TryThis')
1820
1821 " Use prefix match
1822 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1823 call WaitFor({-> exists('g:called_bufnum2')})
1824 call assert_equal(buf, g:called_bufnum2)
1825 call assert_equal(['hello', 123], g:called_arg2)
1826 call StopVimInTerminal(buf)
1827
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001828 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001829
1830 unlet! g:called_bufnum2
1831 unlet! g:called_arg2
1832
1833 call delete('Xscript')
1834 delfunction! ApiCall_TryThis
1835 unlet! g:called_bufnum2
1836 unlet! g:called_arg2
1837endfunc
1838
1839func Test_terminal_api_call_fails()
1840 CheckRunVimInTerminal
1841
1842 func! TryThis(bufnum, arg)
1843 let g:called_bufnum3 = a:bufnum
1844 let g:called_arg3 = a:arg
1845 endfunc
1846
1847 call WriteApiCall('TryThis')
1848
1849 unlet! g:called_bufnum3
1850 unlet! g:called_arg3
1851
1852 " Not permitted
1853 call ch_logfile('Xlog', 'w')
1854 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1855 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1856 call assert_false(exists('g:called_bufnum3'))
1857 call assert_false(exists('g:called_arg3'))
1858 call StopVimInTerminal(buf)
1859
1860 " No match
1861 call ch_logfile('Xlog', 'w')
1862 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1863 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1864 call assert_false(exists('g:called_bufnum3'))
1865 call assert_false(exists('g:called_arg3'))
1866 call StopVimInTerminal(buf)
1867
1868 call delete('Xscript')
1869 call ch_logfile('')
1870 call delete('Xlog')
1871 delfunction! TryThis
1872 unlet! g:called_bufnum3
1873 unlet! g:called_arg3
1874endfunc
1875
1876let s:caught_e937 = 0
1877
1878func Tapi_Delete(bufnum, arg)
1879 try
1880 execute 'bdelete!' a:bufnum
1881 catch /E937:/
1882 let s:caught_e937 = 1
1883 endtry
1884endfunc
1885
1886func Test_terminal_api_call_fail_delete()
1887 CheckRunVimInTerminal
1888
1889 call WriteApiCall('Tapi_Delete')
1890 let buf = RunVimInTerminal('-S Xscript', {})
1891 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1892
1893 call StopVimInTerminal(buf)
1894 call delete('Xscript')
1895 call ch_logfile('', '')
1896endfunc
1897
1898func Test_terminal_setapi_and_call()
1899 CheckRunVimInTerminal
1900
1901 call WriteApiCall('Tapi_TryThis')
1902 call ch_logfile('Xlog', 'w')
1903
1904 unlet! g:called_bufnum
1905 unlet! g:called_arg
1906
1907 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1908 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1909 call assert_false(exists('g:called_bufnum'))
1910 call assert_false(exists('g:called_arg'))
1911
1912 eval buf->term_setapi('Tapi_')
1913 call term_sendkeys(buf, ":set notitle\<CR>")
1914 call term_sendkeys(buf, ":source Xscript\<CR>")
1915 call WaitFor({-> exists('g:called_bufnum')})
1916 call assert_equal(buf, g:called_bufnum)
1917 call assert_equal(['hello', 123], g:called_arg)
1918
1919 call StopVimInTerminal(buf)
1920
1921 call delete('Xscript')
1922 call ch_logfile('')
1923 call delete('Xlog')
1924 unlet! g:called_bufnum
1925 unlet! g:called_arg
1926endfunc
1927
1928func Test_terminal_api_arg()
1929 CheckRunVimInTerminal
1930
1931 call WriteApiCall('Tapi_TryThis')
1932 call ch_logfile('Xlog', 'w')
1933
1934 unlet! g:called_bufnum
1935 unlet! g:called_arg
1936
1937 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1938 let buf = bufnr('%')
1939 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1940 call assert_false(exists('g:called_bufnum'))
1941 call assert_false(exists('g:called_arg'))
1942
1943 call StopVimInTerminal(buf)
1944
1945 call ch_logfile('Xlog', 'w')
1946
1947 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1948 let buf = bufnr('%')
1949 call WaitFor({-> exists('g:called_bufnum')})
1950 call assert_equal(buf, g:called_bufnum)
1951 call assert_equal(['hello', 123], g:called_arg)
1952
1953 call StopVimInTerminal(buf)
1954
1955 call delete('Xscript')
1956 call ch_logfile('')
1957 call delete('Xlog')
1958 unlet! g:called_bufnum
1959 unlet! g:called_arg
1960endfunc
1961
1962func Test_terminal_ansicolors_default()
1963 CheckFunction term_getansicolors
1964
1965 let colors = [
1966 \ '#000000', '#e00000',
1967 \ '#00e000', '#e0e000',
1968 \ '#0000e0', '#e000e0',
1969 \ '#00e0e0', '#e0e0e0',
1970 \ '#808080', '#ff4040',
1971 \ '#40ff40', '#ffff40',
1972 \ '#4040ff', '#ff40ff',
1973 \ '#40ffff', '#ffffff',
1974 \]
1975
1976 let buf = Run_shell_in_terminal({})
1977 call assert_equal(colors, term_getansicolors(buf))
1978 call StopShellInTerminal(buf)
1979 call TermWait(buf)
1980 call assert_equal([], term_getansicolors(buf))
1981
1982 exe buf . 'bwipe'
1983endfunc
1984
1985let s:test_colors = [
1986 \ '#616e64', '#0d0a79',
1987 \ '#6d610d', '#0a7373',
1988 \ '#690d0a', '#6d696e',
1989 \ '#0d0a6f', '#616e0d',
1990 \ '#0a6479', '#6d0d0a',
1991 \ '#617373', '#0d0a69',
1992 \ '#6d690d', '#0a6e6f',
1993 \ '#610d0a', '#6e6479',
1994 \]
1995
1996func Test_terminal_ansicolors_global()
1997 CheckFeature termguicolors
1998 CheckFunction term_getansicolors
1999
2000 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2001 let buf = Run_shell_in_terminal({})
2002 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
2003 call StopShellInTerminal(buf)
2004 call TermWait(buf)
2005
2006 exe buf . 'bwipe'
2007 unlet g:terminal_ansi_colors
2008endfunc
2009
2010func Test_terminal_ansicolors_func()
2011 CheckFeature termguicolors
2012 CheckFunction term_getansicolors
2013
2014 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
2015 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
2016 call assert_equal(s:test_colors, term_getansicolors(buf))
2017
2018 call term_setansicolors(buf, g:terminal_ansi_colors)
2019 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
2020
2021 let colors = [
2022 \ 'ivory', 'AliceBlue',
2023 \ 'grey67', 'dark goldenrod',
2024 \ 'SteelBlue3', 'PaleVioletRed4',
2025 \ 'MediumPurple2', 'yellow2',
2026 \ 'RosyBrown3', 'OrangeRed2',
2027 \ 'white smoke', 'navy blue',
2028 \ 'grey47', 'gray97',
2029 \ 'MistyRose2', 'DodgerBlue4',
2030 \]
2031 eval buf->term_setansicolors(colors)
2032
2033 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002034 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02002035 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
2036
2037 call StopShellInTerminal(buf)
2038 call TermWait(buf)
2039 call assert_equal(0, term_setansicolors(buf, []))
2040 exe buf . 'bwipe'
2041endfunc
2042
2043func Test_terminal_all_ansi_colors()
2044 CheckRunVimInTerminal
2045
2046 " Use all the ANSI colors.
2047 call writefile([
2048 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
2049 \ 'hi Tblack ctermfg=0 ctermbg=8',
2050 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
2051 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
2052 \ 'hi Tbrown ctermfg=3 ctermbg=11',
2053 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
2054 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
2055 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
2056 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
2057 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
2058 \ 'hi Tred ctermfg=9 ctermbg=1',
2059 \ 'hi Tgreen ctermfg=10 ctermbg=2',
2060 \ 'hi Tyellow ctermfg=11 ctermbg=3',
2061 \ 'hi Tblue ctermfg=12 ctermbg=4',
2062 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
2063 \ 'hi Tcyan ctermfg=14 ctermbg=6',
2064 \ 'hi Twhite ctermfg=15 ctermbg=7',
2065 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
2066 \ 'hi TgreenBold ctermfg=10 cterm=bold',
2067 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
2068 \ '',
2069 \ 'call matchadd("Tblack", "A")',
2070 \ 'call matchadd("Tdarkred", "B")',
2071 \ 'call matchadd("Tdarkgreen", "C")',
2072 \ 'call matchadd("Tbrown", "D")',
2073 \ 'call matchadd("Tdarkblue", "E")',
2074 \ 'call matchadd("Tdarkmagenta", "F")',
2075 \ 'call matchadd("Tdarkcyan", "G")',
2076 \ 'call matchadd("Tlightgrey", "H")',
2077 \ 'call matchadd("Tdarkgrey", "I")',
2078 \ 'call matchadd("Tred", "J")',
2079 \ 'call matchadd("Tgreen", "K")',
2080 \ 'call matchadd("Tyellow", "L")',
2081 \ 'call matchadd("Tblue", "M")',
2082 \ 'call matchadd("Tmagenta", "N")',
2083 \ 'call matchadd("Tcyan", "O")',
2084 \ 'call matchadd("Twhite", "P")',
2085 \ 'call matchadd("TdarkredBold", "X")',
2086 \ 'call matchadd("TgreenBold", "Y")',
2087 \ 'call matchadd("TmagentaBold", "Z")',
2088 \ 'redraw',
2089 \ ], 'Xcolorscript')
2090 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2091 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2092
2093 call term_sendkeys(buf, ":q\<CR>")
2094 call StopVimInTerminal(buf)
2095 call delete('Xcolorscript')
2096endfunc
2097
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002098function On_BufFilePost()
2099 doautocmd <nomodeline> User UserEvent
2100endfunction
2101
2102func Test_terminal_nested_autocmd()
2103 new
2104 call setline(1, range(500))
2105 $
2106 let lastline = line('.')
2107
2108 augroup TermTest
2109 autocmd BufFilePost * call On_BufFilePost()
2110 autocmd User UserEvent silent
2111 augroup END
2112
2113 let cmd = Get_cat_123_cmd()
2114 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2115 call assert_equal(lastline, line('.'))
2116
Bram Moolenaar64374752021-04-03 17:22:29 +02002117 let job = term_getjob(buf)
2118 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002119 call delete('Xtext')
2120 augroup TermTest
2121 au!
2122 augroup END
2123endfunc
2124
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002125func Test_terminal_adds_jump()
2126 clearjumps
2127 call term_start("ls", #{curwin: 1})
2128 call assert_equal(1, getjumplist()[0]->len())
2129 bwipe!
2130endfunc
2131
Bram Moolenaareea32af2021-11-21 14:51:13 +00002132func Close_cb(ch, ctx)
2133 call term_wait(a:ctx.bufnr)
2134 let g:close_done = 'done'
2135endfunc
2136
2137func Test_term_wait_in_close_cb()
2138 let g:close_done = ''
2139 let ctx = {}
2140 let ctx.bufnr = term_start('echo "HELLO WORLD"',
2141 \ {'close_cb': {ch -> Close_cb(ch, ctx)}})
2142
2143 call WaitForAssert({-> assert_equal("done", g:close_done)})
2144
2145 unlet g:close_done
2146 bwipe!
2147endfunc
2148
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002149
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002150" vim: shiftwidth=2 sts=2 expandtab