blob: 137de7ddd60b4e9bced213ea2e2def6ed51a05d2 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020035 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020048 call assert_match('%aR[^\n]*running]', execute('ls R'))
49 call assert_notmatch('%[^\n]*running]', execute('ls F'))
50 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
53 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054 call assert_equal('n', mode())
55 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020056 call assert_match('%aF[^\n]*finished]', execute('ls F'))
57 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
58 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020064 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020086 call WaitFor('job_status(g:job) == "dead"')
87 call assert_equal('dead', job_status(g:job))
88 call assert_equal("", bufname(buf))
89
90 unlet g:job
91endfunc
92
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020093func Test_terminal_split_quit()
94 let buf = Run_shell_in_terminal({})
95 call term_wait(buf)
96 split
97 quit!
98 call term_wait(buf)
99 sleep 50m
100 call assert_equal('run', job_status(g:job))
101
102 quit!
103 call WaitFor('job_status(g:job) == "dead"')
104 call assert_equal('dead', job_status(g:job))
105
106 exe buf . 'bwipe'
107 unlet g:job
108endfunc
109
Bram Moolenaar94053a52017-08-01 21:44:33 +0200110func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200111 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200112 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200113 quit
114 for nr in range(1, winnr('$'))
115 call assert_notequal(winbufnr(nr), buf)
116 endfor
117 call assert_true(bufloaded(buf))
118 call assert_true(buflisted(buf))
119
120 exe 'split ' . buf . 'buf'
121 call Stop_shell_in_terminal(buf)
122 exe buf . 'bwipe'
123
124 unlet g:job
125endfunc
126
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200127func! s:Nasty_exit_cb(job, st)
128 exe g:buf . 'bwipe!'
129 let g:buf = 0
130endfunc
131
Bram Moolenaar9d189612017-09-09 18:11:00 +0200132func Get_cat_123_cmd()
133 if has('win32')
134 return 'cmd /c "cls && color 2 && echo 123"'
135 else
136 call writefile(["\<Esc>[32m123"], 'Xtext')
137 return "cat Xtext"
138 endif
139endfunc
140
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200142 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200143 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
144 let g:job = term_getjob(g:buf)
145
146 call WaitFor('job_status(g:job) == "dead"')
147 call WaitFor('g:buf == 0')
148 unlet g:buf
149 unlet g:job
150 call delete('Xtext')
151endfunc
152
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200153func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200154 let l = term_scrape(a:buf, 0)
155 call assert_true(len(l) == 0)
156 let l = term_scrape(a:buf, 999)
157 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200158 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200159 call assert_true(len(l) > 0)
160 call assert_equal('1', l[0].chars)
161 call assert_equal('2', l[1].chars)
162 call assert_equal('3', l[2].chars)
163 call assert_equal('#00e000', l[0].fg)
164 if &background == 'light'
165 call assert_equal('#ffffff', l[0].bg)
166 else
167 call assert_equal('#000000', l[0].bg)
168 endif
169
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200170 let l = term_getline(a:buf, -1)
171 call assert_equal('', l)
172 let l = term_getline(a:buf, 0)
173 call assert_equal('', l)
174 let l = term_getline(a:buf, 999)
175 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200176 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200177 call assert_equal('123', l)
178endfunc
179
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200180func Test_terminal_scrape_123()
181 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200182 let buf = term_start(cmd)
183
184 let termlist = term_list()
185 call assert_equal(1, len(termlist))
186 call assert_equal(buf, termlist[0])
187
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200188 " Nothing happens with invalid buffer number
189 call term_wait(1234)
190
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200192 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200193 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100194 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200195 call Check_123(buf)
196
197 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100198 let job = term_getjob(buf)
199 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200200 call term_wait(buf)
201 call Check_123(buf)
202
203 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200204 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200205endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200206
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200207func Test_terminal_scrape_multibyte()
208 if !has('multi_byte')
209 return
210 endif
211 call writefile(["léttまrs"], 'Xtext')
212 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200213 " Run cmd with UTF-8 codepage to make the type command print the expected
214 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100215 let buf = term_start("cmd /K chcp 65001")
216 call term_sendkeys(buf, "type Xtext\<CR>")
217 call term_sendkeys(buf, "exit\<CR>")
218 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200219 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100220 let buf = term_start("cat Xtext")
221 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200222 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200223
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100224 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
225 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200226 call assert_true(len(l) >= 7)
227 call assert_equal('l', l[0].chars)
228 call assert_equal('é', l[1].chars)
229 call assert_equal(1, l[1].width)
230 call assert_equal('t', l[2].chars)
231 call assert_equal('t', l[3].chars)
232 call assert_equal('ま', l[4].chars)
233 call assert_equal(2, l[4].width)
234 call assert_equal('r', l[5].chars)
235 call assert_equal('s', l[6].chars)
236
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100237 let job = term_getjob(buf)
238 call WaitFor({-> job_status(job) == "dead"})
239 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200240
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200242 call delete('Xtext')
243endfunc
244
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200245func Test_terminal_scroll()
246 call writefile(range(1, 200), 'Xtext')
247 if has('win32')
248 let cmd = 'cmd /c "type Xtext"'
249 else
250 let cmd = "cat Xtext"
251 endif
252 let buf = term_start(cmd)
253
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100254 let job = term_getjob(buf)
255 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200256 call term_wait(buf)
257 if has('win32')
258 " TODO: this should not be needed
259 sleep 100m
260 endif
261
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200262 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200263 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200264 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200265 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200266 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200267 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200268 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200269
270 exe buf . 'bwipe'
271 call delete('Xtext')
272endfunc
273
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200274func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200275 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200276
Bram Moolenaarb2412082017-08-20 18:09:14 +0200277 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200278 let size = term_getsize('')
279 bwipe!
280 call assert_equal(5, size[0])
281
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200282 call term_start(cmd, {'term_rows': 6})
283 let size = term_getsize('')
284 bwipe!
285 call assert_equal(6, size[0])
286
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200287 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200288 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200289 call assert_equal([5, 33], term_getsize(''))
290
291 call term_setsize('', 6, 0)
292 call assert_equal([6, 33], term_getsize(''))
293
294 call term_setsize('', 0, 35)
295 call assert_equal([6, 35], term_getsize(''))
296
297 call term_setsize('', 7, 30)
298 call assert_equal([7, 30], term_getsize(''))
299
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 bwipe!
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200302 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
303 let size = term_getsize('')
304 bwipe!
305 call assert_equal([6, 36], size)
306
Bram Moolenaarb2412082017-08-20 18:09:14 +0200307 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200308 let size = term_getsize('')
309 bwipe!
310 call assert_equal(20, size[1])
311
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200312 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
313 let size = term_getsize('')
314 bwipe!
315 call assert_equal(26, size[1])
316
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200317 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200318 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200319 let size = term_getsize('')
320 bwipe!
321 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200322
323 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
324 let size = term_getsize('')
325 bwipe!
326 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200327
328 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200329endfunc
330
331func Test_terminal_curwin()
332 let cmd = Get_cat_123_cmd()
333 call assert_equal(1, winnr('$'))
334
335 split dummy
336 exe 'terminal ++curwin ' . cmd
337 call assert_equal(2, winnr('$'))
338 bwipe!
339
340 split dummy
341 call term_start(cmd, {'curwin': 1})
342 call assert_equal(2, winnr('$'))
343 bwipe!
344
345 split dummy
346 call setline(1, 'change')
347 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
348 call assert_equal(2, winnr('$'))
349 exe 'terminal! ++curwin ' . cmd
350 call assert_equal(2, winnr('$'))
351 bwipe!
352
353 split dummy
354 call setline(1, 'change')
355 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
356 call assert_equal(2, winnr('$'))
357 bwipe!
358
359 split dummy
360 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200361 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200362endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200363
Bram Moolenaarff546792017-11-21 14:47:57 +0100364func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200365 if s:python != ''
366 let cmd = s:python . " test_short_sleep.py"
367 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200368 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200369 echo 'This will take five seconds...'
370 let waittime = 2000
371 if has('win32')
372 let cmd = $windir . '\system32\timeout.exe 1'
373 else
374 let cmd = 'sleep 1'
375 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200376 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100377 return [cmd, waittime]
378endfunc
379
380func Test_terminal_finish_open_close()
381 call assert_equal(1, winnr('$'))
382
383 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200384
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100385 " shell terminal closes automatically
386 terminal
387 let buf = bufnr('%')
388 call assert_equal(2, winnr('$'))
389 " Wait for the shell to display a prompt
390 call WaitFor({-> term_getline(buf, 1) != ""})
391 call Stop_shell_in_terminal(buf)
392 call WaitFor("winnr('$') == 1", waittime)
393
394 " shell terminal that does not close automatically
395 terminal ++noclose
396 let buf = bufnr('%')
397 call assert_equal(2, winnr('$'))
398 " Wait for the shell to display a prompt
399 call WaitFor({-> term_getline(buf, 1) != ""})
400 call Stop_shell_in_terminal(buf)
401 call assert_equal(2, winnr('$'))
402 quit
403 call assert_equal(1, winnr('$'))
404
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200405 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200406 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200407 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200408 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200409
410 call term_start(cmd, {'term_finish': 'close'})
411 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200412 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200413 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200414 call assert_equal(1, winnr('$'))
415
416 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200417 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200418 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200419 call assert_equal(2, winnr('$'))
420 bwipe
421
422 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200423 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200424 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200425 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200426 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200428 exe 'terminal ++hidden ++open ' . cmd
429 call assert_equal(1, winnr('$'))
430 call WaitFor("winnr('$') == 2", waittime)
431 call assert_equal(2, winnr('$'))
432 bwipe
433
434 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
435 call assert_equal(1, winnr('$'))
436 call WaitFor("winnr('$') == 2", waittime)
437 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200438 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200439
440 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
441 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
442 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
443 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
444
445 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200446 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200447 call WaitFor("winnr('$') == 2", waittime)
448 call assert_equal(2, winnr('$'))
449 call assert_equal(4, winheight(0))
450 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200451endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200452
453func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200454 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200455 return
456 endif
457 call mkdir('Xdir')
458 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200459 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
460 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200461
462 exe buf . 'bwipe'
463 call delete('Xdir', 'rf')
464endfunc
465
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100466func Test_terminal_servername()
467 if !has('clientserver')
468 return
469 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100470 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100471 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100472 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100473 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100474 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100475 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100476 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100477 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100478 call term_wait(buf)
479 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100480 call WaitFor('getline(2) == v:servername')
481 call assert_equal(v:servername, getline(2))
482
Bram Moolenaar012eb662018-03-13 17:55:27 +0100483 exe buf . 'bwipe'
484 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100485endfunc
486
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200487func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100488 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200489 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100490 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100491 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100492 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100493 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100494 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100495 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100496 call term_wait(buf)
497 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200498 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200499 call assert_equal('correct', getline(2))
500
Bram Moolenaar012eb662018-03-13 17:55:27 +0100501 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200502endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200503
504" must be last, we can't go back from GUI to terminal
505func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200506 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200507 return
508 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200509
510 " Ignore the "failed to create input context" error.
511 call test_ignore_error('E285:')
512
Bram Moolenaar679653e2017-08-13 14:13:19 +0200513 gui -f
514
515 call assert_equal(1, winnr('$'))
516 let buf = Run_shell_in_terminal({'term_finish': 'close'})
517 call Stop_shell_in_terminal(buf)
518 call term_wait(buf)
519
520 " closing window wipes out the terminal buffer a with finished job
521 call WaitFor("winnr('$') == 1")
522 call assert_equal(1, winnr('$'))
523 call assert_equal("", bufname(buf))
524
525 unlet g:job
526endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200527
528func Test_terminal_list_args()
529 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
530 call assert_fails(buf . 'bwipe', 'E517')
531 exe buf . 'bwipe!'
532 call assert_equal("", bufname(buf))
533endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200534
535func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100536 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200537 if has('mac')
538 " The shell or something else has a problem dealing with more than 1000
539 " characters at the same time.
540 let len = 1000
541 else
542 let len = 5000
543 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200544
545 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100546 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200547 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100548 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200549
550 " On MS-Windows there is an extra empty line below "done". Find "done" in
551 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100552 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100553 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100554 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200555 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100556 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200557 endif
558 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200559
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100560 let g:job = term_getjob(buf)
561 call Stop_shell_in_terminal(buf)
562 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200563 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200564 bwipe
565endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200566
567func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200568 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200569 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200570 endif
571 new
572 call setline(1, ['one', 'two', 'three'])
573 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200574 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200575 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200576 call assert_equal(['3', '3', '14'], nrs)
577 bwipe
578
Bram Moolenaardada6d22017-09-02 17:18:35 +0200579 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200580 call setline(1, ['one', 'two', 'three', 'four'])
581 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200582 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200583 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200584 call assert_equal(['2', '2', '10'], nrs)
585 bwipe
586
Bram Moolenaardada6d22017-09-02 17:18:35 +0200587 if executable('python')
588 new
589 call setline(1, ['print("hello")'])
590 1term ++eof=exit() python
591 " MS-Windows echoes the input, Unix doesn't.
592 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
593 if getline(1) =~ 'hello'
594 call assert_equal('hello', getline(1))
595 else
596 call assert_equal('hello', getline(line('$') - 1))
597 endif
598 bwipe
599
600 if has('win32')
601 new
602 call setline(1, ['print("hello")'])
603 1term ++eof=<C-Z> python
604 call WaitFor('getline("$") =~ "Z"')
605 call assert_equal('hello', getline(line('$') - 1))
606 bwipe
607 endif
608 endif
609
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200610 bwipe!
611endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200612
613func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200614 " Todo: make this work in the GUI
615 if !has('gui_running')
616 return
617 endif
618 let buf = term_start('NONE', {})
619 call assert_notequal(0, buf)
620
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200621 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200622 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200623 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200624 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200625 else
626 call system('echo "look here" > ' . pty)
627 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100628 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200629
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200630 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200631 bwipe!
632endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200633
634func Test_terminal_special_chars()
635 " this file name only works on Unix
636 if !has('unix')
637 return
638 endif
639 call mkdir('Xdir with spaces')
640 call writefile(['x'], 'Xdir with spaces/quoted"file')
641 term ls Xdir\ with\ spaces/quoted\"file
642 call WaitFor('term_getline("", 1) =~ "quoted"')
643 call assert_match('quoted"file', term_getline('', 1))
644 call term_wait('')
645
646 call delete('Xdir with spaces', 'rf')
647 bwipe
648endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200649
650func Test_terminal_wrong_options()
651 call assert_fails('call term_start(&shell, {
652 \ "in_io": "file",
653 \ "in_name": "xxx",
654 \ "out_io": "file",
655 \ "out_name": "xxx",
656 \ "err_io": "file",
657 \ "err_name": "xxx"
658 \ })', 'E474:')
659 call assert_fails('call term_start(&shell, {
660 \ "out_buf": bufnr("%")
661 \ })', 'E474:')
662 call assert_fails('call term_start(&shell, {
663 \ "err_buf": bufnr("%")
664 \ })', 'E474:')
665endfunc
666
667func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200668 " TODO: this should work on MS-Window
669 if has('unix')
670 let cmd = Get_cat_123_cmd()
671 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
672 call term_wait(buf)
673 call WaitFor('len(readfile("Xfile")) > 0')
674 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200675 let g:job = term_getjob(buf)
676 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200677 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200678 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200679 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200680
681 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200682 call writefile(['one line'], 'Xfile')
683 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
684 call term_wait(buf)
685 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
686 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200687 let g:job = term_getjob(buf)
688 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200689 bwipe
690 call delete('Xfile')
691 endif
692endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200693
694func TerminalTmap(remap)
695 let buf = Run_shell_in_terminal({})
696 call assert_equal('t', mode())
697
698 if a:remap
699 tmap 123 456
700 else
701 tnoremap 123 456
702 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100703 " don't use abcde, it's an existing command
704 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200705 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100706 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200707 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100708 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200709 let lnum = term_getcursor(buf)[0]
710 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100711 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200712 else
713 call assert_match('456', term_getline(buf, lnum))
714 endif
715
716 call term_sendkeys(buf, "\r")
717 call Stop_shell_in_terminal(buf)
718 call term_wait(buf)
719
720 tunmap 123
721 tunmap 456
722 call assert_equal('', maparg('123', 't'))
723 close
724 unlet g:job
725endfunc
726
727func Test_terminal_tmap()
728 call TerminalTmap(1)
729 call TerminalTmap(0)
730endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200731
732func Test_terminal_wall()
733 let buf = Run_shell_in_terminal({})
734 wall
735 call Stop_shell_in_terminal(buf)
736 call term_wait(buf)
737 exe buf . 'bwipe'
738 unlet g:job
739endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200740
Bram Moolenaar7a760922018-02-19 23:10:02 +0100741func Test_terminal_wqall()
742 let buf = Run_shell_in_terminal({})
743 call assert_fails('wqall', 'E948')
744 call Stop_shell_in_terminal(buf)
745 call term_wait(buf)
746 exe buf . 'bwipe'
747 unlet g:job
748endfunc
749
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200750func Test_terminal_composing_unicode()
751 let save_enc = &encoding
752 set encoding=utf-8
753
754 if has('win32')
755 let cmd = "cmd /K chcp 65001"
756 let lnum = [3, 6, 9]
757 else
758 let cmd = &shell
759 let lnum = [1, 3, 5]
760 endif
761
762 enew
763 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100764 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200765 call term_wait(buf, 50)
766
767 " ascii + composing
768 let txt = "a\u0308bc"
769 call term_sendkeys(buf, "echo " . txt . "\r")
770 call term_wait(buf, 50)
771 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
772 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
773 let l = term_scrape(buf, lnum[0] + 1)
774 call assert_equal("a\u0308", l[0].chars)
775 call assert_equal("b", l[1].chars)
776 call assert_equal("c", l[2].chars)
777
778 " multibyte + composing
779 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
780 call term_sendkeys(buf, "echo " . txt . "\r")
781 call term_wait(buf, 50)
782 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
783 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
784 let l = term_scrape(buf, lnum[1] + 1)
785 call assert_equal("\u304b\u3099", l[0].chars)
786 call assert_equal("\u304e", l[1].chars)
787 call assert_equal("\u304f\u3099", l[2].chars)
788 call assert_equal("\u3052", l[3].chars)
789 call assert_equal("\u3053\u3099", l[4].chars)
790
791 " \u00a0 + composing
792 let txt = "abc\u00a0\u0308"
793 call term_sendkeys(buf, "echo " . txt . "\r")
794 call term_wait(buf, 50)
795 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
796 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
797 let l = term_scrape(buf, lnum[2] + 1)
798 call assert_equal("\u00a0\u0308", l[3].chars)
799
800 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100801 call WaitFor('job_status(g:job) == "dead"')
802 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200803 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100804 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200805 let &encoding = save_enc
806endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100807
808func Test_terminal_aucmd_on_close()
809 fun Nop()
810 let s:called = 1
811 endfun
812
813 aug repro
814 au!
815 au BufWinLeave * call Nop()
816 aug END
817
818 let [cmd, waittime] = s:get_sleep_cmd()
819
820 call assert_equal(1, winnr('$'))
821 new
822 call setline(1, ['one', 'two'])
823 exe 'term ++close ' . cmd
824 wincmd p
825 call WaitFor("winnr('$') == 2", waittime)
826 call assert_equal(1, s:called)
827 bwipe!
828
829 unlet s:called
830 au! repro
831 delfunc Nop
832endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100833
834func Test_terminal_term_start_empty_command()
835 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
836 call assert_fails(cmd, 'E474')
837 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
838 call assert_fails(cmd, 'E474')
839 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
840 call assert_fails(cmd, 'E474')
841 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
842 call assert_fails(cmd, 'E474')
843endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100844
845func Test_terminal_response_to_control_sequence()
846 if !has('unix')
847 return
848 endif
849
850 let buf = Run_shell_in_terminal({})
Bram Moolenaar086eb872018-03-25 21:24:12 +0200851 call WaitFor({-> term_getline(buf, 1) != ''})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100852
Bram Moolenaar086eb872018-03-25 21:24:12 +0200853 call term_sendkeys(buf, "cat\<CR>")
854 call WaitFor({-> term_getline(buf, 1) =~ 'cat'})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100855
Bram Moolenaar086eb872018-03-25 21:24:12 +0200856 " Request the cursor position.
857 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100858
859 " Wait for output from tty to display, below an empty line.
Bram Moolenaar086eb872018-03-25 21:24:12 +0200860 call WaitFor({-> term_getline(buf, 4) =~ '3;1R'})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100861
Bram Moolenaar086eb872018-03-25 21:24:12 +0200862 " End "cat" gently.
863 call term_sendkeys(buf, "\<CR>\<C-D>")
864
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100865 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100866 exe buf . 'bwipe'
867 unlet g:job
868endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100869
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100870" Run Vim, start a terminal in that Vim with the kill argument,
871" :qall works.
872func Run_terminal_qall_kill(line1, line2)
873 " 1. Open a terminal window and wait for the prompt to appear
874 " 2. set kill using term_setkill()
875 " 3. make Vim exit, it will kill the shell
876 let after = [
877 \ a:line1,
878 \ 'let buf = bufnr("%")',
879 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
880 \ ' sleep 10m',
881 \ 'endwhile',
882 \ a:line2,
883 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
884 \ 'qall',
885 \ ]
886 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100887 return
888 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100889 call assert_equal("done", readfile("Xdone")[0])
890 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100891endfunc
892
893" Run Vim in a terminal, then start a terminal in that Vim with a kill
894" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100895func Test_terminal_qall_kill_arg()
896 call Run_terminal_qall_kill('term ++kill=kill', '')
897endfunc
898
899" Run Vim, start a terminal in that Vim, set the kill argument with
900" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100901func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100902 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
903endfunc
904
905" Run Vim, start a terminal in that Vim without the kill argument,
906" check that :qall does not exit, :qall! does.
907func Test_terminal_qall_exit()
908 let after = [
909 \ 'term',
910 \ 'let buf = bufnr("%")',
911 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
912 \ ' sleep 10m',
913 \ 'endwhile',
914 \ 'set nomore',
915 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
916 \ 'qall',
917 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
918 \ 'cquit',
919 \ ]
920 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100921 return
922 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100923 call assert_equal("done", readfile("Xdone")[0])
924 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100925endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100926
927" Run Vim in a terminal, then start a terminal in that Vim without a kill
928" argument, check that :confirm qall works.
929func Test_terminal_qall_prompt()
930 if !CanRunVimInTerminal()
931 return
932 endif
933 let buf = RunVimInTerminal('', {})
934
935 " Open a terminal window and wait for the prompt to appear
936 call term_sendkeys(buf, ":term\<CR>")
937 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
938 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
939
940 " make Vim exit, it will prompt to kill the shell
941 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
942 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
943 call term_sendkeys(buf, "y")
944 call WaitFor({-> term_getstatus(buf) == "finished"})
945
946 " close the terminal window where Vim was running
947 quit
948endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100949
Bram Moolenaar012eb662018-03-13 17:55:27 +0100950func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100951 augroup repro
952 au!
953 au TerminalOpen * let s:called += 1
954 augroup END
955
956 let s:called = 0
957
958 " Open a terminal window with :terminal
959 terminal
960 call assert_equal(1, s:called)
961 bwipe!
962
963 " Open a terminal window with term_start()
964 call term_start(&shell)
965 call assert_equal(2, s:called)
966 bwipe!
967
968 " Open a hidden terminal buffer with :terminal
969 terminal ++hidden
970 call assert_equal(3, s:called)
971 for buf in term_list()
972 exe buf . "bwipe!"
973 endfor
974
975 " Open a hidden terminal buffer with term_start()
976 let buf = term_start(&shell, {'hidden': 1})
977 call assert_equal(4, s:called)
978 exe buf . "bwipe!"
979
980 unlet s:called
981 au! repro
982endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +0100983
984func Check_dump01(off)
985 call assert_equal('one two three four five', trim(getline(a:off + 1)))
986 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +0200987 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +0100988endfunc
989
Bram Moolenaarf06b0b62018-03-29 17:22:24 +0200990func Test_terminal_dumpwrite_composing()
991 if !CanRunVimInTerminal()
992 return
993 endif
994 let save_enc = &encoding
995 set encoding=utf-8
996 call assert_equal(1, winnr('$'))
997
998 let text = " a\u0300 e\u0302 o\u0308"
999 call writefile([text], 'Xcomposing')
1000 let buf = RunVimInTerminal('Xcomposing', {})
1001 call WaitFor({-> term_getline(buf, 1) =~ text})
1002 call term_dumpwrite(buf, 'Xdump')
1003 let dumpline = readfile('Xdump')[0]
1004 call assert_match('|à| |ê| |ö', dumpline)
1005
1006 call StopVimInTerminal(buf)
1007 call delete('Xcomposing')
1008 call delete('Xdump')
1009 let &encoding = save_enc
1010endfunc
1011
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001012" just testing basic functionality.
1013func Test_terminal_dumpload()
1014 call assert_equal(1, winnr('$'))
1015 call term_dumpload('dumps/Test_popup_command_01.dump')
1016 call assert_equal(2, winnr('$'))
1017 call assert_equal(20, line('$'))
1018 call Check_dump01(0)
1019 quit
1020endfunc
1021
1022func Test_terminal_dumpdiff()
1023 call assert_equal(1, winnr('$'))
1024 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1025 call assert_equal(2, winnr('$'))
1026 call assert_equal(62, line('$'))
1027 call Check_dump01(0)
1028 call Check_dump01(42)
1029 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1030 quit
1031endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001032
1033func Test_terminal_dumpdiff_options()
1034 set laststatus=0
1035 call assert_equal(1, winnr('$'))
1036 let height = winheight(0)
1037 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1038 call assert_equal(2, winnr('$'))
1039 call assert_equal(height, winheight(winnr()))
1040 call assert_equal(33, winwidth(winnr()))
1041 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1042 quit
1043
1044 call assert_equal(1, winnr('$'))
1045 let width = winwidth(0)
1046 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1047 call assert_equal(2, winnr('$'))
1048 call assert_equal(width, winwidth(winnr()))
1049 call assert_equal(13, winheight(winnr()))
1050 call assert_equal('something else', bufname('%'))
1051 quit
1052
1053 call assert_equal(1, winnr('$'))
1054 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1055 call assert_equal(1, winnr('$'))
1056 bwipe
1057
1058 set laststatus&
1059endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001060
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001061func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001062 call assert_equal(1, winnr('$'))
1063
1064 " Use the title termcap entries to output the escape sequence.
1065 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001066 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001067 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001068 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001069 \ 'redraw',
1070 \ "set t_ts=",
1071 \ ], 'Xscript')
1072 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001073 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001074 call assert_equal('Xtextfile', expand('%:t'))
1075 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001076 return buf
1077endfunc
1078
1079func Test_terminal_api_drop_newwin()
1080 if !CanRunVimInTerminal()
1081 return
1082 endif
1083 let buf = Api_drop_common('')
1084 call assert_equal(0, &bin)
1085 call assert_equal('', &fenc)
1086
1087 call StopVimInTerminal(buf)
1088 call delete('Xscript')
1089 bwipe Xtextfile
1090endfunc
1091
1092func Test_terminal_api_drop_newwin_bin()
1093 if !CanRunVimInTerminal()
1094 return
1095 endif
1096 let buf = Api_drop_common(',{"bin":1}')
1097 call assert_equal(1, &bin)
1098
1099 call StopVimInTerminal(buf)
1100 call delete('Xscript')
1101 bwipe Xtextfile
1102endfunc
1103
1104func Test_terminal_api_drop_newwin_binary()
1105 if !CanRunVimInTerminal()
1106 return
1107 endif
1108 let buf = Api_drop_common(',{"binary":1}')
1109 call assert_equal(1, &bin)
1110
1111 call StopVimInTerminal(buf)
1112 call delete('Xscript')
1113 bwipe Xtextfile
1114endfunc
1115
1116func Test_terminal_api_drop_newwin_nobin()
1117 if !CanRunVimInTerminal()
1118 return
1119 endif
1120 set binary
1121 let buf = Api_drop_common(',{"nobin":1}')
1122 call assert_equal(0, &bin)
1123
1124 call StopVimInTerminal(buf)
1125 call delete('Xscript')
1126 bwipe Xtextfile
1127 set nobinary
1128endfunc
1129
1130func Test_terminal_api_drop_newwin_nobinary()
1131 if !CanRunVimInTerminal()
1132 return
1133 endif
1134 set binary
1135 let buf = Api_drop_common(',{"nobinary":1}')
1136 call assert_equal(0, &bin)
1137
1138 call StopVimInTerminal(buf)
1139 call delete('Xscript')
1140 bwipe Xtextfile
1141 set nobinary
1142endfunc
1143
1144func Test_terminal_api_drop_newwin_ff()
1145 if !CanRunVimInTerminal()
1146 return
1147 endif
1148 let buf = Api_drop_common(',{"ff":"dos"}')
1149 call assert_equal("dos", &ff)
1150
1151 call StopVimInTerminal(buf)
1152 call delete('Xscript')
1153 bwipe Xtextfile
1154endfunc
1155
1156func Test_terminal_api_drop_newwin_fileformat()
1157 if !CanRunVimInTerminal()
1158 return
1159 endif
1160 let buf = Api_drop_common(',{"fileformat":"dos"}')
1161 call assert_equal("dos", &ff)
1162
1163 call StopVimInTerminal(buf)
1164 call delete('Xscript')
1165 bwipe Xtextfile
1166endfunc
1167
1168func Test_terminal_api_drop_newwin_enc()
1169 if !CanRunVimInTerminal()
1170 return
1171 endif
1172 let buf = Api_drop_common(',{"enc":"utf-16"}')
1173 call assert_equal("utf-16", &fenc)
1174
1175 call StopVimInTerminal(buf)
1176 call delete('Xscript')
1177 bwipe Xtextfile
1178endfunc
1179
1180func Test_terminal_api_drop_newwin_encoding()
1181 if !CanRunVimInTerminal()
1182 return
1183 endif
1184 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1185 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001186
1187 call StopVimInTerminal(buf)
1188 call delete('Xscript')
1189 bwipe Xtextfile
1190endfunc
1191
1192func Test_terminal_api_drop_oldwin()
1193 if !CanRunVimInTerminal()
1194 return
1195 endif
1196 let firstwinid = win_getid()
1197 split Xtextfile
1198 let textfile_winid = win_getid()
1199 call assert_equal(2, winnr('$'))
1200 call win_gotoid(firstwinid)
1201
1202 " Use the title termcap entries to output the escape sequence.
1203 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001204 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001205 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1206 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1207 \ 'redraw',
1208 \ "set t_ts=",
1209 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001210 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001211 call WaitFor({-> expand('%:t') =='Xtextfile'})
1212 call assert_equal(textfile_winid, win_getid())
1213
1214 call StopVimInTerminal(buf)
1215 call delete('Xscript')
1216 bwipe Xtextfile
1217endfunc
1218
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001219func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001220 let g:called_bufnum = a:bufnum
1221 let g:called_arg = a:arg
1222endfunc
1223
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001224func WriteApiCall(funcname)
1225 " Use the title termcap entries to output the escape sequence.
1226 call writefile([
1227 \ 'set title',
1228 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1229 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1230 \ 'redraw',
1231 \ "set t_ts=",
1232 \ ], 'Xscript')
1233endfunc
1234
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001235func Test_terminal_api_call()
1236 if !CanRunVimInTerminal()
1237 return
1238 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001239
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001240 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001241 let buf = RunVimInTerminal('-S Xscript', {})
1242 call WaitFor({-> exists('g:called_bufnum')})
1243 call assert_equal(buf, g:called_bufnum)
1244 call assert_equal(['hello', 123], g:called_arg)
1245
1246 call StopVimInTerminal(buf)
1247 call delete('Xscript')
1248 unlet g:called_bufnum
1249 unlet g:called_arg
1250endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001251
1252func Test_terminal_api_call_fails()
1253 if !CanRunVimInTerminal()
1254 return
1255 endif
1256
1257 call WriteApiCall('TryThis')
1258 call ch_logfile('Xlog', 'w')
1259 let buf = RunVimInTerminal('-S Xscript', {})
1260 call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1261
1262 call StopVimInTerminal(buf)
1263 call delete('Xscript')
1264 call ch_logfile('', '')
1265 call delete('Xlog')
1266endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001267
1268func Test_terminal_ansicolors_default()
1269 let colors = [
1270 \ '#000000', '#e00000',
1271 \ '#00e000', '#e0e000',
1272 \ '#0000e0', '#e000e0',
1273 \ '#00e0e0', '#e0e0e0',
1274 \ '#808080', '#ff4040',
1275 \ '#40ff40', '#ffff40',
1276 \ '#4040ff', '#ff40ff',
1277 \ '#40ffff', '#ffffff',
1278 \]
1279
1280 let buf = Run_shell_in_terminal({})
1281 call assert_equal(colors, term_getansicolors(buf))
1282 call Stop_shell_in_terminal(buf)
1283 call term_wait(buf)
1284
1285 exe buf . 'bwipe'
1286endfunc
1287
1288let s:test_colors = [
1289 \ '#616e64', '#0d0a79',
1290 \ '#6d610d', '#0a7373',
1291 \ '#690d0a', '#6d696e',
1292 \ '#0d0a6f', '#616e0d',
1293 \ '#0a6479', '#6d0d0a',
1294 \ '#617373', '#0d0a69',
1295 \ '#6d690d', '#0a6e6f',
1296 \ '#610d0a', '#6e6479',
1297 \]
1298
1299func Test_terminal_ansicolors_global()
1300 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1301 let buf = Run_shell_in_terminal({})
1302 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1303 call Stop_shell_in_terminal(buf)
1304 call term_wait(buf)
1305
1306 exe buf . 'bwipe'
1307 unlet g:terminal_ansi_colors
1308endfunc
1309
1310func Test_terminal_ansicolors_func()
1311 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1312 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1313 call assert_equal(s:test_colors, term_getansicolors(buf))
1314
1315 call term_setansicolors(buf, g:terminal_ansi_colors)
1316 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1317
1318 let colors = [
1319 \ 'ivory', 'AliceBlue',
1320 \ 'grey67', 'dark goldenrod',
1321 \ 'SteelBlue3', 'PaleVioletRed4',
1322 \ 'MediumPurple2', 'yellow2',
1323 \ 'RosyBrown3', 'OrangeRed2',
1324 \ 'white smoke', 'navy blue',
1325 \ 'grey47', 'gray97',
1326 \ 'MistyRose2', 'DodgerBlue4',
1327 \]
1328 call term_setansicolors(buf, colors)
1329
1330 let colors[4] = 'Invalid'
1331 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1332
1333 call Stop_shell_in_terminal(buf)
1334 call term_wait(buf)
1335 exe buf . 'bwipe'
1336endfunc