blob: cd7f477c262cd6de59e1e5271c7a4acd63ef3b91 [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 Moolenaar1e115362019-01-09 23:01:02 +0100162func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200163 exe g:buf . 'bwipe!'
164 let g:buf = 0
165endfunc
166
Bram Moolenaar9d189612017-09-09 18:11:00 +0200167func Get_cat_123_cmd()
168 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100169 if !has('conpty')
170 return 'cmd /c "cls && color 2 && echo 123"'
171 else
172 " When clearing twice, extra sequence is not output.
173 return 'cmd /c "cls && cls && color 2 && echo 123"'
174 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200175 else
176 call writefile(["\<Esc>[32m123"], 'Xtext')
177 return "cat Xtext"
178 endif
179endfunc
180
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200181func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200182 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200183 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
184 let g:job = term_getjob(g:buf)
185
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200186 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
187 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200188 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100189 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200190 call delete('Xtext')
191endfunc
192
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200193func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200194 let l = term_scrape(a:buf, 0)
195 call assert_true(len(l) == 0)
196 let l = term_scrape(a:buf, 999)
197 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200198 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 call assert_true(len(l) > 0)
200 call assert_equal('1', l[0].chars)
201 call assert_equal('2', l[1].chars)
202 call assert_equal('3', l[2].chars)
203 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200204 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
205 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100206 if has('win32')
207 " On Windows 'background' always defaults to dark, even though the terminal
208 " may use a light background. Therefore accept both white and black.
209 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200210 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100211 if &background == 'light'
212 call assert_equal('#ffffff', l[0].bg)
213 else
214 call assert_equal('#000000', l[0].bg)
215 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200216 endif
217
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200218 let l = term_getline(a:buf, -1)
219 call assert_equal('', l)
220 let l = term_getline(a:buf, 0)
221 call assert_equal('', l)
222 let l = term_getline(a:buf, 999)
223 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200224 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200225 call assert_equal('123', l)
226endfunc
227
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200228func Test_terminal_scrape_123()
229 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200230 let buf = term_start(cmd)
231
232 let termlist = term_list()
233 call assert_equal(1, len(termlist))
234 call assert_equal(buf, termlist[0])
235
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200236 " Nothing happens with invalid buffer number
237 call term_wait(1234)
238
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200239 call TermWait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200240 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200241 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200242 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200243 call Check_123(buf)
244
245 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200247 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200248 call TermWait(buf)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200249 call Check_123(buf)
250
251 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200252 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200253endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200254
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200255func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200256 call writefile(["léttまrs"], 'Xtext')
257 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200258 " Run cmd with UTF-8 codepage to make the type command print the expected
259 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100260 let buf = term_start("cmd /K chcp 65001")
261 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200262 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200264 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 let buf = term_start("cat Xtext")
266 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200267 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100269 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
270 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200271 call assert_true(len(l) >= 7)
272 call assert_equal('l', l[0].chars)
273 call assert_equal('é', l[1].chars)
274 call assert_equal(1, l[1].width)
275 call assert_equal('t', l[2].chars)
276 call assert_equal('t', l[3].chars)
277 call assert_equal('ま', l[4].chars)
278 call assert_equal(2, l[4].width)
279 call assert_equal('r', l[5].chars)
280 call assert_equal('s', l[6].chars)
281
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100282 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200283 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200284 call TermWait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200285
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100286 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200287 call delete('Xtext')
288endfunc
289
Bram Moolenaar8b896142020-08-02 15:05:05 +0200290func Test_terminal_one_column()
291 " This creates a terminal, displays a double-wide character and makes the
292 " window one column wide. This used to cause a crash.
293 let width = &columns
294 botright vert term
295 let buf = bufnr('$')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200296 call TermWait(buf, 100)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200297 exe "set columns=" .. (width / 2)
298 redraw
299 call term_sendkeys(buf, "キ")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200300 call TermWait(buf, 10)
Bram Moolenaar8b896142020-08-02 15:05:05 +0200301 exe "set columns=" .. width
302 exe buf . 'bwipe!'
303endfunc
304
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200305func Test_terminal_scroll()
306 call writefile(range(1, 200), 'Xtext')
307 if has('win32')
308 let cmd = 'cmd /c "type Xtext"'
309 else
310 let cmd = "cat Xtext"
311 endif
312 let buf = term_start(cmd)
313
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100314 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200315 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200316 call TermWait(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200317
Bram Moolenaarbfcfd572020-03-25 21:27:22 +0100318 " wait until the scrolling stops
319 while 1
320 let scrolled = buf->term_getscrolled()
321 sleep 20m
322 if scrolled == buf->term_getscrolled()
323 break
324 endif
325 endwhile
326
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200327 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200328 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200329 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200330 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200331 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200332 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200333
334 exe buf . 'bwipe'
335 call delete('Xtext')
336endfunc
337
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200338func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200339 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200340 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200341 call writefile(range(150), 'Xtext')
342 if has('win32')
343 call term_sendkeys(buf, "type Xtext\<CR>")
344 else
345 call term_sendkeys(buf, "cat Xtext\<CR>")
346 endif
347 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200348 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200349 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200350 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200351 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200352
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200353 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200354 call TermWait(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200355 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200356 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100357 call delete('Xtext')
358endfunc
359
360func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200361 " tail -f only works on Unix
362 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100363
364 call writefile(range(50), 'Xtext')
365 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100366 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100367 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100368 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100369 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
370 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100371 \ 'call feedkeys("\<C-W>N", "xt")',
372 \ ], 'XTest_postponed')
373 let buf = RunVimInTerminal('-S XTest_postponed', {})
374 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100375 call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100376
377 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200378 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100379 call term_sendkeys(buf, "k")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100380 call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100381
382 " Back to Terminal-Job mode, text will scroll and show the extra line.
383 call term_sendkeys(buf, "a")
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100384 call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100385
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100386 " stop "tail -f"
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100387 call term_sendkeys(buf, "\<C-C>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200388 call TermWait(buf, 25)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100389 " stop shell
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100390 call term_sendkeys(buf, "exit\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200391 call TermWait(buf, 50)
Bram Moolenaarddbfe232020-03-15 20:33:40 +0100392 " close terminal window
Bram Moolenaar3a05ce62020-03-11 19:30:01 +0100393 let tsk_ret = term_sendkeys(buf, ":q\<CR>")
394
395 " check type of term_sendkeys() return value
396 echo type(tsk_ret)
397
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100398 call StopVimInTerminal(buf)
399 call delete('XTest_postponed')
400 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200401endfunc
402
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100403" Run diff on two dumps with different size.
404func Test_terminal_dumpdiff_size()
405 call assert_equal(1, winnr('$'))
406 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
407 call assert_equal(2, winnr('$'))
408 call assert_match('Test_incsearch_search_01.dump', getline(10))
409 call assert_match(' +++++$', getline(11))
410 call assert_match('Test_popup_command_01.dump', getline(31))
411 call assert_equal(repeat('+', 75), getline(30))
412 quit
413endfunc
414
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200415func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200416 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200417
Bram Moolenaarb2412082017-08-20 18:09:14 +0200418 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200419 let size = term_getsize('')
420 bwipe!
421 call assert_equal(5, size[0])
422
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200423 call term_start(cmd, {'term_rows': 6})
424 let size = term_getsize('')
425 bwipe!
426 call assert_equal(6, size[0])
427
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200428 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200429 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200430 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200431
432 call term_setsize('', 6, 0)
433 call assert_equal([6, 33], term_getsize(''))
434
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200435 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200436 call assert_equal([6, 35], term_getsize(''))
437
438 call term_setsize('', 7, 30)
439 call assert_equal([7, 30], term_getsize(''))
440
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200441 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200442 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200443
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200444 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
445 let size = term_getsize('')
446 bwipe!
447 call assert_equal([6, 36], size)
448
Bram Moolenaarb2412082017-08-20 18:09:14 +0200449 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200450 let size = term_getsize('')
451 bwipe!
452 call assert_equal(20, size[1])
453
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200454 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200455 let size = term_getsize('')
456 bwipe!
457 call assert_equal(26, size[1])
458
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200459 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200460 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200461 let size = term_getsize('')
462 bwipe!
463 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200464
465 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
466 let size = term_getsize('')
467 bwipe!
468 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200469
Bram Moolenaar5300be62021-11-13 10:27:40 +0000470 call assert_fails("call term_start(cmd, {'term_rows': -1})", 'E475:')
471 call assert_fails("call term_start(cmd, {'term_rows': 1001})", 'E475:')
Bram Moolenaar88137392021-11-12 16:01:15 +0000472 if has('float')
473 call assert_fails("call term_start(cmd, {'term_rows': 10.0})", 'E805:')
474 endif
475
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200476 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200477endfunc
478
Bram Moolenaareba13e42021-02-23 17:47:23 +0100479func Test_terminal_zero_height()
480 split
481 wincmd j
482 anoremenu 1.1 WinBar.test :
483 terminal ++curwin
484 wincmd k
485 wincmd _
486 redraw
487
488 call term_sendkeys(bufnr(), "exit\r")
489 bwipe!
490endfunc
491
Bram Moolenaarda43b612017-08-11 22:27:50 +0200492func Test_terminal_curwin()
493 let cmd = Get_cat_123_cmd()
494 call assert_equal(1, winnr('$'))
495
Bram Moolenaarb1009092020-05-31 16:04:42 +0200496 split Xdummy
497 call setline(1, 'dummy')
498 write
499 call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200500 exe 'terminal ++curwin ' . cmd
501 call assert_equal(2, winnr('$'))
Bram Moolenaarb1009092020-05-31 16:04:42 +0200502 call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
Bram Moolenaarda43b612017-08-11 22:27:50 +0200503 bwipe!
504
Bram Moolenaarb1009092020-05-31 16:04:42 +0200505 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200506 call term_start(cmd, {'curwin': 1})
507 call assert_equal(2, winnr('$'))
508 bwipe!
509
Bram Moolenaarb1009092020-05-31 16:04:42 +0200510 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200511 call setline(1, 'change')
512 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
513 call assert_equal(2, winnr('$'))
514 exe 'terminal! ++curwin ' . cmd
515 call assert_equal(2, winnr('$'))
516 bwipe!
517
Bram Moolenaarb1009092020-05-31 16:04:42 +0200518 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200519 call setline(1, 'change')
520 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
521 call assert_equal(2, winnr('$'))
522 bwipe!
523
Bram Moolenaarb1009092020-05-31 16:04:42 +0200524 split Xdummy
Bram Moolenaarda43b612017-08-11 22:27:50 +0200525 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200526 call delete('Xtext')
Bram Moolenaarb1009092020-05-31 16:04:42 +0200527 call delete('Xdummy')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200528endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200529
Bram Moolenaarff546792017-11-21 14:47:57 +0100530func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200531 if s:python != ''
532 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200533 " 500 was not enough for Travis
534 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200535 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200536 echo 'This will take five seconds...'
537 let waittime = 2000
538 if has('win32')
539 let cmd = $windir . '\system32\timeout.exe 1'
540 else
541 let cmd = 'sleep 1'
542 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200543 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100544 return [cmd, waittime]
545endfunc
546
547func Test_terminal_finish_open_close()
548 call assert_equal(1, winnr('$'))
549
550 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200551
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100552 " shell terminal closes automatically
553 terminal
554 let buf = bufnr('%')
555 call assert_equal(2, winnr('$'))
556 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200557 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200558 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200559 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100560
561 " shell terminal that does not close automatically
562 terminal ++noclose
563 let buf = bufnr('%')
564 call assert_equal(2, winnr('$'))
565 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200566 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200567 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100568 call assert_equal(2, winnr('$'))
569 quit
570 call assert_equal(1, winnr('$'))
571
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200572 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200573 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200574 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200575 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200576
577 call term_start(cmd, {'term_finish': 'close'})
578 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200579 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200580 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200581 call assert_equal(1, winnr('$'))
582
583 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200584 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200585 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200586 bwipe
587
588 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200589 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200590 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200591 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200592
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200593 exe 'terminal ++hidden ++open ' . cmd
594 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200595 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200596 bwipe
597
598 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
599 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200600 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200601 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200602
603 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
604 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
605 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
606 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
607
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100608 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 +0200609 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200610 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200611 call assert_equal(4, winheight(0))
Bram Moolenaar47c5ea42020-11-12 15:12:15 +0100612 call assert_equal('opened the buffer in a window', g:result)
613 unlet g:result
Bram Moolenaar37c45832017-08-12 16:01:04 +0200614 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200615endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200616
617func Test_terminal_cwd()
Bram Moolenaar077ff432019-10-28 00:42:21 +0100618 if has('win32')
619 let cmd = 'cmd /c cd'
620 else
621 CheckExecutable pwd
622 let cmd = 'pwd'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200623 endif
624 call mkdir('Xdir')
Bram Moolenaar077ff432019-10-28 00:42:21 +0100625 let buf = term_start(cmd, {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200626 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627
628 exe buf . 'bwipe'
629 call delete('Xdir', 'rf')
630endfunc
631
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200632func Test_terminal_cwd_failure()
633 " Case 1: Provided directory is not actually a directory. Attempt to make
634 " the file executable as well.
635 call writefile([], 'Xfile')
636 call setfperm('Xfile', 'rwx------')
637 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
638 call delete('Xfile')
639
640 " Case 2: Directory does not exist.
641 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
642
643 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100644 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200645 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100646 call mkdir('XdirNoAccess', '', '0600')
647 " return early if the directory permissions could not be set properly
648 if getfperm('XdirNoAccess')[2] == 'x'
649 call delete('XdirNoAccess', 'rf')
650 return
651 endif
652 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
653 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200654 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200655endfunc
656
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100657func Test_terminal_servername()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200658 CheckFeature clientserver
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200659 call s:test_environment("VIM_SERVERNAME", v:servername)
660endfunc
661
662func Test_terminal_version()
663 call s:test_environment("VIM_TERMINAL", string(v:version))
664endfunc
665
666func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100667 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100668 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200669 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100670 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200671 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100672 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200673 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100674 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200675 call TermWait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200676 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200677 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100678
Bram Moolenaar012eb662018-03-13 17:55:27 +0100679 exe buf . 'bwipe'
680 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100681endfunc
682
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200683func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100684 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200685 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200686 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100687 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100688 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100689 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100690 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100691 endif
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200692 eval buf->TermWait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200693 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200694 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200695
Bram Moolenaar012eb662018-03-13 17:55:27 +0100696 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200697endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200698
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200699func Test_terminal_list_args()
700 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100701 call assert_fails(buf . 'bwipe', 'E89:')
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200702 exe buf . 'bwipe!'
703 call assert_equal("", bufname(buf))
704endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200705
706func Test_terminal_noblock()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100707 let g:test_is_flaky = 1
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100708 let buf = term_start(&shell)
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100709 let wait_time = 5000
710 let letters = 'abcdefghijklmnopqrstuvwxyz'
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100711 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200712 " The shell or something else has a problem dealing with more than 1000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100713 " characters at the same time. It's very slow too.
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200714 let len = 1000
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100715 let wait_time = 15000
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100716 let letters = 'abcdefghijklm'
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100717 " NPFS is used in Windows, nonblocking mode does not work properly.
718 elseif has('win32')
719 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200720 else
721 let len = 5000
722 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200723
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100724 " Send a lot of text lines, should be buffered properly.
Bram Moolenaarf3710ee2020-03-24 12:12:30 +0100725 for c in split(letters, '\zs')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100726 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200727 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100728 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200729
730 " On MS-Windows there is an extra empty line below "done". Find "done" in
731 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100732 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaard06dbf32020-03-24 10:33:00 +0100733 call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100734 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200735 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100736 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200737 endif
738 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200739
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100740 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200741 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200742 call TermWait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200743 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200744 bwipe
745endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200746
747func Test_terminal_write_stdin()
Bram Moolenaar21109272020-01-30 16:27:20 +0100748 " TODO: enable once writing to stdin works on MS-Windows
749 CheckNotMSWindows
750 CheckExecutable wc
751
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200752 call setline(1, ['one', 'two', 'three'])
753 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200754 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200755 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200756 call assert_equal(['3', '3', '14'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100757 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200758
759 call setline(1, ['one', 'two', 'three', 'four'])
760 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200761 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200762 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200763 call assert_equal(['2', '2', '10'], nrs)
Bram Moolenaar21109272020-01-30 16:27:20 +0100764 %bwipe!
765endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200766
Bram Moolenaar21109272020-01-30 16:27:20 +0100767func Test_terminal_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200768 call CheckPython(s:python)
Bram Moolenaardada6d22017-09-02 17:18:35 +0200769
Bram Moolenaar21109272020-01-30 16:27:20 +0100770 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200771 exe '1term ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100772 " MS-Windows echoes the input, Unix doesn't.
773 if has('win32')
774 call WaitFor({-> getline('$') =~ 'exit(123)'})
775 call assert_equal('hello', getline(line('$') - 1))
776 else
777 call WaitFor({-> getline('$') =~ 'hello'})
778 call assert_equal('hello', getline('$'))
Bram Moolenaardada6d22017-09-02 17:18:35 +0200779 endif
Bram Moolenaar21109272020-01-30 16:27:20 +0100780 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
781 %bwipe!
782endfunc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200783
Bram Moolenaar21109272020-01-30 16:27:20 +0100784func Test_terminal_eof_arg_win32_ctrl_z()
785 CheckMSWindows
Bram Moolenaara161cb52020-04-30 19:09:35 +0200786 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100787
788 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200789 exe '1term ++eof=<C-Z> ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100790 call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
791 call assert_match('\^Z', getline(line('$') - 1))
792 %bwipe!
793endfunc
794
795func Test_terminal_duplicate_eof_arg()
Bram Moolenaara161cb52020-04-30 19:09:35 +0200796 call CheckPython(s:python)
Bram Moolenaar21109272020-01-30 16:27:20 +0100797
798 " Check the last specified ++eof arg is used and should not memory leak.
799 new
800 call setline(1, ['print("hello")'])
Bram Moolenaara161cb52020-04-30 19:09:35 +0200801 exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
Bram Moolenaar21109272020-01-30 16:27:20 +0100802 " MS-Windows echoes the input, Unix doesn't.
803 if has('win32')
804 call WaitFor({-> getline('$') =~ 'exit(123)'})
805 call assert_equal('hello', getline(line('$') - 1))
806 else
807 call WaitFor({-> getline('$') =~ 'hello'})
808 call assert_equal('hello', getline('$'))
809 endif
810 call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
811 %bwipe!
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200812endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200813
814func Test_terminal_no_cmd()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100815 let g:test_is_flaky = 1
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200816 let buf = term_start('NONE', {})
817 call assert_notequal(0, buf)
818
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200819 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200820 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100821 if has('gui_running') && !has('win32')
822 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200823 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100824 else
825 " Otherwise using a job works on all systems.
826 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200827 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200828 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200829
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200830 bwipe!
831endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200832
833func Test_terminal_special_chars()
834 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200835 CheckUnix
836
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200837 call mkdir('Xdir with spaces')
838 call writefile(['x'], 'Xdir with spaces/quoted"file')
839 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200840 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar95ffd432020-02-23 13:29:31 +0100841 " make sure the job has finished
842 call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200843
844 call delete('Xdir with spaces', 'rf')
845 bwipe
846endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200847
848func Test_terminal_wrong_options()
849 call assert_fails('call term_start(&shell, {
850 \ "in_io": "file",
851 \ "in_name": "xxx",
852 \ "out_io": "file",
853 \ "out_name": "xxx",
854 \ "err_io": "file",
855 \ "err_name": "xxx"
856 \ })', 'E474:')
857 call assert_fails('call term_start(&shell, {
858 \ "out_buf": bufnr("%")
859 \ })', 'E474:')
860 call assert_fails('call term_start(&shell, {
861 \ "err_buf": bufnr("%")
862 \ })', 'E474:')
863endfunc
864
865func Test_terminal_redir_file()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100866 let g:test_is_flaky = 1
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200867 let cmd = Get_cat_123_cmd()
868 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200869 call TermWait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100870 " ConPTY may precede escape sequence. There are things that are not so.
871 if !has('conpty')
872 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
873 call assert_match('123', readfile('Xfile')[0])
874 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200875 let g:job = term_getjob(buf)
876 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
Bram Moolenaarc69950a2020-07-22 22:23:40 +0200877
878 if has('win32')
879 " On Windows we cannot delete a file being used by a process. When
880 " job_status() returns "dead", the process remains for a short time.
881 " Just wait for a moment.
882 sleep 50m
883 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200884 call delete('Xfile')
885 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200886
887 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200888 call writefile(['one line'], 'Xfile')
889 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200890 call TermWait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200891 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200892 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200893 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200894 bwipe
895 call delete('Xfile')
896 endif
897endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200898
899func TerminalTmap(remap)
900 let buf = Run_shell_in_terminal({})
Bram Moolenaarf4a2ed02021-03-23 16:25:09 +0100901 " Wait for the shell to display a prompt
902 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200903 call assert_equal('t', mode())
904
905 if a:remap
906 tmap 123 456
907 else
908 tnoremap 123 456
909 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100910 " don't use abcde, it's an existing command
911 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200912 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100913 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200914 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200915 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200916 let lnum = term_getcursor(buf)[0]
917 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100918 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200919 else
920 call assert_match('456', term_getline(buf, lnum))
921 endif
922
923 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200924 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200925 call TermWait(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200926
927 tunmap 123
928 tunmap 456
929 call assert_equal('', maparg('123', 't'))
930 close
931 unlet g:job
932endfunc
933
934func Test_terminal_tmap()
935 call TerminalTmap(1)
936 call TerminalTmap(0)
937endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200938
939func Test_terminal_wall()
940 let buf = Run_shell_in_terminal({})
941 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200942 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200943 call TermWait(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200944 exe buf . 'bwipe'
945 unlet g:job
946endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200947
Bram Moolenaar7a760922018-02-19 23:10:02 +0100948func Test_terminal_wqall()
949 let buf = Run_shell_in_terminal({})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200950 call assert_fails('wqall', 'E948:')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200951 call StopShellInTerminal(buf)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200952 call TermWait(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100953 exe buf . 'bwipe'
954 unlet g:job
955endfunc
956
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200957func Test_terminal_composing_unicode()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100958 let g:test_is_flaky = 1
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200959 let save_enc = &encoding
960 set encoding=utf-8
961
962 if has('win32')
963 let cmd = "cmd /K chcp 65001"
964 let lnum = [3, 6, 9]
965 else
966 let cmd = &shell
967 let lnum = [1, 3, 5]
968 endif
969
970 enew
Bram Moolenaarc98cdb32020-09-06 21:13:00 +0200971 let buf = term_start(cmd, {'curwin': 1})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100972 let g:job = term_getjob(buf)
Bram Moolenaar41d42992020-05-03 16:29:50 +0200973 call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200974
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200975 if has('win32')
976 call assert_equal('cmd', job_info(g:job).cmd[0])
977 else
978 call assert_equal(&shell, job_info(g:job).cmd[0])
979 endif
980
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200981 " ascii + composing
982 let txt = "a\u0308bc"
Bram Moolenaar41d42992020-05-03 16:29:50 +0200983 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200984 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200985 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200986 call term_sendkeys(buf, "\<cr>")
987 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200988 let l = term_scrape(buf, lnum[0] + 1)
989 call assert_equal("a\u0308", l[0].chars)
990 call assert_equal("b", l[1].chars)
991 call assert_equal("c", l[2].chars)
992
Bram Moolenaar4549dad2021-02-08 21:29:48 +0100993 " multibyte + composing: がぎぐげご
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200994 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
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[1]))
Bram Moolenaar41d42992020-05-03 16:29:50 +0200998 call term_sendkeys(buf, "\<cr>")
999 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001000 let l = term_scrape(buf, lnum[1] + 1)
1001 call assert_equal("\u304b\u3099", l[0].chars)
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001002 call assert_equal(2, l[0].width)
1003 call assert_equal("\u304e", l[1].chars)
1004 call assert_equal(2, l[1].width)
1005 call assert_equal("\u304f\u3099", l[2].chars)
1006 call assert_equal(2, l[2].width)
1007 call assert_equal("\u3052", l[3].chars)
1008 call assert_equal(2, l[3].width)
1009 call assert_equal("\u3053\u3099", l[4].chars)
1010 call assert_equal(2, l[4].width)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001011
1012 " \u00a0 + composing
1013 let txt = "abc\u00a0\u0308"
Bram Moolenaar41d42992020-05-03 16:29:50 +02001014 call term_sendkeys(buf, "echo " . txt)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001015 call TermWait(buf, 25)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001016 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
Bram Moolenaar41d42992020-05-03 16:29:50 +02001017 call term_sendkeys(buf, "\<cr>")
1018 call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001019 let l = term_scrape(buf, lnum[2] + 1)
1020 call assert_equal("\u00a0\u0308", l[3].chars)
1021
1022 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001023 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001024 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +01001025 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +02001026 let &encoding = save_enc
1027endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +01001028
1029func Test_terminal_aucmd_on_close()
1030 fun Nop()
1031 let s:called = 1
1032 endfun
1033
1034 aug repro
1035 au!
1036 au BufWinLeave * call Nop()
1037 aug END
1038
1039 let [cmd, waittime] = s:get_sleep_cmd()
1040
1041 call assert_equal(1, winnr('$'))
1042 new
1043 call setline(1, ['one', 'two'])
1044 exe 'term ++close ' . cmd
1045 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +01001047 call assert_equal(1, s:called)
1048 bwipe!
1049
1050 unlet s:called
1051 au! repro
1052 delfunc Nop
1053endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001054
1055func Test_terminal_term_start_empty_command()
1056 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001057 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001058 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001059 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001060 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001061 call assert_fails(cmd, 'E474:')
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001062 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001063 call assert_fails(cmd, 'E474:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001064 let cmd = "call term_start('', {'term_name' : []})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001065 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001066 let cmd = "call term_start('', {'term_finish' : 'axby'})"
Bram Moolenaare2e40752020-09-04 21:18:46 +02001067 call assert_fails(cmd, 'E475:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001068 let cmd = "call term_start('', {'eof_chars' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001069 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001070 let cmd = "call term_start('', {'term_kill' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001071 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001072 let cmd = "call term_start('', {'tty_type' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001073 call assert_fails(cmd, 'E730:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001074 let cmd = "call term_start('', {'tty_type' : 'abc'})"
1075 call assert_fails(cmd, 'E475:')
1076 let cmd = "call term_start('', {'term_highlight' : []})"
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001077 call assert_fails(cmd, 'E730:')
Bram Moolenaar87202262020-05-24 17:23:45 +02001078 if has('gui') || has('termguicolors')
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001079 let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1080 call assert_fails(cmd, 'E475:')
1081 let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1082 call assert_fails(cmd, 'E730:')
1083 let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
Bram Moolenaar87202262020-05-24 17:23:45 +02001084 if has('gui_running') || has('termguicolors')
1085 call assert_fails(cmd, 'E475:')
1086 else
1087 call assert_fails(cmd, 'E254:')
1088 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001089 endif
Bram Moolenaarede35bb2018-01-26 20:05:18 +01001090endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001091
1092func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001093 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001094
1095 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001096 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001097
Bram Moolenaar086eb872018-03-25 21:24:12 +02001098 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001099 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001100
Bram Moolenaar086eb872018-03-25 21:24:12 +02001101 " Request the cursor position.
1102 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +01001103
1104 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001105 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001106
Bram Moolenaar086eb872018-03-25 21:24:12 +02001107 " End "cat" gently.
1108 call term_sendkeys(buf, "\<CR>\<C-D>")
1109
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001110 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001111 exe buf . 'bwipe'
1112 unlet g:job
1113endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001114
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001115" Run Vim, start a terminal in that Vim with the kill argument,
1116" :qall works.
1117func Run_terminal_qall_kill(line1, line2)
1118 " 1. Open a terminal window and wait for the prompt to appear
1119 " 2. set kill using term_setkill()
1120 " 3. make Vim exit, it will kill the shell
1121 let after = [
1122 \ a:line1,
1123 \ 'let buf = bufnr("%")',
1124 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
1125 \ ' sleep 10m',
1126 \ 'endwhile',
1127 \ a:line2,
1128 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1129 \ 'qall',
1130 \ ]
1131 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001132 return
1133 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001134 call assert_equal("done", readfile("Xdone")[0])
1135 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001136endfunc
1137
1138" Run Vim in a terminal, then start a terminal in that Vim with a kill
1139" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001140func Test_terminal_qall_kill_arg()
1141 call Run_terminal_qall_kill('term ++kill=kill', '')
1142endfunc
1143
1144" Run Vim, start a terminal in that Vim, set the kill argument with
1145" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001146func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001147 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001148endfunc
1149
1150" Run Vim, start a terminal in that Vim without the kill argument,
1151" check that :qall does not exit, :qall! does.
1152func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001153 let after =<< trim [CODE]
1154 term
1155 let buf = bufnr("%")
1156 while term_getline(buf, 1) =~ "^\\s*$"
1157 sleep 10m
1158 endwhile
1159 set nomore
1160 au VimLeavePre * call writefile(["too early"], "Xdone")
1161 qall
1162 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1163 cquit
1164 [CODE]
1165
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001166 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001167 return
1168 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001169 call assert_equal("done", readfile("Xdone")[0])
1170 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001171endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001172
1173" Run Vim in a terminal, then start a terminal in that Vim without a kill
1174" argument, check that :confirm qall works.
1175func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001176 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001177 let buf = RunVimInTerminal('', {})
1178
1179 " Open a terminal window and wait for the prompt to appear
1180 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001181 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1182 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001183
1184 " make Vim exit, it will prompt to kill the shell
1185 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001186 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001187 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001188 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001189
1190 " close the terminal window where Vim was running
1191 quit
1192endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001193
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02001194" Run Vim in a terminal, then start a terminal window with a shell and check
1195" that Vim exits if it is closed.
1196func Test_terminal_exit()
1197 CheckRunVimInTerminal
1198
1199 let lines =<< trim END
1200 let winid = win_getid()
1201 help
1202 term
1203 let termid = win_getid()
1204 call win_gotoid(winid)
1205 close
1206 call win_gotoid(termid)
1207 END
1208 call writefile(lines, 'XtermExit')
1209 let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1210 let job = term_getjob(buf)
1211 call WaitForAssert({-> assert_equal("run", job_status(job))})
1212
1213 " quit the shell, it will make Vim exit
1214 call term_sendkeys(buf, "exit\<CR>")
1215 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1216
1217 call delete('XtermExit')
1218endfunc
1219
Bram Moolenaar012eb662018-03-13 17:55:27 +01001220func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001221 augroup repro
1222 au!
1223 au TerminalOpen * let s:called += 1
1224 augroup END
1225
1226 let s:called = 0
1227
1228 " Open a terminal window with :terminal
1229 terminal
1230 call assert_equal(1, s:called)
1231 bwipe!
1232
1233 " Open a terminal window with term_start()
1234 call term_start(&shell)
1235 call assert_equal(2, s:called)
1236 bwipe!
1237
1238 " Open a hidden terminal buffer with :terminal
1239 terminal ++hidden
1240 call assert_equal(3, s:called)
1241 for buf in term_list()
1242 exe buf . "bwipe!"
1243 endfor
1244
1245 " Open a hidden terminal buffer with term_start()
1246 let buf = term_start(&shell, {'hidden': 1})
1247 call assert_equal(4, s:called)
1248 exe buf . "bwipe!"
1249
1250 unlet s:called
1251 au! repro
Bram Moolenaarf4d61bc2020-11-14 14:22:28 +01001252endfunc
1253
1254func Test_open_term_from_cmd()
1255 CheckUnix
1256 CheckRunVimInTerminal
1257
1258 let lines =<< trim END
1259 call setline(1, ['a', 'b', 'c'])
1260 3
1261 set incsearch
1262 cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1263 END
1264 call writefile(lines, 'Xopenterm')
1265 let buf = RunVimInTerminal('-S Xopenterm', {})
1266
1267 " this opens a window, incsearch should not use the old cursor position
1268 call term_sendkeys(buf, "/\<F3>")
1269 call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1270 call term_sendkeys(buf, "\<Esc>")
1271 call term_sendkeys(buf, ":q\<CR>")
1272
1273 call StopVimInTerminal(buf)
1274 call delete('Xopenterm')
1275endfunc
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001276
Bram Moolenaar4549dad2021-02-08 21:29:48 +01001277func Test_combining_double_width()
1278 CheckUnix
1279 CheckRunVimInTerminal
1280
1281 call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1282 let lines =<< trim END
1283 call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1284 END
1285 call writefile(lines, 'Xcombining')
1286 let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1287
1288 " this opens a window, incsearch should not use the old cursor position
1289 call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1290 call term_sendkeys(buf, ":q\<CR>")
1291
1292 call StopVimInTerminal(buf)
1293 call delete('Xonedouble')
1294 call delete('Xcombining')
1295endfunc
1296
Bram Moolenaar02764712020-11-14 20:21:55 +01001297func Test_terminal_popup_with_cmd()
1298 " this was crashing
1299 let buf = term_start(&shell, #{hidden: v:true})
1300 let s:winid = popup_create(buf, {})
1301 tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1302 call feedkeys("\<F3>", 'xt')
1303
1304 tunmap <F3>
1305 exe 'bwipe! ' .. buf
1306 unlet s:winid
1307endfunc
1308
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001309func Test_terminal_popup_bufload()
1310 let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1311 let winid = popup_create(termbuf, {})
1312 sleep 50m
1313
1314 let newbuf = bufadd('')
1315 call bufload(newbuf)
1316 call setbufline(newbuf, 1, 'foobar')
1317
1318 " must not have switched to another window
1319 call assert_equal(winid, win_getid())
1320
Bram Moolenaare6329e42020-11-16 21:10:34 +01001321 call StopShellInTerminal(termbuf)
1322 call WaitFor({-> win_getid() != winid})
Bram Moolenaar8adc8d92020-11-16 20:47:31 +01001323 exe 'bwipe! ' .. newbuf
1324endfunc
1325
Bram Moolenaare41decc2020-11-14 21:34:59 +01001326func Test_terminal_popup_insert_cmd()
1327 CheckUnix
1328
1329 inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1330 func StartTermInPopup()
Bram Moolenaar27f4f6b2020-11-16 21:02:28 +01001331 call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001332 endfunc
1333 call feedkeys("i\<F3>")
1334 sleep 10m
1335 call assert_equal('n', mode())
1336
1337 call feedkeys("\<C-D>", 'xt')
Bram Moolenaar17ab28d2020-11-18 12:24:01 +01001338 call WaitFor({-> popup_list() == []})
Bram Moolenaare41decc2020-11-14 21:34:59 +01001339 delfunc StartTermInPopup
1340 iunmap <F3>
1341endfunc
1342
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001343func Check_dump01(off)
1344 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1345 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001346 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001347endfunc
1348
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001349func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001350 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001351 let save_enc = &encoding
1352 set encoding=utf-8
1353 call assert_equal(1, winnr('$'))
1354
1355 let text = " a\u0300 e\u0302 o\u0308"
1356 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001357 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001358 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001359 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001360 let dumpline = readfile('Xdump')[0]
1361 call assert_match('|à| |ê| |ö', dumpline)
1362
1363 call StopVimInTerminal(buf)
1364 call delete('Xcomposing')
1365 call delete('Xdump')
1366 let &encoding = save_enc
1367endfunc
1368
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001369" Tests for failures in the term_dumpwrite() function
1370func Test_terminal_dumpwrite_errors()
1371 CheckRunVimInTerminal
1372 call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1373 let buf = RunVimInTerminal('', {})
Bram Moolenaar733d2592020-08-20 18:59:06 +02001374 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001375 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1376 call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1377 call writefile([], 'Xtest.dump')
1378 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1379 call delete('Xtest.dump')
1380 call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1381 call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
Bram Moolenaar98f16712020-05-22 13:34:01 +02001382 call test_garbagecollect_now()
Bram Moolenaara46765a2020-11-01 20:58:26 +01001383 call StopVimInTerminal(buf, 0)
Bram Moolenaar733d2592020-08-20 18:59:06 +02001384 call TermWait(buf)
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001385 call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1386 call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1387 call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1388endfunc
1389
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001390" just testing basic functionality.
1391func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001392 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001393 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001394 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001395 call assert_equal(2, winnr('$'))
1396 call assert_equal(20, line('$'))
1397 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001398
1399 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001400 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001401 call assert_equal(buf, buf2)
1402 call assert_notequal('one two three four five', trim(getline(1)))
1403
1404 " Load the first dump again in the same window
1405 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1406 call assert_equal(buf, buf2)
1407 call Check_dump01(0)
1408
1409 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1410 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1411 new
1412 let closedbuf = winbufnr('')
1413 quit
1414 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001415 call assert_fails('call term_dumpload([])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001416 call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
Bram Moolenaar87abab92019-06-03 21:14:59 +02001417
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001418 quit
1419endfunc
1420
Bram Moolenaar17efc7f2019-10-16 18:11:31 +02001421func Test_terminal_dumpload_dump()
1422 CheckRunVimInTerminal
1423
1424 let lines =<< trim END
1425 call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1426 END
1427 call writefile(lines, 'XtermDumpload')
1428 let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1429 call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1430
1431 call StopVimInTerminal(buf)
1432 call delete('XtermDumpload')
1433endfunc
1434
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001435func Test_terminal_dumpdiff()
1436 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001437 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001438 call assert_equal(2, winnr('$'))
1439 call assert_equal(62, line('$'))
1440 call Check_dump01(0)
1441 call Check_dump01(42)
1442 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1443 quit
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001444
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001445 call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
Bram Moolenaar91689ea2020-05-11 22:04:53 +02001446 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1447 call writefile([], 'X1.dump')
1448 call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1449 call delete('X1.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001450endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001451
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001452func Test_terminal_dumpdiff_swap()
1453 call assert_equal(1, winnr('$'))
1454 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1455 call assert_equal(2, winnr('$'))
1456 call assert_equal(62, line('$'))
1457 call assert_match('Test_popup_command_01.dump', getline(21))
1458 call assert_match('Test_popup_command_03.dump', getline(42))
1459 call assert_match('Undo', getline(3))
1460 call assert_match('three four five', getline(45))
1461
1462 normal s
1463 call assert_match('Test_popup_command_03.dump', getline(21))
1464 call assert_match('Test_popup_command_01.dump', getline(42))
1465 call assert_match('three four five', getline(3))
1466 call assert_match('Undo', getline(45))
1467 quit
Bram Moolenaar98f16712020-05-22 13:34:01 +02001468
1469 " Diff two terminal dump files with different number of rows
1470 " Swap the diffs
1471 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1472 call assert_match('Test_popup_command_01.dump', getline(21))
1473 call assert_match('Test_winline_rnu.dump', getline(42))
1474 normal s
1475 call assert_match('Test_winline_rnu.dump', getline(6))
1476 call assert_match('Test_popup_command_01.dump', getline(27))
1477 quit
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001478endfunc
1479
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001480func Test_terminal_dumpdiff_options()
1481 set laststatus=0
1482 call assert_equal(1, winnr('$'))
1483 let height = winheight(0)
1484 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1485 call assert_equal(2, winnr('$'))
1486 call assert_equal(height, winheight(winnr()))
1487 call assert_equal(33, winwidth(winnr()))
1488 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1489 quit
1490
1491 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001492 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1493 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001494 call assert_equal(&columns, winwidth(0))
1495 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001496 call assert_equal('something else', bufname('%'))
1497 quit
1498
1499 call assert_equal(1, winnr('$'))
1500 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1501 call assert_equal(1, winnr('$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001502 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 +01001503 bwipe
1504
1505 set laststatus&
1506endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001507
Bram Moolenaar10772302019-01-20 18:25:54 +01001508" When drawing the statusline the cursor position may not have been updated
1509" yet.
1510" 1. create a terminal, make it show 2 lines
1511" 2. 0.5 sec later: leave terminal window, execute "i"
1512" 3. 0.5 sec later: clear terminal window, now it's 1 line
1513" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1514" 4. 0.5 sec later: should be done, clean up
1515func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001516 CheckUnix
1517
Bram Moolenaar10772302019-01-20 18:25:54 +01001518 set statusline=x
1519 terminal
1520 let tbuf = bufnr('')
1521 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1522 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1523 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1524 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1525
1526 sleep 2
1527 exe tbuf . 'bwipe!'
1528 au! BufLeave
1529 set statusline=
1530endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02001531
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02001532func Test_terminal_window_focus()
1533 let winid1 = win_getid()
1534 terminal
1535 let winid2 = win_getid()
1536 call feedkeys("\<C-W>j", 'xt')
1537 call assert_equal(winid1, win_getid())
1538 call feedkeys("\<C-W>k", 'xt')
1539 call assert_equal(winid2, win_getid())
1540 " can use a cursor key here
1541 call feedkeys("\<C-W>\<Down>", 'xt')
1542 call assert_equal(winid1, win_getid())
1543 call feedkeys("\<C-W>\<Up>", 'xt')
1544 call assert_equal(winid2, win_getid())
1545
1546 bwipe!
1547endfunc
1548
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001549func Api_drop_common(options)
1550 call assert_equal(1, winnr('$'))
1551
1552 " Use the title termcap entries to output the escape sequence.
1553 call writefile([
1554 \ 'set title',
1555 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1556 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1557 \ 'redraw',
1558 \ "set t_ts=",
1559 \ ], 'Xscript')
1560 let buf = RunVimInTerminal('-S Xscript', {})
1561 call WaitFor({-> bufnr('Xtextfile') > 0})
1562 call assert_equal('Xtextfile', expand('%:t'))
1563 call assert_true(winnr('$') >= 3)
1564 return buf
1565endfunc
1566
1567func Test_terminal_api_drop_newwin()
1568 CheckRunVimInTerminal
1569 let buf = Api_drop_common('')
1570 call assert_equal(0, &bin)
1571 call assert_equal('', &fenc)
1572
1573 call StopVimInTerminal(buf)
1574 call delete('Xscript')
1575 bwipe Xtextfile
1576endfunc
1577
1578func Test_terminal_api_drop_newwin_bin()
1579 CheckRunVimInTerminal
1580 let buf = Api_drop_common(',{"bin":1}')
1581 call assert_equal(1, &bin)
1582
1583 call StopVimInTerminal(buf)
1584 call delete('Xscript')
1585 bwipe Xtextfile
1586endfunc
1587
1588func Test_terminal_api_drop_newwin_binary()
1589 CheckRunVimInTerminal
1590 let buf = Api_drop_common(',{"binary":1}')
1591 call assert_equal(1, &bin)
1592
1593 call StopVimInTerminal(buf)
1594 call delete('Xscript')
1595 bwipe Xtextfile
1596endfunc
1597
1598func Test_terminal_api_drop_newwin_nobin()
1599 CheckRunVimInTerminal
1600 set binary
1601 let buf = Api_drop_common(',{"nobin":1}')
1602 call assert_equal(0, &bin)
1603
1604 call StopVimInTerminal(buf)
1605 call delete('Xscript')
1606 bwipe Xtextfile
1607 set nobinary
1608endfunc
1609
1610func Test_terminal_api_drop_newwin_nobinary()
1611 CheckRunVimInTerminal
1612 set binary
1613 let buf = Api_drop_common(',{"nobinary":1}')
1614 call assert_equal(0, &bin)
1615
1616 call StopVimInTerminal(buf)
1617 call delete('Xscript')
1618 bwipe Xtextfile
1619 set nobinary
1620endfunc
1621
1622func Test_terminal_api_drop_newwin_ff()
1623 CheckRunVimInTerminal
1624 let buf = Api_drop_common(',{"ff":"dos"}')
1625 call assert_equal("dos", &ff)
1626
1627 call StopVimInTerminal(buf)
1628 call delete('Xscript')
1629 bwipe Xtextfile
1630endfunc
1631
1632func Test_terminal_api_drop_newwin_fileformat()
1633 CheckRunVimInTerminal
1634 let buf = Api_drop_common(',{"fileformat":"dos"}')
1635 call assert_equal("dos", &ff)
1636
1637 call StopVimInTerminal(buf)
1638 call delete('Xscript')
1639 bwipe Xtextfile
1640endfunc
1641
1642func Test_terminal_api_drop_newwin_enc()
1643 CheckRunVimInTerminal
1644 let buf = Api_drop_common(',{"enc":"utf-16"}')
1645 call assert_equal("utf-16", &fenc)
1646
1647 call StopVimInTerminal(buf)
1648 call delete('Xscript')
1649 bwipe Xtextfile
1650endfunc
1651
1652func Test_terminal_api_drop_newwin_encoding()
1653 CheckRunVimInTerminal
1654 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1655 call assert_equal("utf-16", &fenc)
1656
1657 call StopVimInTerminal(buf)
1658 call delete('Xscript')
1659 bwipe Xtextfile
1660endfunc
1661
1662func Test_terminal_api_drop_oldwin()
1663 CheckRunVimInTerminal
1664 let firstwinid = win_getid()
1665 split Xtextfile
1666 let textfile_winid = win_getid()
1667 call assert_equal(2, winnr('$'))
1668 call win_gotoid(firstwinid)
1669
1670 " Use the title termcap entries to output the escape sequence.
1671 call writefile([
1672 \ 'set title',
1673 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1674 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1675 \ 'redraw',
1676 \ "set t_ts=",
1677 \ ], 'Xscript')
1678 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1679 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1680 call assert_equal(textfile_winid, win_getid())
1681
1682 call StopVimInTerminal(buf)
1683 call delete('Xscript')
1684 bwipe Xtextfile
1685endfunc
1686
1687func Tapi_TryThis(bufnum, arg)
1688 let g:called_bufnum = a:bufnum
1689 let g:called_arg = a:arg
1690endfunc
1691
1692func WriteApiCall(funcname)
1693 " Use the title termcap entries to output the escape sequence.
1694 call writefile([
1695 \ 'set title',
1696 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1697 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1698 \ 'redraw',
1699 \ "set t_ts=",
1700 \ ], 'Xscript')
1701endfunc
1702
1703func Test_terminal_api_call()
1704 CheckRunVimInTerminal
1705
1706 unlet! g:called_bufnum
1707 unlet! g:called_arg
1708
1709 call WriteApiCall('Tapi_TryThis')
1710
1711 " Default
1712 let buf = RunVimInTerminal('-S Xscript', {})
1713 call WaitFor({-> exists('g:called_bufnum')})
1714 call assert_equal(buf, g:called_bufnum)
1715 call assert_equal(['hello', 123], g:called_arg)
1716 call StopVimInTerminal(buf)
1717
1718 unlet! g:called_bufnum
1719 unlet! g:called_arg
1720
1721 " Enable explicitly
1722 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1723 call WaitFor({-> exists('g:called_bufnum')})
1724 call assert_equal(buf, g:called_bufnum)
1725 call assert_equal(['hello', 123], g:called_arg)
1726 call StopVimInTerminal(buf)
1727
1728 unlet! g:called_bufnum
1729 unlet! g:called_arg
1730
1731 func! ApiCall_TryThis(bufnum, arg)
1732 let g:called_bufnum2 = a:bufnum
1733 let g:called_arg2 = a:arg
1734 endfunc
1735
1736 call WriteApiCall('ApiCall_TryThis')
1737
1738 " Use prefix match
1739 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1740 call WaitFor({-> exists('g:called_bufnum2')})
1741 call assert_equal(buf, g:called_bufnum2)
1742 call assert_equal(['hello', 123], g:called_arg2)
1743 call StopVimInTerminal(buf)
1744
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001745 call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001746
1747 unlet! g:called_bufnum2
1748 unlet! g:called_arg2
1749
1750 call delete('Xscript')
1751 delfunction! ApiCall_TryThis
1752 unlet! g:called_bufnum2
1753 unlet! g:called_arg2
1754endfunc
1755
1756func Test_terminal_api_call_fails()
1757 CheckRunVimInTerminal
1758
1759 func! TryThis(bufnum, arg)
1760 let g:called_bufnum3 = a:bufnum
1761 let g:called_arg3 = a:arg
1762 endfunc
1763
1764 call WriteApiCall('TryThis')
1765
1766 unlet! g:called_bufnum3
1767 unlet! g:called_arg3
1768
1769 " Not permitted
1770 call ch_logfile('Xlog', 'w')
1771 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1772 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1773 call assert_false(exists('g:called_bufnum3'))
1774 call assert_false(exists('g:called_arg3'))
1775 call StopVimInTerminal(buf)
1776
1777 " No match
1778 call ch_logfile('Xlog', 'w')
1779 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1780 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1781 call assert_false(exists('g:called_bufnum3'))
1782 call assert_false(exists('g:called_arg3'))
1783 call StopVimInTerminal(buf)
1784
1785 call delete('Xscript')
1786 call ch_logfile('')
1787 call delete('Xlog')
1788 delfunction! TryThis
1789 unlet! g:called_bufnum3
1790 unlet! g:called_arg3
1791endfunc
1792
1793let s:caught_e937 = 0
1794
1795func Tapi_Delete(bufnum, arg)
1796 try
1797 execute 'bdelete!' a:bufnum
1798 catch /E937:/
1799 let s:caught_e937 = 1
1800 endtry
1801endfunc
1802
1803func Test_terminal_api_call_fail_delete()
1804 CheckRunVimInTerminal
1805
1806 call WriteApiCall('Tapi_Delete')
1807 let buf = RunVimInTerminal('-S Xscript', {})
1808 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1809
1810 call StopVimInTerminal(buf)
1811 call delete('Xscript')
1812 call ch_logfile('', '')
1813endfunc
1814
1815func Test_terminal_setapi_and_call()
1816 CheckRunVimInTerminal
1817
1818 call WriteApiCall('Tapi_TryThis')
1819 call ch_logfile('Xlog', 'w')
1820
1821 unlet! g:called_bufnum
1822 unlet! g:called_arg
1823
1824 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1825 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1826 call assert_false(exists('g:called_bufnum'))
1827 call assert_false(exists('g:called_arg'))
1828
1829 eval buf->term_setapi('Tapi_')
1830 call term_sendkeys(buf, ":set notitle\<CR>")
1831 call term_sendkeys(buf, ":source Xscript\<CR>")
1832 call WaitFor({-> exists('g:called_bufnum')})
1833 call assert_equal(buf, g:called_bufnum)
1834 call assert_equal(['hello', 123], g:called_arg)
1835
1836 call StopVimInTerminal(buf)
1837
1838 call delete('Xscript')
1839 call ch_logfile('')
1840 call delete('Xlog')
1841 unlet! g:called_bufnum
1842 unlet! g:called_arg
1843endfunc
1844
1845func Test_terminal_api_arg()
1846 CheckRunVimInTerminal
1847
1848 call WriteApiCall('Tapi_TryThis')
1849 call ch_logfile('Xlog', 'w')
1850
1851 unlet! g:called_bufnum
1852 unlet! g:called_arg
1853
1854 execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1855 let buf = bufnr('%')
1856 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1857 call assert_false(exists('g:called_bufnum'))
1858 call assert_false(exists('g:called_arg'))
1859
1860 call StopVimInTerminal(buf)
1861
1862 call ch_logfile('Xlog', 'w')
1863
1864 execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1865 let buf = bufnr('%')
1866 call WaitFor({-> exists('g:called_bufnum')})
1867 call assert_equal(buf, g:called_bufnum)
1868 call assert_equal(['hello', 123], g:called_arg)
1869
1870 call StopVimInTerminal(buf)
1871
1872 call delete('Xscript')
1873 call ch_logfile('')
1874 call delete('Xlog')
1875 unlet! g:called_bufnum
1876 unlet! g:called_arg
1877endfunc
1878
1879func Test_terminal_ansicolors_default()
1880 CheckFunction term_getansicolors
1881
1882 let colors = [
1883 \ '#000000', '#e00000',
1884 \ '#00e000', '#e0e000',
1885 \ '#0000e0', '#e000e0',
1886 \ '#00e0e0', '#e0e0e0',
1887 \ '#808080', '#ff4040',
1888 \ '#40ff40', '#ffff40',
1889 \ '#4040ff', '#ff40ff',
1890 \ '#40ffff', '#ffffff',
1891 \]
1892
1893 let buf = Run_shell_in_terminal({})
1894 call assert_equal(colors, term_getansicolors(buf))
1895 call StopShellInTerminal(buf)
1896 call TermWait(buf)
1897 call assert_equal([], term_getansicolors(buf))
1898
1899 exe buf . 'bwipe'
1900endfunc
1901
1902let s:test_colors = [
1903 \ '#616e64', '#0d0a79',
1904 \ '#6d610d', '#0a7373',
1905 \ '#690d0a', '#6d696e',
1906 \ '#0d0a6f', '#616e0d',
1907 \ '#0a6479', '#6d0d0a',
1908 \ '#617373', '#0d0a69',
1909 \ '#6d690d', '#0a6e6f',
1910 \ '#610d0a', '#6e6479',
1911 \]
1912
1913func Test_terminal_ansicolors_global()
1914 CheckFeature termguicolors
1915 CheckFunction term_getansicolors
1916
1917 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1918 let buf = Run_shell_in_terminal({})
1919 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1920 call StopShellInTerminal(buf)
1921 call TermWait(buf)
1922
1923 exe buf . 'bwipe'
1924 unlet g:terminal_ansi_colors
1925endfunc
1926
1927func Test_terminal_ansicolors_func()
1928 CheckFeature termguicolors
1929 CheckFunction term_getansicolors
1930
1931 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1932 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1933 call assert_equal(s:test_colors, term_getansicolors(buf))
1934
1935 call term_setansicolors(buf, g:terminal_ansi_colors)
1936 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1937
1938 let colors = [
1939 \ 'ivory', 'AliceBlue',
1940 \ 'grey67', 'dark goldenrod',
1941 \ 'SteelBlue3', 'PaleVioletRed4',
1942 \ 'MediumPurple2', 'yellow2',
1943 \ 'RosyBrown3', 'OrangeRed2',
1944 \ 'white smoke', 'navy blue',
1945 \ 'grey47', 'gray97',
1946 \ 'MistyRose2', 'DodgerBlue4',
1947 \]
1948 eval buf->term_setansicolors(colors)
1949
1950 let colors[4] = 'Invalid'
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001951 call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001952 call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1953
1954 call StopShellInTerminal(buf)
1955 call TermWait(buf)
1956 call assert_equal(0, term_setansicolors(buf, []))
1957 exe buf . 'bwipe'
1958endfunc
1959
1960func Test_terminal_all_ansi_colors()
1961 CheckRunVimInTerminal
1962
1963 " Use all the ANSI colors.
1964 call writefile([
1965 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1966 \ 'hi Tblack ctermfg=0 ctermbg=8',
1967 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1968 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1969 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1970 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1971 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1972 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1973 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1974 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1975 \ 'hi Tred ctermfg=9 ctermbg=1',
1976 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1977 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1978 \ 'hi Tblue ctermfg=12 ctermbg=4',
1979 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1980 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1981 \ 'hi Twhite ctermfg=15 ctermbg=7',
1982 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1983 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1984 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1985 \ '',
1986 \ 'call matchadd("Tblack", "A")',
1987 \ 'call matchadd("Tdarkred", "B")',
1988 \ 'call matchadd("Tdarkgreen", "C")',
1989 \ 'call matchadd("Tbrown", "D")',
1990 \ 'call matchadd("Tdarkblue", "E")',
1991 \ 'call matchadd("Tdarkmagenta", "F")',
1992 \ 'call matchadd("Tdarkcyan", "G")',
1993 \ 'call matchadd("Tlightgrey", "H")',
1994 \ 'call matchadd("Tdarkgrey", "I")',
1995 \ 'call matchadd("Tred", "J")',
1996 \ 'call matchadd("Tgreen", "K")',
1997 \ 'call matchadd("Tyellow", "L")',
1998 \ 'call matchadd("Tblue", "M")',
1999 \ 'call matchadd("Tmagenta", "N")',
2000 \ 'call matchadd("Tcyan", "O")',
2001 \ 'call matchadd("Twhite", "P")',
2002 \ 'call matchadd("TdarkredBold", "X")',
2003 \ 'call matchadd("TgreenBold", "Y")',
2004 \ 'call matchadd("TmagentaBold", "Z")',
2005 \ 'redraw',
2006 \ ], 'Xcolorscript')
2007 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
2008 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
2009
2010 call term_sendkeys(buf, ":q\<CR>")
2011 call StopVimInTerminal(buf)
2012 call delete('Xcolorscript')
2013endfunc
2014
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002015function On_BufFilePost()
2016 doautocmd <nomodeline> User UserEvent
2017endfunction
2018
2019func Test_terminal_nested_autocmd()
2020 new
2021 call setline(1, range(500))
2022 $
2023 let lastline = line('.')
2024
2025 augroup TermTest
2026 autocmd BufFilePost * call On_BufFilePost()
2027 autocmd User UserEvent silent
2028 augroup END
2029
2030 let cmd = Get_cat_123_cmd()
2031 let buf = term_start(cmd, #{term_finish: 'close', hidden: 1})
2032 call assert_equal(lastline, line('.'))
2033
Bram Moolenaar64374752021-04-03 17:22:29 +02002034 let job = term_getjob(buf)
2035 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002036 call delete('Xtext')
2037 augroup TermTest
2038 au!
2039 augroup END
2040endfunc
2041
Bram Moolenaaraeed2a62021-04-29 20:18:45 +02002042func Test_terminal_adds_jump()
2043 clearjumps
2044 call term_start("ls", #{curwin: 1})
2045 call assert_equal(1, getjumplist()[0]->len())
2046 bwipe!
2047endfunc
2048
Bram Moolenaar91689ea2020-05-11 22:04:53 +02002049
Bram Moolenaarca68ae12020-03-30 19:32:53 +02002050" vim: shiftwidth=2 sts=2 expandtab