blob: 6e101cfc9bd736e10316024fc64681ea88f10d93 [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('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200286 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200287
288 call Stop_shell_in_terminal(buf)
289 call term_wait(buf)
290 exe buf . 'bwipe'
291 set terminalscroll&
292endfunc
293
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200294func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200295 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200296
Bram Moolenaarb2412082017-08-20 18:09:14 +0200297 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 let size = term_getsize('')
299 bwipe!
300 call assert_equal(5, size[0])
301
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200302 call term_start(cmd, {'term_rows': 6})
303 let size = term_getsize('')
304 bwipe!
305 call assert_equal(6, size[0])
306
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200308 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200309 call assert_equal([5, 33], term_getsize(''))
310
311 call term_setsize('', 6, 0)
312 call assert_equal([6, 33], term_getsize(''))
313
314 call term_setsize('', 0, 35)
315 call assert_equal([6, 35], term_getsize(''))
316
317 call term_setsize('', 7, 30)
318 call assert_equal([7, 30], term_getsize(''))
319
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200320 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200321 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200322
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200323 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
324 let size = term_getsize('')
325 bwipe!
326 call assert_equal([6, 36], size)
327
Bram Moolenaarb2412082017-08-20 18:09:14 +0200328 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200329 let size = term_getsize('')
330 bwipe!
331 call assert_equal(20, size[1])
332
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200333 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
334 let size = term_getsize('')
335 bwipe!
336 call assert_equal(26, size[1])
337
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200338 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200339 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200340 let size = term_getsize('')
341 bwipe!
342 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200343
344 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
345 let size = term_getsize('')
346 bwipe!
347 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200348
349 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200350endfunc
351
352func Test_terminal_curwin()
353 let cmd = Get_cat_123_cmd()
354 call assert_equal(1, winnr('$'))
355
356 split dummy
357 exe 'terminal ++curwin ' . cmd
358 call assert_equal(2, winnr('$'))
359 bwipe!
360
361 split dummy
362 call term_start(cmd, {'curwin': 1})
363 call assert_equal(2, winnr('$'))
364 bwipe!
365
366 split dummy
367 call setline(1, 'change')
368 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
369 call assert_equal(2, winnr('$'))
370 exe 'terminal! ++curwin ' . cmd
371 call assert_equal(2, winnr('$'))
372 bwipe!
373
374 split dummy
375 call setline(1, 'change')
376 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
377 call assert_equal(2, winnr('$'))
378 bwipe!
379
380 split dummy
381 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200382 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200383endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200384
Bram Moolenaarff546792017-11-21 14:47:57 +0100385func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200386 if s:python != ''
387 let cmd = s:python . " test_short_sleep.py"
388 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200390 echo 'This will take five seconds...'
391 let waittime = 2000
392 if has('win32')
393 let cmd = $windir . '\system32\timeout.exe 1'
394 else
395 let cmd = 'sleep 1'
396 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200397 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100398 return [cmd, waittime]
399endfunc
400
401func Test_terminal_finish_open_close()
402 call assert_equal(1, winnr('$'))
403
404 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200405
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100406 " shell terminal closes automatically
407 terminal
408 let buf = bufnr('%')
409 call assert_equal(2, winnr('$'))
410 " Wait for the shell to display a prompt
411 call WaitFor({-> term_getline(buf, 1) != ""})
412 call Stop_shell_in_terminal(buf)
413 call WaitFor("winnr('$') == 1", waittime)
414
415 " shell terminal that does not close automatically
416 terminal ++noclose
417 let buf = bufnr('%')
418 call assert_equal(2, winnr('$'))
419 " Wait for the shell to display a prompt
420 call WaitFor({-> term_getline(buf, 1) != ""})
421 call Stop_shell_in_terminal(buf)
422 call assert_equal(2, winnr('$'))
423 quit
424 call assert_equal(1, winnr('$'))
425
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200426 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200429 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200430
431 call term_start(cmd, {'term_finish': 'close'})
432 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200434 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200435 call assert_equal(1, winnr('$'))
436
437 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200438 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200439 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200440 call assert_equal(2, winnr('$'))
441 bwipe
442
443 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200444 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200445 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200447 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200449 exe 'terminal ++hidden ++open ' . cmd
450 call assert_equal(1, winnr('$'))
451 call WaitFor("winnr('$') == 2", waittime)
452 call assert_equal(2, winnr('$'))
453 bwipe
454
455 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
456 call assert_equal(1, winnr('$'))
457 call WaitFor("winnr('$') == 2", waittime)
458 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200459 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200460
461 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
462 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
464 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
465
466 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200467 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200468 call WaitFor("winnr('$') == 2", waittime)
469 call assert_equal(2, winnr('$'))
470 call assert_equal(4, winheight(0))
471 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200472endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200473
474func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200475 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200476 return
477 endif
478 call mkdir('Xdir')
479 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200480 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
481 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200482
483 exe buf . 'bwipe'
484 call delete('Xdir', 'rf')
485endfunc
486
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100487func Test_terminal_servername()
488 if !has('clientserver')
489 return
490 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100491 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100492 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100493 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100494 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100495 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100496 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100497 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100498 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100499 call term_wait(buf)
500 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100501 call WaitFor('getline(2) == v:servername')
502 call assert_equal(v:servername, getline(2))
503
Bram Moolenaar012eb662018-03-13 17:55:27 +0100504 exe buf . 'bwipe'
505 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100506endfunc
507
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200508func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100509 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200510 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100511 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100512 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100513 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100514 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100515 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100516 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100517 call term_wait(buf)
518 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200519 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200520 call assert_equal('correct', getline(2))
521
Bram Moolenaar012eb662018-03-13 17:55:27 +0100522 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200523endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200524
525" must be last, we can't go back from GUI to terminal
526func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200527 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200528 return
529 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200530
531 " Ignore the "failed to create input context" error.
532 call test_ignore_error('E285:')
533
Bram Moolenaar679653e2017-08-13 14:13:19 +0200534 gui -f
535
536 call assert_equal(1, winnr('$'))
537 let buf = Run_shell_in_terminal({'term_finish': 'close'})
538 call Stop_shell_in_terminal(buf)
539 call term_wait(buf)
540
541 " closing window wipes out the terminal buffer a with finished job
542 call WaitFor("winnr('$') == 1")
543 call assert_equal(1, winnr('$'))
544 call assert_equal("", bufname(buf))
545
546 unlet g:job
547endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200548
549func Test_terminal_list_args()
550 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
551 call assert_fails(buf . 'bwipe', 'E517')
552 exe buf . 'bwipe!'
553 call assert_equal("", bufname(buf))
554endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200555
556func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100557 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200558 if has('mac')
559 " The shell or something else has a problem dealing with more than 1000
560 " characters at the same time.
561 let len = 1000
562 else
563 let len = 5000
564 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200565
566 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100567 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200568 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100569 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200570
571 " On MS-Windows there is an extra empty line below "done". Find "done" in
572 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100573 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100574 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100575 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200576 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100577 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200578 endif
579 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200580
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100581 let g:job = term_getjob(buf)
582 call Stop_shell_in_terminal(buf)
583 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200584 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200585 bwipe
586endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200587
588func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200589 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200590 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200591 endif
592 new
593 call setline(1, ['one', 'two', 'three'])
594 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200595 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200596 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200597 call assert_equal(['3', '3', '14'], nrs)
598 bwipe
599
Bram Moolenaardada6d22017-09-02 17:18:35 +0200600 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200601 call setline(1, ['one', 'two', 'three', 'four'])
602 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200603 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200604 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200605 call assert_equal(['2', '2', '10'], nrs)
606 bwipe
607
Bram Moolenaardada6d22017-09-02 17:18:35 +0200608 if executable('python')
609 new
610 call setline(1, ['print("hello")'])
611 1term ++eof=exit() python
612 " MS-Windows echoes the input, Unix doesn't.
613 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
614 if getline(1) =~ 'hello'
615 call assert_equal('hello', getline(1))
616 else
617 call assert_equal('hello', getline(line('$') - 1))
618 endif
619 bwipe
620
621 if has('win32')
622 new
623 call setline(1, ['print("hello")'])
624 1term ++eof=<C-Z> python
625 call WaitFor('getline("$") =~ "Z"')
626 call assert_equal('hello', getline(line('$') - 1))
627 bwipe
628 endif
629 endif
630
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200631 bwipe!
632endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200633
634func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200635 " Todo: make this work in the GUI
636 if !has('gui_running')
637 return
638 endif
639 let buf = term_start('NONE', {})
640 call assert_notequal(0, buf)
641
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200642 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200643 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200644 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200645 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200646 else
647 call system('echo "look here" > ' . pty)
648 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100649 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200650
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200651 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200652 bwipe!
653endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200654
655func Test_terminal_special_chars()
656 " this file name only works on Unix
657 if !has('unix')
658 return
659 endif
660 call mkdir('Xdir with spaces')
661 call writefile(['x'], 'Xdir with spaces/quoted"file')
662 term ls Xdir\ with\ spaces/quoted\"file
663 call WaitFor('term_getline("", 1) =~ "quoted"')
664 call assert_match('quoted"file', term_getline('', 1))
665 call term_wait('')
666
667 call delete('Xdir with spaces', 'rf')
668 bwipe
669endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200670
671func Test_terminal_wrong_options()
672 call assert_fails('call term_start(&shell, {
673 \ "in_io": "file",
674 \ "in_name": "xxx",
675 \ "out_io": "file",
676 \ "out_name": "xxx",
677 \ "err_io": "file",
678 \ "err_name": "xxx"
679 \ })', 'E474:')
680 call assert_fails('call term_start(&shell, {
681 \ "out_buf": bufnr("%")
682 \ })', 'E474:')
683 call assert_fails('call term_start(&shell, {
684 \ "err_buf": bufnr("%")
685 \ })', 'E474:')
686endfunc
687
688func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200689 " TODO: this should work on MS-Window
690 if has('unix')
691 let cmd = Get_cat_123_cmd()
692 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
693 call term_wait(buf)
694 call WaitFor('len(readfile("Xfile")) > 0')
695 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200696 let g:job = term_getjob(buf)
697 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200698 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200699 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200700 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200701
702 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200703 call writefile(['one line'], 'Xfile')
704 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
705 call term_wait(buf)
706 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
707 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200708 let g:job = term_getjob(buf)
709 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200710 bwipe
711 call delete('Xfile')
712 endif
713endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200714
715func TerminalTmap(remap)
716 let buf = Run_shell_in_terminal({})
717 call assert_equal('t', mode())
718
719 if a:remap
720 tmap 123 456
721 else
722 tnoremap 123 456
723 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100724 " don't use abcde, it's an existing command
725 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200726 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100727 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200728 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100729 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200730 let lnum = term_getcursor(buf)[0]
731 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100732 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200733 else
734 call assert_match('456', term_getline(buf, lnum))
735 endif
736
737 call term_sendkeys(buf, "\r")
738 call Stop_shell_in_terminal(buf)
739 call term_wait(buf)
740
741 tunmap 123
742 tunmap 456
743 call assert_equal('', maparg('123', 't'))
744 close
745 unlet g:job
746endfunc
747
748func Test_terminal_tmap()
749 call TerminalTmap(1)
750 call TerminalTmap(0)
751endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200752
753func Test_terminal_wall()
754 let buf = Run_shell_in_terminal({})
755 wall
756 call Stop_shell_in_terminal(buf)
757 call term_wait(buf)
758 exe buf . 'bwipe'
759 unlet g:job
760endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200761
Bram Moolenaar7a760922018-02-19 23:10:02 +0100762func Test_terminal_wqall()
763 let buf = Run_shell_in_terminal({})
764 call assert_fails('wqall', 'E948')
765 call Stop_shell_in_terminal(buf)
766 call term_wait(buf)
767 exe buf . 'bwipe'
768 unlet g:job
769endfunc
770
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200771func Test_terminal_composing_unicode()
772 let save_enc = &encoding
773 set encoding=utf-8
774
775 if has('win32')
776 let cmd = "cmd /K chcp 65001"
777 let lnum = [3, 6, 9]
778 else
779 let cmd = &shell
780 let lnum = [1, 3, 5]
781 endif
782
783 enew
784 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100785 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200786 call term_wait(buf, 50)
787
788 " ascii + composing
789 let txt = "a\u0308bc"
790 call term_sendkeys(buf, "echo " . txt . "\r")
791 call term_wait(buf, 50)
792 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
793 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
794 let l = term_scrape(buf, lnum[0] + 1)
795 call assert_equal("a\u0308", l[0].chars)
796 call assert_equal("b", l[1].chars)
797 call assert_equal("c", l[2].chars)
798
799 " multibyte + composing
800 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
801 call term_sendkeys(buf, "echo " . txt . "\r")
802 call term_wait(buf, 50)
803 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
804 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
805 let l = term_scrape(buf, lnum[1] + 1)
806 call assert_equal("\u304b\u3099", l[0].chars)
807 call assert_equal("\u304e", l[1].chars)
808 call assert_equal("\u304f\u3099", l[2].chars)
809 call assert_equal("\u3052", l[3].chars)
810 call assert_equal("\u3053\u3099", l[4].chars)
811
812 " \u00a0 + composing
813 let txt = "abc\u00a0\u0308"
814 call term_sendkeys(buf, "echo " . txt . "\r")
815 call term_wait(buf, 50)
816 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
817 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
818 let l = term_scrape(buf, lnum[2] + 1)
819 call assert_equal("\u00a0\u0308", l[3].chars)
820
821 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100822 call WaitFor('job_status(g:job) == "dead"')
823 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200824 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100825 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200826 let &encoding = save_enc
827endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100828
829func Test_terminal_aucmd_on_close()
830 fun Nop()
831 let s:called = 1
832 endfun
833
834 aug repro
835 au!
836 au BufWinLeave * call Nop()
837 aug END
838
839 let [cmd, waittime] = s:get_sleep_cmd()
840
841 call assert_equal(1, winnr('$'))
842 new
843 call setline(1, ['one', 'two'])
844 exe 'term ++close ' . cmd
845 wincmd p
846 call WaitFor("winnr('$') == 2", waittime)
847 call assert_equal(1, s:called)
848 bwipe!
849
850 unlet s:called
851 au! repro
852 delfunc Nop
853endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100854
855func Test_terminal_term_start_empty_command()
856 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
857 call assert_fails(cmd, 'E474')
858 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
859 call assert_fails(cmd, 'E474')
860 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
861 call assert_fails(cmd, 'E474')
862 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
863 call assert_fails(cmd, 'E474')
864endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100865
866func Test_terminal_response_to_control_sequence()
867 if !has('unix')
868 return
869 endif
870
871 let buf = Run_shell_in_terminal({})
Bram Moolenaar086eb872018-03-25 21:24:12 +0200872 call WaitFor({-> term_getline(buf, 1) != ''})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100873
Bram Moolenaar086eb872018-03-25 21:24:12 +0200874 call term_sendkeys(buf, "cat\<CR>")
875 call WaitFor({-> term_getline(buf, 1) =~ 'cat'})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100876
Bram Moolenaar086eb872018-03-25 21:24:12 +0200877 " Request the cursor position.
878 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100879
880 " Wait for output from tty to display, below an empty line.
Bram Moolenaar086eb872018-03-25 21:24:12 +0200881 call WaitFor({-> term_getline(buf, 4) =~ '3;1R'})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100882
Bram Moolenaar086eb872018-03-25 21:24:12 +0200883 " End "cat" gently.
884 call term_sendkeys(buf, "\<CR>\<C-D>")
885
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100886 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100887 exe buf . 'bwipe'
888 unlet g:job
889endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100890
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100891" Run Vim, start a terminal in that Vim with the kill argument,
892" :qall works.
893func Run_terminal_qall_kill(line1, line2)
894 " 1. Open a terminal window and wait for the prompt to appear
895 " 2. set kill using term_setkill()
896 " 3. make Vim exit, it will kill the shell
897 let after = [
898 \ a:line1,
899 \ 'let buf = bufnr("%")',
900 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
901 \ ' sleep 10m',
902 \ 'endwhile',
903 \ a:line2,
904 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
905 \ 'qall',
906 \ ]
907 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100908 return
909 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100910 call assert_equal("done", readfile("Xdone")[0])
911 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100912endfunc
913
914" Run Vim in a terminal, then start a terminal in that Vim with a kill
915" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100916func Test_terminal_qall_kill_arg()
917 call Run_terminal_qall_kill('term ++kill=kill', '')
918endfunc
919
920" Run Vim, start a terminal in that Vim, set the kill argument with
921" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100922func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100923 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
924endfunc
925
926" Run Vim, start a terminal in that Vim without the kill argument,
927" check that :qall does not exit, :qall! does.
928func Test_terminal_qall_exit()
929 let after = [
930 \ 'term',
931 \ 'let buf = bufnr("%")',
932 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
933 \ ' sleep 10m',
934 \ 'endwhile',
935 \ 'set nomore',
936 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
937 \ 'qall',
938 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
939 \ 'cquit',
940 \ ]
941 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100942 return
943 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100944 call assert_equal("done", readfile("Xdone")[0])
945 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100946endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100947
948" Run Vim in a terminal, then start a terminal in that Vim without a kill
949" argument, check that :confirm qall works.
950func Test_terminal_qall_prompt()
951 if !CanRunVimInTerminal()
952 return
953 endif
954 let buf = RunVimInTerminal('', {})
955
956 " Open a terminal window and wait for the prompt to appear
957 call term_sendkeys(buf, ":term\<CR>")
958 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
959 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
960
961 " make Vim exit, it will prompt to kill the shell
962 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
963 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
964 call term_sendkeys(buf, "y")
965 call WaitFor({-> term_getstatus(buf) == "finished"})
966
967 " close the terminal window where Vim was running
968 quit
969endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100970
Bram Moolenaar012eb662018-03-13 17:55:27 +0100971func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100972 augroup repro
973 au!
974 au TerminalOpen * let s:called += 1
975 augroup END
976
977 let s:called = 0
978
979 " Open a terminal window with :terminal
980 terminal
981 call assert_equal(1, s:called)
982 bwipe!
983
984 " Open a terminal window with term_start()
985 call term_start(&shell)
986 call assert_equal(2, s:called)
987 bwipe!
988
989 " Open a hidden terminal buffer with :terminal
990 terminal ++hidden
991 call assert_equal(3, s:called)
992 for buf in term_list()
993 exe buf . "bwipe!"
994 endfor
995
996 " Open a hidden terminal buffer with term_start()
997 let buf = term_start(&shell, {'hidden': 1})
998 call assert_equal(4, s:called)
999 exe buf . "bwipe!"
1000
1001 unlet s:called
1002 au! repro
1003endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001004
1005func Check_dump01(off)
1006 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1007 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001008 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001009endfunc
1010
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001011func Test_terminal_dumpwrite_composing()
1012 if !CanRunVimInTerminal()
1013 return
1014 endif
1015 let save_enc = &encoding
1016 set encoding=utf-8
1017 call assert_equal(1, winnr('$'))
1018
1019 let text = " a\u0300 e\u0302 o\u0308"
1020 call writefile([text], 'Xcomposing')
1021 let buf = RunVimInTerminal('Xcomposing', {})
1022 call WaitFor({-> term_getline(buf, 1) =~ text})
1023 call term_dumpwrite(buf, 'Xdump')
1024 let dumpline = readfile('Xdump')[0]
1025 call assert_match('|à| |ê| |ö', dumpline)
1026
1027 call StopVimInTerminal(buf)
1028 call delete('Xcomposing')
1029 call delete('Xdump')
1030 let &encoding = save_enc
1031endfunc
1032
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001033" just testing basic functionality.
1034func Test_terminal_dumpload()
1035 call assert_equal(1, winnr('$'))
1036 call term_dumpload('dumps/Test_popup_command_01.dump')
1037 call assert_equal(2, winnr('$'))
1038 call assert_equal(20, line('$'))
1039 call Check_dump01(0)
1040 quit
1041endfunc
1042
1043func Test_terminal_dumpdiff()
1044 call assert_equal(1, winnr('$'))
1045 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1046 call assert_equal(2, winnr('$'))
1047 call assert_equal(62, line('$'))
1048 call Check_dump01(0)
1049 call Check_dump01(42)
1050 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1051 quit
1052endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001053
1054func Test_terminal_dumpdiff_options()
1055 set laststatus=0
1056 call assert_equal(1, winnr('$'))
1057 let height = winheight(0)
1058 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1059 call assert_equal(2, winnr('$'))
1060 call assert_equal(height, winheight(winnr()))
1061 call assert_equal(33, winwidth(winnr()))
1062 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1063 quit
1064
1065 call assert_equal(1, winnr('$'))
1066 let width = winwidth(0)
1067 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1068 call assert_equal(2, winnr('$'))
1069 call assert_equal(width, winwidth(winnr()))
1070 call assert_equal(13, winheight(winnr()))
1071 call assert_equal('something else', bufname('%'))
1072 quit
1073
1074 call assert_equal(1, winnr('$'))
1075 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1076 call assert_equal(1, winnr('$'))
1077 bwipe
1078
1079 set laststatus&
1080endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001081
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001082func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001083 call assert_equal(1, winnr('$'))
1084
1085 " Use the title termcap entries to output the escape sequence.
1086 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001087 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001088 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001089 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001090 \ 'redraw',
1091 \ "set t_ts=",
1092 \ ], 'Xscript')
1093 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001094 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001095 call assert_equal('Xtextfile', expand('%:t'))
1096 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001097 return buf
1098endfunc
1099
1100func Test_terminal_api_drop_newwin()
1101 if !CanRunVimInTerminal()
1102 return
1103 endif
1104 let buf = Api_drop_common('')
1105 call assert_equal(0, &bin)
1106 call assert_equal('', &fenc)
1107
1108 call StopVimInTerminal(buf)
1109 call delete('Xscript')
1110 bwipe Xtextfile
1111endfunc
1112
1113func Test_terminal_api_drop_newwin_bin()
1114 if !CanRunVimInTerminal()
1115 return
1116 endif
1117 let buf = Api_drop_common(',{"bin":1}')
1118 call assert_equal(1, &bin)
1119
1120 call StopVimInTerminal(buf)
1121 call delete('Xscript')
1122 bwipe Xtextfile
1123endfunc
1124
1125func Test_terminal_api_drop_newwin_binary()
1126 if !CanRunVimInTerminal()
1127 return
1128 endif
1129 let buf = Api_drop_common(',{"binary":1}')
1130 call assert_equal(1, &bin)
1131
1132 call StopVimInTerminal(buf)
1133 call delete('Xscript')
1134 bwipe Xtextfile
1135endfunc
1136
1137func Test_terminal_api_drop_newwin_nobin()
1138 if !CanRunVimInTerminal()
1139 return
1140 endif
1141 set binary
1142 let buf = Api_drop_common(',{"nobin":1}')
1143 call assert_equal(0, &bin)
1144
1145 call StopVimInTerminal(buf)
1146 call delete('Xscript')
1147 bwipe Xtextfile
1148 set nobinary
1149endfunc
1150
1151func Test_terminal_api_drop_newwin_nobinary()
1152 if !CanRunVimInTerminal()
1153 return
1154 endif
1155 set binary
1156 let buf = Api_drop_common(',{"nobinary":1}')
1157 call assert_equal(0, &bin)
1158
1159 call StopVimInTerminal(buf)
1160 call delete('Xscript')
1161 bwipe Xtextfile
1162 set nobinary
1163endfunc
1164
1165func Test_terminal_api_drop_newwin_ff()
1166 if !CanRunVimInTerminal()
1167 return
1168 endif
1169 let buf = Api_drop_common(',{"ff":"dos"}')
1170 call assert_equal("dos", &ff)
1171
1172 call StopVimInTerminal(buf)
1173 call delete('Xscript')
1174 bwipe Xtextfile
1175endfunc
1176
1177func Test_terminal_api_drop_newwin_fileformat()
1178 if !CanRunVimInTerminal()
1179 return
1180 endif
1181 let buf = Api_drop_common(',{"fileformat":"dos"}')
1182 call assert_equal("dos", &ff)
1183
1184 call StopVimInTerminal(buf)
1185 call delete('Xscript')
1186 bwipe Xtextfile
1187endfunc
1188
1189func Test_terminal_api_drop_newwin_enc()
1190 if !CanRunVimInTerminal()
1191 return
1192 endif
1193 let buf = Api_drop_common(',{"enc":"utf-16"}')
1194 call assert_equal("utf-16", &fenc)
1195
1196 call StopVimInTerminal(buf)
1197 call delete('Xscript')
1198 bwipe Xtextfile
1199endfunc
1200
1201func Test_terminal_api_drop_newwin_encoding()
1202 if !CanRunVimInTerminal()
1203 return
1204 endif
1205 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1206 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001207
1208 call StopVimInTerminal(buf)
1209 call delete('Xscript')
1210 bwipe Xtextfile
1211endfunc
1212
1213func Test_terminal_api_drop_oldwin()
1214 if !CanRunVimInTerminal()
1215 return
1216 endif
1217 let firstwinid = win_getid()
1218 split Xtextfile
1219 let textfile_winid = win_getid()
1220 call assert_equal(2, winnr('$'))
1221 call win_gotoid(firstwinid)
1222
1223 " Use the title termcap entries to output the escape sequence.
1224 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001225 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001226 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1227 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1228 \ 'redraw',
1229 \ "set t_ts=",
1230 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001231 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001232 call WaitFor({-> expand('%:t') =='Xtextfile'})
1233 call assert_equal(textfile_winid, win_getid())
1234
1235 call StopVimInTerminal(buf)
1236 call delete('Xscript')
1237 bwipe Xtextfile
1238endfunc
1239
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001240func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001241 let g:called_bufnum = a:bufnum
1242 let g:called_arg = a:arg
1243endfunc
1244
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001245func WriteApiCall(funcname)
1246 " Use the title termcap entries to output the escape sequence.
1247 call writefile([
1248 \ 'set title',
1249 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1250 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1251 \ 'redraw',
1252 \ "set t_ts=",
1253 \ ], 'Xscript')
1254endfunc
1255
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001256func Test_terminal_api_call()
1257 if !CanRunVimInTerminal()
1258 return
1259 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001260
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001261 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001262 let buf = RunVimInTerminal('-S Xscript', {})
1263 call WaitFor({-> exists('g:called_bufnum')})
1264 call assert_equal(buf, g:called_bufnum)
1265 call assert_equal(['hello', 123], g:called_arg)
1266
1267 call StopVimInTerminal(buf)
1268 call delete('Xscript')
1269 unlet g:called_bufnum
1270 unlet g:called_arg
1271endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001272
1273func Test_terminal_api_call_fails()
1274 if !CanRunVimInTerminal()
1275 return
1276 endif
1277
1278 call WriteApiCall('TryThis')
1279 call ch_logfile('Xlog', 'w')
1280 let buf = RunVimInTerminal('-S Xscript', {})
1281 call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1282
1283 call StopVimInTerminal(buf)
1284 call delete('Xscript')
1285 call ch_logfile('', '')
1286 call delete('Xlog')
1287endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001288
1289func Test_terminal_ansicolors_default()
1290 let colors = [
1291 \ '#000000', '#e00000',
1292 \ '#00e000', '#e0e000',
1293 \ '#0000e0', '#e000e0',
1294 \ '#00e0e0', '#e0e0e0',
1295 \ '#808080', '#ff4040',
1296 \ '#40ff40', '#ffff40',
1297 \ '#4040ff', '#ff40ff',
1298 \ '#40ffff', '#ffffff',
1299 \]
1300
1301 let buf = Run_shell_in_terminal({})
1302 call assert_equal(colors, term_getansicolors(buf))
1303 call Stop_shell_in_terminal(buf)
1304 call term_wait(buf)
1305
1306 exe buf . 'bwipe'
1307endfunc
1308
1309let s:test_colors = [
1310 \ '#616e64', '#0d0a79',
1311 \ '#6d610d', '#0a7373',
1312 \ '#690d0a', '#6d696e',
1313 \ '#0d0a6f', '#616e0d',
1314 \ '#0a6479', '#6d0d0a',
1315 \ '#617373', '#0d0a69',
1316 \ '#6d690d', '#0a6e6f',
1317 \ '#610d0a', '#6e6479',
1318 \]
1319
1320func Test_terminal_ansicolors_global()
1321 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1322 let buf = Run_shell_in_terminal({})
1323 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1324 call Stop_shell_in_terminal(buf)
1325 call term_wait(buf)
1326
1327 exe buf . 'bwipe'
1328 unlet g:terminal_ansi_colors
1329endfunc
1330
1331func Test_terminal_ansicolors_func()
1332 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1333 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1334 call assert_equal(s:test_colors, term_getansicolors(buf))
1335
1336 call term_setansicolors(buf, g:terminal_ansi_colors)
1337 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1338
1339 let colors = [
1340 \ 'ivory', 'AliceBlue',
1341 \ 'grey67', 'dark goldenrod',
1342 \ 'SteelBlue3', 'PaleVioletRed4',
1343 \ 'MediumPurple2', 'yellow2',
1344 \ 'RosyBrown3', 'OrangeRed2',
1345 \ 'white smoke', 'navy blue',
1346 \ 'grey47', 'gray97',
1347 \ 'MistyRose2', 'DodgerBlue4',
1348 \]
1349 call term_setansicolors(buf, colors)
1350
1351 let colors[4] = 'Invalid'
1352 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1353
1354 call Stop_shell_in_terminal(buf)
1355 call term_wait(buf)
1356 exe buf . 'bwipe'
1357endfunc