blob: 95a131be7467c078bd73412788c6e52513d7dd70 [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 Moolenaar6e72cd02018-04-14 21:31:35 +0200274func Test_terminal_scrollback()
275 let buf = Run_shell_in_terminal({})
276 set terminalscroll=100
277 call writefile(range(150), 'Xtext')
278 if has('win32')
279 call term_sendkeys(buf, "type Xtext\<CR>")
280 else
281 call term_sendkeys(buf, "cat Xtext\<CR>")
282 endif
283 let rows = term_getsize(buf)[0]
284 call WaitFor({-> term_getline(buf, rows - 1) =~ '149'})
285 let lines = line('$')
286 call assert_true(lines <= 100)
287 call assert_true(lines > 90)
288
289 call Stop_shell_in_terminal(buf)
290 call term_wait(buf)
291 exe buf . 'bwipe'
292 set terminalscroll&
293endfunc
294
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200295func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200296 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200297
Bram Moolenaarb2412082017-08-20 18:09:14 +0200298 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 let size = term_getsize('')
300 bwipe!
301 call assert_equal(5, size[0])
302
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200303 call term_start(cmd, {'term_rows': 6})
304 let size = term_getsize('')
305 bwipe!
306 call assert_equal(6, size[0])
307
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200308 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200309 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200310 call assert_equal([5, 33], term_getsize(''))
311
312 call term_setsize('', 6, 0)
313 call assert_equal([6, 33], term_getsize(''))
314
315 call term_setsize('', 0, 35)
316 call assert_equal([6, 35], term_getsize(''))
317
318 call term_setsize('', 7, 30)
319 call assert_equal([7, 30], term_getsize(''))
320
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200321 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200322 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200323
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200324 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
325 let size = term_getsize('')
326 bwipe!
327 call assert_equal([6, 36], size)
328
Bram Moolenaarb2412082017-08-20 18:09:14 +0200329 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200330 let size = term_getsize('')
331 bwipe!
332 call assert_equal(20, size[1])
333
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200334 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
335 let size = term_getsize('')
336 bwipe!
337 call assert_equal(26, size[1])
338
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200339 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200340 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200341 let size = term_getsize('')
342 bwipe!
343 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200344
345 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
346 let size = term_getsize('')
347 bwipe!
348 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200349
350 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200351endfunc
352
353func Test_terminal_curwin()
354 let cmd = Get_cat_123_cmd()
355 call assert_equal(1, winnr('$'))
356
357 split dummy
358 exe 'terminal ++curwin ' . cmd
359 call assert_equal(2, winnr('$'))
360 bwipe!
361
362 split dummy
363 call term_start(cmd, {'curwin': 1})
364 call assert_equal(2, winnr('$'))
365 bwipe!
366
367 split dummy
368 call setline(1, 'change')
369 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
370 call assert_equal(2, winnr('$'))
371 exe 'terminal! ++curwin ' . cmd
372 call assert_equal(2, winnr('$'))
373 bwipe!
374
375 split dummy
376 call setline(1, 'change')
377 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
378 call assert_equal(2, winnr('$'))
379 bwipe!
380
381 split dummy
382 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200383 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200384endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200385
Bram Moolenaarff546792017-11-21 14:47:57 +0100386func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200387 if s:python != ''
388 let cmd = s:python . " test_short_sleep.py"
389 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200390 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200391 echo 'This will take five seconds...'
392 let waittime = 2000
393 if has('win32')
394 let cmd = $windir . '\system32\timeout.exe 1'
395 else
396 let cmd = 'sleep 1'
397 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200398 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100399 return [cmd, waittime]
400endfunc
401
402func Test_terminal_finish_open_close()
403 call assert_equal(1, winnr('$'))
404
405 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200406
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100407 " shell terminal closes automatically
408 terminal
409 let buf = bufnr('%')
410 call assert_equal(2, winnr('$'))
411 " Wait for the shell to display a prompt
412 call WaitFor({-> term_getline(buf, 1) != ""})
413 call Stop_shell_in_terminal(buf)
414 call WaitFor("winnr('$') == 1", waittime)
415
416 " shell terminal that does not close automatically
417 terminal ++noclose
418 let buf = bufnr('%')
419 call assert_equal(2, winnr('$'))
420 " Wait for the shell to display a prompt
421 call WaitFor({-> term_getline(buf, 1) != ""})
422 call Stop_shell_in_terminal(buf)
423 call assert_equal(2, winnr('$'))
424 quit
425 call assert_equal(1, winnr('$'))
426
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200429 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200430 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200431
432 call term_start(cmd, {'term_finish': 'close'})
433 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200434 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200435 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436 call assert_equal(1, winnr('$'))
437
438 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200439 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200440 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 call assert_equal(2, winnr('$'))
442 bwipe
443
444 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200445 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200446 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200447 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200448 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200449
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200450 exe 'terminal ++hidden ++open ' . cmd
451 call assert_equal(1, winnr('$'))
452 call WaitFor("winnr('$') == 2", waittime)
453 call assert_equal(2, winnr('$'))
454 bwipe
455
456 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
457 call assert_equal(1, winnr('$'))
458 call WaitFor("winnr('$') == 2", waittime)
459 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200460 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200461
462 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
464 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
465 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
466
467 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200468 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200469 call WaitFor("winnr('$') == 2", waittime)
470 call assert_equal(2, winnr('$'))
471 call assert_equal(4, winheight(0))
472 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200473endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200474
475func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200476 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200477 return
478 endif
479 call mkdir('Xdir')
480 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200481 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
482 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200483
484 exe buf . 'bwipe'
485 call delete('Xdir', 'rf')
486endfunc
487
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100488func Test_terminal_servername()
489 if !has('clientserver')
490 return
491 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100492 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100493 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100494 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100495 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100496 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100497 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100498 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100499 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100500 call term_wait(buf)
501 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100502 call WaitFor('getline(2) == v:servername')
503 call assert_equal(v:servername, getline(2))
504
Bram Moolenaar012eb662018-03-13 17:55:27 +0100505 exe buf . 'bwipe'
506 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100507endfunc
508
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200509func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100510 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200511 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100512 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100513 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100514 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100515 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100516 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100517 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100518 call term_wait(buf)
519 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200520 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200521 call assert_equal('correct', getline(2))
522
Bram Moolenaar012eb662018-03-13 17:55:27 +0100523 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200524endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200525
526" must be last, we can't go back from GUI to terminal
527func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200528 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200529 return
530 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200531
532 " Ignore the "failed to create input context" error.
533 call test_ignore_error('E285:')
534
Bram Moolenaar679653e2017-08-13 14:13:19 +0200535 gui -f
536
537 call assert_equal(1, winnr('$'))
538 let buf = Run_shell_in_terminal({'term_finish': 'close'})
539 call Stop_shell_in_terminal(buf)
540 call term_wait(buf)
541
542 " closing window wipes out the terminal buffer a with finished job
543 call WaitFor("winnr('$') == 1")
544 call assert_equal(1, winnr('$'))
545 call assert_equal("", bufname(buf))
546
547 unlet g:job
548endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200549
550func Test_terminal_list_args()
551 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
552 call assert_fails(buf . 'bwipe', 'E517')
553 exe buf . 'bwipe!'
554 call assert_equal("", bufname(buf))
555endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200556
557func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100558 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200559 if has('mac')
560 " The shell or something else has a problem dealing with more than 1000
561 " characters at the same time.
562 let len = 1000
563 else
564 let len = 5000
565 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200566
567 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100568 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200569 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100570 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200571
572 " On MS-Windows there is an extra empty line below "done". Find "done" in
573 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100574 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100575 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100576 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200577 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100578 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200579 endif
580 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200581
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100582 let g:job = term_getjob(buf)
583 call Stop_shell_in_terminal(buf)
584 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200585 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200586 bwipe
587endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200588
589func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200590 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200591 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200592 endif
593 new
594 call setline(1, ['one', 'two', 'three'])
595 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200596 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200597 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200598 call assert_equal(['3', '3', '14'], nrs)
599 bwipe
600
Bram Moolenaardada6d22017-09-02 17:18:35 +0200601 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200602 call setline(1, ['one', 'two', 'three', 'four'])
603 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200604 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200605 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200606 call assert_equal(['2', '2', '10'], nrs)
607 bwipe
608
Bram Moolenaardada6d22017-09-02 17:18:35 +0200609 if executable('python')
610 new
611 call setline(1, ['print("hello")'])
612 1term ++eof=exit() python
613 " MS-Windows echoes the input, Unix doesn't.
614 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
615 if getline(1) =~ 'hello'
616 call assert_equal('hello', getline(1))
617 else
618 call assert_equal('hello', getline(line('$') - 1))
619 endif
620 bwipe
621
622 if has('win32')
623 new
624 call setline(1, ['print("hello")'])
625 1term ++eof=<C-Z> python
626 call WaitFor('getline("$") =~ "Z"')
627 call assert_equal('hello', getline(line('$') - 1))
628 bwipe
629 endif
630 endif
631
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200632 bwipe!
633endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200634
635func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200636 " Todo: make this work in the GUI
637 if !has('gui_running')
638 return
639 endif
640 let buf = term_start('NONE', {})
641 call assert_notequal(0, buf)
642
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200643 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200644 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200645 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200646 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200647 else
648 call system('echo "look here" > ' . pty)
649 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100650 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200651
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200652 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200653 bwipe!
654endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200655
656func Test_terminal_special_chars()
657 " this file name only works on Unix
658 if !has('unix')
659 return
660 endif
661 call mkdir('Xdir with spaces')
662 call writefile(['x'], 'Xdir with spaces/quoted"file')
663 term ls Xdir\ with\ spaces/quoted\"file
664 call WaitFor('term_getline("", 1) =~ "quoted"')
665 call assert_match('quoted"file', term_getline('', 1))
666 call term_wait('')
667
668 call delete('Xdir with spaces', 'rf')
669 bwipe
670endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200671
672func Test_terminal_wrong_options()
673 call assert_fails('call term_start(&shell, {
674 \ "in_io": "file",
675 \ "in_name": "xxx",
676 \ "out_io": "file",
677 \ "out_name": "xxx",
678 \ "err_io": "file",
679 \ "err_name": "xxx"
680 \ })', 'E474:')
681 call assert_fails('call term_start(&shell, {
682 \ "out_buf": bufnr("%")
683 \ })', 'E474:')
684 call assert_fails('call term_start(&shell, {
685 \ "err_buf": bufnr("%")
686 \ })', 'E474:')
687endfunc
688
689func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200690 " TODO: this should work on MS-Window
691 if has('unix')
692 let cmd = Get_cat_123_cmd()
693 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
694 call term_wait(buf)
695 call WaitFor('len(readfile("Xfile")) > 0')
696 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200697 let g:job = term_getjob(buf)
698 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200699 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200700 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200701 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200702
703 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200704 call writefile(['one line'], 'Xfile')
705 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
706 call term_wait(buf)
707 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
708 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200709 let g:job = term_getjob(buf)
710 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200711 bwipe
712 call delete('Xfile')
713 endif
714endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200715
716func TerminalTmap(remap)
717 let buf = Run_shell_in_terminal({})
718 call assert_equal('t', mode())
719
720 if a:remap
721 tmap 123 456
722 else
723 tnoremap 123 456
724 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100725 " don't use abcde, it's an existing command
726 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200727 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100728 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200729 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100730 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200731 let lnum = term_getcursor(buf)[0]
732 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100733 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200734 else
735 call assert_match('456', term_getline(buf, lnum))
736 endif
737
738 call term_sendkeys(buf, "\r")
739 call Stop_shell_in_terminal(buf)
740 call term_wait(buf)
741
742 tunmap 123
743 tunmap 456
744 call assert_equal('', maparg('123', 't'))
745 close
746 unlet g:job
747endfunc
748
749func Test_terminal_tmap()
750 call TerminalTmap(1)
751 call TerminalTmap(0)
752endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200753
754func Test_terminal_wall()
755 let buf = Run_shell_in_terminal({})
756 wall
757 call Stop_shell_in_terminal(buf)
758 call term_wait(buf)
759 exe buf . 'bwipe'
760 unlet g:job
761endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200762
Bram Moolenaar7a760922018-02-19 23:10:02 +0100763func Test_terminal_wqall()
764 let buf = Run_shell_in_terminal({})
765 call assert_fails('wqall', 'E948')
766 call Stop_shell_in_terminal(buf)
767 call term_wait(buf)
768 exe buf . 'bwipe'
769 unlet g:job
770endfunc
771
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200772func Test_terminal_composing_unicode()
773 let save_enc = &encoding
774 set encoding=utf-8
775
776 if has('win32')
777 let cmd = "cmd /K chcp 65001"
778 let lnum = [3, 6, 9]
779 else
780 let cmd = &shell
781 let lnum = [1, 3, 5]
782 endif
783
784 enew
785 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100786 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200787 call term_wait(buf, 50)
788
789 " ascii + composing
790 let txt = "a\u0308bc"
791 call term_sendkeys(buf, "echo " . txt . "\r")
792 call term_wait(buf, 50)
793 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
794 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
795 let l = term_scrape(buf, lnum[0] + 1)
796 call assert_equal("a\u0308", l[0].chars)
797 call assert_equal("b", l[1].chars)
798 call assert_equal("c", l[2].chars)
799
800 " multibyte + composing
801 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
802 call term_sendkeys(buf, "echo " . txt . "\r")
803 call term_wait(buf, 50)
804 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
805 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
806 let l = term_scrape(buf, lnum[1] + 1)
807 call assert_equal("\u304b\u3099", l[0].chars)
808 call assert_equal("\u304e", l[1].chars)
809 call assert_equal("\u304f\u3099", l[2].chars)
810 call assert_equal("\u3052", l[3].chars)
811 call assert_equal("\u3053\u3099", l[4].chars)
812
813 " \u00a0 + composing
814 let txt = "abc\u00a0\u0308"
815 call term_sendkeys(buf, "echo " . txt . "\r")
816 call term_wait(buf, 50)
817 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
818 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
819 let l = term_scrape(buf, lnum[2] + 1)
820 call assert_equal("\u00a0\u0308", l[3].chars)
821
822 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100823 call WaitFor('job_status(g:job) == "dead"')
824 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200825 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100826 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200827 let &encoding = save_enc
828endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100829
830func Test_terminal_aucmd_on_close()
831 fun Nop()
832 let s:called = 1
833 endfun
834
835 aug repro
836 au!
837 au BufWinLeave * call Nop()
838 aug END
839
840 let [cmd, waittime] = s:get_sleep_cmd()
841
842 call assert_equal(1, winnr('$'))
843 new
844 call setline(1, ['one', 'two'])
845 exe 'term ++close ' . cmd
846 wincmd p
847 call WaitFor("winnr('$') == 2", waittime)
848 call assert_equal(1, s:called)
849 bwipe!
850
851 unlet s:called
852 au! repro
853 delfunc Nop
854endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100855
856func Test_terminal_term_start_empty_command()
857 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
858 call assert_fails(cmd, 'E474')
859 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
860 call assert_fails(cmd, 'E474')
861 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
862 call assert_fails(cmd, 'E474')
863 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
864 call assert_fails(cmd, 'E474')
865endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100866
867func Test_terminal_response_to_control_sequence()
868 if !has('unix')
869 return
870 endif
871
872 let buf = Run_shell_in_terminal({})
Bram Moolenaar086eb872018-03-25 21:24:12 +0200873 call WaitFor({-> term_getline(buf, 1) != ''})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100874
Bram Moolenaar086eb872018-03-25 21:24:12 +0200875 call term_sendkeys(buf, "cat\<CR>")
876 call WaitFor({-> term_getline(buf, 1) =~ 'cat'})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100877
Bram Moolenaar086eb872018-03-25 21:24:12 +0200878 " Request the cursor position.
879 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100880
881 " Wait for output from tty to display, below an empty line.
Bram Moolenaar086eb872018-03-25 21:24:12 +0200882 call WaitFor({-> term_getline(buf, 4) =~ '3;1R'})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100883
Bram Moolenaar086eb872018-03-25 21:24:12 +0200884 " End "cat" gently.
885 call term_sendkeys(buf, "\<CR>\<C-D>")
886
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100887 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100888 exe buf . 'bwipe'
889 unlet g:job
890endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100891
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100892" Run Vim, start a terminal in that Vim with the kill argument,
893" :qall works.
894func Run_terminal_qall_kill(line1, line2)
895 " 1. Open a terminal window and wait for the prompt to appear
896 " 2. set kill using term_setkill()
897 " 3. make Vim exit, it will kill the shell
898 let after = [
899 \ a:line1,
900 \ 'let buf = bufnr("%")',
901 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
902 \ ' sleep 10m',
903 \ 'endwhile',
904 \ a:line2,
905 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
906 \ 'qall',
907 \ ]
908 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100909 return
910 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100911 call assert_equal("done", readfile("Xdone")[0])
912 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100913endfunc
914
915" Run Vim in a terminal, then start a terminal in that Vim with a kill
916" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100917func Test_terminal_qall_kill_arg()
918 call Run_terminal_qall_kill('term ++kill=kill', '')
919endfunc
920
921" Run Vim, start a terminal in that Vim, set the kill argument with
922" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100923func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100924 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
925endfunc
926
927" Run Vim, start a terminal in that Vim without the kill argument,
928" check that :qall does not exit, :qall! does.
929func Test_terminal_qall_exit()
930 let after = [
931 \ 'term',
932 \ 'let buf = bufnr("%")',
933 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
934 \ ' sleep 10m',
935 \ 'endwhile',
936 \ 'set nomore',
937 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
938 \ 'qall',
939 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
940 \ 'cquit',
941 \ ]
942 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100943 return
944 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100945 call assert_equal("done", readfile("Xdone")[0])
946 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100947endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100948
949" Run Vim in a terminal, then start a terminal in that Vim without a kill
950" argument, check that :confirm qall works.
951func Test_terminal_qall_prompt()
952 if !CanRunVimInTerminal()
953 return
954 endif
955 let buf = RunVimInTerminal('', {})
956
957 " Open a terminal window and wait for the prompt to appear
958 call term_sendkeys(buf, ":term\<CR>")
959 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
960 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
961
962 " make Vim exit, it will prompt to kill the shell
963 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
964 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
965 call term_sendkeys(buf, "y")
966 call WaitFor({-> term_getstatus(buf) == "finished"})
967
968 " close the terminal window where Vim was running
969 quit
970endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100971
Bram Moolenaar012eb662018-03-13 17:55:27 +0100972func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100973 augroup repro
974 au!
975 au TerminalOpen * let s:called += 1
976 augroup END
977
978 let s:called = 0
979
980 " Open a terminal window with :terminal
981 terminal
982 call assert_equal(1, s:called)
983 bwipe!
984
985 " Open a terminal window with term_start()
986 call term_start(&shell)
987 call assert_equal(2, s:called)
988 bwipe!
989
990 " Open a hidden terminal buffer with :terminal
991 terminal ++hidden
992 call assert_equal(3, s:called)
993 for buf in term_list()
994 exe buf . "bwipe!"
995 endfor
996
997 " Open a hidden terminal buffer with term_start()
998 let buf = term_start(&shell, {'hidden': 1})
999 call assert_equal(4, s:called)
1000 exe buf . "bwipe!"
1001
1002 unlet s:called
1003 au! repro
1004endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001005
1006func Check_dump01(off)
1007 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1008 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001009 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001010endfunc
1011
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001012func Test_terminal_dumpwrite_composing()
1013 if !CanRunVimInTerminal()
1014 return
1015 endif
1016 let save_enc = &encoding
1017 set encoding=utf-8
1018 call assert_equal(1, winnr('$'))
1019
1020 let text = " a\u0300 e\u0302 o\u0308"
1021 call writefile([text], 'Xcomposing')
1022 let buf = RunVimInTerminal('Xcomposing', {})
1023 call WaitFor({-> term_getline(buf, 1) =~ text})
1024 call term_dumpwrite(buf, 'Xdump')
1025 let dumpline = readfile('Xdump')[0]
1026 call assert_match('|à| |ê| |ö', dumpline)
1027
1028 call StopVimInTerminal(buf)
1029 call delete('Xcomposing')
1030 call delete('Xdump')
1031 let &encoding = save_enc
1032endfunc
1033
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001034" just testing basic functionality.
1035func Test_terminal_dumpload()
1036 call assert_equal(1, winnr('$'))
1037 call term_dumpload('dumps/Test_popup_command_01.dump')
1038 call assert_equal(2, winnr('$'))
1039 call assert_equal(20, line('$'))
1040 call Check_dump01(0)
1041 quit
1042endfunc
1043
1044func Test_terminal_dumpdiff()
1045 call assert_equal(1, winnr('$'))
1046 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1047 call assert_equal(2, winnr('$'))
1048 call assert_equal(62, line('$'))
1049 call Check_dump01(0)
1050 call Check_dump01(42)
1051 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1052 quit
1053endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001054
1055func Test_terminal_dumpdiff_options()
1056 set laststatus=0
1057 call assert_equal(1, winnr('$'))
1058 let height = winheight(0)
1059 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1060 call assert_equal(2, winnr('$'))
1061 call assert_equal(height, winheight(winnr()))
1062 call assert_equal(33, winwidth(winnr()))
1063 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1064 quit
1065
1066 call assert_equal(1, winnr('$'))
1067 let width = winwidth(0)
1068 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1069 call assert_equal(2, winnr('$'))
1070 call assert_equal(width, winwidth(winnr()))
1071 call assert_equal(13, winheight(winnr()))
1072 call assert_equal('something else', bufname('%'))
1073 quit
1074
1075 call assert_equal(1, winnr('$'))
1076 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1077 call assert_equal(1, winnr('$'))
1078 bwipe
1079
1080 set laststatus&
1081endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001082
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001083func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001084 call assert_equal(1, winnr('$'))
1085
1086 " Use the title termcap entries to output the escape sequence.
1087 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001088 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001089 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001090 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001091 \ 'redraw',
1092 \ "set t_ts=",
1093 \ ], 'Xscript')
1094 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001095 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001096 call assert_equal('Xtextfile', expand('%:t'))
1097 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001098 return buf
1099endfunc
1100
1101func Test_terminal_api_drop_newwin()
1102 if !CanRunVimInTerminal()
1103 return
1104 endif
1105 let buf = Api_drop_common('')
1106 call assert_equal(0, &bin)
1107 call assert_equal('', &fenc)
1108
1109 call StopVimInTerminal(buf)
1110 call delete('Xscript')
1111 bwipe Xtextfile
1112endfunc
1113
1114func Test_terminal_api_drop_newwin_bin()
1115 if !CanRunVimInTerminal()
1116 return
1117 endif
1118 let buf = Api_drop_common(',{"bin":1}')
1119 call assert_equal(1, &bin)
1120
1121 call StopVimInTerminal(buf)
1122 call delete('Xscript')
1123 bwipe Xtextfile
1124endfunc
1125
1126func Test_terminal_api_drop_newwin_binary()
1127 if !CanRunVimInTerminal()
1128 return
1129 endif
1130 let buf = Api_drop_common(',{"binary":1}')
1131 call assert_equal(1, &bin)
1132
1133 call StopVimInTerminal(buf)
1134 call delete('Xscript')
1135 bwipe Xtextfile
1136endfunc
1137
1138func Test_terminal_api_drop_newwin_nobin()
1139 if !CanRunVimInTerminal()
1140 return
1141 endif
1142 set binary
1143 let buf = Api_drop_common(',{"nobin":1}')
1144 call assert_equal(0, &bin)
1145
1146 call StopVimInTerminal(buf)
1147 call delete('Xscript')
1148 bwipe Xtextfile
1149 set nobinary
1150endfunc
1151
1152func Test_terminal_api_drop_newwin_nobinary()
1153 if !CanRunVimInTerminal()
1154 return
1155 endif
1156 set binary
1157 let buf = Api_drop_common(',{"nobinary":1}')
1158 call assert_equal(0, &bin)
1159
1160 call StopVimInTerminal(buf)
1161 call delete('Xscript')
1162 bwipe Xtextfile
1163 set nobinary
1164endfunc
1165
1166func Test_terminal_api_drop_newwin_ff()
1167 if !CanRunVimInTerminal()
1168 return
1169 endif
1170 let buf = Api_drop_common(',{"ff":"dos"}')
1171 call assert_equal("dos", &ff)
1172
1173 call StopVimInTerminal(buf)
1174 call delete('Xscript')
1175 bwipe Xtextfile
1176endfunc
1177
1178func Test_terminal_api_drop_newwin_fileformat()
1179 if !CanRunVimInTerminal()
1180 return
1181 endif
1182 let buf = Api_drop_common(',{"fileformat":"dos"}')
1183 call assert_equal("dos", &ff)
1184
1185 call StopVimInTerminal(buf)
1186 call delete('Xscript')
1187 bwipe Xtextfile
1188endfunc
1189
1190func Test_terminal_api_drop_newwin_enc()
1191 if !CanRunVimInTerminal()
1192 return
1193 endif
1194 let buf = Api_drop_common(',{"enc":"utf-16"}')
1195 call assert_equal("utf-16", &fenc)
1196
1197 call StopVimInTerminal(buf)
1198 call delete('Xscript')
1199 bwipe Xtextfile
1200endfunc
1201
1202func Test_terminal_api_drop_newwin_encoding()
1203 if !CanRunVimInTerminal()
1204 return
1205 endif
1206 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1207 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001208
1209 call StopVimInTerminal(buf)
1210 call delete('Xscript')
1211 bwipe Xtextfile
1212endfunc
1213
1214func Test_terminal_api_drop_oldwin()
1215 if !CanRunVimInTerminal()
1216 return
1217 endif
1218 let firstwinid = win_getid()
1219 split Xtextfile
1220 let textfile_winid = win_getid()
1221 call assert_equal(2, winnr('$'))
1222 call win_gotoid(firstwinid)
1223
1224 " Use the title termcap entries to output the escape sequence.
1225 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001226 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001227 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1228 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1229 \ 'redraw',
1230 \ "set t_ts=",
1231 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001232 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001233 call WaitFor({-> expand('%:t') =='Xtextfile'})
1234 call assert_equal(textfile_winid, win_getid())
1235
1236 call StopVimInTerminal(buf)
1237 call delete('Xscript')
1238 bwipe Xtextfile
1239endfunc
1240
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001241func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001242 let g:called_bufnum = a:bufnum
1243 let g:called_arg = a:arg
1244endfunc
1245
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001246func WriteApiCall(funcname)
1247 " Use the title termcap entries to output the escape sequence.
1248 call writefile([
1249 \ 'set title',
1250 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1251 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1252 \ 'redraw',
1253 \ "set t_ts=",
1254 \ ], 'Xscript')
1255endfunc
1256
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001257func Test_terminal_api_call()
1258 if !CanRunVimInTerminal()
1259 return
1260 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001261
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001262 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001263 let buf = RunVimInTerminal('-S Xscript', {})
1264 call WaitFor({-> exists('g:called_bufnum')})
1265 call assert_equal(buf, g:called_bufnum)
1266 call assert_equal(['hello', 123], g:called_arg)
1267
1268 call StopVimInTerminal(buf)
1269 call delete('Xscript')
1270 unlet g:called_bufnum
1271 unlet g:called_arg
1272endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001273
1274func Test_terminal_api_call_fails()
1275 if !CanRunVimInTerminal()
1276 return
1277 endif
1278
1279 call WriteApiCall('TryThis')
1280 call ch_logfile('Xlog', 'w')
1281 let buf = RunVimInTerminal('-S Xscript', {})
1282 call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1283
1284 call StopVimInTerminal(buf)
1285 call delete('Xscript')
1286 call ch_logfile('', '')
1287 call delete('Xlog')
1288endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001289
1290func Test_terminal_ansicolors_default()
1291 let colors = [
1292 \ '#000000', '#e00000',
1293 \ '#00e000', '#e0e000',
1294 \ '#0000e0', '#e000e0',
1295 \ '#00e0e0', '#e0e0e0',
1296 \ '#808080', '#ff4040',
1297 \ '#40ff40', '#ffff40',
1298 \ '#4040ff', '#ff40ff',
1299 \ '#40ffff', '#ffffff',
1300 \]
1301
1302 let buf = Run_shell_in_terminal({})
1303 call assert_equal(colors, term_getansicolors(buf))
1304 call Stop_shell_in_terminal(buf)
1305 call term_wait(buf)
1306
1307 exe buf . 'bwipe'
1308endfunc
1309
1310let s:test_colors = [
1311 \ '#616e64', '#0d0a79',
1312 \ '#6d610d', '#0a7373',
1313 \ '#690d0a', '#6d696e',
1314 \ '#0d0a6f', '#616e0d',
1315 \ '#0a6479', '#6d0d0a',
1316 \ '#617373', '#0d0a69',
1317 \ '#6d690d', '#0a6e6f',
1318 \ '#610d0a', '#6e6479',
1319 \]
1320
1321func Test_terminal_ansicolors_global()
1322 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1323 let buf = Run_shell_in_terminal({})
1324 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1325 call Stop_shell_in_terminal(buf)
1326 call term_wait(buf)
1327
1328 exe buf . 'bwipe'
1329 unlet g:terminal_ansi_colors
1330endfunc
1331
1332func Test_terminal_ansicolors_func()
1333 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1334 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1335 call assert_equal(s:test_colors, term_getansicolors(buf))
1336
1337 call term_setansicolors(buf, g:terminal_ansi_colors)
1338 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1339
1340 let colors = [
1341 \ 'ivory', 'AliceBlue',
1342 \ 'grey67', 'dark goldenrod',
1343 \ 'SteelBlue3', 'PaleVioletRed4',
1344 \ 'MediumPurple2', 'yellow2',
1345 \ 'RosyBrown3', 'OrangeRed2',
1346 \ 'white smoke', 'navy blue',
1347 \ 'grey47', 'gray97',
1348 \ 'MistyRose2', 'DodgerBlue4',
1349 \]
1350 call term_setansicolors(buf, colors)
1351
1352 let colors[4] = 'Invalid'
1353 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1354
1355 call Stop_shell_in_terminal(buf)
1356 call term_wait(buf)
1357 exe buf . 'bwipe'
1358endfunc