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